@@ -40,7 +40,9 @@ async def get(cls, username: str, fresh=False) -> XClIdGen:
40
40
tries += 1
41
41
await asyncio .sleep (1 )
42
42
43
- raise Exception ("Failed to create XClIdGen after 3 tries." )
43
+ raise AbortReqError (
44
+ "Faield to create XClIdGen. See: https://github.com/vladkens/twscrape/issues/248"
45
+ )
44
46
45
47
46
48
class Ctx :
@@ -53,21 +55,25 @@ async def aclose(self):
53
55
await self .clt .aclose ()
54
56
55
57
async def req (self , method : str , url : str , params : ReqParams = None ) -> Response :
56
- # if code 404 on first try then generate new x-client-transaction-id and retry once
58
+ # if code 404 on first try then generate new x-client-transaction-id and retry
57
59
# https://github.com/vladkens/twscrape/issues/248
58
60
path = urlparse (url ).path or "/"
59
61
60
- gen = await XClIdGenStore .get (self .acc .username )
61
- hdr = {"x-client-transaction-id" : gen .calc (method , path )}
62
- rep = await self .clt .request (method , url , params = params , headers = hdr )
63
- if rep .status_code != 404 :
64
- return rep
62
+ tries = 0
63
+ while tries < 3 :
64
+ gen = await XClIdGenStore .get (self .acc .username , fresh = tries > 0 )
65
+ hdr = {"x-client-transaction-id" : gen .calc (method , path )}
66
+ rep = await self .clt .request (method , url , params = params , headers = hdr )
67
+ if rep .status_code != 404 :
68
+ return rep
65
69
66
- logger .debug (f"Retrying request with new x-client-transaction-id: { url } " )
70
+ tries += 1
71
+ logger .debug (f"Retrying request with new x-client-transaction-id: { url } " )
72
+ await asyncio .sleep (1 )
67
73
68
- gen = await XClIdGenStore . get ( self . acc . username , fresh = True )
69
- hdr = { "x-client-transaction-id" : gen . calc ( method , path )}
70
- return await self . clt . request ( method , url , params = params , headers = hdr )
74
+ raise AbortReqError (
75
+ "Faield to get XClIdGen. See: https://github.com/vladkens/twscrape/issues/248"
76
+ )
71
77
72
78
73
79
def req_id (rep : Response ):
0 commit comments