mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-05 19:36:43 +08:00
✨ CookieClientManager中提出_generate_anonymous_cookie方法
This commit is contained in:
parent
ff1c3c3159
commit
e84894cd9e
@ -46,16 +46,32 @@ class CookieClientManager(ClientManager):
|
|||||||
_site_name: str # 绑定的 site_name,需要使用 create_cookie_client_manager 创建 Client_mgr 时绑定
|
_site_name: str # 绑定的 site_name,需要使用 create_cookie_client_manager 创建 Client_mgr 时绑定
|
||||||
_default_cd: int = timedelta(seconds=10)
|
_default_cd: int = timedelta(seconds=10)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def _generate_anonymous_cookie(cls) -> Cookie:
|
||||||
|
return Cookie(
|
||||||
|
cookie_name=f"{cls._site_name} anonymous",
|
||||||
|
site_name=cls._site_name,
|
||||||
|
content="{}",
|
||||||
|
is_universal=True,
|
||||||
|
is_anonymous=True,
|
||||||
|
last_usage=datetime.now(),
|
||||||
|
cd_milliseconds=0,
|
||||||
|
tags="{}",
|
||||||
|
status="",
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def refresh_anonymous_cookie(cls):
|
async def refresh_anonymous_cookie(cls):
|
||||||
"""移除已有的匿名cookie,添加一个新的匿名cookie"""
|
"""更新已有的匿名cookie,若不存在则添加"""
|
||||||
existing_anonymous_cookies = await config.get_cookie(cls._site_name, is_anonymous=True)
|
existing_anonymous_cookies = await config.get_cookie(cls._site_name, is_anonymous=True)
|
||||||
new_anonymous_cookie = Cookie(site_name=cls._site_name, content="{}", is_universal=True, is_anonymous=True)
|
if existing_anonymous_cookies:
|
||||||
for cookie in existing_anonymous_cookies:
|
for cookie in existing_anonymous_cookies:
|
||||||
await config.delete_cookie_by_id(cookie.id)
|
new_anonymous_cookie = await cls._generate_anonymous_cookie()
|
||||||
new_anonymous_cookie.id = cookie.id # 保持原有的id
|
new_anonymous_cookie.id = cookie.id # 保持原有的id
|
||||||
new_anonymous_cookie.last_usage = datetime.now() # 使得第一次请求优先使用用户 cookie
|
await config.update_cookie(new_anonymous_cookie)
|
||||||
await config.add_cookie(new_anonymous_cookie)
|
else:
|
||||||
|
new_anonymous_cookie = await cls._generate_anonymous_cookie()
|
||||||
|
await config.add_cookie(new_anonymous_cookie)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def add_user_cookie(cls, content: str):
|
async def add_user_cookie(cls, content: str):
|
||||||
@ -120,7 +136,7 @@ class CookieClientManager(ClientManager):
|
|||||||
return http_client()
|
return http_client()
|
||||||
|
|
||||||
async def refresh_client(self):
|
async def refresh_client(self):
|
||||||
pass
|
self.refresh_anonymous_cookie()
|
||||||
|
|
||||||
|
|
||||||
def is_cookie_client_manager(manger: type[ClientManager]) -> bool:
|
def is_cookie_client_manager(manger: type[ClientManager]) -> bool:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user