Nasa - Space Image of the Day

Random.dog is a fun API that will show you random links of cute dog pictures and videos. They have a really fun selection of media. With the help of webbrowser library we can then open these links right from the python terminal.

Here is a simply code that uses requests, json and webbrowser libraries harmoniously.

Code below will turn up Nasa Astronomy pic; print its title and then open it in your browser.

import requests
import json
import webbrowser
def nasapic():

    f = r"https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&count=1"
    data = requests.get(f)
    tt = json.loads(data.text)
    
    print(tt[0]["title"])
    webbrowser.open(tt[0]["url"])
nasapic()

Here is an example making use of params parameter in requests.get method.

By typing all the parameters inside a dictionary and calling it with params parameter makes everything more manageable and tidy. 

import requests
import json
import webbrowser

def nasapic():

    params = {"api_key": "DEMO_KEY", "hd": True, "count" : 1}    
    f = r"https://api.nasa.gov/planetary/apod?"
    data = requests.get(f, params = params)
    tt = json.loads(data.text)
    
    print(tt[0]["title"])
    webbrowser.open(tt[0]["url"])
nasapic()

If you need a refresher on user defined functions lesson in Python here is a link to our Defining Functions Lesson.

Source of API: Github.com/nasa/apod-api

You can find more NASA APIs here.

Source: Nasa

Nasa has a collection of very interesting deep space images and they are definitely worth exploring.

Recommended Posts