[Python] NASA's astronomy picture of the day

just some simple python scripting + crontab


import re
import urllib3
import urllib.request as request
import os


url = "https://apod.nasa.gov/apod/"
http = urllib3.PoolManager()
p = re.compile(r'(image.*jpg)')

r = http.request('GET',url)

if (r.status == 200):
    html = r.data.decode('utf-8')
    imgurl = re.findall(p,html)
        img = imgurl[0]
        download_url = url + img
        pic = request.urlretrieve(download_url, PATH)
        print('writing file to ', PATH)

    except Exception as e:
        print("hmmmm something went wrong")

    print("network failure...")

Notes: this script will overwrite history downloads. Only the current APOD is kept on the disk. The filepath can be modified to the picture’s name or date to keep all pictures.

I used both urllib3 and urllib, which is kindda messy…

And here is a bash script that downloads picture and set wallpaper:

python ~/scripts/apod.py
echo 'setting bg' 
feh --no-fehbg --bg-scale '/home/shrik3/Pictures/nasa.jpg'

A crontab can be set like:

0 12 * * * nasa_bg