Skip to content

Commit d42048c

Browse files
authored
Merge pull request #1002 from divya0795/fix/tieba-creator-skip-failed-notes
fix(tieba): 创作者模式下单个帖子详情失败不再中断整个采集
2 parents 4d629b5 + 9ddf7b2 commit d42048c

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

‎media_platform/tieba/client.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,18 @@ async def get_all_notes_by_creator_url(
825825
result: List[TiebaNote] = []
826826
page_number = 1
827827
page_size = 20
828+
semaphore = asyncio.Semaphore(config.MAX_CONCURRENCY_NUM)
829+
830+
async def get_note_detail(thread_id: str) -> Optional[TiebaNote]:
831+
async with semaphore:
832+
try:
833+
return await self.get_note_by_id(thread_id)
834+
except Exception as e:
835+
utils.logger.error(
836+
f"[BaiduTieBaClient.get_all_notes_by_creator_url] "
837+
f"Skip thread {thread_id}, get note detail failed: {e}"
838+
)
839+
return None
828840

829841
while max_note_count == 0 or len(result) < max_note_count:
830842
notes_res = await self.get_notes_by_creator_portrait(
@@ -847,7 +859,7 @@ async def get_all_notes_by_creator_url(
847859
f"[BaiduTieBaClient.get_all_notes_by_creator_url] "
848860
f"got portrait:{portrait} thread ids len: {len(thread_id_list)}"
849861
)
850-
note_detail_task = [self.get_note_by_id(thread_id) for thread_id in thread_id_list]
862+
note_detail_task = [get_note_detail(thread_id) for thread_id in thread_id_list]
851863
notes = await asyncio.gather(*note_detail_task)
852864
notes = [note for note in notes if note]
853865
if callback and notes:

‎tests/test_tieba_client_pagination.py‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3+
import asyncio
4+
35
import pytest
46

57
from media_platform.tieba.client import BaiduTieBaClient
@@ -108,3 +110,56 @@ async def fake_sleep(_):
108110

109111
assert pages == [1, 2]
110112
assert [note.note_id for note in notes] == ["1001", "1002"]
113+
114+
115+
@pytest.mark.asyncio
116+
async def test_creator_feed_skips_failed_thread_and_limits_concurrency(monkeypatch):
117+
client = BaiduTieBaClient(playwright_page=DummyPage())
118+
in_flight = 0
119+
peak_in_flight = 0
120+
stored = []
121+
122+
async def fake_get_notes_by_creator_portrait(portrait, page_number, page_size=20):
123+
return {
124+
"error_code": 0,
125+
"data": {
126+
"has_more": 0,
127+
"list": [
128+
{"thread_info": {"id": tid, "tid": tid}}
129+
for tid in ("2001", "2002", "2003")
130+
],
131+
},
132+
}
133+
134+
async def fake_get_note_by_id(note_id):
135+
nonlocal in_flight, peak_in_flight
136+
in_flight += 1
137+
peak_in_flight = max(peak_in_flight, in_flight)
138+
try:
139+
await asyncio.sleep(0)
140+
if note_id == "2002":
141+
raise Exception("Tieba PC API error, url=/c/f/pb/page_pc, response={'error_code': 4}")
142+
return TiebaNote(
143+
note_id=note_id,
144+
title="title",
145+
note_url=f"https://tieba.baidu.com/p/{note_id}",
146+
tieba_name="加工中心吧",
147+
tieba_link="https://tieba.baidu.com/f?kw=%E5%8A%A0%E5%B7%A5%E4%B8%AD%E5%BF%83",
148+
)
149+
finally:
150+
in_flight -= 1
151+
152+
async def store_notes(notes):
153+
stored.extend(note.note_id for note in notes)
154+
155+
client.get_notes_by_creator_portrait = fake_get_notes_by_creator_portrait
156+
client.get_note_by_id = fake_get_note_by_id
157+
monkeypatch.setattr("media_platform.tieba.client.config.MAX_CONCURRENCY_NUM", 1)
158+
159+
notes = await client.get_all_notes_by_creator_url(
160+
"tb.1.creator", crawl_interval=0, callback=store_notes
161+
)
162+
163+
assert [note.note_id for note in notes] == ["2001", "2003"]
164+
assert stored == ["2001", "2003"]
165+
assert peak_in_flight == 1

0 commit comments

Comments
 (0)