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
# change the directory name
dir="/home/my_user/Pictures/wallpapers"
# you can choose to download the daily picture
# or on any specific date
today=$(date +"%Y-%m-%d")
#today='2020-07-31'
# change the app id to what you get once registering
# to the API at https://api.nasa.gov
appid="fYFXXzZMnXm7MQXXqXX1OpXhrAdXXXZd9qA84XXX"
nasa_url="https://api.nasa.gov/planetary/apod?api_key=$appid&date=$today"
set_wallpaper () {
if test -f "$pic"; then
# the crontab doesn't have privileges - the solution is partly taken from:
# https://www.unix.com/shell-programming-and-scripting/203631-dbus_session_bus_address-script-called-crontab.html
rtuid=$(id --real --user)
# echo $rtuid
rtpid=$(pgrep --euid $rtuid gnome-session)
# echo $rtpid
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$rtpid/environ|cut -d= -f2-)
# set the wallpaper
gsettings set org.gnome.desktop.background picture-uri "file://$pic"
echo "Mission complete"
return 1
else
echo "We have a problem"
return 0
fi
}
find_other () {
echo $(find $dir -type f | shuf -n 1)
}
pic="$dir/$today__nasa_daily.jpg"
# get the API
res=$(curl -s $auth $nasa_url)
# extract the media type
res_type=$(echo $res | jq -r '.media_type')
# echo $res
# echo $res_type
if [ $res_type == "image" ]; then
# extract the image url
res_url=$(echo $res | jq -r '.url')
# copy to the local directory
curl -X GET ${res_url} > ${pic}
set_wallpaper $pic
if [[ $? != 1 ]]; then
pic=$(find_other)
set_wallpaper $pic
fi
else
pic=$(find_other)
set_wallpaper $pic
fi
echo Done
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.