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
+23 -23
View File
@@ -36,7 +36,7 @@ def fake_profile(monkeypatch):
用例再单独覆盖这个 fixture。
"""
async def _profile_call(sec_user_id, *, cookie=""):
async def _profile_call(sec_user_id, *, cookie="", identity=None):
return _profile()
monkeypatch.setattr(douyin_api, "author_profile", _profile_call)
@@ -99,10 +99,10 @@ async def _collect(tmp_path, **overrides):
class TestHappyPath:
@pytest.mark.asyncio
async def test_writes_the_layout_ingest_expects(self, monkeypatch, tmp_path):
async def fake_videos(sec_user_id, count=20, *, cookie=""):
async def fake_videos(sec_user_id, count=20, *, cookie="", identity=None):
return [_video("111"), _video("222")]
async def fake_comments(aweme_id, count=20, *, cookie=""):
async def fake_comments(aweme_id, count=20, *, cookie="", identity=None):
return [_comment(aweme_id, 1)]
monkeypatch.setattr(douyin_api, "author_videos", fake_videos)
@@ -140,7 +140,7 @@ class TestHappyPath:
两种情况的含义完全不同。
"""
async def fake_videos(sec_user_id, count=20, *, cookie=""):
async def fake_videos(sec_user_id, count=20, *, cookie="", identity=None):
return [_video("111")]
monkeypatch.setattr(douyin_api, "author_videos", fake_videos)
@@ -151,7 +151,7 @@ class TestHappyPath:
@pytest.mark.asyncio
async def test_duplicate_works_are_written_once(self, monkeypatch, tmp_path):
async def fake_videos(sec_user_id, count=20, *, cookie=""):
async def fake_videos(sec_user_id, count=20, *, cookie="", identity=None):
return [_video("111"), _video("111")]
monkeypatch.setattr(douyin_api, "author_videos", fake_videos)
@@ -175,7 +175,7 @@ class TestNoteMode:
async def must_not_be_called(*args, **kwargs):
raise AssertionError("作品模式不该去拉博主的作品列表")
async def fake_detail(aweme_id, *, cookie=""):
async def fake_detail(aweme_id, *, cookie="", identity=None):
return _video(aweme_id)
monkeypatch.setattr(douyin_api, "author_videos", must_not_be_called)
@@ -196,7 +196,7 @@ class TestNoteMode:
@pytest.mark.asyncio
async def test_a_broken_work_does_not_lose_the_others(self, monkeypatch, tmp_path):
async def flaky(aweme_id, *, cookie=""):
async def flaky(aweme_id, *, cookie="", identity=None):
if aweme_id == "222":
raise douyin_api.DouyinApiError("作品已被删除")
return _video(aweme_id)
@@ -219,12 +219,12 @@ class TestDegradation:
@pytest.mark.asyncio
async def test_falls_back_to_refreshing_known_works(self, monkeypatch, tmp_path):
async def blocked(sec_user_id, count=20, *, cookie=""):
async def blocked(sec_user_id, count=20, *, cookie="", identity=None):
raise douyin_api.DouyinApiError("接口返回了空内容")
refreshed = []
async def fake_detail(aweme_id, *, cookie=""):
async def fake_detail(aweme_id, *, cookie="", identity=None):
refreshed.append(aweme_id)
return _video(aweme_id, likes="9")
@@ -249,12 +249,12 @@ class TestDegradation:
Duplicate entry for key 'uq_note_metric')。
"""
async def blocked(sec_user_id, count=20, *, cookie=""):
async def blocked(sec_user_id, count=20, *, cookie="", identity=None):
raise douyin_api.DouyinApiError("接口返回了空内容")
calls = []
async def fake_detail(aweme_id, *, cookie=""):
async def fake_detail(aweme_id, *, cookie="", identity=None):
calls.append(aweme_id)
return _video(aweme_id)
@@ -273,7 +273,7 @@ class TestDegradation:
@pytest.mark.asyncio
async def test_nothing_at_all_still_reports_the_reason(self, monkeypatch, tmp_path):
async def blocked(sec_user_id, count=20, *, cookie=""):
async def blocked(sec_user_id, count=20, *, cookie="", identity=None):
raise douyin_api.DouyinApiError("接口返回了空内容")
monkeypatch.setattr(douyin_api, "author_videos", blocked)
@@ -287,10 +287,10 @@ class TestDegradation:
@pytest.mark.asyncio
async def test_a_failing_comment_fetch_does_not_lose_the_work(self, monkeypatch, tmp_path):
async def fake_videos(sec_user_id, count=20, *, cookie=""):
async def fake_videos(sec_user_id, count=20, *, cookie="", identity=None):
return [_video("111")]
async def broken_comments(aweme_id, count=20, *, cookie=""):
async def broken_comments(aweme_id, count=20, *, cookie="", identity=None):
raise douyin_api.DouyinApiError("评论接口抽风")
monkeypatch.setattr(douyin_api, "author_videos", fake_videos)
@@ -318,7 +318,7 @@ class TestCreatorProfile:
@pytest.mark.asyncio
async def test_the_profile_lands_in_the_run_dir(self, monkeypatch, tmp_path):
async def fake_videos(sec_user_id, count=20, *, cookie=""):
async def fake_videos(sec_user_id, count=20, *, cookie="", identity=None):
return [_video("111")]
monkeypatch.setattr(douyin_api, "author_videos", fake_videos)
@@ -336,11 +336,11 @@ class TestCreatorProfile:
而且是静默的:表里有数据,界面上什么都没有。
"""
async def fake_videos(sec_user_id, count=20, *, cookie=""):
async def fake_videos(sec_user_id, count=20, *, cookie="", identity=None):
return [_video("111")] # 作品带的哈希是 "hash"
# 资料接口自己算出来的是另一个值(比如它那边 uid 缺字段、只能拿 sec_uid 算)。
async def off_hash_profile(sec_user_id, *, cookie=""):
async def off_hash_profile(sec_user_id, *, cookie="", identity=None):
return _profile(creator_hash="另一个哈希")
monkeypatch.setattr(douyin_api, "author_videos", fake_videos)
@@ -355,10 +355,10 @@ class TestCreatorProfile:
async def test_a_profile_without_an_identity_is_dropped(self, monkeypatch, tmp_path):
"""哈希都算不出来的快照,落下去只会是一条谁也查不到的垃圾。"""
async def fake_videos(sec_user_id, count=20, *, cookie=""):
async def fake_videos(sec_user_id, count=20, *, cookie="", identity=None):
return []
async def anonymous_profile(sec_user_id, *, cookie=""):
async def anonymous_profile(sec_user_id, *, cookie="", identity=None):
return _profile(creator_hash="")
monkeypatch.setattr(douyin_api, "author_videos", fake_videos)
@@ -373,10 +373,10 @@ class TestCreatorProfile:
async def test_a_failing_profile_does_not_lose_the_works(self, monkeypatch, tmp_path):
"""附加信息拿不到,这一轮采到的作品不能跟着判成失败。"""
async def fake_videos(sec_user_id, count=20, *, cookie=""):
async def fake_videos(sec_user_id, count=20, *, cookie="", identity=None):
return [_video("111")]
async def broken_profile(sec_user_id, *, cookie=""):
async def broken_profile(sec_user_id, *, cookie="", identity=None):
raise douyin_api.DouyinApiError("资料接口抽风")
monkeypatch.setattr(douyin_api, "author_videos", fake_videos)
@@ -394,10 +394,10 @@ class TestCreatorProfile:
asked = []
async def fake_detail(aweme_id, *, cookie=""):
async def fake_detail(aweme_id, *, cookie="", identity=None):
return _video(aweme_id)
async def recording_profile(sec_user_id, *, cookie=""):
async def recording_profile(sec_user_id, *, cookie="", identity=None):
asked.append(sec_user_id)
return _profile()