-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
69 lines (62 loc) · 2.25 KB
/
main.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import time
from json import JSONDecodeError
from Spider import *
from config import *
import os
def get_first_page(search_url: str):
token_num = 0
response = session.get(search_url, headers=headers)
while response.status_code != 200:
token_num += 1
if token_num == len(access_token):
print("waitting...download")
time.sleep(1000 * 60)
token_num = 0
headers.get('Authorization').format(access_token[token_num])
response = session.get(search_url, headers=headers)
page = response.json()
return page
def push_download_url(search_url, first_page):
with open('download_url.txt', 'a+') as f:
all_page_num = int(first_page['total_pages'])
if all_page_num > download_pages:
all_page_num = download_pages
for page in range(1, all_page_num):
response = session.get(search_url + f'&page={page}', headers=headers)
while response.status_code == 403:
print("waitting...download")
time.sleep(1000 * 60)
response = session.get(search_url + f'&page={page}', headers=headers)
dataset = response.json()['results']
for data in dataset:
download_url = data['links']['download']
f.write(download_url + '\n')
Downloader.put_download_url(pic={
'tag': tag,
'url': download_url
})
f.close()
if __name__ == '__main__':
headers = {
'Authorization': f'Client-ID {access_token[0]}'
} # 请求头(不需要改)
session = requests.session()
downloads = []
for i in range(0, 4):
downloads.append(Downloader())
if not os.path.exists(f'./File'):
os.mkdir(f'./File')
# try:
for tag in tags:
if not os.path.exists(f'./File/{tag}'):
os.mkdir(f'./File/{tag}')
search_url = f'{url}{keyword}?query={tag}'
first_page = get_first_page(search_url)
push_download_url(search_url=search_url, first_page=first_page)
# except Exception as e:
# print(e)
# pass
for i in range(0, 4):
print(f"get pic over.....{i}")
downloads[i].set_over()
downloads[i].join()