Automated NASA APOD Background

The NASA APOD (Astronomy Photo of the Day) is one of the most profound sources for images related to astronomy. The default screensaver that comes with Linux is limited in the sense that it only shows a set of predetermined images. To enhance my computing experience I decided to write a script that updates my background image with the latest APOD.

Getting the source data was easier than I expected. Turns out that NASA has publicly available APIs that includes the APOD. Requesting an API key is as simple as verifying your email address. With the JSON payload I use a regex and grep to capture the high resolution image, and then use wget to download the image and save it as the same name every time. The requirement of an API key sucks if I am trying to make the script/package more usable by anyone, which we will go into later.

Setting the background became a struggle as the standard way and the way to override in random places are both highly rated Stack Exchange answers. Where there are stylesheets to update such things, it really depends on the version and or theme. There were different commands and different settings that all changed things in marginally different ways as I explored them all. Initially, the sure way I found to get the background image and lock screen image to be in sync is to select them via the built-in gnome-control-center settings tool.

Getting the script to run consistently without affecting performance was a challenge. Initially I figured that on login or anytime the password is entered would be a good idea, but the synchronous nature of the scripts running in /etc/profile any changes here introduce a delay that was substantially noticeable. With no internet the script would error out in 15 seconds, which makes for a really long login. Next I tried using crontab to set up a job, but that was inconsistent as the times that the computer was on varied in day to day use. Finally I decided that whenever the network connects is the best time to update the background makes the most sense. To accomplish this you add the script to /etc/network/if-up.d WITHOUT a .sh extension and mark the file as executable, now everytime the network connects to a wifi connection the script will be run.

Since I want to make this a package I am listing out the requirements.
Install Script or symlink to /etc/network/if-up.d via dpkg
Run Once to create background.jpg file in /usr/backgrounds/
Set via gsettings
Message user to set background image via default manager if error
Add to XML settings to allow for resetting to APOD if background does change

With those requirements in mind I decide to rewrite the script to ensure I am adhering to any standards that seem relevant (i.e. putting the background image with other background images). The script is much more succulent now, adding to configurations automatically that are persistent rather than a fragile manual setup.

https://www.internalpointers.com/post/build-binary-deb-package-practical-guide

Before doing the dpkg magic I decided to read up on the process and what it takes to get an application listed on the Debian/Ubuntu store. I do not care about those sorts of things, but it is a nice thought for the future. Basically it needs to be gated via peer review, which since it is all ran by volunteers can take some time.

The dpkg’ing process was very straightforward. The only issue I was tripped up on was the architecture, since it is required for the CONTROL file it made me a little unsure of if I needed one for each architecture, which I think one works since it is a very high-level script.

Download it here: https://github.com/evinr/bash-scripts/raw/master/nasa-apod-background_0.0.1_x86_64.deb
Or grab this script and figure it out: https://raw.githubusercontent.com/evinr/bash-scripts/master/NASA-to-Background.sh

Plans for the future include trying to get this mainlined since that seems like it would be a fun process to experience. That also means I need to work on solid error messaging and ensuring stability via some unit tests. I will update this article if it gets posted to any official sources! Overall this was a nice little dabble into Bash scripting and understanding the nuances of Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *