feat(matrix): 「第一条视频」改成主页最新一条,即使不在监控目标里
之前取的是本任务已有作品里**最早**的那条 —— 既不是主页上看到的第一条, 也只看得到被监控的那批链接。现在采集时对每个博主额外问一次作品列表 (倒序,只要第一条),把它的发布时间落进账号快照;矩阵优先用它,没问到 时退回库里最新的一条作品。 - 作品模式:每个博主每轮一次请求,同轮内去重;连接超时重试一次。 - 博主模式:本来就在拉作品列表,直接用其中的最新时间。 - 新列 monitor_creator_stat.latest_published_at(毫秒,可空)。
This commit is contained in:
@@ -41,6 +41,12 @@ def fake_profile(monkeypatch):
|
||||
|
||||
monkeypatch.setattr(douyin_api, "author_profile", _profile_call)
|
||||
|
||||
# 作品模式会为了「主页最新视频的时间」问一次作品列表;不挡住就会真连抖音。
|
||||
async def _videos_call(sec_user_id, count=20, *, cookie="", identity=None):
|
||||
return []
|
||||
|
||||
monkeypatch.setattr(douyin_api, "author_videos", _videos_call)
|
||||
|
||||
|
||||
def _video(aweme_id: str, likes: str = "1") -> dict:
|
||||
return {
|
||||
@@ -172,13 +178,18 @@ class TestNoteMode:
|
||||
「粘贴作品链接的监控」今天本来是能用的,别让它因为这一处走错而废掉。
|
||||
"""
|
||||
|
||||
async def must_not_be_called(*args, **kwargs):
|
||||
raise AssertionError("作品模式不该去拉博主的作品列表")
|
||||
async def only_by_creator_id(sec_user_id, count=20, *, cookie="", identity=None):
|
||||
# 可以为了「主页最新视频」问一次作品列表,但**只能用详情里的 sec_uid 问**,
|
||||
# 不能拿作品目标 id 去当 sec_uid —— 那必然失败,而且报错很难看懂。
|
||||
assert sec_user_id == "MS4wLjABAAAAtestsecuid"
|
||||
return []
|
||||
|
||||
async def fake_detail(aweme_id, *, cookie="", identity=None):
|
||||
return _video(aweme_id)
|
||||
record = _video(aweme_id)
|
||||
record["creator_id"] = "MS4wLjABAAAAtestsecuid"
|
||||
return record
|
||||
|
||||
monkeypatch.setattr(douyin_api, "author_videos", must_not_be_called)
|
||||
monkeypatch.setattr(douyin_api, "author_videos", only_by_creator_id)
|
||||
monkeypatch.setattr(douyin_api, "video_detail", fake_detail)
|
||||
|
||||
result = await _collect(
|
||||
@@ -252,6 +263,33 @@ class TestNoteMode:
|
||||
# 详情里没有作品总数,留 None 而不是 0。
|
||||
assert profiles[0]["works"] is None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_the_latest_video_time_comes_from_the_profile(self, monkeypatch, tmp_path):
|
||||
"""主页第一条视频可能不在监控目标里 —— 单独问一次,用它的发布时间。"""
|
||||
|
||||
async def fake_detail(aweme_id, *, cookie="", identity=None):
|
||||
record = _video(aweme_id)
|
||||
record.update(
|
||||
{"creator_id": "MS4wLjABAAAAtestsecuid", "creator_fans": 10}
|
||||
)
|
||||
return record
|
||||
|
||||
async def fake_videos(sec_user_id, count=20, *, cookie="", identity=None):
|
||||
assert sec_user_id == "MS4wLjABAAAAtestsecuid"
|
||||
return [{"create_time": 1793000000}]
|
||||
|
||||
monkeypatch.setattr(douyin_api, "video_detail", fake_detail)
|
||||
monkeypatch.setattr(douyin_api, "author_videos", fake_videos)
|
||||
|
||||
await _collect(
|
||||
tmp_path, mode="note", want_comments=False, targets=[_Target("111")]
|
||||
)
|
||||
|
||||
profiles = _read(
|
||||
list((tmp_path / "douyin" / "jsonl").glob("*_profile_*.jsonl"))[0]
|
||||
)
|
||||
assert profiles[0]["latest_published_at"] == 1793000000 * 1000
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_no_stats_means_no_profile_row(self, monkeypatch, tmp_path):
|
||||
"""精简版 author(字段全缺)不该写一条全 NULL 的快照。"""
|
||||
@@ -382,7 +420,10 @@ class TestCreatorProfile:
|
||||
|
||||
await _collect(tmp_path, want_comments=False)
|
||||
|
||||
assert self._profiles(tmp_path) == [_profile()]
|
||||
profile = self._profiles(tmp_path)[0]
|
||||
# 作品列表里的最新一条也带出来了(秒 -> 毫秒)。
|
||||
assert profile.pop("latest_published_at") == 1790574515 * 1000
|
||||
assert profile == _profile()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_the_profile_is_keyed_by_the_same_hash_as_the_works(
|
||||
|
||||
@@ -263,6 +263,7 @@ async def _stat(
|
||||
run_id: int = 1,
|
||||
fans: int | None = None,
|
||||
total_favorited: int | None = None,
|
||||
latest_published_at: int | None = None,
|
||||
) -> None:
|
||||
async with monitor_db.get_session() as session:
|
||||
session.add(
|
||||
@@ -275,6 +276,7 @@ async def _stat(
|
||||
works_count=0,
|
||||
fans=fans,
|
||||
total_favorited=total_favorited,
|
||||
latest_published_at=latest_published_at,
|
||||
captured_at=captured_at or get_current_timestamp(),
|
||||
)
|
||||
)
|
||||
@@ -340,29 +342,46 @@ async def test_the_creator_row_carries_the_total_likes_from_the_profile(db):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_the_first_video_date_comes_from_the_whole_table(db):
|
||||
"""第一条视频可能远在日期窗口之外 —— 要查全库,不能只看这 7 天。"""
|
||||
async def test_the_latest_video_date_comes_from_the_profile(db):
|
||||
"""主页第一条视频可能根本不在监控目标里 —— 采集时单独问过,就用它。"""
|
||||
task_id = await _task()
|
||||
now = get_current_timestamp() - 1000
|
||||
first = now - 400 * DAY_MS
|
||||
await _note(task_id, "old", "博主A", first)
|
||||
tracked = now - 3 * DAY_MS
|
||||
profile_latest = now - 60_000
|
||||
await _note(task_id, "n1", "博主A", tracked)
|
||||
await _stat(
|
||||
task_id, "hash-a", "博主A", latest_published_at=profile_latest
|
||||
)
|
||||
|
||||
async with monitor_db.get_session() as session:
|
||||
data = await service.daily_tag_matrix(session, "dy", days=7)
|
||||
|
||||
assert data["creators"][0]["latest_published_at"] == profile_latest
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_it_falls_back_to_the_newest_tracked_note(db):
|
||||
"""主页那次没问到(接口挡了/超时)时,退到库里最新的一条作品。"""
|
||||
task_id = await _task()
|
||||
now = get_current_timestamp() - 1000
|
||||
await _note(task_id, "old", "博主A", now - 400 * DAY_MS)
|
||||
await _note(task_id, "new", "博主A", now)
|
||||
|
||||
async with monitor_db.get_session() as session:
|
||||
data = await service.daily_tag_matrix(session, "dy", days=7)
|
||||
|
||||
assert data["creators"][0]["first_published_at"] == first
|
||||
assert data["creators"][0]["latest_published_at"] == now
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_a_creator_without_works_has_no_first_video_date(db):
|
||||
async def test_a_creator_without_works_has_no_latest_video_date(db):
|
||||
task_id = await _task()
|
||||
await _stat(task_id, "hash-a", "博主A", fans=10)
|
||||
|
||||
async with monitor_db.get_session() as session:
|
||||
data = await service.daily_tag_matrix(session, "dy", days=7)
|
||||
|
||||
assert data["creators"][0]["first_published_at"] is None
|
||||
assert data["creators"][0]["latest_published_at"] is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
Reference in New Issue
Block a user