-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunsplash.py
34 lines (28 loc) · 1001 Bytes
/
unsplash.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
from typing import List
import requests as _requests
import os as _os
import dotenv as _dotenv
import random as _random
import constants as _constants
_dotenv.load_dotenv()
ACCESS_KEY = _os.getenv("ACCESS_KEY")
def _search_images() -> List:
query = _random.choice(_constants.UNSPLASH_QUERIES)
url=f"https://api.unsplash.com/search/photos?page=1&query={query}&client_id={ACCESS_KEY}"
response = _requests.get(url)
data=response.json()["results"]
return data
def _get_random_image_link():
response_data=_search_images()
randon_image_data=_random.choice(response_data)
link=randon_image_data["urls"]["regular"]
return link
def download_image():
filename="picture.jpg"
image_url=_get_random_image_link()
image_response=_requests.get(image_url, stream=True)
if image_response.status_code==200:
with open(filename, "wb") as image_file:
for chunk in image_response:
image_file.write(chunk)
download_image()