-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_nasa_daytime_photos.py
48 lines (39 loc) · 1.72 KB
/
fetch_nasa_daytime_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
40
41
42
43
44
45
46
47
48
import os
import argparse
from argparse import RawTextHelpFormatter
from wsgiref.simple_server import demo_app
import requests
from dotenv import load_dotenv
from general_functions import download_images, get_file_extension
def fetch_nasa_daytime_photos(token: str, count: int) -> None:
img_folder_path = "images"
img_name_template = "nasa_apod_"
params = (
("api_key", token),
("count", count),
)
response = requests.get("https://api.nasa.gov/planetary/apod", params=params)
response.raise_for_status()
photos_collection_with_metadata = response.json()
daytime_photos_urls = [photo_metadata["url"] for photo_metadata in photos_collection_with_metadata]
download_images(daytime_photos_urls, img_folder_path, img_name_template)
def create_argparser() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="""\
Скрипт возвращает указанное количество Astronomy Picture of the Day (APOD) фотографий сделанные NASA.\n
По умолчанию скрипт возвращает 5 фотографий""",
formatter_class=RawTextHelpFormatter
)
parser.add_argument('-count', help='Количество запрашиваемых фотографий', type=int, default=5)
return parser.parse_args()
def main():
load_dotenv()
args = create_argparser()
nasa_api_token = os.environ['NASA_API_TOKEN']
try:
fetch_nasa_daytime_photos(nasa_api_token, args.count)
except requests.exceptions.RequestException as error:
print('Request error:\n', error.response)
print('Request error text:\n', error.response.text)
if __name__ == '__main__':
main()