-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixel_async.py
41 lines (29 loc) · 1.23 KB
/
pixel_async.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
import asyncio
import httpx
from key import keys
async def downloadd(query, current_page):
header = keys
params = {"query": query, "per_page": 1, "page": current_page}
url = 'https://api.pexels.com/v1/search'
async with httpx.AsyncClient() as client:
r = await client.get(url, headers=header, params=params)
if r.status_code == 200:
_r = r.json()
for item in _r.get("photos"):
print(item.get("src").get("original"))
else:
print(r.status_code)
print(f'{query} = {current_page}')
async def main():
queve = asyncio.Queue()
query = input('введите раздел картинок которые вы хотите загрузить: ')
page_count = int(input('введите количество картинок которые вы хотите загрузить: '))
current_page = 0
task_list = []
while current_page < page_count:
current_page += 1
task = asyncio.create_task(downloadd(query, current_page))
task_list.append(task)
await queve.join()
await asyncio.gather(*task_list, return_exceptions=True) # <-- return_exceptions = возврат исключений
asyncio.run(main())