mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-07 20:33:01 +08:00
130 lines
4.0 KiB
Markdown
130 lines
4.0 KiB
Markdown
---
|
||
prev: /usage/
|
||
#next: /dev/cookie
|
||
---
|
||
|
||
# Cookie 开发须知
|
||
|
||
本项目将大部分 Cookie 相关逻辑提出到了 Site 及 Client Manger 模块中,你只需要继承相关类即可获得使用 Cookie 的能力。
|
||
|
||
::: tip
|
||
|
||
在开发Cookie功能之前,你应该对[基本开发](/dev/#基本开发)有一定的了解。
|
||
|
||
:::
|
||
|
||
## Cookie相关的基本概念
|
||
|
||
- `nonebot_bison.config.db_model.Cookie`: 用于存储Cookie的实体类,包含了Cookie的名称、内容、状态等信息
|
||
- `nonebot_bison.config.db_model.CookieTarget`: 用于存储Cookie与订阅的关联关系
|
||
- `nonebot_bison.utils.site.CookieClientManager`: 添加了Cookie功能的ClientManager,是Cookie管理功能的核心,调度Cookie的功能就在这里实现
|
||
- `nonebot_bison.utils.site.CookieSite`: 添加了Cookie功能的Site类,根据需求添加了和Site强相关的Cookie功能实现方法
|
||
|
||
## 快速上手
|
||
|
||
例如,你现在有一个这样子的 Site 类:
|
||
|
||
```python
|
||
class WeiboSite(Site):
|
||
name = "weibo.com"
|
||
schedule_type = "interval"
|
||
schedule_setting = {"seconds": 3}
|
||
```
|
||
|
||
简而言之,要让你的站点获得 Cookie 能力,只需要:
|
||
|
||
1. 将父类从`Site`改为`CookieSite`
|
||
|
||
```python {1}
|
||
class WeiboSite(CookieSite):
|
||
name = "weibo.com"
|
||
schedule_type = "interval"
|
||
schedule_setting = {"seconds": 3}
|
||
```
|
||
|
||
2. 为你的 Site 类添加一个`client_mgr`属性
|
||
|
||
```python {5}
|
||
class WeiboSite(CookieSite):
|
||
name = "weibo.com"
|
||
schedule_type = "interval"
|
||
schedule_setting = {"seconds": 3}
|
||
client_mgr = create_cookie_client_manager(name)
|
||
```
|
||
|
||
至此,你的站点就可以使用 Cookie 了!
|
||
|
||
## 更好的体验
|
||
|
||
为了给用户提供更好的体验,你还可以为你的Site重写`validate_cookie`和`get_target_name`方法。
|
||
|
||
- `async def validate_cookie(cls, content: str) -> bool`该方法将会在Cookie添加时被调用,你可以在这里验证Cookie的有效性
|
||
- `async def get_cookie_name(cls, content: str) -> str`该方法将会在验证Cookie成功后被调用,你可以在这里设置Cookie的名字并展示给用户
|
||
|
||
## 我要自己调度Cookie
|
||
|
||
当默认的Cookie调度逻辑无法满足你的需求时,你可以重写`CookieClientManager`的`get_cookie`方法。
|
||
|
||
目前整体的调度逻辑是:
|
||
|
||
```mermaid
|
||
|
||
|
||
```
|
||
|
||
<script type="module">
|
||
import mermaid from "mermaid";
|
||
import zenuml from "@mermaid-js/mermaid-zenuml";
|
||
async function initializeMermaid() {
|
||
await mermaid.registerExternalDiagrams([zenuml]);
|
||
}
|
||
initializeMermaid();
|
||
</script>
|
||
|
||
<pre class="mermaid">zenuml
|
||
title Cookie调度逻辑
|
||
Scheduler #661ae6
|
||
Platform #2b2d30
|
||
CookieClientManager #FFEBE6
|
||
DB #f26522
|
||
Internet #0747A6
|
||
@Starter(Scheduler)
|
||
Scheduler.exec_fetch{
|
||
Post = Platform.do_fetch_new_post(SubUnit) {
|
||
Platform.get_sub_list(Target){
|
||
client = CookieClientManager.get_client(Target){
|
||
cookie = CookieClientManager._choose_cookie(Target) {
|
||
cookies = DB.get_cookies()
|
||
}
|
||
client = CookieClientManager._assemble_client(Target, cookie)
|
||
}
|
||
res = Internet.client.get(Target)
|
||
CookieClientManager._response_hook(){
|
||
DB.update_cookie()
|
||
}
|
||
}
|
||
}
|
||
}
|
||
</pre>
|
||
|
||
```mermaid
|
||
flowchart TB
|
||
init[初始化] --> schedule_pool[调度池]
|
||
schedule_pool((调度池)) --> choose_platform[选择Platform]
|
||
choose_platform --> get_targets[获取Target列表]
|
||
get_targets --> get_rawposts[获取Target的RawPost列表]
|
||
get_rawposts --> compare_with_old[(与上次状态对比)]
|
||
compare_with_old -- 有新增 --> dispatch[分发到对应用户]
|
||
compare_with_old -- 无新增 --> schedule_pool
|
||
dispatch --> user_filter[用户订阅过滤]
|
||
user_filter -- 不匹配 --> schedule_pool
|
||
user_filter -- 匹配 --> parse[选择Post解析方式]
|
||
parse -.-> to_text((文本Post))
|
||
parse -.-> to_pic((图片Post))
|
||
parse -.-> to_other((...))
|
||
to_text -.-> message_render[消息渲染]
|
||
to_pic -.-> message_render
|
||
to_other -.-> message_render
|
||
message_render --> send_to[发送到对应用户]
|
||
```
|