feat(matrix): 「第一条视频」改成主页最早一条,后台翻页扫描 + 缓存
Deploy VitePress site to Pages / build (push) Canceled after 0s
Deploy VitePress site to Pages / Deploy (push) Canceled after 0s

要的是这个博主**最早发的**那条(可能根本不在监控目标里),不是主页第一屏。
按游标翻到底才知道,所以:

- 新增 monitor_creator_first_video 缓存表,按 sec_uid 存最早发布时间,
  30 天内不再扫;
- 调度器后台任务慢慢扫(每页 20 条、页间 0.5 秒、单次最多 50 页 / 3 分钟),
  不占调度循环;
- 矩阵只读缓存,读不到退回库里最早的一条作品;
- 失败只锁 6 小时再试,不会把没扫到的号锁 30 天。

同时撤掉上一版「每轮采集多问一次主页最新视频」的实现(方向反了)。
This commit is contained in:
2026-11-02 11:07:48 +08:00
parent 34a962f75a
commit 6c2c8faf1d
11 changed files with 494 additions and 188 deletions
+109
View File
@@ -0,0 +1,109 @@
# -*- coding: utf-8 -*-
"""主页「最早一条视频」的扫描:翻页、取最小值、缓存与失败重试。"""
import pytest
import pytest_asyncio
from sqlalchemy import select
from api.monitor import creator_scan, db as monitor_db, douyin_api
from api.monitor.models import MonitorCreatorFirstVideo
from tools.time_util import get_current_timestamp
@pytest_asyncio.fixture
async def session(tmp_path):
monitor_db.set_sqlite_path(tmp_path / "scan.db")
await monitor_db.init_db()
async with monitor_db.get_session() as db_session:
yield db_session
await monitor_db.dispose_engine()
@pytest.fixture(autouse=True)
def _fast(monkeypatch):
monkeypatch.setattr(creator_scan, "REQUEST_GAP_SECONDS", 0)
async def fake_identity(cookie="", force=False):
return None
monkeypatch.setattr(douyin_api, "browser_identity", fake_identity)
class TestScan:
@pytest.mark.asyncio
async def test_pages_to_the_end_and_takes_the_oldest(self, monkeypatch):
pages = {
0: ([{"create_time": 1793000000}, {"create_time": 1792000000}], 100, True),
100: ([{"create_time": 1700000000}], 50, True),
50: ([{"create_time": 1600000000}], 0, False),
}
async def fake_page(sec_user_id, max_cursor=0, count=20, *, cookie="", identity=None):
return pages[max_cursor]
monkeypatch.setattr(douyin_api, "author_videos_page", fake_page)
oldest, page_count, complete = await creator_scan._scan("MS4w-sec")
assert oldest == 1600000000 * 1000
assert page_count == 3
assert complete is True
@pytest.mark.asyncio
async def test_an_unchanging_cursor_does_not_loop_forever(self, monkeypatch):
async def fake_page(sec_user_id, max_cursor=0, count=20, *, cookie="", identity=None):
return [{"create_time": 1600000000}], 7, True
monkeypatch.setattr(douyin_api, "author_videos_page", fake_page)
_oldest, page_count, complete = await creator_scan._scan("MS4w-sec")
assert page_count == 2
assert complete is False
class TestCache:
@pytest.mark.asyncio
async def test_a_successful_scan_is_written_to_the_cache(self, session, monkeypatch):
async def fake_scan(creator_id):
return 1700000000000, 4, True
monkeypatch.setattr(creator_scan, "_scan", fake_scan)
ok = await creator_scan.scan_one(session, "dy", "hash-a", "MS4w-sec")
assert ok is True
row = await session.scalar(
select(MonitorCreatorFirstVideo).where(
MonitorCreatorFirstVideo.creator_id == "MS4w-sec"
)
)
assert row.first_published_at == 1700000000000
assert row.scanned_pages == 4
assert row.complete is True
assert row.error == ""
@pytest.mark.asyncio
async def test_a_failure_is_recorded_and_retried_after_a_while(
self, session, monkeypatch
):
async def boom(creator_id):
raise douyin_api.DouyinApiError("接口超时")
monkeypatch.setattr(creator_scan, "_scan", boom)
now = get_current_timestamp()
ok = await creator_scan.scan_one(
session, "dy", "hash-a", "MS4w-sec", now=now
)
assert ok is False
row = await session.scalar(
select(MonitorCreatorFirstVideo).where(
MonitorCreatorFirstVideo.creator_id == "MS4w-sec"
)
)
assert "接口超时" in row.error
assert row.first_published_at is None
# checked_at 往前拨:大约 6 小时后就重新到期,而不是锁 30 天。
assert row.checked_at == now - creator_scan.TTL_MS + creator_scan.RETRY_MS