diff --git a/api/monitor/ingest.py b/api/monitor/ingest.py index 93f64a2..58d102a 100644 --- a/api/monitor/ingest.py +++ b/api/monitor/ingest.py @@ -61,6 +61,7 @@ from .models import ( MonitorNote, MonitorNoteMetric, MonitorRun, + MonitorTarget, MonitorTask, RUN_FAILED, RUN_PARTIAL, @@ -307,6 +308,30 @@ async def _previous_run_started_at( _AUTH_PROOF_WINDOW_MS = 6 * 60 * 60 * 1000 +async def _targets_that_are_not_ids(session: AsyncSession, task_id: int) -> List[str]: + """找出那些**明显不是 ID** 的目标值 —— 也就是原样存下来的、没解析开的链接。 + + 判定刻意收得很紧:只看「是不是长得像一串 URL」。宽松一点(比如拿平台的 bare-id 正则 + 去套)会误伤本来就合法的老目标,而这条消息是在**一轮已经失败之后**才出现的 —— + 把方向说错,比什么都不说更糟。 + + 这条存在的原因:链接校验关掉之后(见 ``service.parse_target_input``),认不出的链接 + 会被原样存成 ``external_id``,而它正是采集时唯一拿去请求的东西。结果就是「永远抓不到 + 东西」,并且会被读成登录态失效。 + """ + rows = await session.scalars( + select(MonitorTarget.external_id).where( + MonitorTarget.task_id == task_id, + MonitorTarget.enabled.is_(True), + ) + ) + return [ + value + for value in rows.all() + if value and ("://" in value or value.lower().startswith("http")) + ] + + async def _another_task_succeeded_recently(session: AsyncSession, task_id: int) -> bool: """Whether a different task fetched data recently, proving the login is valid.""" since = get_current_timestamp() - _AUTH_PROOF_WINDOW_MS @@ -765,6 +790,33 @@ async def ingest_run( comments_fetched=len(comments), ) + # 再排除「目标本身就不是个 ID」。链接校验关掉之后,认不出来的链接会被**原样**存成 + # external_id(见 service.parse_target_input),而 external_id 是采集时唯一拿去 + # 请求的东西 —— 于是永远抓不到,还会被读成「登录态失效」。 + # + # 这一条放在兄弟任务那条**之前**:它比那一条具体,而且确定得多 —— 兄弟任务那条 + # 赌的是「过去 6 小时里有没有别的任务成功过」,而 6 小时是很长的窗口,隔壁任务晚 + # 几秒成功就撞不上(真事:任务 1 的成功比这一轮的失败晚 6 秒,于是报成了登录失效)。 + broken = await _targets_that_are_not_ids(session, run.task_id) + if broken: + run.error_message = ( + f"Target {broken[0]} looks like an unparsed URL, not a target id" + ) + await _emit( + session, + run, + EVENT_NO_DATA, + f"本次未抓到任何作品:目标「{broken[0]}」看起来是一串**没解析开的链接**," + f"不是博主/作品 ID。请重新粘贴完整链接 —— 登录态和它没关系。", + severity="error", + payload={"out_dir": str(out_dir), "targets": broken}, + ) + return IngestResult( + status=RUN_PARTIAL, + error=run.error_message, + comments_fetched=len(comments), + ) + # Blaming the cookie is only honest if nothing else is authenticating. # A sibling task that just succeeded proves the login works, so the # fault is with this target (bad/expired per-creator token, an empty diff --git a/tests/test_monitor_ingest.py b/tests/test_monitor_ingest.py index 413eff0..5c4cdb9 100644 --- a/tests/test_monitor_ingest.py +++ b/tests/test_monitor_ingest.py @@ -59,6 +59,7 @@ from api.monitor.models import ( MonitorNote, MonitorNoteMetric, MonitorRun, + MonitorTarget, MonitorTask, RUN_FAILED, RUN_PARTIAL, @@ -1089,3 +1090,59 @@ class TestNicknameRefresh: await ingest_run(db, run, task, tmp_path) assert (await db.scalar(select(MonitorNote))).creator_name == "有名字" + + +class TestABrokenTargetIsNotBlamedOnTheCookie: + """目标是一串没解析开的链接时,**不许报「登录态失效」**。 + + 真事(2026-10-14):任务 22 的目标被存成了 `https://xhslink.cn/o/AhQpHsqcme5`(短链, + 链接校验关掉之后原样存下来的),于是永远抓不到东西。而报出来的是「疑似登录态失效」, + 因为那一刻往前 6 小时正好没有别的任务成功过 —— 隔壁任务 1 的成功**比它晚 6 秒**, + 就是撞不上。同一份 cookie,任务 1 采集正常。 + """ + + @staticmethod + async def _seed_target(db, task, external_id: str) -> None: + db.add( + MonitorTarget( + task_id=task.id, + kind=task.mode, + external_id=external_id, + xsec_token="", + xsec_source="", + raw_value=external_id, + label=external_id, + enabled=True, + created_at=0, + ) + ) + await db.flush() + + @pytest.mark.asyncio + async def test_an_unparsed_url_target_is_reported_as_such(self, db, tmp_path): + task = await _make_task(db) + await self._seed_target(db, task, "https://xhslink.cn/o/AhQpHsqcme5") + run = await _make_run(db, task, started_at=1) + _write_run_dir(tmp_path, [], comments=[]) + + await ingest_run(db, run, task, tmp_path) + + # 不许出现「登录失效」那条 —— 那会把人的注意力引到 cookie 上,而 cookie 是好的。 + assert await _events(db, EVENT_AUTH_FAILURE) == [] + no_data = await _events(db, EVENT_NO_DATA) + assert no_data + assert "没解析开" in no_data[0].title + assert "xhslink.cn" in no_data[0].title + + @pytest.mark.asyncio + async def test_a_normal_target_still_gets_the_usual_diagnosis(self, db, tmp_path): + """不能因为加了这条就把原有的判断顶掉 —— 正常的目标还是走原来那套。""" + task = await _make_task(db) + await self._seed_target(db, task, "5f58bd990000000001003753") + run = await _make_run(db, task, started_at=1) + _write_run_dir(tmp_path, [], comments=[]) + + await ingest_run(db, run, task, tmp_path) + + assert await _events(db, EVENT_AUTH_FAILURE) + assert await _events(db, EVENT_NO_DATA) == []