2025-05-14 03:42:11 +02:00
|
|
|
# /// script
|
|
|
|
|
# requires-python = ">=3.13"
|
|
|
|
|
# dependencies = [
|
|
|
|
|
# "httpx",
|
|
|
|
|
# ]
|
|
|
|
|
# ///
|
2021-10-23 18:08:25 +05:30
|
|
|
|
2025-05-14 03:42:11 +02:00
|
|
|
import httpx
|
2021-10-23 16:26:26 +05:30
|
|
|
|
|
|
|
|
|
2024-03-20 17:00:17 +03:00
|
|
|
def get_apod_data(api_key: str) -> dict:
|
2021-10-23 16:26:26 +05:30
|
|
|
"""
|
|
|
|
|
Get the APOD(Astronomical Picture of the day) data
|
2021-10-23 18:08:25 +05:30
|
|
|
Get your API Key from: https://api.nasa.gov/
|
2021-10-23 16:26:26 +05:30
|
|
|
"""
|
2021-10-23 18:08:25 +05:30
|
|
|
url = "https://api.nasa.gov/planetary/apod"
|
2025-05-14 03:42:11 +02:00
|
|
|
return httpx.get(url, params={"api_key": api_key}, timeout=10).json()
|
2021-10-23 16:26:26 +05:30
|
|
|
|
|
|
|
|
|
2021-10-23 18:08:25 +05:30
|
|
|
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]
|
2025-05-14 03:42:11 +02:00
|
|
|
response = httpx.get(img_url, timeout=10)
|
2021-10-23 18:08:25 +05:30
|
|
|
|
|
|
|
|
with open(f"{path}/{img_name}", "wb+") as img_file:
|
2025-05-14 03:42:11 +02:00
|
|
|
img_file.write(response.content)
|
2021-10-23 18:08:25 +05:30
|
|
|
del response
|
|
|
|
|
return apod_data
|
|
|
|
|
|
|
|
|
|
|
2021-10-23 16:26:26 +05:30
|
|
|
def get_archive_data(query: str) -> dict:
|
|
|
|
|
"""
|
|
|
|
|
Get the data of a particular query from NASA archives
|
|
|
|
|
"""
|
2021-10-23 18:08:25 +05:30
|
|
|
url = "https://images-api.nasa.gov/search"
|
2025-05-14 03:42:11 +02:00
|
|
|
return httpx.get(url, params={"q": query}, timeout=10).json()
|
2021-10-23 16:26:26 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-10-23 18:08:25 +05:30
|
|
|
print(save_apod("YOUR API KEY"))
|
|
|
|
|
apollo_2011_items = get_archive_data("apollo 2011")["collection"]["items"]
|
|
|
|
|
print(apollo_2011_items[0]["data"][0]["description"])
|