feat(monitor): 抖音多身份池 —— 目标按登录态串行分片
Deploy VitePress site to Pages / build (push) Canceled after 0s
Deploy VitePress site to Pages / Deploy (push) Canceled after 0s

把 44 个博主打散给多个抖音号,降低**单个账号**每轮的请求量(约 35 个博主后开始
出现限流是已知现象)。**默认串行,不是并行**:同一个出口 IP 上并发多个 cookie,
平台视角是一台设备在不停换访客,比单账号高频更容易被判异常。并发数做成了设置项,
但要先给每个账号配独立出口 IP 再考虑调大。

新增:
* `monitor_identity` 表:label / source(stored|browser) / cookie / **user_agent** /
  client_hints / status / enabled / 冷却字段。cookie 与 UA 必须成套存 —— a_bogus
  把 UA 算进签名,空 UA 签出来的请求一定被挡。
* `api/monitor/identities.py`:可用性判定、解析、**Rendezvous Hashing 分片**
  (加一个身份只迁移约 1/(n+1) 的目标)、健康状态与指数退避冷却。
* `douyin_api`:四个接口都接受显式 `identity`,走身份池时**不再回读浏览器**(否则
  浏览器里的 cookie 会把指定账号顶掉);新增 `shared_fingerprint()` 给粘贴 cookie
  的身份兜底 UA。
* `douyin_fetch.collect`:按身份分片执行,分片之间默认串行、可配切换间隔;落盘前
  按 note_id 去重(并发时两个分片可能刷到同一件作品)。
* `runner`:池模式下加载身份、不足 `identity_min_available` 就快速失败(不产生请求),
  结束后按各自分片结果回写健康。
* 设置项:identity_mode / identity_min_available / identity_switch_delay_sec /
  identity_concurrency。
* API:GET/POST/PATCH/DELETE `/api/monitor/identities` + `/{id}/check`(真请求一次,
  没有可探测博主时退化成格式检查并在 verified 里标 false)。
* 前端:设置页(仅抖音)新增「多身份(分片采集)」面板。

测试 9 条新增:分片确定性与覆盖、加身份只迁移一小部分、缺 UA/cookie 拒绝、
两次失败进冷却、成功清零、冷却中不可用、采集按身份分片。
This commit is contained in:
butubb
2026-10-22 16:37:12 +08:00
parent 4665dac667
commit d95e5ec8e2
15 changed files with 1402 additions and 65 deletions
+43 -8
View File
@@ -101,6 +101,9 @@ class BrowserIdentity:
cookie: str
user_agent: str
client_hints: Dict[str, str]
# 身份池里的标识:``"browser"`` / ``"id:<n>"`` / ``"legacy"``。只用于日志与分片,
# 不参与请求。放在最后且有默认值,现有构造点不用改。
identity_key: str = "legacy"
def headers(self) -> Dict[str, str]:
headers = {
@@ -299,6 +302,19 @@ def forget_identity() -> None:
_identity_cache = None
async def shared_fingerprint() -> Tuple[str, Dict[str, str]]:
"""只从浏览器读 UA + client hints,**不要它的 cookie**。
给「粘贴 cookie」的身份兜底:cookie 来自哪个浏览器我们不知道,至少让 UA 和
``sec-ch-ua`` 成套。读不到返回空 —— 调用方应当拒绝创建没有 UA 的身份,因为
评论接口的 ``a_bogus`` 要拿 UA 去签名,空 UA 签出来的请求一定被挡。
"""
identity = await _read_browser()
if identity is None:
return "", {}
return identity.user_agent, identity.client_hints
def _cookie_header(cookie: str) -> str:
"""把 ``a=1; b=2`` 形式的 cookie 串规整成请求头用的形状。"""
pairs = []
@@ -427,14 +443,19 @@ def normalize_aweme(aweme: Dict[str, Any]) -> Dict[str, Any]:
async def author_videos(
sec_user_id: str, count: int = MAX_PAGE_SIZE, *, cookie: str = ""
sec_user_id: str,
count: int = MAX_PAGE_SIZE,
*,
cookie: str = "",
identity: Optional[BrowserIdentity] = None,
) -> List[Dict[str, Any]]:
"""某个博主最新发布的作品(按发布时间倒序),已翻译成 store 的键名。
用 ``sec_user_id`` 而不是数字 uid:监控任务里存的就是主页链接里的那段 sec_uid,
而且这个接口两种都收(爬虫那边用的也是 sec_user_id)。
"""
identity = await browser_identity(cookie)
# 身份池传进来的身份直接用 —— 不要再去读浏览器,那会把别的账号的 cookie 顶进来。
identity = identity or await browser_identity(cookie)
if not _has_session(identity.cookie):
raise DouyinApiError("抖音登录态不可用:浏览器里没有会话,库里的 cookie 也没有")
@@ -458,13 +479,18 @@ async def author_videos(
return [normalize_aweme(aweme) for aweme in awemes]
async def video_detail(aweme_id: str, *, cookie: str = "") -> Dict[str, Any]:
async def video_detail(
aweme_id: str,
*,
cookie: str = "",
identity: Optional[BrowserIdentity] = None,
) -> Dict[str, Any]:
"""单条作品的详情,已翻译成 store 的键名。
这个接口**没有**被那道真校验挡着(实测 200 / 45425 字节),所以在拿不到作品列表时,
它是「刷新已知作品指标」的唯一途径。
"""
identity = await browser_identity(cookie)
identity = identity or await browser_identity(cookie)
if not _has_session(identity.cookie):
raise DouyinApiError("抖音登录态不可用:浏览器里没有会话,库里的 cookie 也没有")
@@ -481,9 +507,14 @@ async def video_detail(aweme_id: str, *, cookie: str = "") -> Dict[str, Any]:
return normalize_aweme(aweme)
async def author_profile(sec_user_id: str, *, cookie: str = "") -> Dict[str, Any]:
async def author_profile(
sec_user_id: str,
*,
cookie: str = "",
identity: Optional[BrowserIdentity] = None,
) -> Dict[str, Any]:
"""博主主页指标:昵称 / 粉丝数 / 总获赞 / 作品数。"""
identity = await browser_identity(cookie)
identity = identity or await browser_identity(cookie)
if not _has_session(identity.cookie):
raise DouyinApiError("抖音登录态不可用:浏览器里没有会话,库里的 cookie 也没有")
@@ -541,14 +572,18 @@ def normalize_comment(comment: Dict[str, Any], aweme_id: str) -> Dict[str, Any]:
async def video_comments(
aweme_id: str, count: int = 20, *, cookie: str = ""
aweme_id: str,
count: int = 20,
*,
cookie: str = "",
identity: Optional[BrowserIdentity] = None,
) -> List[Dict[str, Any]]:
"""一条作品的评论,翻译成 store 的评论键名。
刻意不带 ``sub_comment_count`` / ``parent_comment_id`` 的猜测值 —— 接口给了就用,
没给就留空,不编。
"""
identity = await browser_identity(cookie)
identity = identity or await browser_identity(cookie)
if not _has_session(identity.cookie):
raise DouyinApiError("抖音登录态不可用")