Files
MediaCrawler/docs/queries/douyin-latest-video.sql
T
butubb 36f41450ae
Deploy VitePress site to Pages / build (push) Waiting to run
Deploy VitePress site to Pages / Deploy (push) Blocked by required conditions
fix(monitor): 博主改名之后,旧作品上的名字也跟着改
昵称是**博主级**的属性,而作品是逐条刷的:一个博主只有落在采集窗口里的那几条会被
重刷,掉出窗口的老作品会一直留着旧名字。于是改名之后库里一半新一半旧 ——

* 作品栏组头取的是该博主名下作品的 max(name),**恰好可能取到旧的那个**(比如从
  「BBB」改成「AAA」,max 还是 BBB);
* 导出和 SQL 直接看到旧名字。

两处修:

1. ingest 收完这一轮之后,把这一轮看到的 (博主, 昵称) 铺到该博主在本任务下的
   **全部**作品上。带上 `!= name` 让没变化的不产生写入。只限本任务 —— 另一个任务里
   的同一个人是另一条跟踪线。
2. `list_creators` 的昵称**优先取账号快照**:那一行每次采集都从资料接口重写,是
   最新的一份;小红书那条路没有快照,才退回作品上的名字。

顺带把两份 SQL 里的 COALESCE 顺序调成一致(不然查出来还是旧名字)。
2026-10-11 10:08:58 +08:00

86 lines
4.3 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 抖音所有账号,各自最近一条视频 + 标签 + 详细数据
--
-- 直接在 MySQL 5.7 里跑(生产库 mediacrawler_prod)。已实测:42 个账号,包含
-- **一条作品都没有的**(那些行的作品列是空的)—— 它们出现在列表里是有意的,
-- 和界面上的作品栏一致。
--
-- 为什么写成这个形状:
--
-- * **5.7 没有窗口函数**,ROW_NUMBER() 用不了。所以「每个博主最新那条」是用
-- 相关子查询 `ORDER BY published_at DESC LIMIT 1` 挑的,而不是 rank 之后取 1。
-- * 账号清单取的是 **作品里出现过的 ∪ 有账号资料的**(`UNION` 去重)。只从作品里取
-- 的话,那些「加了目标、也采到了粉丝数、但一条作品都没有」的号会整行消失。
-- * 「最新」按 **published_at**(作者发布时间)排,不是 last_seen_at。降级路径
-- (作品列表被风控挡住)会把所有已知作品都刷一遍,那时 last_seen_at 全都一样,
-- 按它排等于没排。
-- * 指标取**最近一轮**(run_id 最大)的快照。注意抖音作品目前没有 published_at
-- 为空的,所以不存在「没有日期的作品排哪儿」的问题。
--
-- 只想看某一个任务的话,把四处 `t.platform = 'dy'` 换成对应的 task_id,例如:
-- WHERE t1.id = 7
-- (四处指的是:账号清单里两处、最新作品子查询一处、账号快照子查询一处。)
SELECT
acct.creator_hash AS 博主哈希,
ca.alias AS 博主备注,
COALESCE(st.nickname, n.creator_name, '') AS 博主昵称,
st.fans AS 粉丝数,
st.total_favorited AS 总获赞,
st.works_count AS 主页作品数,
n.note_id AS 作品ID,
n.title AS 作品标题,
n.note_url AS 作品链接,
FROM_UNIXTIME(n.published_at / 1000) AS 发布时间,
m.liked_count AS 点赞,
m.comment_count AS 评论,
m.collected_count AS 收藏,
m.share_count AS 分享,
(SELECT GROUP_CONCAT(t.name ORDER BY t.sort_order SEPARATOR ' / ')
FROM monitor_note_tag nt
JOIN monitor_tag t ON t.id = nt.tag_id
WHERE nt.platform = 'dy' AND nt.note_id = n.note_id) AS 标签,
(SELECT na.alias FROM monitor_note_alias na
WHERE na.platform = 'dy' AND na.note_id = n.note_id LIMIT 1) AS 作品备注
FROM (
-- 账号清单:作品里出现过的
SELECT n1.creator_hash
FROM monitor_note n1
JOIN monitor_task t1 ON t1.id = n1.task_id
WHERE t1.platform = 'dy'
UNION
-- ∪ 有账号资料的(包含一条作品都没有的博主)
SELECT s1.creator_hash
FROM monitor_creator_stat s1
JOIN monitor_task t2 ON t2.id = s1.task_id
WHERE t2.platform = 'dy'
) acct
-- 这个博主最新的一条作品
LEFT JOIN monitor_note n
ON n.id = (SELECT n2.id
FROM monitor_note n2
JOIN monitor_task t3 ON t3.id = n2.task_id
WHERE t3.platform = 'dy'
AND n2.creator_hash = acct.creator_hash
ORDER BY n2.published_at DESC, n2.id DESC
LIMIT 1)
-- 那条作品最近一轮的指标
LEFT JOIN monitor_note_metric m
ON m.id = (SELECT m2.id
FROM monitor_note_metric m2
WHERE m2.task_id = n.task_id
AND m2.note_id = n.note_id
ORDER BY m2.run_id DESC
LIMIT 1)
LEFT JOIN monitor_creator_alias ca
ON ca.platform = 'dy' AND ca.creator_hash = acct.creator_hash
-- 博主最近一轮的账号级指标
LEFT JOIN monitor_creator_stat st
ON st.id = (SELECT s2.id
FROM monitor_creator_stat s2
JOIN monitor_task t4 ON t4.id = s2.task_id
WHERE t4.platform = 'dy'
AND s2.creator_hash = acct.creator_hash
ORDER BY s2.run_id DESC
LIMIT 1)
ORDER BY st.fans DESC, acct.creator_hash