feat: retry 3 times in one host

This commit is contained in:
xtaodada 2023-12-13 13:47:51 +08:00
parent b9ad65b01b
commit 7765f33700
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659

View File

@ -18,15 +18,28 @@ class HostNeedChange(Exception):
pass pass
def retry(func):
async def wrapper(*args, **kwargs):
for i in range(3):
try:
return await func(*args, **kwargs)
except HostNeedChange:
if i == 2:
raise HostNeedChange
continue
return wrapper
@retry
async def get(username: str, host: str) -> Optional[FeedParserDict]: async def get(username: str, host: str) -> Optional[FeedParserDict]:
url = f"{host}/twitter/user/{username}" url = f"{host}/twitter/user/{username}"
response = await request.get(url) response = await request.get(url)
if response.status_code == 200: if response.status_code == 200:
return parse(response.text) return parse(response.text)
elif response.status_code == 404: elif response.status_code == 404:
raise UsernameNotFound
raise HostNeedChange raise HostNeedChange
else:
return None
async def parse_tweets(data: List[FeedParserDict]) -> List[Tweet]: async def parse_tweets(data: List[FeedParserDict]) -> List[Tweet]: