Shantun Parmar
Shantun's Blog

Shantun's Blog

Real-Time Asteroid Watch using Python

Real-Time Asteroid Watch using Python

In this article, we will figure out how to build real time Asteroid Watch. We will see the execution in Python with barely a couple of lines of code.

Subscribe to my newsletter and never miss my upcoming articles

Listen to this article

Consistently space rocks (asteroids) are passing near planet Earth. Their direction is firmly checked by NASA as a portion of these might actually represent a danger to our planet. In this python challenge we will utilize the NeoWs API to recover some continuous information from NASA about these close to Earth asteroids.

You may have a look about NASA API on


This API provided to you JSON format, JSON stands for JavaScript Object Notation. It is a lightweight data-interchange format. The JSON format is linguistically like the code for creating JavaScript objects. Along these lines, a JavaScript program can without much of a stretch believer JSON information into JavaScript objects.

You can check how this api return JSON data here

Let's get started!


  • Request Module
  • Json Module

Requests in Python is an elegant library that lets you send HTTP/1.1 requests to web pages via Python.


Navigate your command line to the location of PIP, and type the following:

C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>pip install requests
C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>pip install json

You can reach to source code at my GitHub Repository. Drop a star if you love it.

#Real time Asteroid Watch by nasa
import requests, time
import webbrowser

today = time.strftime('%Y-%m-%d', time.gmtime())
print("Date: " + today)

#Our JSON request to retrieve data about asteroids approaching planet Earth.
url = "" + today + "&end_date=" + today + "&api_key=DEMO_KEY"

r = requests.get(url, headers={'User-Agent':'Mozilla/5.0'})

my_dict = r.json()

print("Today " + str(my_dict["element_count"]) + " asteroids will be passing close to planet Earth:")

asteroids = my_dict["near_earth_objects"]

#Parsing all the JSON data:
for asteroid in asteroids:
    for field in asteroids[asteroid]:
        min_diameter = field["estimated_diameter"]["meters"]["estimated_diameter_min"]
        max_diameter = field["estimated_diameter"]["meters"]["estimated_diameter_min"]
        miss_distance = str(field["close_approach_data"][0]["miss_distance"]["kilometers"])
        as_name = field["name"]
        velocity = str(field["close_approach_data"][0]["relative_velocity"]["kilometers_per_hour"])
        diameter = str(round(((min_diameter+max_diameter)/2),2))
        close_approach = field["close_approach_data"][0]["close_approach_date_full"]

        if field["is_potentially_hazardous_asteroid"]:   
          print ("This asteroid might be dangerous to planet Earth!")
          print ("This asteroid is safe")

According to above code kindly install all dependencies as given above, Once you install all module then, at that point pass date to API, This information depends on current date, But assuming you need to create dynamic then you can add not few more lines

start_date= input('input start date')
end_date= input('input end date')

It's time to call your API using requests module.

r = requests.get(url)

Note : In case if you got error unable to fetch all data, You must pass header in your request

r = requests.get(url,headers={'User-Agent':'Mozilla/5.0'})

All data put away in asteroids in JSON format, Now you can parse data according to your desired UI ,Let's display data in Table:

astroid wathc.png

I hope you found the article helpful! Share your thoughts in the comments section. If this is something that intrigues you, kindly share the article.

Much thanks to you for perusing, If you have reached up until this point, if it's not too much trouble, similar to the article, It will urge me to compose all the more such articles. Do share your feedback.

It's been never ending, Take care and see you in next article

Share this
Proudly part of