-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_spacex.py
More file actions
28 lines (22 loc) · 881 Bytes
/
fetch_spacex.py
File metadata and controls
28 lines (22 loc) · 881 Bytes
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
import logging
import os
import requests
from save_pictures import save_pic
def fetch_spacex_last_launch():
spacex_link = "https://api.spacexdata.com/v3/launches/67"
response = requests.get(spacex_link)
response.raise_for_status()
spacex_links = response.json()['links']['flickr_images']
directory = "images/SpaceX"
os.makedirs(directory, exist_ok=True)
for number, pic_url in enumerate(spacex_links):
pic_path = f"{directory}/SpaceX{number}.jpeg"
try:
save_pic(pic_url, pic_path)
except requests.exceptions.HTTPError as error:
logging.error("Failed to save image from SpaceX:\n{0}".format(error))
if __name__ == '__main__':
try:
fetch_spacex_last_launch()
except requests.exceptions.HTTPError as error:
logging.error("Can't get data from SPACEX server:\n{0}".format(error))