1717 Browser ,
1818 PlaywrightContextManager ,
1919)
20- from playwright ._impl ._api_types import Error
20+ from playwright ._impl ._api_types import Error as PlaywrightError
2121
2222from .data import CookieManager , StreamResponse
2323
@@ -76,6 +76,7 @@ async def playwright_start(self):
7676 if self .proxies
7777 else None , # your proxy
7878 )
79+ # ua = None
7980 ua = f"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/{ self .browser .version } "
8081 # ua = f"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{self.browser.version} Safari/537.36"
8182 self .content = await self .browser .new_context (user_agent = ua )
@@ -128,8 +129,10 @@ async def init_page(self, restart: bool = False):
128129 await page .expose_function ("get_cookie" , self .get_cookie )
129130 return page
130131
131- async def get_cf_cookies (self , retry : int = 20 ) -> None :
132+ async def get_cf_cookies (self , retry : int = 20 , wait : bool = False ) -> None :
132133 await self .set_status (False )
134+ if wait :
135+ await self .wait_for_task ()
133136 logger .debug ("Start get Cloudflare cookies" )
134137 await self .content .add_cookies (
135138 [
@@ -180,6 +183,7 @@ async def get_cf_cookies(self, retry: int = 20) -> None:
180183
181184 async def wait_for_task (self , timeout : int = 60 ):
182185 """Wait for task, called when restart"""
186+ logger .debug ("Wait for task..." )
183187 await self .set_status (False )
184188 await self .page .evaluate (
185189 "([timeout]) => waitForNoFetch(timeout)" , [timeout * 1000 ]
@@ -246,7 +250,7 @@ async def _call_api(
246250 logger .warning (
247251 "Cloudflare cookies had expired, trying to get new Cloudflare cookies..."
248252 )
249- asyncio .ensure_future (self .get_cf_cookies ())
253+ asyncio .ensure_future (self .get_cf_cookies (wait = True ))
250254 elif response .status == 429 and "/api/auth/session" in url :
251255 logger .warning (
252256 "Too many session requests, trying to get new Cloudflare cookies..."
@@ -262,12 +266,11 @@ async def _call_api(
262266 "set-cookie"
263267 ] = f"{ SESSION_TOKEN_KEY } ={ token } ; path=/; max-age=31536000; secure; httponly"
264268 yield response
265- except Error as e :
269+ except PlaywrightError as e :
266270 logger .error (f"Playwright Error: { e .message } " )
267271 yield StreamResponse (status = 403 )
268- except Exception as e :
269- logger .opt (exception = e ).error ("Call API failed" )
270- yield response
272+ # except Exception as e:
273+ # logger.opt(exception=e).error("Call API failed")
271274
272275
273276class SessionManager :
@@ -290,7 +293,7 @@ async def get_sessions(self):
290293 return [
291294 session
292295 for session in self .sessions
293- if await session .get_status () and not session .running
296+ if ( await session .get_status () and not session .running )
294297 ]
295298
296299 async def get_session (self ):
@@ -331,14 +334,14 @@ async def call_api(
331334 method , url , headers , data = data , session_token = session_token
332335 ) as resp :
333336 if resp .status == 403 :
334- if first :
335- # Retry once
336- async with self .call_api (
337- method , url , headers , data , session_token , False
338- ) as resp :
339- yield resp
340- else :
337+ if not first :
341338 resp .status = 499
342339 yield resp
343340 else :
344341 yield resp
342+ if resp .status == 403 and first :
343+ # Retry once
344+ async with self .call_api (
345+ method , url , headers , data , session_token , False
346+ ) as resp :
347+ yield resp
0 commit comments