@@ -40,7 +40,9 @@ async def get(cls, username: str, fresh=False) -> XClIdGen:
4040 tries += 1
4141 await asyncio .sleep (1 )
4242
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+ )
4446
4547
4648class Ctx :
@@ -53,21 +55,25 @@ async def aclose(self):
5355 await self .clt .aclose ()
5456
5557 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
5759 # https://github.com/vladkens/twscrape/issues/248
5860 path = urlparse (url ).path or "/"
5961
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
6569
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 )
6773
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+ )
7177
7278
7379def req_id (rep : Response ):
0 commit comments