Download NASA's astronomy picture of the day and set as background on a Linux machine
NASA provides astonishing astronomy pictures on a daily basis. You can see for yourself in their archive Astronomy Picture of the Day.
Another example, is this stunning picture from the 31th of July 2020:
I wanted to set these pictures as the background for my Linux computer and also to update the display on a daily basis. I also wanted the computer to do the boring stuff for me.
The solution that I came up with is a short Bash script that downloads the daily picture from NASA's API as well as running the script with the crontab utility on my computer.
Here I share my way of making the task work.
This page has 4 parts:
1. The API
The script downloads the daily pictures from NASA's API. The data returned is in a JSON format. From which we will extract the URL of the image that we are going to copy to a folder on our Linux system.
NASA's API is free. To consume it you need to register at: https://api.nasa.gov were you'll find a unique identifier, app id, which identifies the app to the service provider. The app id that you'll get is going to be similar to this one:
fY2SW6ZMnXXXXXXXVl31OpXXX8dcbMZXXXX8TG34
- Don't use the above id since it's only for demonstration purposes.
2. The script
The script has 3 parts:
- A reference to the API.
- Turning the returned JSON into data that Python can use.
- Copying the image to a directory on your PC.
- In case of not getting an image the script will take a random image from the directory.
nasa_daily.sh
#!/bin/bash
APP_DIR="$HOME/automated_tasks/daily_wallpaper"
IMG_DIR="$APP_DIR/images"
LOG_FILE="$APP_DIR/script.log"
REAL_USER_ID=1000
# Make directory if it does not exist
mkdir -p "$IMG_DIR"
# Get today's date
TODAY=$(date +"%Y-%m-%d")
# API
API_ID="YOUR_API_ID"
API_URL="https://api.nasa.gov/planetary/apod?api_key=$API_ID&date=$TODAY"
write_in_log() {
local message="$1"
local urgency="${2:-normal}"
log_record="$(date '+%Y-%m-%d %H:%M:%S') - $urgency - $message"
echo "$log_record">> "$LOG_FILE"
echo "$log_record"
}
# Function to set wallpaper
set_wallpaper () {
local pic=$1
if test -f "$pic"; then
rtpid=$(pgrep --euid $REAL_USER_ID gnome-session)
if [ -z "$rtpid" ]; then
message="No gnome-session process found for the current user."
write_in_log "$message" "critical"
return 0
fi
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
if [ -z "$DBUS_SESSION_BUS_ADDRESS" ]; then
message="Failed to get D-Bus session address."
write_in_log "$message" "critical"
return 0
fi
gsettings set org.gnome.desktop.background picture-uri "file://$pic"
message="Wallpaper set to $pic"
write_in_log "$message" "normal"
return 1
else
message="Error: File $pic not found."
write_in_log "$message" "critical"
return 0
fi
}
# Function to find a random image from the directory
find_other () {
find "$IMG_DIR" -type f | shuf -n 1
}
# Path for today's image
pic="$IMG_DIR/${TODAY}_nasa_daily.jpg"
# Fetch NASA APOD data
res=$(curl -s "$API_URL")
# Extract media type
res_type=$(echo "$res" | jq -r '.media_type')
if [ "$res_type" == "image" ]; then
# Extract image URL
res_url=$(echo "$res" | jq -r '.url')
# Download the image
curl -s -o "$pic" "$res_url"
# Set wallpaper
set_wallpaper "$pic"
# If setting wallpaper fails, set a random wallpaper
if [[ $? != 1 ]]; then
pic=$(find_other)
set_wallpaper "$pic"
fi
else
# If the media type is not an image, set a random wallpaper
pic=$(find_other)
set_wallpaper "$pic"
fi
3. How to use the script
In order to use the script you need to:
Copy the script to bash script on you PC.
Inside the script, change the directory name and the API id.
Set a directory for the images.
Run chmod +x on the script to make it executable.
Set the crontab utility on your Linux to run daily.
To edit crontab run the command crontab -e inside the terminal and inside the file that opens write the following command:
0 9 * * * bash /home/my_user/bash_scripts/nasa_daily.sh
I set mine to run daily at 9 am. Make the necessary adjustments.
4. Disclaimer
As far as I know, the script within this page is functional and harmless. However I make no guarantees. I also take no responsibility for any damage you may do when using the script (the chances of any problems occuring are slim but who knows). Please use this script responsibly and judgementally.