NASA NEO V1 (Python)

My last few Python projects have been utilising Python reading JSON (JavaScript Object Notation) files and accessing online resources. Looking for a new challenge I did a quick Google search for publicly available JSON files and (in my opinion) found the ultimate files to play with – files from NASA. In case you don’t know, NASA (National Aeronautics and Space Administration) is the United States of America’s space administration.

For more information on NASA’s open API (Application Programming Interface) usage please visit https://api.nasa.gov/api.html

Please note I have signed up for an API key from NASA and I’m using that when running my program, however for the use of posting my code to my website/GitHub I have replaced my API key with NASA’s default of DEMO_KEY. If you want to use NASA’s open API I recommend that you sign up for a free API key.

NASA uses HTTPS (Hyper Text Transfer Protocol Secure) for access to it’s APIs. When writing my programme I hit a bit of a wall using Python 3.6 on Mac OS X in that it kept generating an SSL (Secure Socket Layer) certificate error:

urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed

I tried changing the address to HTTP but that failed and there are resources online advising on how to bypass SSL certificates in Python (not recommended!). The answer to this “SSL certificate verify failed” issue is really simple – using the Mac OS Finder, browse to /Applications/Python 3.6/ and run the “Install Certificates.command” file. This file installs a bundle of default root certificates from the certifi package and more information around this can be found in the “ReadMe.rtf” file in the same folder location.

I’ve started off with a basic program to get the JSON file and just check how many NEOs existed in a user defined time period.

———

#!/bin/python3

#geektechstuff

#NASA NEO

#Big thanks to NASA API (https://api.nasa.gov/api.html)

import json, urllib.request

#variables

api=’DEMO_KEY’

start_date=””

end_date=””

result=””

potentially_hazardous=””

#message to welcome the user

print(‘Hi and welcome to GeekTechStuff\’s NASA Near Earth Object (NEO) Python program’)

print(‘This programme uses NASA\’s open API system’)

print(”)

#asks the users for dates, these must be in yyyy-mm-dd format

start_date=input(‘Please enter an eight digit date in yyyy-mm-dd format for the start date:’)

end_date=input(‘Please enter an eight digit date in yyyy-mm-dd format for the end date:’)

#opens JSON file containing NEO data

url=’https://api.nasa.gov/neo/rest/v1/feed?start_date=’+start_date+’&end_date=’+end_date+’&api_key=’+api

response=urllib.request.urlopen(url)

result=json.loads(response.read())

print(‘Loading data from’,url)

print(”)

#uses the json loaded from get_json

print(‘There were’,result[‘element_count’],’NEO\’s between’,start_date,’and’,end_date)

———

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.