Spaces:
Sleeping
Sleeping
Writing changes fixed locally
Browse files
tools/nasa_neo_data_fetcher.py
CHANGED
@@ -1,11 +1,12 @@
|
|
1 |
import os
|
2 |
import requests
|
3 |
-
import json
|
4 |
from calendar import monthrange
|
5 |
from smolagents.tools import Tool
|
6 |
from typing import Generator
|
7 |
from dotenv import load_dotenv
|
8 |
|
|
|
|
|
9 |
|
10 |
class NasaNeoDataFetcher(Tool):
|
11 |
name = "nasa_neo_data_fetcher"
|
@@ -23,17 +24,16 @@ class NasaNeoDataFetcher(Tool):
|
|
23 |
output_type = "string"
|
24 |
|
25 |
def __init__(self):
|
26 |
-
load_dotenv()
|
27 |
self.api_key = os.getenv("NASA_API_KEY")
|
28 |
self.root_url = "https://api.nasa.gov/neo/rest/v1/feed?"
|
29 |
self.is_initialized = False
|
30 |
|
31 |
-
def forward(self, start_date: str, end_date: str) -> list:
|
32 |
"""A function to fetch Near Earth Object data from NASA API for a given date range.
|
33 |
Args:
|
34 |
start_date: A string representing the start date of the data to be fetched.
|
35 |
end_date: A string representing the end date of the data to be fetched.
|
36 |
-
Returns: The data fetched from the API as a list of tuples containing JSON-like dictionaries.
|
37 |
"""
|
38 |
return self._fetch_neo_data_in_chunks(start_date, end_date)
|
39 |
|
@@ -93,13 +93,15 @@ class NasaNeoDataFetcher(Tool):
|
|
93 |
data = self._get_nasa_neo_data(start_date=start_date, end_date=end_date)
|
94 |
yield data
|
95 |
|
96 |
-
def _fetch_neo_data_in_chunks(
|
|
|
|
|
97 |
"""A function that fetches Near Earth Object data from NASA API in chunks of 7 days. NB: The API
|
98 |
returns an error when you try to pull data covering longer than 7 days.
|
99 |
Args:
|
100 |
start_date: A string representing the start date of the data to be fetched.
|
101 |
end_date: A string representing the end date of the data to be fetched.
|
102 |
-
Returns: A list of JSON-like dictionaries of Near Earth Object data fetched from the API.
|
103 |
"""
|
104 |
neo_data = []
|
105 |
start_year, start_month, start_day = self._split_date_into_ymd(start_date)
|
@@ -127,4 +129,4 @@ class NasaNeoDataFetcher(Tool):
|
|
127 |
for item in data:
|
128 |
if "near_earth_objects" in item:
|
129 |
neo_data.extend(item["near_earth_objects"].items())
|
130 |
-
return neo_data
|
|
|
1 |
import os
|
2 |
import requests
|
|
|
3 |
from calendar import monthrange
|
4 |
from smolagents.tools import Tool
|
5 |
from typing import Generator
|
6 |
from dotenv import load_dotenv
|
7 |
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
|
11 |
class NasaNeoDataFetcher(Tool):
|
12 |
name = "nasa_neo_data_fetcher"
|
|
|
24 |
output_type = "string"
|
25 |
|
26 |
def __init__(self):
|
|
|
27 |
self.api_key = os.getenv("NASA_API_KEY")
|
28 |
self.root_url = "https://api.nasa.gov/neo/rest/v1/feed?"
|
29 |
self.is_initialized = False
|
30 |
|
31 |
+
def forward(self, start_date: str, end_date: str) -> list[tuple[str, dict]]:
|
32 |
"""A function to fetch Near Earth Object data from NASA API for a given date range.
|
33 |
Args:
|
34 |
start_date: A string representing the start date of the data to be fetched.
|
35 |
end_date: A string representing the end date of the data to be fetched.
|
36 |
+
Returns: The data fetched from the API as a list of tuples containing the date and the JSON-like dictionaries.
|
37 |
"""
|
38 |
return self._fetch_neo_data_in_chunks(start_date, end_date)
|
39 |
|
|
|
93 |
data = self._get_nasa_neo_data(start_date=start_date, end_date=end_date)
|
94 |
yield data
|
95 |
|
96 |
+
def _fetch_neo_data_in_chunks(
|
97 |
+
self, start_date: str, end_date: str
|
98 |
+
) -> list[tuple[str, dict]]:
|
99 |
"""A function that fetches Near Earth Object data from NASA API in chunks of 7 days. NB: The API
|
100 |
returns an error when you try to pull data covering longer than 7 days.
|
101 |
Args:
|
102 |
start_date: A string representing the start date of the data to be fetched.
|
103 |
end_date: A string representing the end date of the data to be fetched.
|
104 |
+
Returns: A list of tuples containing the date and the JSON-like dictionaries of Near Earth Object data fetched from the API.
|
105 |
"""
|
106 |
neo_data = []
|
107 |
start_year, start_month, start_day = self._split_date_into_ymd(start_date)
|
|
|
129 |
for item in data:
|
130 |
if "near_earth_objects" in item:
|
131 |
neo_data.extend(item["near_earth_objects"].items())
|
132 |
+
return neo_data
|