添加不合法cookie的提示

This commit is contained in:
suyiiyii 2024-09-06 10:08:08 +08:00
parent afd1bee762
commit 418a941448
2 changed files with 10 additions and 7 deletions

View File

@ -55,8 +55,9 @@ def do_add_cookie(add_cookie: type[Matcher]):
async def got_cookie(state: T_State, cookie: Message = Arg()):
client_mgr: CookieClientManager = platform_manager[state["platform"]].site.client_mgr
cookie_text = cookie.extract_plain_text()
if not await client_mgr.valid_cookie(cookie_text):
await add_cookie.reject("无效的 Cookie请检查后重新输入详情见<待添加的文档>")
state["cookie"] = cookie_text
state["name"] = await client_mgr.valid_cookie(cookie_text)
@add_cookie.handle()
async def add_cookie_process(state: T_State):

View File

@ -1,5 +1,6 @@
import json
from typing import Literal
from json import JSONDecodeError
from abc import ABC, abstractmethod
from datetime import datetime, timedelta
@ -61,30 +62,31 @@ class CookieClientManager(ClientManager):
@classmethod
async def init_cookie(cls, cookie: Cookie) -> Cookie:
"""初始化cookie添加用户cookie时使用"""
"""初始化 cookie添加用户 cookie 时使用"""
cookie.cd = cls._cookie_cd
cookie.last_usage = datetime.now() # 使得优先使用用户 cookie
return cookie
@classmethod
async def valid_cookie(cls, content: str) -> bool:
"""验证cookie是否有效添加cookie时用,可根据平台的具体情况进行重写"""
"""验证 cookie 内容是否有效,添加 cookie 时用,可根据平台的具体情况进行重写"""
try:
data = json.loads(content)
if not isinstance(data, dict):
raise ValueError
except Exception:
return False
except JSONDecodeError:
return False
return True
@classmethod
async def get_cookie_friendly_name(cls, cookie: Cookie) -> str:
"""获取友好的cookie名字,用于展示"""
"""获取 cookie 的友好名字,用于展示"""
from . import text_fletten
return text_fletten(f"{cookie.platform_name} [{cookie.content[:10]}]")
def _generate_hook(self, cookie: Cookie):
"""hook函数生成器,用于回写请求状态到数据库"""
"""hook 函数生成器,用于回写请求状态到数据库"""
async def _response_hook(resp: httpx.Response):
if resp.status_code == 200: