Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 402b5b3

Browse files
committedMar 21, 2024
fix: asyncio
1 parent 72b72cb commit 402b5b3

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed
 

‎api_request.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
download_folder.mkdir()
1919

2020
headers = {
21-
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
21+
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;'
22+
'q=0.8,application/signed-exchange;v=b3;q=0.7',
2223
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
2324
'cache-control': 'no-cache',
2425
'dnt': '1',
@@ -31,7 +32,8 @@
3132
'sec-fetch-site': 'none',
3233
'sec-fetch-user': '?1',
3334
'upgrade-insecure-requests': '1',
34-
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36',
35+
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) '
36+
'Chrome/123.0.0.0 Safari/537.36',
3537
}
3638
httpx_cookies = httpx.Cookies()
3739
limit = asyncio.Semaphore(20)
@@ -73,11 +75,11 @@ def load_cookies():
7375
async def get(url: str, params: dict) -> Response:
7476
try:
7577
async with limit:
76-
async with httpx.AsyncClient() as client:
78+
async with httpx.AsyncClient(timeout=30) as client:
7779
response = await client.get(url, params=params, cookies=httpx_cookies, headers=headers)
7880
return response
7981
except Exception as e:
80-
print(e)
82+
tqdm.tqdm.write(f'Error: {url} {e}')
8183
time.sleep(1)
8284
return await get(url, params)
8385

@@ -105,7 +107,7 @@ async def download(url: str, emote_name: str, ids: str, filename: str) -> None:
105107
else:
106108
return
107109
except Exception as e:
108-
print(e, url)
110+
tqdm.tqdm.write(f'Error: {e} {url}')
109111

110112

111113
# 获取表情列表
@@ -117,7 +119,7 @@ async def get_emote_list() -> Union[None, Dict[str, Union[str, Dict[str, str]]]]
117119
if response['code'] == 0:
118120
return response['data']['all_packages']
119121
else:
120-
print('error code:', response['code'])
122+
tqdm.tqdm.write('error code:', response['code'])
121123
return None
122124

123125

@@ -134,11 +136,11 @@ async def get_emote_detail_list(data) -> List:
134136
return list(chain(*resp))
135137

136138

137-
def download_emote_list() -> List:
138-
# emote_list = await get_emote_list()
139-
# emote_detail_list = await get_emote_detail_list(emote_list)
140-
emote_list = asyncio.run(get_emote_list())
141-
emote_detail_list = asyncio.run(get_emote_detail_list(emote_list))
139+
async def download_emote_list() -> List:
140+
emote_list = await get_emote_list()
141+
emote_detail_list = await get_emote_detail_list(emote_list)
142+
# emote_list = asyncio.run(get_emote_list())
143+
# emote_detail_list = asyncio.run(get_emote_detail_list(emote_list))
142144
if emote_list is None:
143145
return []
144146
else:
@@ -167,7 +169,7 @@ def download_emote_list() -> List:
167169

168170
async def main():
169171
load_cookies()
170-
resp = download_emote_list()
172+
resp = await download_emote_list()
171173
task_list = []
172174
for i in resp:
173175
task_list.append(asyncio.create_task(download(*i)))

0 commit comments

Comments
 (0)
Please sign in to comment.