-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_hubble.py
53 lines (40 loc) · 1.49 KB
/
fetch_hubble.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
51
52
53
import requests
import os
import urllib3
urllib3.disable_warnings()
def get_response_json(url, name):
urll = f'{url}{name}'
response = requests.get(urll)
response.raise_for_status()
return response.json()
def get_img_habbl(response_json_photos):
if not response_json_photos:
raise ValueError('нет данных в json обьекте')
return [i['id'] for i in response_json_photos]
def get_photo(photo, response_photos):
image_files = response_photos.get('image_files')
url_photo = f"https:{image_files[-1]['file_url']}"
file_extension = os.path.splitext(url_photo)[1]
filename = f'{photo}{file_extension}'
response_photo = requests.get(url_photo, verify=False)
response_photo.raise_for_status()
data = {'filname': filename,
'response_photo': response_photo}
return data
def write_photo(data, path):
with open(os.path.join(path, data['filname']), 'wb') as file:
file.write(data['response_photo'].content)
def main():
path = 'images'
os.makedirs(path, exist_ok=True)
url_images = 'https://hubblesite.org/api/v3/images/'
name = 'wallpaper'
response_images = get_response_json(url_images, name)
photos_name = get_img_habbl(response_images)
url_image = 'https://hubblesite.org/api/v3/image/'
for photo in photos_name:
response_photo = get_response_json(url_image, photo)
data = get_photo(photo, response_photo)
write_photo(data, path)
if __name__ == '__main__':
main()