NASA API with Python

API Interaction with NASA using Python


*Featured Code Submitted and Created by 

VENOM666



#365 DAYS OF CODE: DAY 3: NASA OPEN API INTERACTION - 1ST ATTEMPT
# by: VENOM666

# Developer's Note: I don't have a grasp on how API interaction works, please help me

#    What does this do?
#        Shows the data for the Astronomical Picture of the Day (APOD)

from urllib.request import urlopen as url_open
import json #javascript object notation
#from PIL import Image #Python Imaging Library

url = 'https://api.nasa.gov/planetary/apod?' # I don't get how url queries work, someone help
API_KEY = 'api_key=dWpZtxYFFudFeJi4KnQmoDXZz6y8rf7pPus6yoqu' #own API key, please register for your own at NASA Open APIs

decoded_string = url_open(url + API_KEY).read().decode('utf-8') #url_open().read() returns a string with b prepended, which needs to be decoded first with decode('utf-8') before it can be transformed into json format
decoded_json = json.loads(decoded_string)

for key in decoded_json: #iterate through the json dictionary
    value = decoded_json[key]
    print('{}: {}'.format(key, value))

#Image.open(url_open(decoded_json['url'])) # Outputs the picture on the screen. Doesn't work on Sololearn tho. Try it on your working environment. Cheers to Kuba for this addition to my code!!!


Click here to Run this Code



Comments

Post a Comment

Popular posts from this blog