Skip to content

Instantly share code, notes, and snippets.

@prail
Last active October 27, 2017 21:28
Show Gist options
  • Save prail/e9c2180569518fc403d6de9a422d1dbe to your computer and use it in GitHub Desktop.
Save prail/e9c2180569518fc403d6de9a422d1dbe to your computer and use it in GitHub Desktop.
Gets a random picture from the nasa APOD.

APOD Desktop

To set this up you'll need to set your desktop background to slideshow mode. Create a directory in your pictures folder called APOD The script will put pictures in the directory. Set up a windows service to run the script once a day. profit! The script also creates a file on your desktop named description.txt it contains the pictures descript in a nice readable format if you're curious. Be careful when changing the APOD_DIRECTORY! This directory is removed and recreated very often so don't set it to something important like your Pictures directory.

import requests, json, getpass, os
requests.packages.urllib3.disable_warnings()
#Configuration variables.
API_KEY="DEMO_KEY" #You can replace this with your NASA api key if you'd like.
DESCRIPTION_FILE=r"C:\Users\{}\Desktop\description.txt".format(getpass.getuser()) #Where you want the description file put.
APOD_DIRECTORY=r"C:\Users\{}\Pictures\APOD".format(getpass.getuser()) #The directory where the image will be stored. Don't set this to something you don't want deleted!!!
API_ENDPOINT=r"https://api.nasa.gov/planetary/apod?api_key={}".format(API_KEY) #Don't really need to change this.
def make_lines(text):
buf=""
i=1
for char in text:
buf+=char
if i > 80:
buf+="\n"
i=0
i+=1
return buf
os.system('rmdir /S /Q "{}"'.format(APOD_DIRECTORY))
os.mkdir(APOD_DIRECTORY)
req=requests.get(API_ENDPOINT, verify=False)
if req.status_code == requests.codes.ok:
apod = json.loads(req.content.decode("utf-8"))
f=open(DESCRIPTION_FILE,"w")
f.write(make_lines(apod["explanation"].split(" "*3)[0]))
f.close()
r = requests.get(apod["hdurl"], verify=False, stream=True)
with open(APOD_DIRECTORY+r"\image.png", 'ab') as f:
for chunk in r.iter_content():
f.write(chunk)
@prail
Copy link
Author

prail commented Oct 18, 2017

This is mostly because I have no idea what to set my desktop to usually. 😆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment