Skip to content

Commit c6fe868

Browse files
authored
Create main.py
1 parent 66d3527 commit c6fe868

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

Pixabay Scraper/main.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import requests
2+
3+
4+
class Pixabay():
5+
"""
6+
Class - `Pixabay`\n
7+
8+
| Methods | Details |
9+
| --------------------------- | ---------------------------------------------------------------------------------------------------- |
10+
| `.get_video()` | Downloads the videos from pixaby to the local storage. |
11+
| `.get_photo()` | Downloads the photos from pixaby to local storage. |
12+
13+
14+
"""
15+
def __init__(self, verbose=True):
16+
self.__api_key = '38504833-19606430bd8fde504120d1630'
17+
self.name = 'Pixabay'
18+
self.verbose = verbose
19+
20+
def __get_params_video(self, query, num, update_params={}):
21+
22+
"""
23+
Class - `Pixabay`
24+
Example:
25+
```
26+
quora = Pixabay()
27+
quora.get_params_video(query,num)
28+
```
29+
Returns:
30+
```js
31+
{
32+
'key': API Key from Pixaby,
33+
'q': query given by user,
34+
'video_type': typeof video(default is film),
35+
'orientation': orientation of the video,
36+
'safesearch': prevents adult content,
37+
'per_page': number of videos to be fetched
38+
}
39+
```
40+
"""
41+
params = {
42+
'key': self.__api_key,
43+
'q': query,
44+
'video_type': 'film',
45+
'orientation': 'horizontal',
46+
'safesearch': 'true',
47+
'per_page': num
48+
}
49+
params.update(update_params)
50+
return params
51+
52+
def get_video(self, query, num=10, params={}):
53+
54+
"""
55+
Class - `Pixabay`
56+
Example:
57+
```
58+
quora = Pixabay()
59+
quora.get_video(query,num)
60+
```
61+
Returns: Downloads num number of videos into local storage.
62+
63+
"""
64+
if self.key == '':
65+
return 'Call setkey(key) method to set up the key first before using the scraper.'
66+
BASE_URL = 'https://pixabay.com/api/videos/'
67+
_params = self.__get_params_video(query, num, params)
68+
response = requests.get(BASE_URL, params=_params)
69+
70+
data = response.json()
71+
hits = data['hits']
72+
for i, hit in enumerate(hits):
73+
if self.verbose:
74+
print(f" Downloading Pixabay videos {i+1}/{num}")
75+
video_url = hit['videos']['large']['url']
76+
response = requests.get(video_url)
77+
if response is not None:
78+
with open(f'video_pixabay_{i+1:02d}.mp4', 'wb') as f:
79+
f.write(response.content)
80+
81+
def __get_params_photo(self, query, num, update_params={}):
82+
"""
83+
Class - `Pixabay`
84+
Example:
85+
```
86+
quora = Pixabay()
87+
quora.get_params_photo(query,num)
88+
```
89+
Returns:
90+
```js
91+
{
92+
'key': API Key from Pixaby,
93+
'q': query given by user,
94+
'video_type': type of photo,
95+
'orientation': orientation of the photo,
96+
'safesearch': prevents adult content,
97+
'per_page': number of images to be fetched
98+
}
99+
```
100+
"""
101+
params = {
102+
'key': self.__api_key,
103+
'q': query,
104+
'image_type': 'photo',
105+
'orientation': 'horizontal',
106+
'safesearch': 'true',
107+
'per_page': num
108+
}
109+
params.update(update_params)
110+
return params
111+
112+
def get_photo(self, query, num=10, params={}):
113+
"""
114+
Class - `Pixabay`
115+
Example:
116+
```
117+
quora = Pixabay()
118+
quora.get_photo(query,num)
119+
```
120+
Returns: Downloads num number of photos into local storage.
121+
122+
"""
123+
if self.key == '':
124+
return 'Call setkey(key) method to set up the key first before using the scraper.'
125+
BASE_URL = 'https://pixabay.com/api/'
126+
_params = self.__get_params_photo(query, num, params)
127+
response = requests.get(BASE_URL, params=_params)
128+
129+
data = response.json()
130+
hits = data['hits']
131+
for i, hit in enumerate(hits):
132+
if self.verbose:
133+
print(f" Downloading Pixabay photos {i+1}/{num}")
134+
image_url = hit['largeImageURL']
135+
response = requests.get(image_url)
136+
if response is not None:
137+
with open(f'photo_pixabay_{i+1:02d}.jpg', 'wb') as f:
138+
f.write(response.content)
139+
140+
141+

0 commit comments

Comments
 (0)