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
+43 -22
View File
@@ -479,6 +479,46 @@ def normalize_aweme(aweme: Dict[str, Any]) -> Dict[str, Any]:
}
async def author_videos_page(
sec_user_id: str,
max_cursor: int = 0,
count: int = MAX_PAGE_SIZE,
*,
cookie: str = "",
identity: Optional[BrowserIdentity] = None,
) -> Tuple[List[Dict[str, Any]], int, bool]:
"""一页博主作品,返回 ``(作品, 下一页游标, 还有没有更多)``。
翻页是**按时间游标**的(``max_cursor``),不是 offset —— 所以想拿「最早一条」
只能一页页翻到底,没有直接跳最后一页的办法(见 ``creator_scan``)。
"""
# 身份池传进来的身份直接用 —— 不要再去读浏览器,那会把别的账号的 cookie 顶进来。
identity = identity or await browser_identity(cookie)
if not _has_session(identity.cookie):
raise DouyinApiError("抖音登录态不可用:浏览器里没有会话,库里的 cookie 也没有")
payload = await _get(
POSTS_PATH,
{
"sec_user_id": sec_user_id,
"count": max(1, min(count, MAX_PAGE_SIZE)),
"max_cursor": max_cursor,
"device_platform": "webapp",
"aid": 6383,
},
identity,
)
awemes = payload.get("aweme_list") or []
if not awemes and payload.get("status_code") not in (0, None):
raise DouyinApiError(
f"接口拒绝了请求(status_code={payload.get('status_code')})"
)
next_cursor = _as_int(payload.get("max_cursor"))
has_more = bool(payload.get("has_more"))
return [normalize_aweme(aweme) for aweme in awemes], next_cursor, has_more
async def author_videos(
sec_user_id: str,
count: int = MAX_PAGE_SIZE,
@@ -491,29 +531,10 @@ async def author_videos(
用 ``sec_user_id`` 而不是数字 uid:监控任务里存的就是主页链接里的那段 sec_uid,
而且这个接口两种都收(爬虫那边用的也是 sec_user_id)。
"""
# 身份池传进来的身份直接用 —— 不要再去读浏览器,那会把别的账号的 cookie 顶进来。
identity = identity or await browser_identity(cookie)
if not _has_session(identity.cookie):
raise DouyinApiError("抖音登录态不可用:浏览器里没有会话,库里的 cookie 也没有")
payload = await _get(
POSTS_PATH,
{
"sec_user_id": sec_user_id,
"count": max(1, min(count, MAX_PAGE_SIZE)),
"max_cursor": 0,
"device_platform": "webapp",
"aid": 6383,
},
identity,
videos, _cursor, _has_more = await author_videos_page(
sec_user_id, 0, count, cookie=cookie, identity=identity
)
awemes = payload.get("aweme_list") or []
if not awemes and payload.get("status_code") not in (0, None):
raise DouyinApiError(
f"接口拒绝了请求(status_code={payload.get('status_code')})"
)
return [normalize_aweme(aweme) for aweme in awemes]
return videos
async def video_detail(