Consuming NASA API using Python — Part 1 | Daily Python #17
This article is a tutorial on how to consume the NASA API using Python. This is the first part of the NASA API series.
This article is a part of Daily Python challenge that I have taken up for myself. I will be writing short python articles daily.
Sign up on the NASA API website: https://api.nasa.gov/. After this step, you will receive an API key via email. This API Key will be used for further communication with the NASA API.
Requirements:
- Python 3.0
- Pip
Install the following packages:
- requests — Library to making HTTP requests.
- pprint — Library for printing structures of data in a formatted way.
- urllib — Library for handling URLs
pip install requests pprint urllib3
Let’s import these libraries
import requests
from urllib.request import urlretrieve
from pprint import PrettyPrinter
pp = PrettyPrinter()pp = PrettyPrinter()
The ‘pp’ instance of PrettyPrinter will be used to beautify the JSON output.
Let’s store the API Key in a variable to be used in the program
apiKey = 'YOUR_NASA_API_KEY'
We will cover the following APIs in this article
- APOD — Astronomy picture of the day
- Asteroids NeoWs — NeoWs (Near Earth Object Web Service)
- EPIC — Earth Polychromatic Imaging Camera
Let’s start by writing a function to consume the APOD API
def fetchAPOD():
URL_APOD = "https://api.nasa.gov/planetary/apod"
date = '2020-01-22'
params = {
'api_key':api_key,
'date':date,
'hd':'True'
}
response = requests.get(URL_APOD,params=params).json()
pp.pprint(response)fetchAPOD()
Let’s define functions to consume the Asteroids NeoWs API
There are 3 operations for fetching information of the Near Earth Objects.
- Feed
- Lookup
- Browse
def fetchAsteroidNeowsFeed():
URL_NeoFeed = "https://api.nasa.gov/neo/rest/v1/feed"
params = {
'api_key':api_key,
'start_date':'2020-01-22',
'end_date':'2020-01-23'
}
response = requests.get(URL_NeoFeed,params=params).json()
pp.pprint(response)fetchAsteroidNeowsFeed()
Note: The above data can be used to study the path of the asteroids and their probability of impact with other Neos.
def fetchAsteroidNeowsLookup():
asteroid_id = '3542519'
URL_NeoLookup = "https://api.nasa.gov/neo/rest/v1/neo/"+ asteroid_id
params = {
'api_key':api_key
}
response = requests.get(URL_NeoLookup,params=params).json()
pp.pprint(response)fetchAsteroidNeowsLookup()
def fetchAsteroidNeowsBrowse():
URL_NeoBrowse = "https://api.nasa.gov/neo/rest/v1/neo/browse/"
params = {
'api_key':api_key
}
response = requests.get(URL_NeoBrowse,params=params).json()
pp.pprint(response)fetchAsteroidNeowsBrowse()
Finally let’s define a function to consume the EPIC ( Earth Polychromatic Imaging Camera) API
def fetchEPICData():
URL_EPIC = "https://api.nasa.gov/EPIC/api/natural/"
params = {
'api_key':api_key,
}
response = requests.get(URL_EPIC,params=params).json()
pp.pprint(response)fetchEPICData()
Let’s save an image using the ‘image’ id we fetched from ‘fetchEPICData’
def fetchEPICImage():
YEAR = '2015'
MONTH = '10'
DAY = '31'
IMAGE_ID = 'epic_1b_20151031074844'
URL_EPIC = "https://epic.gsfc.nasa.gov/archive/natural/"
URL_EPIC = URL_EPIC + YEAR +'/' + MONTH + '/'+DAY
URL_EPIC = URL_EPIC + '/png'
URL_EPIC = URL_EPIC + '/' + IMAGE_ID + '.png'
print(URL_EPIC)
urlretrieve(URL_EPIC,IMAGE_ID+'.png')
fetchEPICImage()
Saved Image:
I hope this article was helpful, do leave some claps if you liked it.