👽️ 由于 osk 封了 aiohttp 所以换 httpx

This commit is contained in:
2023-09-21 11:57:53 +08:00
parent 5dbd01b15c
commit 0d78007262

View File

@@ -1,7 +1,7 @@
import os import os
from typing import Any from typing import Any
import aiohttp from httpx import AsyncClient, HTTPError
from nonebot import get_driver from nonebot import get_driver
from nonebot.log import logger from nonebot.log import logger
from playwright.async_api import Browser, Response, async_playwright from playwright.async_api import Browser, Response, async_playwright
@@ -27,26 +27,27 @@ async def _():
class Request: class Request:
'''网络请求相关类''' """网络请求相关类"""
_browser: Browser | None = None _browser: Browser | None = None
_headers: dict | None = None _headers: dict | None = None
_cookies: dict | None = None _cookies: dict | None = None
@classmethod @classmethod
async def _init_playwright(cls) -> Browser: async def _init_playwright(cls) -> Browser:
'''初始化playwright''' """初始化playwright"""
playwright = await async_playwright().start() playwright = await async_playwright().start()
cls._browser = await playwright.firefox.launch() cls._browser = await playwright.firefox.launch()
return cls._browser return cls._browser
@classmethod @classmethod
async def _get_browser(cls) -> Browser: async def _get_browser(cls) -> Browser:
'''获取浏览器对象''' """获取浏览器对象"""
return cls._browser or await cls._init_playwright() return cls._browser or await cls._init_playwright()
@classmethod @classmethod
async def _anti_cloudflare(cls, url: str) -> tuple[bool, bool, dict[str, Any]]: async def _anti_cloudflare(cls, url: str) -> tuple[bool, bool, dict[str, Any]]:
'''用firefox硬穿五秒盾''' """用firefox硬穿五秒盾"""
browser = await cls._get_browser() browser = await cls._get_browser()
context = await browser.new_context() context = await browser.new_context()
page = await context.new_page() page = await context.new_page()
@@ -58,7 +59,7 @@ class Request:
if text is None: if text is None:
await page.wait_for_timeout(1000) await page.wait_for_timeout(1000)
continue continue
if await page.title() == 'Please Wait... | Cloudflare': if await page.title() == "Please Wait... | Cloudflare":
# TODO 有无人来做一个过验证码( # TODO 有无人来做一个过验证码(
break break
try: try:
@@ -69,40 +70,35 @@ class Request:
assert isinstance(response, Response) assert isinstance(response, Response)
cls._headers = await response.request.all_headers() cls._headers = await response.request.all_headers()
try: try:
cls._cookies = {i['name']: i['value'] for i in await context.cookies()} cls._cookies = {
i["name"]: i["value"] for i in await context.cookies()
}
except KeyError: except KeyError:
cls._cookies = None cls._cookies = None
await page.close() await page.close()
await context.close() await context.close()
return True, data['success'], data return True, data["success"], data
await page.close() await page.close()
await context.close() await context.close()
return True, False, {'error': '绕过五秒盾失败'} return True, False, {"error": "绕过五秒盾失败"}
@classmethod @classmethod
async def init_cache(cls) -> None: async def init_cache(cls) -> None:
'''初始化缓存文件''' """初始化缓存文件"""
if not os.path.exists(os.path.dirname(config.cache_path)): if not os.path.exists(os.path.dirname(config.cache_path)):
os.makedirs(os.path.dirname(config.cache_path)) os.makedirs(os.path.dirname(config.cache_path))
if not os.path.exists(config.cache_path): if not os.path.exists(config.cache_path):
with open(file=config.cache_path, mode='w', encoding='UTF-8') as file: with open(file=config.cache_path, mode="w", encoding="UTF-8") as file:
file.write( file.write(dumps({"headers": cls._headers, "cookies": cls._cookies}))
dumps(
{
'headers': cls._headers,
'cookies': cls._cookies
}
)
)
@classmethod @classmethod
async def read_cache(cls) -> None: async def read_cache(cls) -> None:
'''读取缓存文件''' """读取缓存文件"""
try: try:
with open(file=config.cache_path, mode='r', encoding='UTF-8') as file: with open(file=config.cache_path, mode="r", encoding="UTF-8") as file:
json = loads(file.read()) json = loads(file.read())
cls._headers = json['headers'] cls._headers = json["headers"]
cls._cookies = json['cookies'] cls._cookies = json["cookies"]
except FileNotFoundError: except FileNotFoundError:
await cls.init_cache() await cls.init_cache()
except PermissionError: except PermissionError:
@@ -114,17 +110,10 @@ class Request:
@classmethod @classmethod
async def write_cache(cls) -> None: async def write_cache(cls) -> None:
'''写入缓存文件''' """写入缓存文件"""
try: try:
with open(file=config.cache_path, mode='r+', encoding='UTF-8') as file: with open(file=config.cache_path, mode="r+", encoding="UTF-8") as file:
file.write( file.write(dumps({"headers": cls._headers, "cookies": cls._cookies}))
dumps(
{
'headers': cls._headers,
'cookies': cls._cookies
}
)
)
except FileNotFoundError: except FileNotFoundError:
await cls.init_cache() await cls.init_cache()
except PermissionError: except PermissionError:
@@ -136,20 +125,20 @@ class Request:
@classmethod @classmethod
async def request(cls, url: str) -> tuple[bool, bool, dict[str, Any]]: async def request(cls, url: str) -> tuple[bool, bool, dict[str, Any]]:
'''请求api''' """请求api"""
try: try:
async with aiohttp.ClientSession(cookies=cls._cookies) as session: async with AsyncClient(cookies=cls._cookies) as session:
async with session.get(url, headers=cls._headers) as resp: response = await session.get(url, headers=cls._headers)
data = await resp.json() data = loads(response.content)
return True, data['success'], data return True, data["success"], data
except aiohttp.client_exceptions.ClientConnectorError as error: # type: ignore except HTTPError as error:
logger.error(f'请求错误\n{error}') logger.error(f"请求错误\n{error}")
return False, False, {} return False, False, {}
except aiohttp.client_exceptions.ContentTypeError: # type: ignore except JSONDecodeError:
return await cls._anti_cloudflare(url) return await cls._anti_cloudflare(url)
@classmethod @classmethod
async def close_browser(cls) -> None: async def close_browser(cls) -> None:
'''关闭浏览器对象''' """关闭浏览器对象"""
if isinstance(cls._browser, Browser): if isinstance(cls._browser, Browser):
await cls._browser.close() await cls._browser.close()