forked from tonedev/python-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unsplash.py
55 lines (44 loc) · 1.82 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import requests
import os
class Unsplash:
def __init__(self,search_term,per_page=20,quality="thumb"):
self.search_term = search_term
self.per_page = per_page
self.page = 0
self.quality = quality
self.headers =
{
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.5",
"Host": "unsplash.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0"
}
def set_url(self):
return f"https://unsplash.com/napi/search/photos?query={self.search_term}&xp=&per_page={self.per_page}&page={self.page}"
def make_request(self):
url = self.set_url()
return requests.request("GET",url,headers=self.headers)
def get_data(self):
self.data = self.make_request().json()
def save_path(self,name):
download_dir = "unsplash"
if not os.path.exists(download_dir):
os.mkdir(download_dir)
return f"{os.path.join(os.path.realpath(os.getcwd()),download_dir,name)}.jpg"
def download(self,url,name):
filepath = self.save_path(name)
with open(filepath,"wb") as f:
f.write(requests.request("GET",url,headers=self.headers).content)
def Scrapper(self,pages):
for page in range(0,pages+1):
self.make_request()
self.get_data()
for item in self.data['results']:
name = item['id']
url = item['urls'][self.quality]
self.download(url,name)
self.pages += 1
if __name__ == "__main__":
scrapper = Unsplash("cars")
scrapper.Scrapper(1)