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.
#Big thanks to NASA API (https://api.nasa.gov/api.html)
import json, urllib.request
#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’)
#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
print(‘Loading data from’,url)
#uses the json loaded from get_json
print(‘There were’,result[‘element_count’],’NEO\’s between’,start_date,’and’,end_date)