HonkaiStarRailWikiDataParser/func/client.py

39 lines
1.3 KiB
Python
Raw Normal View History

2023-08-29 05:15:55 +00:00
import asyncio
2023-04-23 14:06:11 +00:00
2024-01-16 16:18:03 +00:00
from httpx import AsyncClient, TimeoutException, HTTPStatusError
2023-04-23 14:06:11 +00:00
headers = {
2023-08-29 05:15:55 +00:00
"authority": "api-static.mihoyo.com",
"accept": "application/json, text/plain, */*",
"accept-language": "zh-CN,zh;q=0.9,zh-Hans;q=0.8,und;q=0.7,en;q=0.6,zh-Hant;q=0.5,ja;q=0.4",
"dnt": "1",
"origin": "https://bbs.mihoyo.com",
"referer": "https://bbs.mihoyo.com/",
"sec-ch-ua": '"Chromium";v="110", "Not A(Brand";v="24"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"Windows"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36",
2023-04-23 14:06:11 +00:00
}
2023-04-27 08:44:27 +00:00
client = AsyncClient(headers=headers, timeout=120.0)
2023-08-29 05:15:55 +00:00
def retry(func):
async def wrapper(*args, **kwargs):
2024-01-16 16:18:03 +00:00
for i in range(5):
2023-08-29 05:15:55 +00:00
try:
return await func(*args, **kwargs)
2024-01-16 16:18:03 +00:00
except HTTPStatusError as exc:
if exc.response.status_code == 522:
print(f"重试 {func.__name__} {i + 1}")
await asyncio.sleep(1)
else:
raise exc
2023-08-29 05:15:55 +00:00
except TimeoutException:
print(f"重试 {func.__name__} {i + 1}")
await asyncio.sleep(1)
return wrapper