mirror of
https://github.com/Xtao-Labs/twitter2telegram.git
synced 2024-11-24 00:31:28 +00:00
feat: retry 3 times in one host
This commit is contained in:
parent
b9ad65b01b
commit
7765f33700
19
defs/feed.py
19
defs/feed.py
@ -18,15 +18,28 @@ class HostNeedChange(Exception):
|
||||
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]:
|
||||
url = f"{host}/twitter/user/{username}"
|
||||
response = await request.get(url)
|
||||
if response.status_code == 200:
|
||||
return parse(response.text)
|
||||
elif response.status_code == 404:
|
||||
raise HostNeedChange
|
||||
else:
|
||||
return None
|
||||
raise UsernameNotFound
|
||||
raise HostNeedChange
|
||||
|
||||
|
||||
async def parse_tweets(data: List[FeedParserDict]) -> List[Tweet]:
|
||||
|
Loading…
Reference in New Issue
Block a user