Compare commits

..

3 Commits
0.4.2 ... 0.4.3

Author SHA1 Message Date
1219aeda8f 🔖 0.4.3 2023-09-21 11:58:21 +08:00
0d78007262 👽️ 由于 osk 封了 aiohttp 所以换 httpx 2023-09-21 11:57:53 +08:00
5dbd01b15c 添加依赖 httpx 2023-09-21 11:55:37 +08:00
3 changed files with 91 additions and 46 deletions

View File

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

61
poetry.lock generated
View File

@@ -1,4 +1,4 @@
# This file is automatically @generated by Poetry 1.5.1 and should not be changed by hand.
# This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand.
[[package]]
name = "aiohttp"
@@ -308,6 +308,17 @@ files = [
{file = "Brotli-1.0.9.zip", hash = "sha256:4d1b810aa0ed773f81dceda2cc7b403d01057458730e309856356d4ef4188438"},
]
[[package]]
name = "certifi"
version = "2023.7.22"
description = "Python package for providing Mozilla's CA Bundle."
optional = false
python-versions = ">=3.6"
files = [
{file = "certifi-2023.7.22-py3-none-any.whl", hash = "sha256:92d6037539857d8206b8f6ae472e8b77db8058fec5937a1ef3f54304089edbb9"},
{file = "certifi-2023.7.22.tar.gz", hash = "sha256:539cc1d13202e33ca466e88b2807e29f4c13049d6d87031a3c110744495cb082"},
]
[[package]]
name = "charset-normalizer"
version = "2.1.1"
@@ -549,6 +560,27 @@ files = [
{file = "h11-0.14.0.tar.gz", hash = "sha256:8f19fbbe99e72420ff35c00b27a34cb9937e902a8b810e2c88300c6f0a3b699d"},
]
[[package]]
name = "httpcore"
version = "0.18.0"
description = "A minimal low-level HTTP client."
optional = false
python-versions = ">=3.8"
files = [
{file = "httpcore-0.18.0-py3-none-any.whl", hash = "sha256:adc5398ee0a476567bf87467063ee63584a8bce86078bf748e48754f60202ced"},
{file = "httpcore-0.18.0.tar.gz", hash = "sha256:13b5e5cd1dca1a6636a6aaea212b19f4f85cd88c366a2b82304181b769aab3c9"},
]
[package.dependencies]
anyio = ">=3.0,<5.0"
certifi = "*"
h11 = ">=0.13,<0.15"
sniffio = "==1.*"
[package.extras]
http2 = ["h2 (>=3,<5)"]
socks = ["socksio (==1.*)"]
[[package]]
name = "httptools"
version = "0.5.0"
@@ -602,6 +634,29 @@ files = [
[package.extras]
test = ["Cython (>=0.29.24,<0.30.0)"]
[[package]]
name = "httpx"
version = "0.25.0"
description = "The next generation HTTP client."
optional = false
python-versions = ">=3.8"
files = [
{file = "httpx-0.25.0-py3-none-any.whl", hash = "sha256:181ea7f8ba3a82578be86ef4171554dd45fec26a02556a744db029a0a27b7100"},
{file = "httpx-0.25.0.tar.gz", hash = "sha256:47ecda285389cb32bb2691cc6e069e3ab0205956f681c5b2ad2325719751d875"},
]
[package.dependencies]
certifi = "*"
httpcore = ">=0.18.0,<0.19.0"
idna = "*"
sniffio = "*"
[package.extras]
brotli = ["brotli", "brotlicffi"]
cli = ["click (==8.*)", "pygments (==2.*)", "rich (>=10,<14)"]
http2 = ["h2 (>=3,<5)"]
socks = ["socksio (==1.*)"]
[[package]]
name = "idna"
version = "3.4"
@@ -1116,7 +1171,7 @@ files = [
[package.dependencies]
numpy = [
{version = ">=1.21.0", markers = "python_version >= \"3.10\""},
{version = ">=1.21.0", markers = "python_version >= \"3.10\" and python_version < \"3.11\""},
{version = ">=1.23.2", markers = "python_version >= \"3.11\""},
]
python-dateutil = ">=2.8.1"
@@ -1901,4 +1956,4 @@ multidict = ">=4.0"
[metadata]
lock-version = "2.0"
python-versions = "^3.10,<3.12"
content-hash = "7c10b6db4d8cf72d4453cf8eaa0d622eef3958483cc0fc239c08bd4f7dc9151b"
content-hash = "afbec27d3b608d5bceef47355368e1643dc211e194cb5d0f6b021b3a9fed60ef"

View File

@@ -1,6 +1,6 @@
[tool.poetry]
name = "nonebot-plugin-tetris-stats"
version = "0.4.2"
version = "0.4.3"
description = "一个基于nonebot2的用于查询TETRIS相关游戏玩家数据的插件"
authors = ["scdhh <wallfjjd@gmail.com>"]
readme = "README.md"
@@ -19,6 +19,7 @@ pandas = "^1.4.3"
playwright = "^1.24.1"
ujson = "^5.4.0"
Brotli = "^1.0.9"
httpx = "^0.25.0"
[tool.poetry.dev-dependencies]
mypy = "^0.991"