Get the NASA Photo of the Day with Python, Save the Image, and Open it!
#nasa api (you need to generate an api key off their website at https://api.nasa.gov/)
#this program first checks for an HD image but defaults to the low-res image if HD is not found
import requests
import json
import urllib.request
from PIL import Image
API_KEY=""
response = requests.get('https://api.nasa.gov/planetary/apod?api_key='+API_KEY)
try:
imgHDURL = response.json()['hdurl']
except:
pass
try:
imgURL = response.json()['url']
except:
pass
if imgHDURL:
print(imgHDURL)
fileName = imgHDURL.split('/')[-1]
urllib.request.urlretrieve(imgHDURL, fileName)
img = Image.open(fileName)
img.show()
else:
print(imgURL)
fileName = imgHDURL.split('/')[-1]
urllib.request.urlretrieve(imgURL, fileName)
img = Image.open(fileName)
img.show()