diff --git a/main.py b/main.py index c4b6f40..2f0a129 100644 --- a/main.py +++ b/main.py @@ -249,27 +249,27 @@ class AnimePahe: return res_data async def _embed_to_m3u8(self, embed_url: str) -> Optional[str]: - """Open embed URL and capture the first .m3u8 network request.""" p = await self.context.new_page() m3u8 = None + found = asyncio.Event() def capture(req): nonlocal m3u8 - if ".m3u8" in req.url: + if ".m3u8" in req.url and not found.is_set(): m3u8 = req.url + found.set() p.on("request", capture) try: await p.set_extra_http_headers({"Referer": BASE_URL}) await p.goto(embed_url, wait_until="domcontentloaded") - for _ in range(10): - if m3u8: - break - await p.evaluate( - "document.querySelectorAll('button, video, [class*=play]')" - ".forEach(el => el.click())" - ) - await asyncio.sleep(0.5) + await p.evaluate( + "document.querySelectorAll('button, video, [class*=play]').forEach(el => el.click())" + ) + try: + await asyncio.wait_for(found.wait(), timeout=5.0) + except asyncio.TimeoutError: + pass finally: await p.close() return m3u8