feat(matrix): 「第一条视频」改成主页最早一条,后台翻页扫描 + 缓存
要的是这个博主**最早发的**那条(可能根本不在监控目标里),不是主页第一屏。 按游标翻到底才知道,所以: - 新增 monitor_creator_first_video 缓存表,按 sec_uid 存最早发布时间, 30 天内不再扫; - 调度器后台任务慢慢扫(每页 20 条、页间 0.5 秒、单次最多 50 页 / 3 分钟), 不占调度循环; - 矩阵只读缓存,读不到退回库里最早的一条作品; - 失败只锁 6 小时再试,不会把没扫到的号锁 30 天。 同时撤掉上一版「每轮采集多问一次主页最新视频」的实现(方向反了)。
This commit is contained in:
+38
-30
@@ -40,6 +40,7 @@ from .models import (
|
||||
MODE_NOTE,
|
||||
MonitorComment,
|
||||
MonitorCreatorAlias,
|
||||
MonitorCreatorFirstVideo,
|
||||
MonitorCreatorStat,
|
||||
MonitorEvent,
|
||||
MonitorNote,
|
||||
@@ -1580,7 +1581,7 @@ async def daily_tag_matrix(
|
||||
{"dates": ["2026-10-21", ...], # 新到旧
|
||||
"creators": [{"hash": ..., "name": ..., "fans": 1200,
|
||||
"total_favorited": 83000,
|
||||
"latest_published_at": 1780000000000}], # 按昵称排序
|
||||
"first_published_at": 1780000000000}], # 按昵称排序
|
||||
"cells": {"2026-10-21": {"<hash>": {
|
||||
"count": 2, "tags": ["大字报", "养号"],
|
||||
"likes": 120, "comments": 8, "collected": 30, "shares": 4,
|
||||
@@ -1636,6 +1637,7 @@ async def daily_tag_matrix(
|
||||
MonitorNote.title,
|
||||
MonitorNote.note_url,
|
||||
MonitorNote.creator_hash,
|
||||
MonitorNote.creator_id,
|
||||
MonitorNote.creator_name,
|
||||
MonitorNote.published_at,
|
||||
)
|
||||
@@ -1700,11 +1702,11 @@ async def daily_tag_matrix(
|
||||
await session.execute(
|
||||
select(
|
||||
MonitorCreatorStat.creator_hash,
|
||||
MonitorCreatorStat.creator_id,
|
||||
MonitorCreatorStat.nickname,
|
||||
MonitorCreatorStat.captured_at,
|
||||
MonitorCreatorStat.fans,
|
||||
MonitorCreatorStat.total_favorited,
|
||||
MonitorCreatorStat.latest_published_at,
|
||||
).where(
|
||||
MonitorCreatorStat.task_id.in_(task_ids),
|
||||
MonitorCreatorStat.creator_hash != "",
|
||||
@@ -1717,9 +1719,11 @@ async def daily_tag_matrix(
|
||||
latest_fans: Dict[str, tuple] = {}
|
||||
# 主页上的「获赞总数」,和粉丝数一样取最近一次读到的值。
|
||||
latest_likes: Dict[str, tuple] = {}
|
||||
# 主页第一条(最新)视频的发布时间。采集时问过主页,这条可能不在监控目标里。
|
||||
latest_video: Dict[str, tuple] = {}
|
||||
# 哈希 -> 主页 id。矩阵要查「最早一条视频」的缓存,缓存是按 sec_uid 存的。
|
||||
creator_ids: Dict[str, str] = {}
|
||||
for stat in stat_rows:
|
||||
if stat.creator_id:
|
||||
creator_ids.setdefault(stat.creator_hash, stat.creator_id)
|
||||
if stat.fans is not None:
|
||||
current_fans = latest_fans.get(stat.creator_hash)
|
||||
if current_fans is None or stat.captured_at > current_fans[1]:
|
||||
@@ -1731,13 +1735,6 @@ async def daily_tag_matrix(
|
||||
stat.total_favorited,
|
||||
stat.captured_at,
|
||||
)
|
||||
if stat.latest_published_at is not None:
|
||||
current_video = latest_video.get(stat.creator_hash)
|
||||
if current_video is None or stat.captured_at > current_video[1]:
|
||||
latest_video[stat.creator_hash] = (
|
||||
stat.latest_published_at,
|
||||
stat.captured_at,
|
||||
)
|
||||
# **空昵称不算名字。** 资料接口偶尔会回一个空对象(昵称、sec_uid、粉丝、
|
||||
# 作品数全空),如果把这一轮当最新快照,就会用一个空字符串把之前的好名字
|
||||
# 盖掉,界面上变成「未知(hash)」。实测线上有两个号就是这么丢的名字。
|
||||
@@ -1750,6 +1747,8 @@ async def daily_tag_matrix(
|
||||
known: Dict[str, str] = {}
|
||||
for row in notes:
|
||||
key = row.creator_hash or f"note:{row.note_id}"
|
||||
if row.creator_id and row.creator_hash:
|
||||
creator_ids.setdefault(row.creator_hash, row.creator_id)
|
||||
if row.creator_name:
|
||||
known[key] = row.creator_name
|
||||
else:
|
||||
@@ -1824,11 +1823,12 @@ async def daily_tag_matrix(
|
||||
# 作品是按发布时间倒序取回来的,明细按时间正序更好读(1、2、3…)。
|
||||
bucket["details"].reverse()
|
||||
|
||||
# 兜底:库里这个博主**已有作品里最新的**发布时间。首选是采集时从主页拿到的
|
||||
# 「第一条视频」(它可能根本不在监控目标里),主页那次没问到时才用它。
|
||||
tracked_rows = (
|
||||
# 「最早一条视频」:首选缓存表(后台把这个博主的作品列表翻到底扫出来的,可能根本
|
||||
# 不在监控目标里);没扫到就退回库里**最早**的一条作品 —— 后者只覆盖被监控的那些
|
||||
# 链接,但总比空着强。
|
||||
first_rows = (
|
||||
await session.execute(
|
||||
select(MonitorNote.creator_hash, func.max(MonitorNote.published_at))
|
||||
select(MonitorNote.creator_hash, func.min(MonitorNote.published_at))
|
||||
.where(
|
||||
MonitorNote.task_id.in_(task_ids),
|
||||
MonitorNote.published_at.is_not(None),
|
||||
@@ -1837,12 +1837,29 @@ async def daily_tag_matrix(
|
||||
.group_by(MonitorNote.creator_hash)
|
||||
)
|
||||
).all()
|
||||
latest_tracked: Dict[str, int] = {
|
||||
creator_hash: latest_at
|
||||
for creator_hash, latest_at in tracked_rows
|
||||
if latest_at
|
||||
tracked_first: Dict[str, int] = {
|
||||
creator_hash: first_at for creator_hash, first_at in first_rows if first_at
|
||||
}
|
||||
|
||||
sec_uids = {value for value in creator_ids.values() if value}
|
||||
scanned: Dict[str, int] = {}
|
||||
if sec_uids:
|
||||
scanned_rows = await session.execute(
|
||||
select(
|
||||
MonitorCreatorFirstVideo.creator_id,
|
||||
MonitorCreatorFirstVideo.first_published_at,
|
||||
).where(
|
||||
MonitorCreatorFirstVideo.platform == platform,
|
||||
MonitorCreatorFirstVideo.creator_id.in_(sec_uids),
|
||||
MonitorCreatorFirstVideo.first_published_at.is_not(None),
|
||||
)
|
||||
)
|
||||
scanned = {
|
||||
creator_id: first_at
|
||||
for creator_id, first_at in scanned_rows.all()
|
||||
if first_at
|
||||
}
|
||||
|
||||
creators = [
|
||||
{
|
||||
"hash": hash_,
|
||||
@@ -1851,17 +1868,8 @@ async def daily_tag_matrix(
|
||||
# 前端据此不画那个数字,而不是显示 0。
|
||||
"fans": latest_fans.get(hash_, (None, 0))[0],
|
||||
"total_favorited": latest_likes.get(hash_, (None, 0))[0],
|
||||
# 两个来源取较新的一个:主页数据可能比库里的作品旧(快照是上一轮的)。
|
||||
"latest_published_at": max(
|
||||
[
|
||||
value
|
||||
for value in (
|
||||
latest_video.get(hash_, (None, 0))[0],
|
||||
latest_tracked.get(hash_),
|
||||
)
|
||||
if value
|
||||
],
|
||||
default=None,
|
||||
"first_published_at": (
|
||||
scanned.get(creator_ids.get(hash_) or "") or tracked_first.get(hash_)
|
||||
),
|
||||
}
|
||||
for hash_, name in known.items()
|
||||
|
||||
Reference in New Issue
Block a user