From 75a6989a7f76d8e34aad58d9703435bba6c02e2c Mon Sep 17 00:00:00 2001 From: scdhh Date: Wed, 29 Nov 2023 11:00:55 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E4=BD=BF=E7=94=A8=E4=B8=8A?= =?UTF-8?q?=E4=B8=8B=E6=96=87=E7=AE=A1=E7=90=86=E5=99=A8=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_tetris_stats/utils/request.py | 55 +++++++++----------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/nonebot_plugin_tetris_stats/utils/request.py b/nonebot_plugin_tetris_stats/utils/request.py index 5919d1d..f599e2d 100644 --- a/nonebot_plugin_tetris_stats/utils/request.py +++ b/nonebot_plugin_tetris_stats/utils/request.py @@ -38,7 +38,7 @@ def splice_url(url_list: list[str]) -> str: class Request: """网络请求相关类""" - _CACHE_FILE = CACHE_PATH.joinpath('cloudflare_cache.json') + _CACHE_FILE = CACHE_PATH / 'cloudflare_cache.json' _headers: dict | None = None _cookies: dict | None = None @@ -46,36 +46,31 @@ class Request: async def _anti_cloudflare(cls, url: str) -> bytes: """用firefox硬穿五秒盾""" browser = await BrowserManager.get_browser() - context = await browser.new_context() - page = await context.new_page() - response = await page.goto(url) - attempts = 0 - while attempts < 60: # noqa: PLR2004 - attempts += 1 - text = await page.locator('body').text_content() - if text is None: - await page.wait_for_timeout(1000) - continue - if await page.title() == 'Please Wait... | Cloudflare': - logger.warning('疑似触发了 Cloudflare 的验证码') - break - try: - loads(text) - except JSONDecodeError: - await page.wait_for_timeout(1000) - else: - if not isinstance(response, Response): - raise RequestError('api请求失败') - cls._headers = await response.request.all_headers() + async with await browser.new_context() as context, await context.new_page() as page: + response = await page.goto(url) + attempts = 0 + while attempts < 60: # noqa: PLR2004 + attempts += 1 + text = await page.locator('body').text_content() + if text is None: + await page.wait_for_timeout(1000) + continue + if await page.title() == 'Please Wait... | Cloudflare': + logger.warning('疑似触发了 Cloudflare 的验证码') + break try: - cls._cookies = {i['name']: i['value'] for i in await context.cookies()} - except KeyError: - cls._cookies = None - await page.close() - await context.close() - return await response.body() - await page.close() - await context.close() + loads(text) + except JSONDecodeError: + await page.wait_for_timeout(1000) + else: + if not isinstance(response, Response): + raise RequestError('api请求失败') + cls._headers = await response.request.all_headers() + try: + cls._cookies = {i['name']: i['value'] for i in await context.cookies()} + except KeyError: + cls._cookies = None + return await response.body() raise RequestError('绕过五秒盾失败') @classmethod