xiaohongshu.py 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761
  1. # -*- coding: utf-8 -*-
  2. """
  3. 小红书视频发布器
  4. 参考: matrix/xhs_uploader/main.py
  5. 使用 xhs SDK API 方式发布,更稳定
  6. """
  7. import asyncio
  8. import os
  9. import sys
  10. import time
  11. from pathlib import Path
  12. from typing import List
  13. from .base import (
  14. BasePublisher, PublishParams, PublishResult,
  15. WorkItem, WorksResult, CommentItem, CommentsResult
  16. )
  17. # 添加 matrix 项目路径,用于导入签名脚本
  18. MATRIX_PATH = Path(__file__).parent.parent.parent.parent / "matrix"
  19. sys.path.insert(0, str(MATRIX_PATH))
  20. # 尝试导入 xhs SDK
  21. try:
  22. from xhs import XhsClient
  23. XHS_SDK_AVAILABLE = True
  24. except ImportError:
  25. print("[Warning] xhs 库未安装,请运行: pip install xhs")
  26. XhsClient = None
  27. XHS_SDK_AVAILABLE = False
  28. # 签名脚本路径
  29. STEALTH_JS_PATH = MATRIX_PATH / "xhs-api" / "js" / "stealth.min.js"
  30. class XiaohongshuPublisher(BasePublisher):
  31. """
  32. 小红书视频发布器
  33. 优先使用 xhs SDK API 方式发布
  34. """
  35. platform_name = "xiaohongshu"
  36. login_url = "https://creator.xiaohongshu.com/"
  37. publish_url = "https://creator.xiaohongshu.com/publish/publish"
  38. cookie_domain = ".xiaohongshu.com"
  39. async def get_sign(self, uri: str, data=None, a1: str = "", web_session: str = ""):
  40. """获取小红书 API 签名"""
  41. from playwright.async_api import async_playwright
  42. try:
  43. async with async_playwright() as playwright:
  44. browser = await playwright.chromium.launch(headless=True)
  45. browser_context = await browser.new_context()
  46. if STEALTH_JS_PATH.exists():
  47. await browser_context.add_init_script(path=str(STEALTH_JS_PATH))
  48. page = await browser_context.new_page()
  49. await page.goto("https://www.xiaohongshu.com")
  50. await asyncio.sleep(1)
  51. await page.reload()
  52. await asyncio.sleep(1)
  53. if a1:
  54. await browser_context.add_cookies([
  55. {'name': 'a1', 'value': a1, 'domain': ".xiaohongshu.com", 'path': "/"}
  56. ])
  57. await page.reload()
  58. await asyncio.sleep(0.5)
  59. encrypt_params = await page.evaluate(
  60. "([url, data]) => window._webmsxyw(url, data)",
  61. [uri, data]
  62. )
  63. await browser_context.close()
  64. await browser.close()
  65. return {
  66. "x-s": encrypt_params["X-s"],
  67. "x-t": str(encrypt_params["X-t"])
  68. }
  69. except Exception as e:
  70. import traceback
  71. traceback.print_exc()
  72. raise Exception(f"签名失败: {e}")
  73. def sign_sync(self, uri, data=None, a1="", web_session=""):
  74. """
  75. 同步签名函数,供 XhsClient 使用。
  76. 注意:发布流程运行在 asyncio 事件循环中(通过 asyncio.run 启动),
  77. 这里如果再调用 asyncio.run 会触发 “asyncio.run() cannot be called from a running event loop”。
  78. 因此改为使用 sync_playwright 的同步实现(参考 matrix/xhs_uploader)。
  79. """
  80. try:
  81. from playwright.sync_api import sync_playwright
  82. except Exception as e:
  83. raise Exception(f"缺少 playwright 同步接口支持: {e}")
  84. last_exc: Exception | None = None
  85. for attempt in range(1, 6):
  86. try:
  87. with sync_playwright() as playwright:
  88. browser = playwright.chromium.launch(headless=True)
  89. context = browser.new_context()
  90. if STEALTH_JS_PATH.exists():
  91. context.add_init_script(path=str(STEALTH_JS_PATH))
  92. page = context.new_page()
  93. page.goto("https://www.xiaohongshu.com", wait_until="domcontentloaded", timeout=60000)
  94. if a1:
  95. context.add_cookies([
  96. {'name': 'a1', 'value': a1, 'domain': ".xiaohongshu.com", 'path': "/"}
  97. ])
  98. page.reload(wait_until="domcontentloaded")
  99. # 参考 matrix:设置完 cookie 后需要稍等,否则可能出现 window._webmsxyw 不存在
  100. time.sleep(1.5)
  101. encrypt_params = page.evaluate(
  102. "([url, data]) => window._webmsxyw(url, data)",
  103. [uri, data]
  104. )
  105. context.close()
  106. browser.close()
  107. return {
  108. "x-s": encrypt_params["X-s"],
  109. "x-t": str(encrypt_params["X-t"])
  110. }
  111. except Exception as e:
  112. last_exc = e
  113. # 轻微退避重试
  114. time.sleep(0.4 * attempt)
  115. raise Exception(f"签名失败: {last_exc}")
  116. async def publish_via_api(self, cookies: str, params: PublishParams) -> PublishResult:
  117. """通过 API 发布视频"""
  118. if not XHS_SDK_AVAILABLE:
  119. raise Exception("xhs SDK 未安装,请运行: pip install xhs")
  120. self.report_progress(10, "正在通过 API 发布...")
  121. print(f"[{self.platform_name}] 使用 XHS SDK API 发布...")
  122. print(f"[{self.platform_name}] 视频路径: {params.video_path}")
  123. print(f"[{self.platform_name}] 标题: {params.title}")
  124. # 转换 cookie 格式
  125. cookie_list = self.parse_cookies(cookies)
  126. cookie_string = self.cookies_to_string(cookie_list) if cookie_list else cookies
  127. print(f"[{self.platform_name}] Cookie 长度: {len(cookie_string)}")
  128. self.report_progress(20, "正在上传视频...")
  129. # 创建客户端
  130. xhs_client = XhsClient(cookie_string, sign=self.sign_sync)
  131. print(f"[{self.platform_name}] 开始调用 create_video_note...")
  132. # 发布视频
  133. try:
  134. result = xhs_client.create_video_note(
  135. title=params.title,
  136. desc=params.description or params.title,
  137. topics=params.tags or [],
  138. post_time=params.publish_date.strftime("%Y-%m-%d %H:%M:%S") if params.publish_date else None,
  139. video_path=params.video_path,
  140. cover_path=params.cover_path if params.cover_path and os.path.exists(params.cover_path) else None
  141. )
  142. print(f"[{self.platform_name}] SDK 返回结果: {result}")
  143. except Exception as e:
  144. import traceback
  145. traceback.print_exc()
  146. print(f"[{self.platform_name}] SDK 调用失败: {e}")
  147. raise Exception(f"XHS SDK 发布失败: {e}")
  148. # 验证返回结果
  149. if not result:
  150. raise Exception("XHS SDK 返回空结果")
  151. # 检查是否有错误
  152. if isinstance(result, dict):
  153. if result.get("code") and result.get("code") != 0:
  154. raise Exception(f"发布失败: {result.get('msg', '未知错误')}")
  155. if result.get("success") == False:
  156. raise Exception(f"发布失败: {result.get('msg', result.get('error', '未知错误'))}")
  157. note_id = result.get("note_id", "") if isinstance(result, dict) else ""
  158. video_url = result.get("url", "") if isinstance(result, dict) else ""
  159. if not note_id:
  160. print(f"[{self.platform_name}] 警告: 未获取到 note_id,返回结果: {result}")
  161. self.report_progress(100, "发布成功")
  162. print(f"[{self.platform_name}] 发布成功! note_id={note_id}, url={video_url}")
  163. return PublishResult(
  164. success=True,
  165. platform=self.platform_name,
  166. video_id=note_id,
  167. video_url=video_url,
  168. message="发布成功"
  169. )
  170. async def publish(self, cookies: str, params: PublishParams) -> PublishResult:
  171. """发布视频到小红书 - 参考 matrix/xhs_uploader/main.py"""
  172. print(f"\n{'='*60}")
  173. print(f"[{self.platform_name}] 开始发布视频")
  174. print(f"[{self.platform_name}] 视频路径: {params.video_path}")
  175. print(f"[{self.platform_name}] 标题: {params.title}")
  176. print(f"[{self.platform_name}] Headless: {self.headless}")
  177. print(f"[{self.platform_name}] XHS SDK 可用: {XHS_SDK_AVAILABLE}")
  178. print(f"{'='*60}")
  179. # 检查视频文件
  180. if not os.path.exists(params.video_path):
  181. raise Exception(f"视频文件不存在: {params.video_path}")
  182. print(f"[{self.platform_name}] 视频文件存在,大小: {os.path.getsize(params.video_path)} bytes")
  183. self.report_progress(5, "正在准备发布...")
  184. # 参考 matrix: 优先使用 XHS SDK API 方式发布(更稳定)
  185. if XHS_SDK_AVAILABLE:
  186. try:
  187. print(f"[{self.platform_name}] 尝试使用 XHS SDK API 发布...")
  188. result = await self.publish_via_api(cookies, params)
  189. print(f"[{self.platform_name}] API 发布完成: success={result.success}")
  190. # 如果 API 返回成功,直接返回
  191. if result.success:
  192. return result
  193. # 如果 API 返回失败但有具体错误,也返回
  194. if result.error and "请刷新" not in result.error:
  195. return result
  196. # 其他情况尝试 Playwright 方式
  197. print(f"[{self.platform_name}] API 方式未成功,尝试 Playwright...")
  198. except Exception as e:
  199. import traceback
  200. traceback.print_exc()
  201. print(f"[{self.platform_name}] API 发布失败: {e}")
  202. print(f"[{self.platform_name}] 尝试使用 Playwright 方式...")
  203. # 使用 Playwright 方式发布
  204. print(f"[{self.platform_name}] 使用 Playwright 方式发布...")
  205. return await self.publish_via_playwright(cookies, params)
  206. async def publish_via_playwright(self, cookies: str, params: PublishParams) -> PublishResult:
  207. """通过 Playwright 发布视频"""
  208. self.report_progress(10, "正在初始化浏览器...")
  209. print(f"[{self.platform_name}] Playwright 方式开始...")
  210. await self.init_browser()
  211. cookie_list = self.parse_cookies(cookies)
  212. print(f"[{self.platform_name}] 设置 {len(cookie_list)} 个 cookies")
  213. await self.set_cookies(cookie_list)
  214. if not self.page:
  215. raise Exception("Page not initialized")
  216. self.report_progress(15, "正在打开发布页面...")
  217. # 直接访问视频发布页面
  218. publish_url = "https://creator.xiaohongshu.com/publish/publish?source=official"
  219. print(f"[{self.platform_name}] 打开页面: {publish_url}")
  220. await self.page.goto(publish_url)
  221. await asyncio.sleep(3)
  222. current_url = self.page.url
  223. print(f"[{self.platform_name}] 当前 URL: {current_url}")
  224. # 检查登录状态
  225. if "login" in current_url or "passport" in current_url:
  226. screenshot_base64 = await self.capture_screenshot()
  227. return PublishResult(
  228. success=False,
  229. platform=self.platform_name,
  230. error="登录已过期,请重新登录",
  231. screenshot_base64=screenshot_base64,
  232. page_url=current_url,
  233. status='need_captcha',
  234. need_captcha=True,
  235. captcha_type='login'
  236. )
  237. # 使用 AI 检查验证码
  238. ai_captcha = await self.ai_check_captcha()
  239. if ai_captcha['has_captcha']:
  240. print(f"[{self.platform_name}] AI检测到验证码: {ai_captcha['captcha_type']}", flush=True)
  241. screenshot_base64 = await self.capture_screenshot()
  242. return PublishResult(
  243. success=False,
  244. platform=self.platform_name,
  245. error=f"检测到{ai_captcha['captcha_type']}验证码,需要使用有头浏览器完成验证",
  246. screenshot_base64=screenshot_base64,
  247. page_url=current_url,
  248. status='need_captcha',
  249. need_captcha=True,
  250. captcha_type=ai_captcha['captcha_type']
  251. )
  252. self.report_progress(20, "正在上传视频...")
  253. # 等待页面加载
  254. await asyncio.sleep(2)
  255. # 上传视频
  256. upload_triggered = False
  257. # 方法1: 直接设置隐藏的 file input
  258. print(f"[{self.platform_name}] 尝试方法1: 设置 file input")
  259. file_inputs = self.page.locator('input[type="file"]')
  260. input_count = await file_inputs.count()
  261. print(f"[{self.platform_name}] 找到 {input_count} 个 file input")
  262. if input_count > 0:
  263. # 找到接受视频的 input
  264. for i in range(input_count):
  265. input_el = file_inputs.nth(i)
  266. accept = await input_el.get_attribute('accept') or ''
  267. print(f"[{self.platform_name}] Input {i} accept: {accept}")
  268. if 'video' in accept or '*' in accept or not accept:
  269. await input_el.set_input_files(params.video_path)
  270. upload_triggered = True
  271. print(f"[{self.platform_name}] 视频文件已设置到 input {i}")
  272. break
  273. # 方法2: 点击上传区域触发文件选择器
  274. if not upload_triggered:
  275. print(f"[{self.platform_name}] 尝试方法2: 点击上传区域")
  276. try:
  277. upload_area = self.page.locator('[class*="upload-wrapper"], [class*="upload-area"], .upload-input').first
  278. if await upload_area.count() > 0:
  279. async with self.page.expect_file_chooser(timeout=5000) as fc_info:
  280. await upload_area.click()
  281. file_chooser = await fc_info.value
  282. await file_chooser.set_files(params.video_path)
  283. upload_triggered = True
  284. print(f"[{self.platform_name}] 通过点击上传区域上传成功")
  285. except Exception as e:
  286. print(f"[{self.platform_name}] 方法2失败: {e}")
  287. if not upload_triggered:
  288. screenshot_base64 = await self.capture_screenshot()
  289. page_url = await self.get_page_url()
  290. return PublishResult(
  291. success=False,
  292. platform=self.platform_name,
  293. error="无法上传视频文件",
  294. screenshot_base64=screenshot_base64,
  295. page_url=page_url,
  296. status='need_action'
  297. )
  298. self.report_progress(40, "等待视频上传完成...")
  299. print(f"[{self.platform_name}] 等待视频上传和处理...")
  300. # 等待上传完成(检测页面变化)
  301. upload_complete = False
  302. for i in range(60): # 最多等待3分钟
  303. await asyncio.sleep(3)
  304. # 检查是否有标题输入框(上传完成后出现)
  305. title_input_count = await self.page.locator('input[placeholder*="标题"], input[placeholder*="填写标题"]').count()
  306. # 或者检查编辑器区域
  307. editor_count = await self.page.locator('[class*="ql-editor"], [contenteditable="true"]').count()
  308. # 检查发布按钮是否可见
  309. publish_btn_count = await self.page.locator('.publishBtn, button:has-text("发布")').count()
  310. print(f"[{self.platform_name}] 检测 {i+1}: 标题框={title_input_count}, 编辑器={editor_count}, 发布按钮={publish_btn_count}")
  311. if title_input_count > 0 or (editor_count > 0 and publish_btn_count > 0):
  312. upload_complete = True
  313. print(f"[{self.platform_name}] 视频上传完成!")
  314. break
  315. if not upload_complete:
  316. screenshot_base64 = await self.capture_screenshot()
  317. page_url = await self.get_page_url()
  318. return PublishResult(
  319. success=False,
  320. platform=self.platform_name,
  321. error="视频上传超时",
  322. screenshot_base64=screenshot_base64,
  323. page_url=page_url,
  324. status='need_action'
  325. )
  326. await asyncio.sleep(2)
  327. self.report_progress(60, "正在填写笔记信息...")
  328. print(f"[{self.platform_name}] 填写标题: {params.title[:20]}")
  329. # 填写标题
  330. title_filled = False
  331. title_selectors = [
  332. 'input[placeholder*="标题"]',
  333. 'input[placeholder*="填写标题"]',
  334. '[class*="title"] input',
  335. '.c-input_inner',
  336. ]
  337. for selector in title_selectors:
  338. title_input = self.page.locator(selector).first
  339. if await title_input.count() > 0:
  340. await title_input.click()
  341. await title_input.fill('') # 先清空
  342. await title_input.fill(params.title[:20])
  343. title_filled = True
  344. print(f"[{self.platform_name}] 标题已填写,使用选择器: {selector}")
  345. break
  346. if not title_filled:
  347. print(f"[{self.platform_name}] 警告: 未找到标题输入框")
  348. # 填写描述和标签
  349. if params.description or params.tags:
  350. desc_filled = False
  351. desc_selectors = [
  352. '[class*="ql-editor"]',
  353. '[class*="content-input"] [contenteditable="true"]',
  354. '[class*="editor"] [contenteditable="true"]',
  355. '.ql-editor',
  356. ]
  357. for selector in desc_selectors:
  358. desc_input = self.page.locator(selector).first
  359. if await desc_input.count() > 0:
  360. await desc_input.click()
  361. await asyncio.sleep(0.5)
  362. if params.description:
  363. await self.page.keyboard.type(params.description, delay=20)
  364. print(f"[{self.platform_name}] 描述已填写")
  365. if params.tags:
  366. # 添加标签
  367. await self.page.keyboard.press("Enter")
  368. for tag in params.tags[:5]: # 最多5个标签
  369. await self.page.keyboard.type(f"#{tag}", delay=20)
  370. await asyncio.sleep(0.3)
  371. await self.page.keyboard.press("Space")
  372. print(f"[{self.platform_name}] 标签已填写: {params.tags[:5]}")
  373. desc_filled = True
  374. break
  375. if not desc_filled:
  376. print(f"[{self.platform_name}] 警告: 未找到描述输入框")
  377. await asyncio.sleep(2)
  378. self.report_progress(80, "正在发布...")
  379. await asyncio.sleep(2)
  380. # 滚动到页面底部确保发布按钮可见
  381. await self.page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
  382. await asyncio.sleep(1)
  383. print(f"[{self.platform_name}] 查找发布按钮...")
  384. # 点击发布
  385. publish_selectors = [
  386. 'button.publishBtn',
  387. '.publishBtn',
  388. 'button.d-button.red',
  389. 'button:has-text("发布"):not(:has-text("定时发布"))',
  390. '[class*="publish"][class*="btn"]',
  391. ]
  392. publish_clicked = False
  393. for selector in publish_selectors:
  394. try:
  395. btn = self.page.locator(selector).first
  396. if await btn.count() > 0:
  397. is_visible = await btn.is_visible()
  398. is_enabled = await btn.is_enabled()
  399. print(f"[{self.platform_name}] 按钮 {selector}: visible={is_visible}, enabled={is_enabled}")
  400. if is_visible and is_enabled:
  401. box = await btn.bounding_box()
  402. if box:
  403. print(f"[{self.platform_name}] 点击发布按钮: {selector}, 位置: ({box['x']}, {box['y']})")
  404. # 使用真实鼠标点击
  405. await self.page.mouse.click(box['x'] + box['width']/2, box['y'] + box['height']/2)
  406. publish_clicked = True
  407. break
  408. except Exception as e:
  409. print(f"[{self.platform_name}] 选择器 {selector} 错误: {e}")
  410. if not publish_clicked:
  411. # 保存截图用于调试
  412. screenshot_path = f"debug_publish_failed_{self.platform_name}.png"
  413. await self.page.screenshot(path=screenshot_path, full_page=True)
  414. print(f"[{self.platform_name}] 未找到发布按钮,截图保存到: {screenshot_path}")
  415. # 打印页面 HTML 结构用于调试
  416. buttons = await self.page.query_selector_all('button')
  417. print(f"[{self.platform_name}] 页面上共有 {len(buttons)} 个按钮")
  418. for i, btn in enumerate(buttons[:10]):
  419. text = await btn.text_content() or ''
  420. cls = await btn.get_attribute('class') or ''
  421. print(f" 按钮 {i}: text='{text.strip()[:30]}', class='{cls[:50]}'")
  422. raise Exception("未找到发布按钮")
  423. print(f"[{self.platform_name}] 已点击发布按钮,等待发布完成...")
  424. self.report_progress(90, "等待发布结果...")
  425. # 等待发布完成(检测 URL 变化或成功提示)
  426. publish_success = False
  427. for i in range(20): # 最多等待 20 秒
  428. await asyncio.sleep(1)
  429. current_url = self.page.url
  430. # 检查是否跳转到发布成功页面或内容管理页面
  431. if "published=true" in current_url or "success" in current_url or "content" in current_url:
  432. publish_success = True
  433. print(f"[{self.platform_name}] 发布成功! 跳转到: {current_url}")
  434. break
  435. # 检查是否有成功提示
  436. try:
  437. success_msg = await self.page.locator('[class*="success"], .toast-success, [class*="Toast"]').first.is_visible()
  438. if success_msg:
  439. publish_success = True
  440. print(f"[{self.platform_name}] 检测到成功提示!")
  441. break
  442. except:
  443. pass
  444. # 检查是否有错误提示
  445. try:
  446. error_elements = self.page.locator('[class*="error"], .toast-error, [class*="fail"]')
  447. if await error_elements.count() > 0:
  448. error_text = await error_elements.first.text_content()
  449. if error_text and len(error_text.strip()) > 0:
  450. raise Exception(f"发布失败: {error_text.strip()}")
  451. except Exception as e:
  452. if "发布失败" in str(e):
  453. raise
  454. # 如果没有明确的成功标志,返回截图供 AI 分析
  455. if not publish_success:
  456. final_url = self.page.url
  457. print(f"[{self.platform_name}] 发布结果不确定,当前 URL: {final_url}")
  458. screenshot_base64 = await self.capture_screenshot()
  459. print(f"[{self.platform_name}] 已获取截图供 AI 分析")
  460. # 如果 URL 还是发布页面,可能需要继续操作
  461. if "publish/publish" in final_url:
  462. return PublishResult(
  463. success=False,
  464. platform=self.platform_name,
  465. error="发布结果待确认,请查看截图",
  466. screenshot_base64=screenshot_base64,
  467. page_url=final_url,
  468. status='need_action'
  469. )
  470. self.report_progress(100, "发布完成")
  471. print(f"[{self.platform_name}] Playwright 方式发布完成!")
  472. screenshot_base64 = await self.capture_screenshot()
  473. page_url = await self.get_page_url()
  474. return PublishResult(
  475. success=True,
  476. platform=self.platform_name,
  477. message="发布完成",
  478. screenshot_base64=screenshot_base64,
  479. page_url=page_url,
  480. status='success'
  481. )
  482. async def get_account_info(self, cookies: str) -> dict:
  483. """获取账号信息"""
  484. print(f"\n{'='*60}")
  485. print(f"[{self.platform_name}] 获取账号信息")
  486. print(f"{'='*60}")
  487. captured_info = {}
  488. try:
  489. await self.init_browser()
  490. cookie_list = self.parse_cookies(cookies)
  491. await self.set_cookies(cookie_list)
  492. if not self.page:
  493. raise Exception("Page not initialized")
  494. # 监听个人信息 API
  495. async def handle_response(response):
  496. nonlocal captured_info
  497. if 'api/galaxy/creator/home/personal_info' in response.url:
  498. try:
  499. json_data = await response.json()
  500. print(f"[{self.platform_name}] 捕获个人信息 API", flush=True)
  501. if json_data.get('success') or json_data.get('code') == 0:
  502. data = json_data.get('data', {})
  503. captured_info = {
  504. "account_id": f"xhs_{data.get('red_num', '')}",
  505. "account_name": data.get('name', ''),
  506. "avatar_url": data.get('avatar', ''),
  507. "fans_count": data.get('fans_count', 0),
  508. "works_count": 0 # 暂时无法直接获取准确的作品数,需要从作品列表获取
  509. }
  510. except Exception as e:
  511. print(f"[{self.platform_name}] 解析个人信息失败: {e}", flush=True)
  512. self.page.on('response', handle_response)
  513. # 访问首页
  514. print(f"[{self.platform_name}] 访问创作者首页...", flush=True)
  515. await self.page.goto("https://creator.xiaohongshu.com/new/home", wait_until="domcontentloaded")
  516. # 等待 API 响应
  517. for _ in range(10):
  518. if captured_info:
  519. break
  520. await asyncio.sleep(1)
  521. if not captured_info:
  522. print(f"[{self.platform_name}] 未捕获到个人信息,尝试刷新...", flush=True)
  523. await self.page.reload()
  524. for _ in range(10):
  525. if captured_info:
  526. break
  527. await asyncio.sleep(1)
  528. if not captured_info:
  529. raise Exception("无法获取账号信息")
  530. # 尝试获取作品数(从首页或其他地方)
  531. # 或者简单地返回已获取的信息,作品数由 get_works 更新
  532. return {
  533. "success": True,
  534. **captured_info
  535. }
  536. except Exception as e:
  537. import traceback
  538. traceback.print_exc()
  539. return {
  540. "success": False,
  541. "error": str(e)
  542. }
  543. finally:
  544. await self.close_browser()
  545. async def get_works(self, cookies: str, page: int = 0, page_size: int = 20) -> WorksResult:
  546. """获取小红书作品列表 - 通过直接调用创作者笔记列表 API 获取"""
  547. print(f"\n{'='*60}", flush=True)
  548. print(f"[{self.platform_name}] 获取作品列表", flush=True)
  549. print(f"[{self.platform_name}] page={page}, page_size={page_size}", flush=True)
  550. print(f"{'='*60}", flush=True)
  551. works: List[WorkItem] = []
  552. total = 0
  553. has_more = False
  554. next_page = ""
  555. api_page_size = 20
  556. try:
  557. await self.init_browser()
  558. cookie_list = self.parse_cookies(cookies)
  559. # 打印 cookies 信息用于调试
  560. print(f"[{self.platform_name}] 解析到 {len(cookie_list)} 个 cookies", flush=True)
  561. await self.set_cookies(cookie_list)
  562. if not self.page:
  563. raise Exception("Page not initialized")
  564. # 访问笔记管理页面 - 页面会自动发起 API 请求
  565. print(f"[{self.platform_name}] 访问笔记管理页面...", flush=True)
  566. try:
  567. await self.page.goto("https://creator.xiaohongshu.com/new/note-manager", wait_until="domcontentloaded", timeout=30000)
  568. except Exception as nav_error:
  569. print(f"[{self.platform_name}] 导航超时,但继续尝试: {nav_error}", flush=True)
  570. # 检查登录状态
  571. current_url = self.page.url
  572. print(f"[{self.platform_name}] 当前页面: {current_url}", flush=True)
  573. if "login" in current_url:
  574. raise Exception("Cookie 已过期,请重新登录")
  575. # 等待页面完全加载,确保签名函数可用
  576. print(f"[{self.platform_name}] 等待页面完全加载和签名函数初始化...", flush=True)
  577. await asyncio.sleep(3)
  578. # 检查签名函数是否可用
  579. sign_check_attempts = 0
  580. max_sign_check_attempts = 10
  581. while sign_check_attempts < max_sign_check_attempts:
  582. sign_available = await self.page.evaluate("""() => {
  583. return typeof window !== 'undefined' && typeof window._webmsxyw === 'function';
  584. }""")
  585. if sign_available:
  586. print(f"[{self.platform_name}] ✓ 签名函数 _webmsxyw 已可用", flush=True)
  587. break
  588. sign_check_attempts += 1
  589. print(f"[{self.platform_name}] ⏳ 等待签名函数... ({sign_check_attempts}/{max_sign_check_attempts})", flush=True)
  590. await asyncio.sleep(1)
  591. if sign_check_attempts >= max_sign_check_attempts:
  592. print(f"[{self.platform_name}] ⚠️ 警告: 签名函数 _webmsxyw 在 {max_sign_check_attempts} 次检查后仍不可用", flush=True)
  593. print(f"[{self.platform_name}] 继续尝试,但 API 调用可能会失败", flush=True)
  594. async def fetch_notes_page(p):
  595. # 再次检查签名函数(每次调用前都检查)
  596. sign_available = await self.page.evaluate("""() => {
  597. return typeof window !== 'undefined' && typeof window._webmsxyw === 'function';
  598. }""")
  599. if not sign_available:
  600. print(f"[{self.platform_name}] ⚠️ 签名函数 _webmsxyw 不可用,等待...", flush=True)
  601. await asyncio.sleep(2)
  602. return await self.page.evaluate(
  603. """async (pageNum) => {
  604. try {
  605. // 使用正确的 API 端点:/api/galaxy/v2/creator/note/user/posted
  606. const url = `/api/galaxy/v2/creator/note/user/posted?tab=0&page=${pageNum}`;
  607. const headers = {
  608. 'Accept': 'application/json, text/plain, */*',
  609. 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
  610. 'Referer': 'https://creator.xiaohongshu.com/new/note-manager',
  611. 'Sec-Fetch-Dest': 'empty',
  612. 'Sec-Fetch-Mode': 'cors',
  613. 'Sec-Fetch-Site': 'same-origin'
  614. };
  615. // 尝试获取签名
  616. let signResult = { hasSign: false, x_s: '', x_t: '', x_s_common: '', error: '' };
  617. if (typeof window !== 'undefined' && typeof window._webmsxyw === 'function') {
  618. try {
  619. const sign = window._webmsxyw(url, '');
  620. headers['x-s'] = sign['X-s'];
  621. headers['x-t'] = String(sign['X-t']);
  622. // 检查是否有 x-s-common
  623. if (sign['X-s-common']) {
  624. headers['x-s-common'] = sign['X-s-common'];
  625. }
  626. signResult = {
  627. hasSign: true,
  628. x_s: sign['X-s'] ? sign['X-s'].substring(0, 50) + '...' : '',
  629. x_t: String(sign['X-t']),
  630. x_s_common: sign['X-s-common'] ? sign['X-s-common'].substring(0, 50) + '...' : '',
  631. error: ''
  632. };
  633. console.log('签名生成成功:', signResult);
  634. } catch (e) {
  635. signResult.error = e.toString();
  636. console.error('签名生成失败:', e);
  637. }
  638. } else {
  639. signResult.error = '_webmsxyw function not found';
  640. console.error('签名函数不存在');
  641. }
  642. const res = await fetch(url, {
  643. method: 'GET',
  644. credentials: 'include',
  645. headers
  646. });
  647. const responseData = await res.json();
  648. return {
  649. ...responseData,
  650. _debug: {
  651. signResult: signResult,
  652. status: res.status,
  653. statusText: res.statusText
  654. }
  655. };
  656. } catch (e) {
  657. return { success: false, error: e.toString() };
  658. }
  659. }""",
  660. p
  661. )
  662. def parse_notes(notes_list):
  663. parsed = []
  664. for note in notes_list:
  665. note_id = note.get('id', '')
  666. if not note_id:
  667. continue
  668. cover_url = ''
  669. images_list = note.get('images_list', [])
  670. if images_list:
  671. cover_url = images_list[0].get('url', '')
  672. if cover_url.startswith('http://'):
  673. cover_url = cover_url.replace('http://', 'https://')
  674. duration = note.get('video_info', {}).get('duration', 0)
  675. status = 'published'
  676. tab_status = note.get('tab_status', 1)
  677. if tab_status == 0:
  678. status = 'draft'
  679. elif tab_status == 2:
  680. status = 'reviewing'
  681. elif tab_status == 3:
  682. status = 'rejected'
  683. parsed.append(WorkItem(
  684. work_id=note_id,
  685. title=note.get('display_title', '') or '无标题',
  686. cover_url=cover_url,
  687. duration=duration,
  688. status=status,
  689. publish_time=note.get('time', ''),
  690. play_count=note.get('view_count', 0),
  691. like_count=note.get('likes', 0),
  692. comment_count=note.get('comments_count', 0),
  693. share_count=note.get('shared_count', 0),
  694. collect_count=note.get('collected_count', 0),
  695. ))
  696. return parsed
  697. resp = None
  698. for attempt in range(1, 4):
  699. resp = await fetch_notes_page(page)
  700. # 打印调试信息
  701. if resp and isinstance(resp, dict) and resp.get('_debug'):
  702. debug_info = resp.get('_debug', {})
  703. sign_result = debug_info.get('signResult', {})
  704. print(f"[{self.platform_name}] 🔍 调试信息: 签名可用: {sign_result.get('hasSign', False)}, X-S: {sign_result.get('x_s', '')}, X-T: {sign_result.get('x_t', '')}, X-S-Common: {sign_result.get('x_s_common', '')}, 签名错误: {sign_result.get('error', '')}, HTTP 状态: {debug_info.get('status', 'N/A')}", flush=True)
  705. resp.pop('_debug', None)
  706. if resp and (resp.get('success') or resp.get('code') == 0) and resp.get('data'):
  707. break
  708. print(f"[{self.platform_name}] 拉取作品列表失败,重试 {attempt}/3: {str(resp)[:200]}", flush=True)
  709. await asyncio.sleep(1.2 * attempt)
  710. if not resp or not (resp.get('success') or resp.get('code') == 0) or not resp.get('data'):
  711. error_msg = resp.get('msg') if isinstance(resp, dict) else str(resp)
  712. # 打印详细的错误信息
  713. if isinstance(resp, dict):
  714. if resp.get('msg'):
  715. print(f"[{self.platform_name}] 错误消息: {resp.get('msg')}", flush=True)
  716. if resp.get('message'):
  717. print(f"[{self.platform_name}] 错误消息: {resp.get('message')}", flush=True)
  718. if resp.get('error'):
  719. print(f"[{self.platform_name}] 错误: {resp.get('error')}", flush=True)
  720. raise Exception(f"无法获取作品列表数据: {error_msg}")
  721. data = resp.get('data', {}) or {}
  722. notes = data.get('notes', []) or []
  723. print(f"[{self.platform_name}] 第 {page} 页 notes 数量: {len(notes)}", flush=True)
  724. tags = data.get('tags', []) or []
  725. if tags:
  726. preferred = 0
  727. for tag in tags:
  728. if tag.get('id') == 'special.note_time_desc':
  729. preferred = tag.get('notes_count', 0) or tag.get('notesCount', 0) or tag.get('count', 0) or 0
  730. break
  731. if preferred:
  732. total = preferred
  733. else:
  734. total = max([int(t.get('notes_count', 0) or t.get('notesCount', 0) or t.get('count', 0) or 0) for t in tags] + [0])
  735. if not total:
  736. total = int(data.get('total', 0) or data.get('total_count', 0) or data.get('totalCount', 0) or 0)
  737. if not total and isinstance(data.get('page', {}), dict):
  738. total = int(data.get('page', {}).get('total', 0) or data.get('page', {}).get('totalCount', 0) or 0)
  739. next_page = data.get('page', "")
  740. if next_page == page:
  741. next_page = page + 1
  742. works.extend(parse_notes(notes))
  743. if total:
  744. has_more = (page * api_page_size + len(notes)) < total
  745. if has_more and (next_page == -1 or str(next_page) == "-1" or next_page == "" or next_page is None):
  746. next_page = page + 1
  747. else:
  748. if len(notes) == 0:
  749. has_more = False
  750. else:
  751. next_resp = await fetch_notes_page(page + 1)
  752. next_data = (next_resp or {}).get('data', {}) if isinstance(next_resp, dict) else {}
  753. next_notes = next_data.get('notes', []) or []
  754. has_more = len(next_notes) > 0
  755. next_page = next_data.get('page', next_page)
  756. except Exception as e:
  757. import traceback
  758. print(f"[{self.platform_name}] 发生异常: {e}", flush=True)
  759. traceback.print_exc()
  760. return WorksResult(
  761. success=False,
  762. platform=self.platform_name,
  763. error=str(e)
  764. )
  765. finally:
  766. # 确保关闭浏览器
  767. await self.close_browser()
  768. return WorksResult(
  769. success=True,
  770. platform=self.platform_name,
  771. works=works,
  772. total=total or (page * api_page_size + len(works)),
  773. has_more=has_more,
  774. next_page=next_page
  775. )
  776. async def get_all_works(self, cookies: str) -> WorksResult:
  777. """获取小红书全部作品(单次请求内自动翻页抓全量,避免 Node 侧分页不一致)"""
  778. print(f"\n{'='*60}", flush=True)
  779. print(f"[{self.platform_name}] 获取全部作品(auto paging)", flush=True)
  780. print(f"{'='*60}", flush=True)
  781. works: List[WorkItem] = []
  782. total = 0
  783. seen_ids = set()
  784. cursor: object = 0
  785. max_iters = 800
  786. api_page_size = 20
  787. try:
  788. await self.init_browser()
  789. cookie_list = self.parse_cookies(cookies)
  790. print(f"[{self.platform_name}] 解析到 {len(cookie_list)} 个 cookies", flush=True)
  791. await self.set_cookies(cookie_list)
  792. if not self.page:
  793. raise Exception("Page not initialized")
  794. print(f"[{self.platform_name}] 访问笔记管理页面...", flush=True)
  795. try:
  796. await self.page.goto("https://creator.xiaohongshu.com/new/note-manager", wait_until="domcontentloaded", timeout=60000)
  797. print(f"[{self.platform_name}] 页面加载成功", flush=True)
  798. except Exception as nav_error:
  799. print(f"[{self.platform_name}] 导航超时,但继续尝试: {nav_error}", flush=True)
  800. # 即使超时也检查当前页面状态
  801. try:
  802. await asyncio.sleep(2)
  803. current_url = self.page.url
  804. print(f"[{self.platform_name}] 超时后当前页面: {current_url}", flush=True)
  805. except Exception as e:
  806. print(f"[{self.platform_name}] 检查页面状态时出错: {e}", flush=True)
  807. current_url = self.page.url
  808. print(f"[{self.platform_name}] 当前页面: {current_url}", flush=True)
  809. if "login" in current_url:
  810. raise Exception("Cookie 已过期,请重新登录")
  811. # 等待页面完全加载,确保签名函数可用
  812. print(f"[{self.platform_name}] 等待页面完全加载和签名函数初始化...", flush=True)
  813. await asyncio.sleep(3)
  814. # 检查签名函数是否可用
  815. sign_check_attempts = 0
  816. max_sign_check_attempts = 10
  817. while sign_check_attempts < max_sign_check_attempts:
  818. sign_available = await self.page.evaluate("""() => {
  819. return typeof window !== 'undefined' && typeof window._webmsxyw === 'function';
  820. }""")
  821. if sign_available:
  822. print(f"[{self.platform_name}] ✓ 签名函数 _webmsxyw 已可用", flush=True)
  823. break
  824. sign_check_attempts += 1
  825. print(f"[{self.platform_name}] ⏳ 等待签名函数... ({sign_check_attempts}/{max_sign_check_attempts})", flush=True)
  826. await asyncio.sleep(1)
  827. if sign_check_attempts >= max_sign_check_attempts:
  828. print(f"[{self.platform_name}] ⚠️ 警告: 签名函数 _webmsxyw 在 {max_sign_check_attempts} 次检查后仍不可用", flush=True)
  829. print(f"[{self.platform_name}] 继续尝试,但 API 调用可能会失败", flush=True)
  830. async def fetch_notes_page(p):
  831. # 再次检查签名函数(每次调用前都检查)
  832. sign_available = await self.page.evaluate("""() => {
  833. return typeof window !== 'undefined' && typeof window._webmsxyw === 'function';
  834. }""")
  835. if not sign_available:
  836. print(f"[{self.platform_name}] ⚠️ 签名函数 _webmsxyw 不可用,等待...", flush=True)
  837. await asyncio.sleep(2)
  838. return await self.page.evaluate(
  839. """async (pageNum) => {
  840. try {
  841. // 使用正确的 API 端点:/api/galaxy/v2/creator/note/user/posted
  842. const url = `/api/galaxy/v2/creator/note/user/posted?tab=0&page=${pageNum}`;
  843. const headers = {
  844. 'Accept': 'application/json, text/plain, */*',
  845. 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
  846. 'Referer': 'https://creator.xiaohongshu.com/new/note-manager',
  847. 'Sec-Fetch-Dest': 'empty',
  848. 'Sec-Fetch-Mode': 'cors',
  849. 'Sec-Fetch-Site': 'same-origin'
  850. };
  851. // 尝试获取签名
  852. let signResult = { hasSign: false, x_s: '', x_t: '', x_s_common: '', error: '' };
  853. if (typeof window !== 'undefined' && typeof window._webmsxyw === 'function') {
  854. try {
  855. const sign = window._webmsxyw(url, '');
  856. headers['x-s'] = sign['X-s'];
  857. headers['x-t'] = String(sign['X-t']);
  858. // 检查是否有 x-s-common
  859. if (sign['X-s-common']) {
  860. headers['x-s-common'] = sign['X-s-common'];
  861. }
  862. signResult = {
  863. hasSign: true,
  864. x_s: sign['X-s'] ? sign['X-s'].substring(0, 50) + '...' : '',
  865. x_t: String(sign['X-t']),
  866. x_s_common: sign['X-s-common'] ? sign['X-s-common'].substring(0, 50) + '...' : '',
  867. error: ''
  868. };
  869. console.log('签名生成成功:', signResult);
  870. } catch (e) {
  871. signResult.error = e.toString();
  872. console.error('签名生成失败:', e);
  873. }
  874. } else {
  875. signResult.error = '_webmsxyw function not found';
  876. console.error('签名函数不存在');
  877. }
  878. const res = await fetch(url, {
  879. method: 'GET',
  880. credentials: 'include',
  881. headers
  882. });
  883. const responseData = await res.json();
  884. return {
  885. ...responseData,
  886. _debug: {
  887. signResult: signResult,
  888. status: res.status,
  889. statusText: res.statusText
  890. }
  891. };
  892. } catch (e) {
  893. return { success: false, error: e.toString() };
  894. }
  895. }""",
  896. p
  897. )
  898. def parse_notes(notes_list):
  899. parsed = []
  900. for note in notes_list:
  901. note_id = note.get('id', '')
  902. if not note_id:
  903. continue
  904. cover_url = ''
  905. images_list = note.get('images_list', [])
  906. if images_list:
  907. cover_url = images_list[0].get('url', '')
  908. if cover_url.startswith('http://'):
  909. cover_url = cover_url.replace('http://', 'https://')
  910. duration = note.get('video_info', {}).get('duration', 0)
  911. status = 'published'
  912. tab_status = note.get('tab_status', 1)
  913. if tab_status == 0:
  914. status = 'draft'
  915. elif tab_status == 2:
  916. status = 'reviewing'
  917. elif tab_status == 3:
  918. status = 'rejected'
  919. parsed.append(WorkItem(
  920. work_id=note_id,
  921. title=note.get('display_title', '') or '无标题',
  922. cover_url=cover_url,
  923. duration=duration,
  924. status=status,
  925. publish_time=note.get('time', ''),
  926. play_count=note.get('view_count', 0),
  927. like_count=note.get('likes', 0),
  928. comment_count=note.get('comments_count', 0),
  929. share_count=note.get('shared_count', 0),
  930. collect_count=note.get('collected_count', 0),
  931. ))
  932. return parsed
  933. async def collect_by_scrolling() -> WorksResult:
  934. print(f"[{self.platform_name}] 直连接口被拒绝,切换为滚动页面 + 监听 API 响应模式", flush=True)
  935. captured: List[WorkItem] = []
  936. captured_total = 0
  937. captured_seen = set()
  938. lock = asyncio.Lock()
  939. async def handle_response(response):
  940. nonlocal captured_total
  941. url = response.url
  942. if ("creator.xiaohongshu.com" not in url and "edith.xiaohongshu.com" not in url) or "creator/note/user/posted" not in url:
  943. return
  944. try:
  945. json_data = await response.json()
  946. except Exception:
  947. return
  948. if not isinstance(json_data, dict):
  949. return
  950. if not (json_data.get("success") or json_data.get("code") == 0) or not json_data.get("data"):
  951. return
  952. data = json_data.get("data", {}) or {}
  953. notes = data.get("notes", []) or []
  954. tags = data.get("tags", []) or []
  955. declared = 0
  956. if tags:
  957. preferred = 0
  958. for tag in tags:
  959. if tag.get("id") == "special.note_time_desc":
  960. preferred = tag.get("notes_count", 0) or tag.get("notesCount", 0) or tag.get("count", 0) or 0
  961. break
  962. if preferred:
  963. declared = int(preferred)
  964. else:
  965. declared = max([int(t.get("notes_count", 0) or t.get("notesCount", 0) or t.get("count", 0) or 0) for t in tags] + [0])
  966. if not declared:
  967. declared = int(data.get("total", 0) or data.get("total_count", 0) or data.get("totalCount", 0) or 0)
  968. if not declared and isinstance(data.get("page", {}), dict):
  969. declared = int(data.get("page", {}).get("total", 0) or data.get("page", {}).get("totalCount", 0) or 0)
  970. async with lock:
  971. if declared:
  972. captured_total = max(captured_total, declared)
  973. parsed = parse_notes(notes)
  974. new_count = 0
  975. for w in parsed:
  976. if w.work_id and w.work_id not in captured_seen:
  977. captured_seen.add(w.work_id)
  978. captured.append(w)
  979. new_count += 1
  980. if new_count > 0:
  981. print(
  982. f"[{self.platform_name}] 捕获 notes 响应: notes={len(notes)}, new={new_count}, total_now={len(captured)}, declared_total={captured_total}",
  983. flush=True
  984. )
  985. self.page.on("response", handle_response)
  986. try:
  987. try:
  988. # 使用更宽松的等待条件,避免超时
  989. await self.page.goto("https://creator.xiaohongshu.com/new/note-manager", wait_until="domcontentloaded", timeout=90000)
  990. print(f"[{self.platform_name}] 页面加载成功", flush=True)
  991. except Exception as nav_error:
  992. print(f"[{self.platform_name}] 导航异常(继续):{nav_error}", flush=True)
  993. # 即使超时也继续尝试,可能页面已经部分加载
  994. try:
  995. await asyncio.sleep(3)
  996. current_url = self.page.url
  997. print(f"[{self.platform_name}] 超时后当前页面: {current_url}", flush=True)
  998. if "login" in current_url:
  999. raise Exception("Cookie 已过期,请重新登录")
  1000. except Exception as e:
  1001. if "Cookie" in str(e):
  1002. raise
  1003. print(f"[{self.platform_name}] 检查页面状态时出错: {e}", flush=True)
  1004. await asyncio.sleep(2.0)
  1005. idle_rounds = 0
  1006. last_count = 0
  1007. last_height = 0
  1008. for _ in range(1, 400):
  1009. scroll_state = await self.page.evaluate(
  1010. """() => {
  1011. const isScrollable = (el) => {
  1012. if (!el) return false;
  1013. const style = window.getComputedStyle(el);
  1014. const oy = style.overflowY;
  1015. return (oy === 'auto' || oy === 'scroll') && (el.scrollHeight - el.clientHeight > 200);
  1016. };
  1017. const pickBest = () => {
  1018. const nodes = Array.from(document.querySelectorAll('*'));
  1019. let best = document.scrollingElement || document.documentElement || document.body;
  1020. let bestScroll = (best.scrollHeight || 0) - (best.clientHeight || 0);
  1021. for (const el of nodes) {
  1022. if (!isScrollable(el)) continue;
  1023. const diff = el.scrollHeight - el.clientHeight;
  1024. if (diff > bestScroll) {
  1025. best = el;
  1026. bestScroll = diff;
  1027. }
  1028. }
  1029. return best;
  1030. };
  1031. const el = pickBest();
  1032. const beforeTop = el.scrollTop || 0;
  1033. const beforeHeight = el.scrollHeight || 0;
  1034. el.scrollTo(0, beforeHeight);
  1035. return {
  1036. beforeTop,
  1037. afterTop: el.scrollTop || 0,
  1038. height: el.scrollHeight || 0,
  1039. client: el.clientHeight || 0,
  1040. };
  1041. }"""
  1042. )
  1043. await asyncio.sleep(1.2)
  1044. async with lock:
  1045. count_now = len(captured)
  1046. total_now = captured_total
  1047. if total_now and count_now >= total_now:
  1048. break
  1049. height_now = int(scroll_state.get("height", 0) or 0) if isinstance(scroll_state, dict) else 0
  1050. if count_now == last_count and height_now == last_height:
  1051. idle_rounds += 1
  1052. else:
  1053. idle_rounds = 0
  1054. last_count = count_now
  1055. last_height = height_now
  1056. if idle_rounds >= 6:
  1057. break
  1058. async with lock:
  1059. final_works = list(captured)
  1060. final_total = captured_total or len(final_works)
  1061. return WorksResult(
  1062. success=True,
  1063. platform=self.platform_name,
  1064. works=final_works,
  1065. total=final_total,
  1066. has_more=False,
  1067. next_page=-1
  1068. )
  1069. finally:
  1070. try:
  1071. self.page.remove_listener("response", handle_response)
  1072. except Exception:
  1073. pass
  1074. # 添加请求监听,捕获请求头信息
  1075. captured_requests = []
  1076. async def handle_request(request):
  1077. url = request.url
  1078. if ("creator.xiaohongshu.com" in url or "edith.xiaohongshu.com" in url) and "creator/note/user/posted" in url:
  1079. headers = request.headers
  1080. captured_requests.append({
  1081. "url": url,
  1082. "method": request.method,
  1083. "headers": dict(headers),
  1084. "timestamp": asyncio.get_event_loop().time()
  1085. })
  1086. # 打印关键头部信息
  1087. x_s = headers.get('x-s', '')
  1088. x_t = headers.get('x-t', '')
  1089. x_s_common = headers.get('x-s-common', '')
  1090. print(f"[{self.platform_name}] 📡 API 请求: {url}", flush=True)
  1091. print(f"[{self.platform_name}] Method: {request.method}", flush=True)
  1092. print(f"[{self.platform_name}] X-S: {x_s[:50] if x_s else '(none)'}...", flush=True)
  1093. print(f"[{self.platform_name}] X-T: {x_t}", flush=True)
  1094. print(f"[{self.platform_name}] X-S-Common: {x_s_common[:50] if x_s_common else '(none)'}...", flush=True)
  1095. print(f"[{self.platform_name}] Cookie: {headers.get('cookie', '')[:100]}...", flush=True)
  1096. self.page.on("request", handle_request)
  1097. iters = 0
  1098. page_count = 0 # 统计实际获取到的页数
  1099. print(f"[{self.platform_name}] ========== 开始自动分页获取作品 ==========", flush=True)
  1100. print(f"[{self.platform_name}] 最大迭代次数: {max_iters}, 每页大小: {api_page_size}", flush=True)
  1101. while iters < max_iters:
  1102. iters += 1
  1103. print(f"\n[{self.platform_name}] ---------- 第 {iters} 次请求 (cursor={cursor}) ----------", flush=True)
  1104. resp = await fetch_notes_page(cursor)
  1105. # 打印调试信息
  1106. if resp and isinstance(resp, dict) and resp.get('_debug'):
  1107. debug_info = resp.get('_debug', {})
  1108. sign_result = debug_info.get('signResult', {})
  1109. print(f"[{self.platform_name}] 🔍 调试信息:", flush=True)
  1110. print(f"[{self.platform_name}] 签名可用: {sign_result.get('hasSign', False)}", flush=True)
  1111. if sign_result.get('x_s'):
  1112. print(f"[{self.platform_name}] X-S: {sign_result.get('x_s', '')}", flush=True)
  1113. if sign_result.get('x_t'):
  1114. print(f"[{self.platform_name}] X-T: {sign_result.get('x_t', '')}", flush=True)
  1115. if sign_result.get('error'):
  1116. print(f"[{self.platform_name}] 签名错误: {sign_result.get('error', '')}", flush=True)
  1117. print(f"[{self.platform_name}] HTTP 状态: {debug_info.get('status', 'N/A')} {debug_info.get('statusText', '')}", flush=True)
  1118. # 移除调试信息,避免影响后续处理
  1119. resp.pop('_debug', None)
  1120. if not resp or not isinstance(resp, dict):
  1121. print(f"[{self.platform_name}] ❌ 第 {iters} 次拉取无响应,cursor={cursor}", flush=True)
  1122. print(f"[{self.platform_name}] 响应类型: {type(resp)}, 响应内容: {str(resp)[:500]}", flush=True)
  1123. break
  1124. if not (resp.get('success') or resp.get('code') == 0) or not resp.get('data'):
  1125. error_msg = str(resp)[:500]
  1126. print(f"[{self.platform_name}] ❌ 拉取失败 cursor={cursor}", flush=True)
  1127. print(f"[{self.platform_name}] 响应详情: {error_msg}", flush=True)
  1128. print(f"[{self.platform_name}] success={resp.get('success')}, code={resp.get('code')}, has_data={bool(resp.get('data'))}", flush=True)
  1129. # 打印详细的错误信息
  1130. if resp.get('msg'):
  1131. print(f"[{self.platform_name}] 错误消息: {resp.get('msg')}", flush=True)
  1132. if resp.get('message'):
  1133. print(f"[{self.platform_name}] 错误消息: {resp.get('message')}", flush=True)
  1134. if resp.get('error'):
  1135. print(f"[{self.platform_name}] 错误: {resp.get('error')}", flush=True)
  1136. # 打印调试信息
  1137. if resp.get('_debug'):
  1138. debug_info = resp.get('_debug', {})
  1139. print(f"[{self.platform_name}] HTTP 状态: {debug_info.get('status', 'N/A')} {debug_info.get('statusText', '')}", flush=True)
  1140. sign_result = debug_info.get('signResult', {})
  1141. if sign_result.get('error'):
  1142. print(f"[{self.platform_name}] 签名错误: {sign_result.get('error')}", flush=True)
  1143. if iters == 1:
  1144. print(f"[{self.platform_name}] 第一次请求失败,切换到滚动模式", flush=True)
  1145. return await collect_by_scrolling()
  1146. break
  1147. data = resp.get('data', {}) or {}
  1148. notes = data.get('notes', []) or []
  1149. if not notes:
  1150. print(f"[{self.platform_name}] ⚠️ cursor={cursor} 无作品,停止分页", flush=True)
  1151. break
  1152. # 统计页数
  1153. page_count += 1
  1154. print(f"[{self.platform_name}] ✅ 第 {page_count} 页获取成功,本页作品数: {len(notes)}", flush=True)
  1155. tags = data.get('tags', []) or []
  1156. if tags:
  1157. preferred = 0
  1158. for tag in tags:
  1159. if tag.get('id') == 'special.note_time_desc':
  1160. preferred = tag.get('notes_count', 0) or tag.get('notesCount', 0) or tag.get('count', 0) or 0
  1161. break
  1162. if preferred:
  1163. total = max(total, int(preferred))
  1164. print(f"[{self.platform_name}] 📊 从 tags 获取总数: {total} (preferred)", flush=True)
  1165. else:
  1166. tag_total = max([int(t.get('notes_count', 0) or t.get('notesCount', 0) or t.get('count', 0) or 0) for t in tags] + [0])
  1167. total = max(total, tag_total)
  1168. if tag_total > 0:
  1169. print(f"[{self.platform_name}] 📊 从 tags 获取总数: {total}", flush=True)
  1170. if not total:
  1171. t2 = int(data.get('total', 0) or data.get('total_count', 0) or data.get('totalCount', 0) or 0)
  1172. if not t2 and isinstance(data.get('page', {}), dict):
  1173. t2 = int(data.get('page', {}).get('total', 0) or data.get('page', {}).get('totalCount', 0) or 0)
  1174. total = max(total, t2)
  1175. if t2 > 0:
  1176. print(f"[{self.platform_name}] 📊 从 data.total 获取总数: {total}", flush=True)
  1177. parsed = parse_notes(notes)
  1178. new_items = []
  1179. for w in parsed:
  1180. if w.work_id and w.work_id not in seen_ids:
  1181. seen_ids.add(w.work_id)
  1182. new_items.append(w)
  1183. works.extend(new_items)
  1184. print(f"[{self.platform_name}] 📈 累计统计: 本页新作品={len(new_items)}, 累计作品数={len(works)}, 声明总数={total}", flush=True)
  1185. if total and len(works) >= total:
  1186. print(f"[{self.platform_name}] ✅ 已获取全部作品 (累计={len(works)} >= 总数={total}),停止分页", flush=True)
  1187. break
  1188. if len(new_items) == 0:
  1189. print(f"[{self.platform_name}] ⚠️ 本页无新作品,停止分页", flush=True)
  1190. break
  1191. next_page = data.get('page', "")
  1192. old_cursor = cursor
  1193. if next_page == cursor:
  1194. next_page = ""
  1195. if next_page == -1 or str(next_page) == "-1":
  1196. next_page = ""
  1197. if next_page is None or next_page == "":
  1198. if isinstance(cursor, int):
  1199. cursor = cursor + 1
  1200. else:
  1201. cursor = len(works) // api_page_size
  1202. print(f"[{self.platform_name}] 🔄 下一页 cursor: {old_cursor} -> {cursor} (自动递增)", flush=True)
  1203. else:
  1204. cursor = next_page
  1205. print(f"[{self.platform_name}] 🔄 下一页 cursor: {old_cursor} -> {cursor} (API返回)", flush=True)
  1206. await asyncio.sleep(0.5)
  1207. # 移除请求监听器
  1208. try:
  1209. self.page.remove_listener("request", handle_request)
  1210. except Exception:
  1211. pass
  1212. print(f"\n[{self.platform_name}] ========== 分页完成 ==========", flush=True)
  1213. print(f"[{self.platform_name}] 📊 分页统计: 总请求次数={iters}, 成功获取页数={page_count}, 累计作品数={len(works)}, 声明总数={total}", flush=True)
  1214. if captured_requests:
  1215. print(f"[{self.platform_name}] 📡 捕获到 {len(captured_requests)} 个 API 请求", flush=True)
  1216. for i, req in enumerate(captured_requests[:3], 1): # 只显示前3个
  1217. print(f"[{self.platform_name}] 请求 {i}: {req['method']} {req['url']}", flush=True)
  1218. if 'x-s' in req['headers']:
  1219. print(f"[{self.platform_name}] X-S: {req['headers']['x-s'][:50]}...", flush=True)
  1220. if 'x-t' in req['headers']:
  1221. print(f"[{self.platform_name}] X-T: {req['headers']['x-t']}", flush=True)
  1222. print(f"[{self.platform_name}] ========================================\n", flush=True)
  1223. except Exception as e:
  1224. import traceback
  1225. error_trace = traceback.format_exc()
  1226. print(f"[{self.platform_name}] 发生异常: {e}", flush=True)
  1227. traceback.print_exc()
  1228. return WorksResult(
  1229. success=False,
  1230. platform=self.platform_name,
  1231. error=str(e),
  1232. debug_info=f"异常详情: {error_trace[:500]}"
  1233. )
  1234. finally:
  1235. await self.close_browser()
  1236. debug_info = f"总请求次数={iters}, 成功获取页数={page_count}, 累计作品数={len(works)}, 声明总数={total}"
  1237. if len(works) == 0:
  1238. debug_info += " | 警告: 没有获取到任何作品,可能原因: Cookie失效、API调用失败、或账号无作品"
  1239. return WorksResult(
  1240. success=True,
  1241. platform=self.platform_name,
  1242. works=works,
  1243. total=total or len(works),
  1244. has_more=False,
  1245. next_page=-1,
  1246. debug_info=debug_info
  1247. )
  1248. async def get_comments(self, cookies: str, work_id: str, cursor: str = "") -> CommentsResult:
  1249. """获取小红书作品评论 - 通过创作者后台评论管理页面"""
  1250. print(f"\n{'='*60}")
  1251. print(f"[{self.platform_name}] 获取作品评论")
  1252. print(f"[{self.platform_name}] work_id={work_id}, cursor={cursor}")
  1253. print(f"{'='*60}")
  1254. comments: List[CommentItem] = []
  1255. total = 0
  1256. has_more = False
  1257. next_cursor = ""
  1258. captured_data = {}
  1259. try:
  1260. await self.init_browser()
  1261. cookie_list = self.parse_cookies(cookies)
  1262. await self.set_cookies(cookie_list)
  1263. if not self.page:
  1264. raise Exception("Page not initialized")
  1265. # 设置 API 响应监听器
  1266. async def handle_response(response):
  1267. nonlocal captured_data
  1268. url = response.url
  1269. # 监听评论相关 API - 创作者后台和普通页面的 API
  1270. if '/comment/' in url and ('page' in url or 'list' in url):
  1271. try:
  1272. json_data = await response.json()
  1273. print(f"[{self.platform_name}] 捕获到评论 API: {url[:100]}...", flush=True)
  1274. if json_data.get('success') or json_data.get('code') == 0:
  1275. data = json_data.get('data', {})
  1276. comment_list = data.get('comments') or data.get('list') or []
  1277. if comment_list:
  1278. captured_data = json_data
  1279. print(f"[{self.platform_name}] 评论 API 响应成功,comments={len(comment_list)}", flush=True)
  1280. else:
  1281. print(f"[{self.platform_name}] 评论 API 响应成功但无评论", flush=True)
  1282. except Exception as e:
  1283. print(f"[{self.platform_name}] 解析评论响应失败: {e}", flush=True)
  1284. self.page.on('response', handle_response)
  1285. print(f"[{self.platform_name}] 已注册评论 API 响应监听器", flush=True)
  1286. # 访问创作者后台评论管理页面
  1287. comment_url = "https://creator.xiaohongshu.com/creator/comment"
  1288. print(f"[{self.platform_name}] 访问评论管理页面: {comment_url}", flush=True)
  1289. await self.page.goto(comment_url, wait_until="domcontentloaded", timeout=30000)
  1290. await asyncio.sleep(5)
  1291. # 检查是否被重定向到登录页
  1292. current_url = self.page.url
  1293. print(f"[{self.platform_name}] 当前页面 URL: {current_url}", flush=True)
  1294. if "login" in current_url:
  1295. raise Exception("Cookie 已过期,请重新登录")
  1296. # 等待评论加载
  1297. if not captured_data:
  1298. print(f"[{self.platform_name}] 等待评论 API 响应...", flush=True)
  1299. # 尝试滚动页面触发评论加载
  1300. await self.page.evaluate('window.scrollBy(0, 500)')
  1301. await asyncio.sleep(3)
  1302. if not captured_data:
  1303. # 再等待一会,可能评论 API 加载较慢
  1304. print(f"[{self.platform_name}] 继续等待评论加载...", flush=True)
  1305. await asyncio.sleep(5)
  1306. # 移除监听器
  1307. self.page.remove_listener('response', handle_response)
  1308. # 解析评论数据
  1309. if captured_data:
  1310. data = captured_data.get('data', {})
  1311. comment_list = data.get('comments') or data.get('list') or []
  1312. has_more = data.get('has_more', False)
  1313. next_cursor = data.get('cursor', '')
  1314. print(f"[{self.platform_name}] 解析评论: has_more={has_more}, comments={len(comment_list)}", flush=True)
  1315. for comment in comment_list:
  1316. cid = comment.get('id', '')
  1317. if not cid:
  1318. continue
  1319. user_info = comment.get('user_info', {})
  1320. # 解析子评论
  1321. replies = []
  1322. sub_comments = comment.get('sub_comments', []) or []
  1323. for sub in sub_comments:
  1324. sub_user = sub.get('user_info', {})
  1325. replies.append(CommentItem(
  1326. comment_id=sub.get('id', ''),
  1327. work_id=work_id,
  1328. content=sub.get('content', ''),
  1329. author_id=sub_user.get('user_id', ''),
  1330. author_name=sub_user.get('nickname', ''),
  1331. author_avatar=sub_user.get('image', ''),
  1332. like_count=sub.get('like_count', 0),
  1333. create_time=sub.get('create_time', ''),
  1334. ))
  1335. comments.append(CommentItem(
  1336. comment_id=cid,
  1337. work_id=work_id,
  1338. content=comment.get('content', ''),
  1339. author_id=user_info.get('user_id', ''),
  1340. author_name=user_info.get('nickname', ''),
  1341. author_avatar=user_info.get('image', ''),
  1342. like_count=comment.get('like_count', 0),
  1343. reply_count=comment.get('sub_comment_count', 0),
  1344. create_time=comment.get('create_time', ''),
  1345. replies=replies,
  1346. ))
  1347. total = len(comments)
  1348. print(f"[{self.platform_name}] 解析到 {total} 条评论", flush=True)
  1349. else:
  1350. print(f"[{self.platform_name}] 未捕获到评论 API 响应", flush=True)
  1351. except Exception as e:
  1352. import traceback
  1353. traceback.print_exc()
  1354. return CommentsResult(
  1355. success=False,
  1356. platform=self.platform_name,
  1357. work_id=work_id,
  1358. error=str(e)
  1359. )
  1360. finally:
  1361. await self.close_browser()
  1362. result = CommentsResult(
  1363. success=True,
  1364. platform=self.platform_name,
  1365. work_id=work_id,
  1366. comments=comments,
  1367. total=total,
  1368. has_more=has_more
  1369. )
  1370. result.__dict__['cursor'] = next_cursor
  1371. return result
  1372. async def get_all_comments(self, cookies: str) -> dict:
  1373. """获取所有作品的评论 - 通过评论管理页面"""
  1374. print(f"\n{'='*60}")
  1375. print(f"[{self.platform_name}] 获取所有作品评论")
  1376. print(f"{'='*60}")
  1377. all_work_comments = []
  1378. captured_comments = []
  1379. captured_notes = {} # note_id -> note_info
  1380. try:
  1381. await self.init_browser()
  1382. cookie_list = self.parse_cookies(cookies)
  1383. await self.set_cookies(cookie_list)
  1384. if not self.page:
  1385. raise Exception("Page not initialized")
  1386. # 设置 API 响应监听器
  1387. async def handle_response(response):
  1388. nonlocal captured_comments, captured_notes
  1389. url = response.url
  1390. try:
  1391. # 监听评论列表 API - 多种格式
  1392. if '/comment/' in url and ('page' in url or 'list' in url):
  1393. json_data = await response.json()
  1394. print(f"[{self.platform_name}] 捕获到评论 API: {url[:100]}...", flush=True)
  1395. if json_data.get('success') or json_data.get('code') == 0:
  1396. data = json_data.get('data', {})
  1397. comments = data.get('comments', []) or data.get('list', [])
  1398. # 从 URL 中提取 note_id
  1399. import re
  1400. note_id_match = re.search(r'note_id=([^&]+)', url)
  1401. note_id = note_id_match.group(1) if note_id_match else ''
  1402. if comments:
  1403. for comment in comments:
  1404. # 添加 note_id 到评论中
  1405. if note_id and 'note_id' not in comment:
  1406. comment['note_id'] = note_id
  1407. captured_comments.append(comment)
  1408. print(f"[{self.platform_name}] 捕获到 {len(comments)} 条评论 (note_id={note_id}),总计: {len(captured_comments)}", flush=True)
  1409. # 监听笔记列表 API
  1410. if '/note/' in url and ('list' in url or 'posted' in url or 'manager' in url):
  1411. json_data = await response.json()
  1412. if json_data.get('success') or json_data.get('code') == 0:
  1413. data = json_data.get('data', {})
  1414. notes = data.get('notes', []) or data.get('list', [])
  1415. print(f"[{self.platform_name}] 捕获到笔记列表 API: {len(notes)} 个笔记", flush=True)
  1416. for note in notes:
  1417. note_id = note.get('note_id', '') or note.get('id', '')
  1418. if note_id:
  1419. cover_url = ''
  1420. cover = note.get('cover', {})
  1421. if isinstance(cover, dict):
  1422. cover_url = cover.get('url', '') or cover.get('url_default', '')
  1423. elif isinstance(cover, str):
  1424. cover_url = cover
  1425. captured_notes[note_id] = {
  1426. 'title': note.get('title', '') or note.get('display_title', ''),
  1427. 'cover': cover_url,
  1428. }
  1429. except Exception as e:
  1430. print(f"[{self.platform_name}] 解析响应失败: {e}", flush=True)
  1431. self.page.on('response', handle_response)
  1432. print(f"[{self.platform_name}] 已注册 API 响应监听器", flush=True)
  1433. # 访问评论管理页面
  1434. print(f"[{self.platform_name}] 访问评论管理页面...", flush=True)
  1435. await self.page.goto("https://creator.xiaohongshu.com/creator/comment", wait_until="domcontentloaded", timeout=30000)
  1436. await asyncio.sleep(5)
  1437. # 检查登录状态
  1438. current_url = self.page.url
  1439. if "login" in current_url:
  1440. raise Exception("Cookie 已过期,请重新登录")
  1441. print(f"[{self.platform_name}] 页面加载完成,当前捕获: {len(captured_comments)} 条评论, {len(captured_notes)} 个笔记", flush=True)
  1442. # 滚动加载更多评论
  1443. for i in range(5):
  1444. await self.page.evaluate('window.scrollBy(0, 500)')
  1445. await asyncio.sleep(1)
  1446. await asyncio.sleep(3)
  1447. # 移除监听器
  1448. self.page.remove_listener('response', handle_response)
  1449. print(f"[{self.platform_name}] 最终捕获: {len(captured_comments)} 条评论, {len(captured_notes)} 个笔记", flush=True)
  1450. # 按作品分组评论
  1451. work_comments_map = {} # note_id -> work_comments
  1452. for comment in captured_comments:
  1453. # 获取笔记信息
  1454. note_info = comment.get('note_info', {}) or comment.get('note', {})
  1455. note_id = comment.get('note_id', '') or note_info.get('note_id', '') or note_info.get('id', '')
  1456. if not note_id:
  1457. continue
  1458. if note_id not in work_comments_map:
  1459. saved_note = captured_notes.get(note_id, {})
  1460. cover_url = ''
  1461. cover = note_info.get('cover', {})
  1462. if isinstance(cover, dict):
  1463. cover_url = cover.get('url', '') or cover.get('url_default', '')
  1464. elif isinstance(cover, str):
  1465. cover_url = cover
  1466. if not cover_url:
  1467. cover_url = saved_note.get('cover', '')
  1468. work_comments_map[note_id] = {
  1469. 'work_id': note_id,
  1470. 'title': note_info.get('title', '') or note_info.get('display_title', '') or saved_note.get('title', ''),
  1471. 'cover_url': cover_url,
  1472. 'comments': []
  1473. }
  1474. cid = comment.get('id', '') or comment.get('comment_id', '')
  1475. if not cid:
  1476. continue
  1477. user_info = comment.get('user_info', {}) or comment.get('user', {})
  1478. work_comments_map[note_id]['comments'].append({
  1479. 'comment_id': cid,
  1480. 'author_id': user_info.get('user_id', '') or user_info.get('id', ''),
  1481. 'author_name': user_info.get('nickname', '') or user_info.get('name', ''),
  1482. 'author_avatar': user_info.get('image', '') or user_info.get('avatar', ''),
  1483. 'content': comment.get('content', ''),
  1484. 'like_count': comment.get('like_count', 0),
  1485. 'create_time': comment.get('create_time', ''),
  1486. })
  1487. all_work_comments = list(work_comments_map.values())
  1488. total_comments = sum(len(w['comments']) for w in all_work_comments)
  1489. print(f"[{self.platform_name}] 获取到 {len(all_work_comments)} 个作品的 {total_comments} 条评论", flush=True)
  1490. except Exception as e:
  1491. import traceback
  1492. traceback.print_exc()
  1493. return {
  1494. 'success': False,
  1495. 'platform': self.platform_name,
  1496. 'error': str(e),
  1497. 'work_comments': []
  1498. }
  1499. finally:
  1500. await self.close_browser()
  1501. return {
  1502. 'success': True,
  1503. 'platform': self.platform_name,
  1504. 'work_comments': all_work_comments,
  1505. 'total': len(all_work_comments)
  1506. }