Repository navigation
fix(douyin): 私信纯 HTTP 快路径 + 纯媒体消息(表情/图片)解析 - #248
Open
zeroshun422 wants to merge 1 commit into
Open
zeroshun422 wants to merge 1 commit into
zeroshun422 wants to merge 1 commit into
Conversation
zeroshun422
force-pushed
the
fix/douyin-fastpath
branch
from
November 29, 2026 12:54
fd0c76f to
c9a258f
Compare
私信轮询原来每个会话都要走浏览器 DOM(进会话 → 等渲染 → 抓气泡), 一轮 1-4 分钟;且纯表情消息在没解析出图片地址时会被 normalizeDouyinMessage 的 `text === '' && images.length === 0` 静默丢弃。 改动: - 新增 lib/douyin-http-fast.js:纯 HTTP 读私信(imapi get_message_by_init, cmd 2043,只认 Cookie)。一次请求拿回全部会话 + 每会话最近 20 条, 带真实毫秒时间戳和发送者 uid。实测 878-1120ms,不启浏览器。 思路借鉴 dsh-douyin-scroll 的 tools/im-http.py,用 Node 原生 fetch 重写, 不引入 Python/curl_cffi 依赖。 - contentOf 解析媒体直链:aweType 501(表情)→ url.url_list, 2702(图片)→ resource_url.large_url_list。纯表情不再被当成空消息丢弃。 - readConversation 优先用快路径,任何失败返回 null 自动退回原 DOM 路径, 身份铁闸(convId / identity 校验)不变。 - 游标增加 mediaDoneAt / mediaTries:只记「确认完整处理过」的位置, 不依赖每轮无条件推进的 readAt,避免部分补读成功后剩余消息永久丢失; 重试超限则明确放弃并记 warn。 - 轮询节拍从「本轮开始」算(wait = 间隔 - 本轮耗时)。 - canary 只在真正用了浏览器时跑,避免纯 HTTP 路径误判「页面改版」而停掉自动回复。 - 每 3 分钟补扫一次 DOM 会话列表:快路径只返回「已建立会话」, 陌生人私信它看不见,不补扫那些人永远收不到回复(实测有个陌生人发 2 条无人处理)。 实测:一轮 pollOnce 956-1099ms、零浏览器调用; 5 条连发纯表情全部入库且带图(img=1、sticker=true),延迟 0ms。 Closes Britneycode#247
zeroshun422
force-pushed
the
fix/douyin-fastpath
branch
from
November 29, 2026 13:12
c9a258f to
3b24063
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
这个 PR 解决什么
针对 #247 的两个问题:
根因
① 媒体消息丢失
normalizeDouyinMessage里有:而
contentOf()只读text/content_title,纯表情消息两者都为空 ⇒在入库前就被丢掉,上层连
unknown/gap都记不到。关键发现:表情/图片的直链本来就在
content.json里,读出来即可。aweType501url.url_list[](多域名备用)+display_name2702resource_url.large_url_list[]800content_title② 延迟
pollRangeMs的间隔是跑完一轮再等,有效周期 2–5 分钟改了什么
1. 新增
lib/douyin-http-fast.js:纯 HTTP 读私信imapi.douyin.com/v1/message/get_message_by_init(cmd2043)只认 Cookie,不需要
a_bogus、不需要开浏览器。一次请求拿回全部已建立会话 + 每会话最近 20 条,每条带真实毫秒时间戳
和发送者 uid(DOM 路这两样都拿不准)。
实测本机 878–1120ms 读完全部 3 个会话 59 条消息。
2.
contentOf()解析媒体直链解析后挂成
media,type设为emoji/image,直接复用既有的formatDouyinMedia,normalizeDouyinMessage的空消息判据不再命中。解析不出文字也没有媒体的消息(未知类型 / 字段变更)会被跳过并记一条
warn,方便发现接口变化:
3. 快路径作为首选,DOM 自动降级
readConversation先试readViaHttp,任何失败返回null立刻退回原 DOM 路径。身份铁闸不变:
convIdidentity(douyin:<对方uid>)比对convId里的 peer uidnull走 DOM(靠浏览器建立身份)4. 轮询节拍从「本轮开始」算
5.
canary只在真正用了浏览器时跑页面自检是纯 DOM 检查。纯 HTTP 路径没开浏览器时
canary(null)必然「没过」⇒replyPaused = true⇒ 自动回复被误停,还会弹一条「抖音改版了」的假警报(实测第一轮就中)。6. 定期补扫 DOM 会话列表(修快路径的盲区)
这是快路径引入的一个回归,必须一起修。
get_message_by_init只返回已建立会话——陌生人私信在抖音另一个接口,快路径根本看不见。而原来
pollInner每轮都会调driver.list()打开私信面板,那些陌生人就是这样被发现的。
快路径成功之后不再需要
driver.list(),于是:修法:每
DOM_LIST_EVERY_MS(默认 3 分钟)补扫一次 DOM 会话列表,以 DOM 为准合并(名字准、含陌生人),快路径补上 DOM 没列出的。
陌生人拿到名字后,
readViaHttp匹配不上会返回null,自动退回 DOM 建身份——正是「第一次见」该走的路。
pollOnce里原来有一道硬闸门:浏览器没起来 ⇒ 整轮直接 return,
pollInner一次都跑不到。我改成了「有 Cookie 就继续」,理由是快路径本来就不需要浏览器,不该被它拦住。
但这是行为变更:原来「不开浏览器就不收私信」,现在「有 Cookie 就收」。
如果维护者认为这属于产品决策而非 bug,把
pollOnce里hasCookie那几行还原即可,不影响其余改动。
实测效果
img=1,0msp9-sign.douyinpic.com等,sticker=true)兼容性
lib/channels/douyin.js与lib/index.js在 0.19.12 上完全一致,可干净应用
lib/douyin-http-fast.js零外部依赖(只用 Node 内置fetch)实现说明:删掉了一套设计
初版曾为「快路径解析不出媒体的消息」设计过一套兜底机制(退 DOM 补读 +
mediaDoneAt水位 +
mediaTries重试上限)。补上contentOf的媒体解析后发现:为不存在的场景维护状态机不值得,所以删掉了,换成上面那行
warn日志 ——既能在字段真的变化时报警,又不需要两个游标字段和几十行对账逻辑。
(这也让本 PR 小了 111 行。)
遗留 / 边界
/jingxuan后,DOM 补媒体会直接ok:false——这也是「靠 DOM 兜底」不可靠的原因