Real-Time Asteroid Watch using Python

Real Time asteroid watch in python

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

You may have a squint well-nigh NASA API on https://api.nasa.gov

JSON Data

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 lawmaking for creating JavaScript objects. Along these lines, a JavaScript program can without much of a stretch parishioner JSON information into JavaScript objects.

You can trammels how this api return JSON data here

Let’s get started!

Resources

  • Request Module
  • Json Module

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

Installation

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

C:UsersYour NameAppDataLocalProgramsPythonPython36-32Scripts>pip install requests
C:UsersYour NameAppDataLocalProgramsPythonPython36-32Scripts>pip install json

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


import requests, time
import webbrowser


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


url = "https://api.nasa.gov/neo/rest/v1/feed?start_date="   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 tropical to planet Earth:")

asteroids = my_dict["near_earth_objects"]


for asteroid in asteroids:
    for field in asteroids[asteroid]:
        print(field)
        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!")
        else:
          print ("This asteroid is safe")

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

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

It’s time to undeniability your API using requests module.

r = requests.get(url)

Note : In specimen 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 yonder in asteroids in JSON format, Now you can parse data equal to your desired UI ,Let’s walkout data in Table:

astroid wathc.png

I hope you found the vendible 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 etch all the increasingly such articles. Do share your feedback.

It’s been never ending, Take superintendency and see you in next article

Author: Shantun Parmar

Leave a Reply

Your email address will not be published.