Skip navigation

OK, NASA’s astronomy photos of the day are cool. I like spacey-type backgrounds. I’d like to have NASA’s photos as my background. But, you see, I’m lazy. I don’t want to have to download and set it every sodding day. I’m on GNOME though, so there’s room for automation with gconftool-2. So I cobbled together some kludge of a script to do it for me.

NASA don’t seem to provide a feed or anything fancy, so it’s a screen-scraper (urgh). Either way, here it is.

Start with some configuration:

DIRPATH=~/bin/shell/backgrounds

Change that to wherever you want the pictures to go to. I decided to make the script keep the files around after they’ve been used, so I can go back and look at them.

Now for the screen-scraping:

wget -O page http://apod.nasa.gov/apod/astropix.html

Grab the page.

IMG=$(grep -ioP '<img.*' page | sed 's/.*"\(.*\)"/\1/')
URL="http://apod.nasa.gov/apod/$IMG"

Search the page for an IMG tag. The end of the line where the IMG tag is the SRC attribute, so just strip the quotes off that and we’re there. If the format changes, this will be screwed. It’s not flexible, but hey, I can deal.

IMGNAME=$(echo $IMG | sed 's/\(.*\/\)*\(.*\)/\2/')
IMGPATH="$DIRPATH/$IMGNAME"

rm page

Then we mess around with that string to make a local path for the image, and clean up the page.

In order to save some useless work, we can check to see if the image is already there, and quit with a message.

if [ -f "$IMGPATH" ]; then
echo "Picture $IMGNAME already exists, exiting"
exit
fi

Then, if the file isn’t there, we get to actually downloading the thing and setting the relevant GNOME option to point to the file, and we’re done.

wget -O "$IMGPATH" "$URL"

gconftool-2 --type string \
--set /desktop/gnome/background/picture_filename \
"$IMGPATH"

Wehay! Easy. And bloody nasty. But there you go.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: