-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_spacex_images.py
50 lines (42 loc) · 1.46 KB
/
fetch_spacex_images.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
49
50
import argparse
from pathlib import Path
import requests
from environs import Env
from download_images import download_pictures_to_dir, compress_image
def fetch_spacex_last_launch(launch_id: str) -> list:
api_url = 'https://api.spacexdata.com/v4/launches'
api_response = requests.get(url=api_url)
api_response.raise_for_status()
spacex_response: list = api_response.json()
for launch in spacex_response:
flickr_links = launch['links']['flickr']['original']
if launch_id:
if launch['id'] == launch_id:
return flickr_links
elif flickr_links:
return flickr_links
def main():
env = Env()
env.read_env()
images_dir: str = env('IMAGES_DIR', default='images')
images_path = Path(images_dir)
images_path.mkdir(parents=True, exist_ok=True)
parser = argparse.ArgumentParser()
parser.add_argument('--id', help='ID of launch to get images of')
args = parser.parse_args()
if args.id:
args_id: str = args.id
args_id = args_id.strip()
else:
args_id = ''
spacex_images = fetch_spacex_last_launch(launch_id=args_id)
if spacex_images:
downloaded_pictures = download_pictures_to_dir(
images_path, spacex_images, prefix='spacex'
)
for picture in downloaded_pictures:
compress_image(picture)
else:
print(f'The launch with id "{args_id}" has no photos.')
if __name__ == "__main__":
main()