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
+38 -19
View File
@@ -21,6 +21,7 @@ from api.monitor import db as monitor_db
from api.monitor import service
from api.monitor.models import (
MODE_CREATOR,
MonitorCreatorFirstVideo,
MonitorCreatorStat,
MonitorNote,
MonitorNoteMetric,
@@ -263,7 +264,7 @@ async def _stat(
run_id: int = 1,
fans: int | None = None,
total_favorited: int | None = None,
latest_published_at: int | None = None,
creator_id: str = "",
) -> None:
async with monitor_db.get_session() as session:
session.add(
@@ -272,16 +273,36 @@ async def _stat(
run_id=run_id,
creator_hash=creator_hash,
nickname=nickname,
creator_id="",
creator_id=creator_id,
works_count=0,
fans=fans,
total_favorited=total_favorited,
latest_published_at=latest_published_at,
captured_at=captured_at or get_current_timestamp(),
)
)
async def _first_video(
platform: str,
creator_id: str,
first_at: int | None,
creator_hash: str = "hash-a",
) -> None:
async with monitor_db.get_session() as session:
session.add(
MonitorCreatorFirstVideo(
platform=platform,
creator_id=creator_id,
creator_hash=creator_hash,
first_published_at=first_at,
scanned_pages=3,
complete=True,
checked_at=get_current_timestamp(),
error="",
)
)
@pytest.mark.asyncio
async def test_creators_without_notes_still_get_a_row(db):
"""只在账号快照里出现的号不能消失。
@@ -342,46 +363,44 @@ async def test_the_creator_row_carries_the_total_likes_from_the_profile(db):
@pytest.mark.asyncio
async def test_the_latest_video_date_comes_from_the_profile(db):
"""主页第一条视频可能根本不在监控目标里 —— 采集时单独问过,就用它。"""
async def test_the_first_video_date_comes_from_the_scan_cache(db):
"""后台扫过主页就把最早那条的日期存下来 —— 它可能根本不在监控目标里。"""
task_id = await _task()
now = get_current_timestamp() - 1000
tracked = now - 3 * DAY_MS
profile_latest = now - 60_000
await _note(task_id, "n1", "博主A", tracked)
await _stat(
task_id, "hash-a", "博主A", latest_published_at=profile_latest
)
sec = "MS4wLjABAAAAsecuid"
scanned_first = get_current_timestamp() - 300 * DAY_MS
await _stat(task_id, "hash-a", "博主A", creator_id=sec)
await _first_video("dy", sec, scanned_first)
async with monitor_db.get_session() as session:
data = await service.daily_tag_matrix(session, "dy", days=7)
assert data["creators"][0]["latest_published_at"] == profile_latest
assert data["creators"][0]["first_published_at"] == scanned_first
@pytest.mark.asyncio
async def test_it_falls_back_to_the_newest_tracked_note(db):
"""主页那次没问到(接口挡了/超时)时,退到库里最新的一条作品。"""
async def test_it_falls_back_to_the_earliest_tracked_note(db):
"""还没扫到主页时,退到库里**最早**的一条作品。"""
task_id = await _task()
now = get_current_timestamp() - 1000
await _note(task_id, "old", "博主A", now - 400 * DAY_MS)
first = now - 400 * DAY_MS
await _note(task_id, "old", "博主A", first)
await _note(task_id, "new", "博主A", now)
async with monitor_db.get_session() as session:
data = await service.daily_tag_matrix(session, "dy", days=7)
assert data["creators"][0]["latest_published_at"] == now
assert data["creators"][0]["first_published_at"] == first
@pytest.mark.asyncio
async def test_a_creator_without_works_has_no_latest_video_date(db):
async def test_a_creator_without_works_has_no_first_video_date(db):
task_id = await _task()
await _stat(task_id, "hash-a", "博主A", fans=10)
async with monitor_db.get_session() as session:
data = await service.daily_tag_matrix(session, "dy", days=7)
assert data["creators"][0]["latest_published_at"] is None
assert data["creators"][0]["first_published_at"] is None
@pytest.mark.asyncio