-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_nasa_epic_photos.py
39 lines (32 loc) · 1.31 KB
/
fetch_nasa_epic_photos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os
import datetime
import requests
from dotenv import load_dotenv
from general_functions import download_images
def fetch_nasa_epic_photos(token: str) -> None:
img_folder_path = "images"
img_name_template = "nasa_epic_"
params = (
("api_key", token),
)
response = requests.get("https://api.nasa.gov/EPIC/api/natural/images", params=params)
response.raise_for_status()
photos_collection_with_metadata = response.json()
nasa_epic_photos_urls = []
for photo_metadata in photos_collection_with_metadata:
img_name = photo_metadata["image"]
img_datetime = photo_metadata["date"].split()
img_date = datetime.date.fromisoformat(img_datetime[0])
url = f"https://api.nasa.gov/EPIC/archive/natural/{img_date.year}/{img_date.strftime('%m')}/{img_date.strftime('%d')}/png/{img_name}.png"
nasa_epic_photos_urls.append(url)
download_images(nasa_epic_photos_urls, img_folder_path, img_name_template, params)
def main():
load_dotenv()
nasa_api_token = os.environ['NASA_API_TOKEN']
try:
fetch_nasa_epic_photos(nasa_api_token)
except requests.exceptions.RequestException as error:
print('Request error:\n', error.response)
print('Request error text:\n', error.response.text)
if __name__ == '__main__':
main()