Spaces:
Sleeping
Sleeping
Create nasa_neo_data_fetcher.py
Browse files
tools/nasa_neo_data_fetcher.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
from calendar import monthrange
|
5 |
+
|
6 |
+
class NASANeoDataFetcher:
|
7 |
+
def __init__(self):
|
8 |
+
self.api_key = os.getenv("NASA_API_KEY")
|
9 |
+
self.root_url = "https://api.nasa.gov/neo/rest/v1/feed?"
|
10 |
+
|
11 |
+
def get_nasa_neo_data(self, start_date: str, end_date: str) -> dict:
|
12 |
+
"""A function to get Near Earth Object data from NASA API. Try this function first before
|
13 |
+
calling the fetch_neo_data_in_chunks function.
|
14 |
+
Args:
|
15 |
+
start_date: A string representing the start date of the data to be fetched.
|
16 |
+
end_date: A string representing the end date of the data to be fetched.
|
17 |
+
Returns: The data fetched from the API as a JSON-like dictionary.
|
18 |
+
"""
|
19 |
+
params = f"start_date={start_date}&end_date={end_date}&api_key={self.api_key}"
|
20 |
+
url = self.root_url + params
|
21 |
+
response = requests.get(url)
|
22 |
+
if repr(response.status_code) == "200":
|
23 |
+
data = response.json()
|
24 |
+
return data
|
25 |
+
else:
|
26 |
+
print("Error: ", response.status_code)
|
27 |
+
|
28 |
+
def split_month_into_chunks(self, year: int, month: int) -> list:
|
29 |
+
days_in_month = monthrange(year, month)[1]
|
30 |
+
first_day = 1
|
31 |
+
last_day = 7
|
32 |
+
week_dates = []
|
33 |
+
while last_day <= days_in_month:
|
34 |
+
start_date = f"{year}-{month}-{first_day}"
|
35 |
+
end_date = f"{year}-{month}-{last_day}"
|
36 |
+
week_dates.append((start_date, end_date))
|
37 |
+
first_day += 7
|
38 |
+
last_day += 7
|
39 |
+
remaining_days = days_in_month - first_day + 1
|
40 |
+
if remaining_days > 0:
|
41 |
+
start_date = f"{year}-{month}-{first_day}"
|
42 |
+
end_date = f"{year}-{month}-{days_in_month}"
|
43 |
+
week_dates.append((start_date, end_date))
|
44 |
+
return week_dates
|
45 |
+
|
46 |
+
def fetch_neo_data_in_chunks(
|
47 |
+
self, start_year: int, end_year: int
|
48 |
+
) -> list[tuple[str, list[dict]]]:
|
49 |
+
"""A function to fetch Near Earth Object data from NASA API in chunks. Call this function if the API
|
50 |
+
returns an error when you try to pull data e.g. a 400 or 429.
|
51 |
+
Args:
|
52 |
+
start_year: An integer representing the start year of the data to be fetched.
|
53 |
+
end_year: An integer representing the end year of the data to be fetched.
|
54 |
+
Returns: A list of Near Earth Object data fetched from the API as a JSON-like dictionary.
|
55 |
+
"""
|
56 |
+
neo_data = []
|
57 |
+
for year in range(start_year, end_year + 1):
|
58 |
+
for month in range(1, 13):
|
59 |
+
for chunk in self.split_month_into_chunks(year, month):
|
60 |
+
start_date, end_date = chunk
|
61 |
+
print(f"Fetching data for {start_date} to {end_date}")
|
62 |
+
data = self.get_nasa_neo_data(
|
63 |
+
start_date=start_date, end_date=end_date
|
64 |
+
)
|
65 |
+
if data and "near_earth_objects" in data:
|
66 |
+
neo_data.extend(data["near_earth_objects"].items())
|
67 |
+
neo_data_json = json.dumps(neo_data)
|
68 |
+
return neo_data_json
|