feat(matrix): 「第一条视频」改成主页最早一条,后台翻页扫描 + 缓存
要的是这个博主**最早发的**那条(可能根本不在监控目标里),不是主页第一屏。 按游标翻到底才知道,所以: - 新增 monitor_creator_first_video 缓存表,按 sec_uid 存最早发布时间, 30 天内不再扫; - 调度器后台任务慢慢扫(每页 20 条、页间 0.5 秒、单次最多 50 页 / 3 分钟), 不占调度循环; - 矩阵只读缓存,读不到退回库里最早的一条作品; - 失败只锁 6 小时再试,不会把没扫到的号锁 30 天。 同时撤掉上一版「每轮采集多问一次主页最新视频」的实现(方向反了)。
This commit is contained in:
@@ -32,8 +32,6 @@
|
||||
import asyncio
|
||||
import json
|
||||
from datetime import datetime
|
||||
|
||||
import httpx
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, Iterable, List, Optional, Sequence
|
||||
|
||||
@@ -209,37 +207,10 @@ async def _collect_batch(
|
||||
videos = []
|
||||
# 作品详情里带着作者的粉丝/总获赞。作品模式**不会**单独问博主资料 ——
|
||||
# 不从这里收一份,这类任务(超级编导导入的都在这里)的博主粉丝数永远是空的。
|
||||
#
|
||||
# 顺带问一次这个博主**主页上最新的视频**:它可能根本不在监控目标里(自然发的、
|
||||
# 别的活动发的),但「他最近还发不发」只能从主页看。同一轮里一个博主只问一次。
|
||||
latest_by_creator: Dict[str, Optional[int]] = {}
|
||||
for video in videos:
|
||||
profile = _profile_from_author(video)
|
||||
creator_id = str(video.get("creator_id") or "")
|
||||
latest: Optional[int] = None
|
||||
if creator_id:
|
||||
if creator_id not in latest_by_creator:
|
||||
latest_by_creator[creator_id] = await _latest_video_at(
|
||||
creator_id, cookie, identity
|
||||
)
|
||||
latest = latest_by_creator[creator_id]
|
||||
|
||||
if profile is None:
|
||||
if latest is None:
|
||||
continue
|
||||
# 账号指标一个都没拿到,但知道了最新视频时间 —— 也值得落一条快照,
|
||||
# 否则矩阵上这个号的「最新视频」永远是空的。
|
||||
profile = {
|
||||
"creator_hash": str(video.get("creator_hash") or ""),
|
||||
"creator_id": creator_id,
|
||||
"nickname": str(video.get("nickname") or ""),
|
||||
"fans": None,
|
||||
"total_favorited": None,
|
||||
"works": None,
|
||||
"following": None,
|
||||
}
|
||||
profile["latest_published_at"] = latest
|
||||
profiles.append(profile)
|
||||
if profile is not None:
|
||||
profiles.append(profile)
|
||||
else:
|
||||
videos = await _creator_works(
|
||||
external_id,
|
||||
@@ -315,37 +286,6 @@ def _profile_from_author(video: Dict[str, Any]) -> Optional[Dict[str, Any]]:
|
||||
}
|
||||
|
||||
|
||||
async def _latest_video_at(
|
||||
sec_user_id: str, cookie: str, identity: Any = None
|
||||
) -> Optional[int]:
|
||||
"""博主主页第一条(最新)视频的发布时间(毫秒);拿不到返回 None。
|
||||
|
||||
作品列表按发布时间倒序,只要最新那条,所以一次请求就够,不翻页。连接超时重试一次
|
||||
(容器到抖音的 TCP 连接偶尔会卡);其它错误直接放弃 —— 这是附加信息,不值得为一轮
|
||||
采集增加更多失败面。
|
||||
"""
|
||||
if not sec_user_id:
|
||||
return None
|
||||
try:
|
||||
videos = await douyin_api.author_videos(
|
||||
sec_user_id, count=1, cookie=cookie, identity=identity
|
||||
)
|
||||
except httpx.TransportError:
|
||||
await asyncio.sleep(1.0)
|
||||
try:
|
||||
videos = await douyin_api.author_videos(
|
||||
sec_user_id, count=1, cookie=cookie, identity=identity
|
||||
)
|
||||
except Exception: # noqa: BLE001 - 附加信息,失败就留空
|
||||
return None
|
||||
except Exception: # noqa: BLE001
|
||||
return None
|
||||
|
||||
stamps = [adapters.DY.to_ms(video.get("create_time")) for video in videos]
|
||||
stamps = [stamp for stamp in stamps if stamp]
|
||||
return max(stamps) if stamps else None
|
||||
|
||||
|
||||
async def _creator_profile(
|
||||
sec_user_id: str,
|
||||
videos: Sequence[Dict[str, Any]],
|
||||
@@ -371,11 +311,6 @@ async def _creator_profile(
|
||||
|
||||
if videos:
|
||||
profile["creator_hash"] = videos[0].get("creator_hash") or profile["creator_hash"]
|
||||
# 作品列表是倒序的,最早取到的这 N 条里最新的那条就是主页第一条。
|
||||
stamps = [adapters.DY.to_ms(video.get("create_time")) for video in videos]
|
||||
stamps = [stamp for stamp in stamps if stamp]
|
||||
if stamps:
|
||||
profile["latest_published_at"] = max(stamps)
|
||||
if not profile.get("creator_hash"):
|
||||
# 哈希都算不出来的快照没人能查到,落下去只是垃圾。
|
||||
errors.append(f"博主 {sec_user_id} 的资料里没有可用的身份标识,跳过账号指标")
|
||||
|
||||
Reference in New Issue
Block a user