Introduction

I will go ahead and assume that if you are reading this, you have a slight interest in the sciences, maths, and engineering. The one thing that actually started me down this path above all was SPACE. I went to space camp as a kid by myself, dammit, and it was AWESOME. Currently, I work in the field of food process engineering; and at times, it feels like I am lightyears away from the daily pursuit of increasing our understanding of space. I have developed a small solution to at least keep my interest in space at the forefront of my daily life, if for even just a personal enjoyment.

NASA released the Astornomy Picture of the Day (APOD) 26 years ago by the day of this article (release date June 16th, 1995)! The site has been maintained and updated through the evolution of the internet to show the world the magnificent wonders of nature, space, and man-made engineering feats. The pictures are always stunning, with a description to help you out if you don't know what the hell you're looking at. After years of routinely getting lost on the APOD website, finding galaxies I liked, right-click, save as set as desktop background, an awful tragedy happened to me. I forgot about it. Life gets busy, you leave your computer for some godforsaken reason, get married, step outside to see the real world, and you forget to visit one of the most excellent websites ever made.

Once I rediscovered the portal to universe partially paid by my hard-earned taxpayer dollars, I wanted to capitalize on all of the beauty it has to offer with minimal waste of time and, better yet, not worry about forgetting ever again! That's why you're here. We're going to make NASA's Astronomy Picture of the Day our desktop background every day

How?

Making a simple python script, we can grab the image from the site, use some OS tools to set it, and put it on a schedule. Plenty of web-scraping tools exist, such as Beautiful Soup, but why do something tedious when NASA allows free API usage for their services (with limits) at api.nasa.gov.

Register for your key, and lets start hacking.

#Imports
import requests
import datetime
import urllib.request
import ctypes
from PIL import Image
import os
#API key generated from api.nasa.gov
key =  'INSERT_KEY_HERE'
#Current Date
now = datetime.datetime.today()
date = now.strftime("%Y-%m-%d") 
url = 'https://api.nasa.gov/planetary/apod'

#I always make sure I am actually certain what day it is
print(now)
param = {'api_key':key,"date":date}
resp = requests.get(url,params=param).json()
apod_url = resp['hdurl']

urllib.request.urlretrieve(apod_url, "./apod.jpg")

print(resp["title"])
print(resp["date"])
print(resp["explanation"])
print(apod_url)

Request.

A simple HTTP library for python, insert params (in this case API_key and date) put it in the URL and you get all responses. APOD delivers a title, picture, and explaination upon request, providing us with a lovely tidbit of information while sipping our morning coffee.

Set as Wallpaper

The next part is Windows-specific for me, as soon as someone tells there is a decent Word processor for a *NIX environment, I will gladly move. But Pythons OS and Ctypes have ways to work with Linux and macOS as well. Using the Ctypes library, you can access DLLs that execute on the OS, specifically for our case is the SystemParameterInfo (SPI) Function https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfoa in case you would like to learn more. Code 20 is to set the desktop wallpaper and 0x2 ensures that the change happens immediately.

SPI_SETDESKWALLPAPER = 20
SPIF_UPDATEINIFILE   = 0x2
path = os.getcwd()
path = os.path.join(path,'apod.bmp')
print(path)
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, path, SPIF_UPDATEINIFILE)

The last thing we need to do is put this all in a single script and make sure it executes every day. I do miss cronjobs using Windows. However, for the more accessible route to the non-techies, Windows has Task Scheduler. Here I have put my script on a schedule to run every day at 0900 EST where I'm due for my second cup of coffee anyways. My desktop changes to some unique latest imagery of space, and I pop up a text file with the description for further appreciation.

This method isn't foolproof… yet. Since the advent of YouTube, NASA has decided that videos are allowed on the 'Astronomy Picture of the Day', which is nothing short of heresy for the sake of correctness. More so, it challenges my ability to execute my script successfully. One day I will code a workaround to go back to the last picture; however, simple is better for the sake of this post.

I hope this post helps inspire you to change your desktop background, make some process of your life better and never forget your analog to space camp. Code is on my GitHub if you would like to enjoy it!

Cheers,

Q


Comments

comments powered by Disqus