2021-10-23 12:38:25 +00:00
|
|
|
import shutil
|
|
|
|
|
2021-10-23 10:56:26 +00:00
|
|
|
import requests
|
|
|
|
|
|
|
|
|
2024-03-20 14:00:17 +00:00
|
|
|
def get_apod_data(api_key: str) -> dict:
|
2021-10-23 10:56:26 +00:00
|
|
|
"""
|
|
|
|
Get the APOD(Astronomical Picture of the day) data
|
2021-10-23 12:38:25 +00:00
|
|
|
Get your API Key from: https://api.nasa.gov/
|
2021-10-23 10:56:26 +00:00
|
|
|
"""
|
2021-10-23 12:38:25 +00:00
|
|
|
url = "https://api.nasa.gov/planetary/apod"
|
2024-04-21 17:34:18 +00:00
|
|
|
return requests.get(url, params={"api_key": api_key}, timeout=10).json()
|
2021-10-23 10:56:26 +00:00
|
|
|
|
|
|
|
|
2021-10-23 12:38:25 +00:00
|
|
|
def save_apod(api_key: str, path: str = ".") -> dict:
|
|
|
|
apod_data = get_apod_data(api_key)
|
|
|
|
img_url = apod_data["url"]
|
|
|
|
img_name = img_url.split("/")[-1]
|
2024-04-21 17:34:18 +00:00
|
|
|
response = requests.get(img_url, stream=True, timeout=10)
|
2021-10-23 12:38:25 +00:00
|
|
|
|
|
|
|
with open(f"{path}/{img_name}", "wb+") as img_file:
|
|
|
|
shutil.copyfileobj(response.raw, img_file)
|
|
|
|
del response
|
|
|
|
return apod_data
|
|
|
|
|
|
|
|
|
2021-10-23 10:56:26 +00:00
|
|
|
def get_archive_data(query: str) -> dict:
|
|
|
|
"""
|
|
|
|
Get the data of a particular query from NASA archives
|
|
|
|
"""
|
2021-10-23 12:38:25 +00:00
|
|
|
url = "https://images-api.nasa.gov/search"
|
2024-04-21 17:34:18 +00:00
|
|
|
return requests.get(url, params={"q": query}, timeout=10).json()
|
2021-10-23 10:56:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-10-23 12:38:25 +00:00
|
|
|
print(save_apod("YOUR API KEY"))
|
|
|
|
apollo_2011_items = get_archive_data("apollo 2011")["collection"]["items"]
|
|
|
|
print(apollo_2011_items[0]["data"][0]["description"])
|