mirror of
				https://github.com/suyiiyii/nonebot-bison.git
				synced 2025-11-04 13:34:52 +08:00 
			
		
		
		
	♻️ user_cookie -> identified_cookie
This commit is contained in:
		
							parent
							
								
									504cbdff25
								
							
						
					
					
						commit
						eb2eae61bf
					
				@ -83,10 +83,8 @@ sequenceDiagram
 | 
			
		||||
目前 CookieClientManager 具有以下方法
 | 
			
		||||
 | 
			
		||||
- `refresh_anonymous_cookie(cls)` 移除已有的匿名 cookie,添加一个新的匿名 cookie,应该在 CCM 初始化时调用
 | 
			
		||||
- `add_user_cookie(cls, content: str)` 添加用户 cookie,在这里对 Cookie 进行检查并获取 cookie_name,写入数据库
 | 
			
		||||
- `_generate_hook(self, cookie: Cookie) -> Callable` hook 函数生成器,用于回写请求状态到数据库
 | 
			
		||||
- `_choose_cookie(self, target: Target) -> Cookie` 选择 cookie 的具体算法
 | 
			
		||||
- `add_user_cookie(cls, content: str, cookie_name: str | None = None) -> Cookie` 对外的接口,添加用户 cookie,内部会调用 Site 的方法进行检查
 | 
			
		||||
- `add_identified_cookie(cls, content: str, cookie_name: str | None = None) -> Cookie` 对外的接口,添加实名 cookie,内部会调用 Site 的方法进行检查
 | 
			
		||||
- `get_client(self, target: Target | None) -> AsyncClient` 对外的接口,获取 client,根据 target 选择 cookie
 | 
			
		||||
- `_assemble_client(self, client, cookie) -> AsyncClient` 组装 client,可以自定义 cookie 对象的 content 装配到 client 中的方式
 | 
			
		||||
 | 
			
		||||
@ -100,7 +98,7 @@ sequenceDiagram
 | 
			
		||||
 | 
			
		||||
简单来说:
 | 
			
		||||
 | 
			
		||||
- 如果需要修改 Cookie 的默认参数,可以重写`add_user_cookie`方法,这里设置需要的字段
 | 
			
		||||
- 如果需要修改 Cookie 的默认参数,可以重写`add_identified_cookie`方法,这里设置需要的字段
 | 
			
		||||
- 如果需要修改选择 Cookie 的逻辑,可以重写`_choose_cookie`方法,使用自己的算法选择合适的 Cookie 并返回
 | 
			
		||||
- 如果需要自定义 Cookie 的格式,可以重写`valid_cookie`方法,自定义验证 Cookie 的逻辑,并重写`_assemble_client`方法,自定义将 Cookie 装配到 Client 中的逻辑
 | 
			
		||||
- 如果要在请求结束后做一些操作(例如保存此次请求的结果/状态),可以重写`_response_hook`方法,自定义请求结束后的行为
 | 
			
		||||
@ -137,7 +135,7 @@ sequenceDiagram
 | 
			
		||||
 | 
			
		||||
- **无 Target 平台的 Cookie 处理方式**
 | 
			
		||||
 | 
			
		||||
  对于不存在 Target 的平台,如小刻食堂,可以重写 add_user_cookie 方法,为用户 Cookie 设置 is_universal 字段。这样,在获取 Client 时,由于传入的 Target 为空,就只会选择 is_universal 的 cookie。实现了无 Target 平台的用户 Cookie 调度。
 | 
			
		||||
  对于不存在 Target 的平台,如小刻食堂,可以重写 add_identified_cookie 方法,为实名 cookie 设置 is_universal 字段。这样,在获取 Client 时,由于传入的 Target 为空,就只会选择 is_universal 的 cookie。实现了无 Target 平台的实名 cookie 调度。
 | 
			
		||||
 | 
			
		||||
## 默认的调度策略
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -235,7 +235,7 @@ async def get_cookie(site_name: str | None = None, target: str | None = None) ->
 | 
			
		||||
@router.post("/cookie", dependencies=[Depends(check_is_superuser)])
 | 
			
		||||
async def add_cookie(site_name: str, content: str) -> StatusResp:
 | 
			
		||||
    client_mgr = cast(CookieClientManager, scheduler_dict[site_manager[site_name]].client_mgr)
 | 
			
		||||
    await client_mgr.add_user_cookie(content)
 | 
			
		||||
    await client_mgr.add_identified_cookie(content)
 | 
			
		||||
    return StatusResp(ok=True, msg="")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -76,7 +76,7 @@ def do_add_cookie(add_cookie: type[Matcher]):
 | 
			
		||||
    @add_cookie.handle()
 | 
			
		||||
    async def add_cookie_process(state: T_State):
 | 
			
		||||
        client_mgr = cast(CookieClientManager, scheduler_dict[platform_manager[state["platform"]].site].client_mgr)
 | 
			
		||||
        new_cookie = await client_mgr.add_user_cookie(state["cookie"], state["cookie_name"])
 | 
			
		||||
        new_cookie = await client_mgr.add_identified_cookie(state["cookie"], state["cookie_name"])
 | 
			
		||||
        await add_cookie.finish(
 | 
			
		||||
            f"已添加 Cookie: {new_cookie.cookie_name} 到平台 {state['platform']}"
 | 
			
		||||
            + "\n请使用“关联cookie”为 Cookie 关联订阅"
 | 
			
		||||
 | 
			
		||||
@ -34,7 +34,7 @@ def do_add_cookie_target(add_cookie_target_matcher: type[Matcher]):
 | 
			
		||||
 | 
			
		||||
    @add_cookie_target_matcher.handle()
 | 
			
		||||
    async def init_promote_cookie(state: T_State):
 | 
			
		||||
        # 获取 site 的所有用户 cookie,再排除掉已经关联的 cookie,剩下的就是可以关联的 cookie
 | 
			
		||||
        # 获取 site 的所有实名 cookie,再排除掉已经关联的 cookie,剩下的就是可以关联的 cookie
 | 
			
		||||
        cookies = await config.get_cookie(site_name=state["site"].name, is_anonymous=False)
 | 
			
		||||
        associated_cookies = await config.get_cookie(
 | 
			
		||||
            target=state["target"]["target"],
 | 
			
		||||
 | 
			
		||||
@ -79,8 +79,8 @@ class CookieClientManager(ClientManager):
 | 
			
		||||
            new_anonymous_cookie = await self._generate_anonymous_cookie()
 | 
			
		||||
            await config.add_cookie(new_anonymous_cookie)
 | 
			
		||||
 | 
			
		||||
    async def add_user_cookie(self, content: str, cookie_name: str | None = None) -> Cookie:
 | 
			
		||||
        """添加用户 cookie"""
 | 
			
		||||
    async def add_identified_cookie(self, content: str, cookie_name: str | None = None) -> Cookie:
 | 
			
		||||
        """添加实名 cookie"""
 | 
			
		||||
 | 
			
		||||
        if not await self.validate_cookie(content):
 | 
			
		||||
            raise ValueError()
 | 
			
		||||
@ -133,9 +133,9 @@ class CookieClientManager(ClientManager):
 | 
			
		||||
        client = http_client()
 | 
			
		||||
        cookie = await self._choose_cookie(target)
 | 
			
		||||
        if cookie.is_universal:
 | 
			
		||||
            logger.trace(f"平台 {self._site_name} 未获取到用户cookie, 使用匿名cookie")
 | 
			
		||||
            logger.trace(f"平台 {self._site_name} 未获取到实名cookie, 使用匿名cookie")
 | 
			
		||||
        else:
 | 
			
		||||
            logger.trace(f"平台 {self._site_name} 获取到用户cookie: {cookie.id}")
 | 
			
		||||
            logger.trace(f"平台 {self._site_name} 获取到实名cookie: {cookie.id}")
 | 
			
		||||
 | 
			
		||||
        return await self._assemble_client(client, cookie)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -34,9 +34,9 @@ async def test_cookie(app: App, init_scheduler):
 | 
			
		||||
 | 
			
		||||
    cookies = await config.get_cookie(site_name=site.name)
 | 
			
		||||
    assert len(cookies) == 1
 | 
			
		||||
    # 添加用户cookie
 | 
			
		||||
    await client_mgr.add_user_cookie(json.dumps({"test_cookie": "1"}))
 | 
			
		||||
    await client_mgr.add_user_cookie(json.dumps({"test_cookie": "2"}))
 | 
			
		||||
    # 添加实名cookie
 | 
			
		||||
    await client_mgr.add_identified_cookie(json.dumps({"test_cookie": "1"}))
 | 
			
		||||
    await client_mgr.add_identified_cookie(json.dumps({"test_cookie": "2"}))
 | 
			
		||||
 | 
			
		||||
    cookies = await config.get_cookie(site_name=site.name)
 | 
			
		||||
    assert len(cookies) == 3
 | 
			
		||||
@ -68,7 +68,7 @@ async def test_cookie(app: App, init_scheduler):
 | 
			
		||||
        tags=[],
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    await client_mgr.add_user_cookie(json.dumps({"test_cookie": "3"}))
 | 
			
		||||
    await client_mgr.add_identified_cookie(json.dumps({"test_cookie": "3"}))
 | 
			
		||||
    cookies = await config.get_cookie(site_name=site.name, is_anonymous=False)
 | 
			
		||||
 | 
			
		||||
    # 多个target,多个cookie
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user