xiaohongshu.py 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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. from pathlib import Path
  11. from typing import List
  12. from .base import (
  13. BasePublisher, PublishParams, PublishResult,
  14. WorkItem, WorksResult, CommentItem, CommentsResult
  15. )
  16. # 添加 matrix 项目路径,用于导入签名脚本
  17. MATRIX_PATH = Path(__file__).parent.parent.parent.parent / "matrix"
  18. sys.path.insert(0, str(MATRIX_PATH))
  19. # 尝试导入 xhs SDK
  20. try:
  21. from xhs import XhsClient
  22. XHS_SDK_AVAILABLE = True
  23. except ImportError:
  24. print("[Warning] xhs 库未安装,请运行: pip install xhs")
  25. XhsClient = None
  26. XHS_SDK_AVAILABLE = False
  27. # 签名脚本路径
  28. STEALTH_JS_PATH = MATRIX_PATH / "xhs-api" / "js" / "stealth.min.js"
  29. class XiaohongshuPublisher(BasePublisher):
  30. """
  31. 小红书视频发布器
  32. 优先使用 xhs SDK API 方式发布
  33. """
  34. platform_name = "xiaohongshu"
  35. login_url = "https://creator.xiaohongshu.com/"
  36. publish_url = "https://creator.xiaohongshu.com/publish/publish"
  37. cookie_domain = ".xiaohongshu.com"
  38. async def get_sign(self, uri: str, data=None, a1: str = "", web_session: str = ""):
  39. """获取小红书 API 签名"""
  40. from playwright.async_api import async_playwright
  41. try:
  42. async with async_playwright() as playwright:
  43. browser = await playwright.chromium.launch(headless=True)
  44. browser_context = await browser.new_context()
  45. if STEALTH_JS_PATH.exists():
  46. await browser_context.add_init_script(path=str(STEALTH_JS_PATH))
  47. page = await browser_context.new_page()
  48. await page.goto("https://www.xiaohongshu.com")
  49. await asyncio.sleep(1)
  50. await page.reload()
  51. await asyncio.sleep(1)
  52. if a1:
  53. await browser_context.add_cookies([
  54. {'name': 'a1', 'value': a1, 'domain': ".xiaohongshu.com", 'path': "/"}
  55. ])
  56. await page.reload()
  57. await asyncio.sleep(0.5)
  58. encrypt_params = await page.evaluate(
  59. "([url, data]) => window._webmsxyw(url, data)",
  60. [uri, data]
  61. )
  62. await browser_context.close()
  63. await browser.close()
  64. return {
  65. "x-s": encrypt_params["X-s"],
  66. "x-t": str(encrypt_params["X-t"])
  67. }
  68. except Exception as e:
  69. import traceback
  70. traceback.print_exc()
  71. raise Exception(f"签名失败: {e}")
  72. def sign_sync(self, uri, data=None, a1="", web_session=""):
  73. """同步签名函数,供 XhsClient 使用"""
  74. return asyncio.run(self.get_sign(uri, data, a1, web_session))
  75. async def publish_via_api(self, cookies: str, params: PublishParams) -> PublishResult:
  76. """通过 API 发布视频"""
  77. if not XHS_SDK_AVAILABLE:
  78. raise Exception("xhs SDK 未安装,请运行: pip install xhs")
  79. self.report_progress(10, "正在通过 API 发布...")
  80. print(f"[{self.platform_name}] 使用 XHS SDK API 发布...")
  81. print(f"[{self.platform_name}] 视频路径: {params.video_path}")
  82. print(f"[{self.platform_name}] 标题: {params.title}")
  83. # 转换 cookie 格式
  84. cookie_list = self.parse_cookies(cookies)
  85. cookie_string = self.cookies_to_string(cookie_list) if cookie_list else cookies
  86. print(f"[{self.platform_name}] Cookie 长度: {len(cookie_string)}")
  87. self.report_progress(20, "正在上传视频...")
  88. # 创建客户端
  89. xhs_client = XhsClient(cookie_string, sign=self.sign_sync)
  90. print(f"[{self.platform_name}] 开始调用 create_video_note...")
  91. # 发布视频
  92. try:
  93. result = xhs_client.create_video_note(
  94. title=params.title,
  95. desc=params.description or params.title,
  96. topics=params.tags or [],
  97. post_time=params.publish_date.strftime("%Y-%m-%d %H:%M:%S") if params.publish_date else None,
  98. video_path=params.video_path,
  99. cover_path=params.cover_path if params.cover_path and os.path.exists(params.cover_path) else None
  100. )
  101. print(f"[{self.platform_name}] SDK 返回结果: {result}")
  102. except Exception as e:
  103. import traceback
  104. traceback.print_exc()
  105. print(f"[{self.platform_name}] SDK 调用失败: {e}")
  106. raise Exception(f"XHS SDK 发布失败: {e}")
  107. # 验证返回结果
  108. if not result:
  109. raise Exception("XHS SDK 返回空结果")
  110. # 检查是否有错误
  111. if isinstance(result, dict):
  112. if result.get("code") and result.get("code") != 0:
  113. raise Exception(f"发布失败: {result.get('msg', '未知错误')}")
  114. if result.get("success") == False:
  115. raise Exception(f"发布失败: {result.get('msg', result.get('error', '未知错误'))}")
  116. note_id = result.get("note_id", "") if isinstance(result, dict) else ""
  117. video_url = result.get("url", "") if isinstance(result, dict) else ""
  118. if not note_id:
  119. print(f"[{self.platform_name}] 警告: 未获取到 note_id,返回结果: {result}")
  120. self.report_progress(100, "发布成功")
  121. print(f"[{self.platform_name}] 发布成功! note_id={note_id}, url={video_url}")
  122. return PublishResult(
  123. success=True,
  124. platform=self.platform_name,
  125. video_id=note_id,
  126. video_url=video_url,
  127. message="发布成功"
  128. )
  129. async def publish(self, cookies: str, params: PublishParams) -> PublishResult:
  130. """发布视频到小红书"""
  131. print(f"\n{'='*60}")
  132. print(f"[{self.platform_name}] 开始发布视频")
  133. print(f"[{self.platform_name}] 视频路径: {params.video_path}")
  134. print(f"[{self.platform_name}] 标题: {params.title}")
  135. print(f"[{self.platform_name}] XHS SDK 可用: {XHS_SDK_AVAILABLE}")
  136. print(f"{'='*60}")
  137. # 检查视频文件
  138. if not os.path.exists(params.video_path):
  139. raise Exception(f"视频文件不存在: {params.video_path}")
  140. print(f"[{self.platform_name}] 视频文件存在,大小: {os.path.getsize(params.video_path)} bytes")
  141. self.report_progress(5, "正在准备发布...")
  142. # 临时禁用 API 方式,直接使用 Playwright(更稳定)
  143. # TODO: 后续优化 API 方式的返回值验证
  144. # if XHS_SDK_AVAILABLE:
  145. # try:
  146. # result = await self.publish_via_api(cookies, params)
  147. # print(f"[{self.platform_name}] API 发布完成: {result}")
  148. # return result
  149. # except Exception as e:
  150. # import traceback
  151. # traceback.print_exc()
  152. # print(f"[{self.platform_name}] API 发布失败: {e}")
  153. # print(f"[{self.platform_name}] 尝试使用 Playwright 方式...")
  154. # 使用 Playwright 方式发布(更可靠)
  155. print(f"[{self.platform_name}] 使用 Playwright 方式发布...")
  156. return await self.publish_via_playwright(cookies, params)
  157. async def publish_via_playwright(self, cookies: str, params: PublishParams) -> PublishResult:
  158. """通过 Playwright 发布视频"""
  159. self.report_progress(10, "正在初始化浏览器...")
  160. print(f"[{self.platform_name}] Playwright 方式开始...")
  161. await self.init_browser()
  162. cookie_list = self.parse_cookies(cookies)
  163. print(f"[{self.platform_name}] 设置 {len(cookie_list)} 个 cookies")
  164. await self.set_cookies(cookie_list)
  165. if not self.page:
  166. raise Exception("Page not initialized")
  167. self.report_progress(15, "正在打开发布页面...")
  168. # 直接访问视频发布页面
  169. publish_url = "https://creator.xiaohongshu.com/publish/publish?source=official"
  170. print(f"[{self.platform_name}] 打开页面: {publish_url}")
  171. await self.page.goto(publish_url)
  172. await asyncio.sleep(3)
  173. current_url = self.page.url
  174. print(f"[{self.platform_name}] 当前 URL: {current_url}")
  175. # 检查登录状态
  176. if "login" in current_url or "passport" in current_url:
  177. screenshot_base64 = await self.capture_screenshot()
  178. return PublishResult(
  179. success=False,
  180. platform=self.platform_name,
  181. error="登录已过期,请重新登录",
  182. screenshot_base64=screenshot_base64,
  183. page_url=current_url,
  184. status='need_captcha',
  185. need_captcha=True,
  186. captcha_type='login'
  187. )
  188. self.report_progress(20, "正在上传视频...")
  189. # 等待页面加载
  190. await asyncio.sleep(2)
  191. # 上传视频
  192. upload_triggered = False
  193. # 方法1: 直接设置隐藏的 file input
  194. print(f"[{self.platform_name}] 尝试方法1: 设置 file input")
  195. file_inputs = self.page.locator('input[type="file"]')
  196. input_count = await file_inputs.count()
  197. print(f"[{self.platform_name}] 找到 {input_count} 个 file input")
  198. if input_count > 0:
  199. # 找到接受视频的 input
  200. for i in range(input_count):
  201. input_el = file_inputs.nth(i)
  202. accept = await input_el.get_attribute('accept') or ''
  203. print(f"[{self.platform_name}] Input {i} accept: {accept}")
  204. if 'video' in accept or '*' in accept or not accept:
  205. await input_el.set_input_files(params.video_path)
  206. upload_triggered = True
  207. print(f"[{self.platform_name}] 视频文件已设置到 input {i}")
  208. break
  209. # 方法2: 点击上传区域触发文件选择器
  210. if not upload_triggered:
  211. print(f"[{self.platform_name}] 尝试方法2: 点击上传区域")
  212. try:
  213. upload_area = self.page.locator('[class*="upload-wrapper"], [class*="upload-area"], .upload-input').first
  214. if await upload_area.count() > 0:
  215. async with self.page.expect_file_chooser(timeout=5000) as fc_info:
  216. await upload_area.click()
  217. file_chooser = await fc_info.value
  218. await file_chooser.set_files(params.video_path)
  219. upload_triggered = True
  220. print(f"[{self.platform_name}] 通过点击上传区域上传成功")
  221. except Exception as e:
  222. print(f"[{self.platform_name}] 方法2失败: {e}")
  223. if not upload_triggered:
  224. screenshot_base64 = await self.capture_screenshot()
  225. page_url = await self.get_page_url()
  226. return PublishResult(
  227. success=False,
  228. platform=self.platform_name,
  229. error="无法上传视频文件",
  230. screenshot_base64=screenshot_base64,
  231. page_url=page_url,
  232. status='need_action'
  233. )
  234. self.report_progress(40, "等待视频上传完成...")
  235. print(f"[{self.platform_name}] 等待视频上传和处理...")
  236. # 等待上传完成(检测页面变化)
  237. upload_complete = False
  238. for i in range(60): # 最多等待3分钟
  239. await asyncio.sleep(3)
  240. # 检查是否有标题输入框(上传完成后出现)
  241. title_input_count = await self.page.locator('input[placeholder*="标题"], input[placeholder*="填写标题"]').count()
  242. # 或者检查编辑器区域
  243. editor_count = await self.page.locator('[class*="ql-editor"], [contenteditable="true"]').count()
  244. # 检查发布按钮是否可见
  245. publish_btn_count = await self.page.locator('.publishBtn, button:has-text("发布")').count()
  246. print(f"[{self.platform_name}] 检测 {i+1}: 标题框={title_input_count}, 编辑器={editor_count}, 发布按钮={publish_btn_count}")
  247. if title_input_count > 0 or (editor_count > 0 and publish_btn_count > 0):
  248. upload_complete = True
  249. print(f"[{self.platform_name}] 视频上传完成!")
  250. break
  251. if not upload_complete:
  252. screenshot_base64 = await self.capture_screenshot()
  253. page_url = await self.get_page_url()
  254. return PublishResult(
  255. success=False,
  256. platform=self.platform_name,
  257. error="视频上传超时",
  258. screenshot_base64=screenshot_base64,
  259. page_url=page_url,
  260. status='need_action'
  261. )
  262. await asyncio.sleep(2)
  263. self.report_progress(60, "正在填写笔记信息...")
  264. print(f"[{self.platform_name}] 填写标题: {params.title[:20]}")
  265. # 填写标题
  266. title_filled = False
  267. title_selectors = [
  268. 'input[placeholder*="标题"]',
  269. 'input[placeholder*="填写标题"]',
  270. '[class*="title"] input',
  271. '.c-input_inner',
  272. ]
  273. for selector in title_selectors:
  274. title_input = self.page.locator(selector).first
  275. if await title_input.count() > 0:
  276. await title_input.click()
  277. await title_input.fill('') # 先清空
  278. await title_input.fill(params.title[:20])
  279. title_filled = True
  280. print(f"[{self.platform_name}] 标题已填写,使用选择器: {selector}")
  281. break
  282. if not title_filled:
  283. print(f"[{self.platform_name}] 警告: 未找到标题输入框")
  284. # 填写描述和标签
  285. if params.description or params.tags:
  286. desc_filled = False
  287. desc_selectors = [
  288. '[class*="ql-editor"]',
  289. '[class*="content-input"] [contenteditable="true"]',
  290. '[class*="editor"] [contenteditable="true"]',
  291. '.ql-editor',
  292. ]
  293. for selector in desc_selectors:
  294. desc_input = self.page.locator(selector).first
  295. if await desc_input.count() > 0:
  296. await desc_input.click()
  297. await asyncio.sleep(0.5)
  298. if params.description:
  299. await self.page.keyboard.type(params.description, delay=20)
  300. print(f"[{self.platform_name}] 描述已填写")
  301. if params.tags:
  302. # 添加标签
  303. await self.page.keyboard.press("Enter")
  304. for tag in params.tags[:5]: # 最多5个标签
  305. await self.page.keyboard.type(f"#{tag}", delay=20)
  306. await asyncio.sleep(0.3)
  307. await self.page.keyboard.press("Space")
  308. print(f"[{self.platform_name}] 标签已填写: {params.tags[:5]}")
  309. desc_filled = True
  310. break
  311. if not desc_filled:
  312. print(f"[{self.platform_name}] 警告: 未找到描述输入框")
  313. await asyncio.sleep(2)
  314. self.report_progress(80, "正在发布...")
  315. await asyncio.sleep(2)
  316. # 滚动到页面底部确保发布按钮可见
  317. await self.page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
  318. await asyncio.sleep(1)
  319. print(f"[{self.platform_name}] 查找发布按钮...")
  320. # 点击发布
  321. publish_selectors = [
  322. 'button.publishBtn',
  323. '.publishBtn',
  324. 'button.d-button.red',
  325. 'button:has-text("发布"):not(:has-text("定时发布"))',
  326. '[class*="publish"][class*="btn"]',
  327. ]
  328. publish_clicked = False
  329. for selector in publish_selectors:
  330. try:
  331. btn = self.page.locator(selector).first
  332. if await btn.count() > 0:
  333. is_visible = await btn.is_visible()
  334. is_enabled = await btn.is_enabled()
  335. print(f"[{self.platform_name}] 按钮 {selector}: visible={is_visible}, enabled={is_enabled}")
  336. if is_visible and is_enabled:
  337. box = await btn.bounding_box()
  338. if box:
  339. print(f"[{self.platform_name}] 点击发布按钮: {selector}, 位置: ({box['x']}, {box['y']})")
  340. # 使用真实鼠标点击
  341. await self.page.mouse.click(box['x'] + box['width']/2, box['y'] + box['height']/2)
  342. publish_clicked = True
  343. break
  344. except Exception as e:
  345. print(f"[{self.platform_name}] 选择器 {selector} 错误: {e}")
  346. if not publish_clicked:
  347. # 保存截图用于调试
  348. screenshot_path = f"debug_publish_failed_{self.platform_name}.png"
  349. await self.page.screenshot(path=screenshot_path, full_page=True)
  350. print(f"[{self.platform_name}] 未找到发布按钮,截图保存到: {screenshot_path}")
  351. # 打印页面 HTML 结构用于调试
  352. buttons = await self.page.query_selector_all('button')
  353. print(f"[{self.platform_name}] 页面上共有 {len(buttons)} 个按钮")
  354. for i, btn in enumerate(buttons[:10]):
  355. text = await btn.text_content() or ''
  356. cls = await btn.get_attribute('class') or ''
  357. print(f" 按钮 {i}: text='{text.strip()[:30]}', class='{cls[:50]}'")
  358. raise Exception("未找到发布按钮")
  359. print(f"[{self.platform_name}] 已点击发布按钮,等待发布完成...")
  360. self.report_progress(90, "等待发布结果...")
  361. # 等待发布完成(检测 URL 变化或成功提示)
  362. publish_success = False
  363. for i in range(20): # 最多等待 20 秒
  364. await asyncio.sleep(1)
  365. current_url = self.page.url
  366. # 检查是否跳转到发布成功页面或内容管理页面
  367. if "published=true" in current_url or "success" in current_url or "content" in current_url:
  368. publish_success = True
  369. print(f"[{self.platform_name}] 发布成功! 跳转到: {current_url}")
  370. break
  371. # 检查是否有成功提示
  372. try:
  373. success_msg = await self.page.locator('[class*="success"], .toast-success, [class*="Toast"]').first.is_visible()
  374. if success_msg:
  375. publish_success = True
  376. print(f"[{self.platform_name}] 检测到成功提示!")
  377. break
  378. except:
  379. pass
  380. # 检查是否有错误提示
  381. try:
  382. error_elements = self.page.locator('[class*="error"], .toast-error, [class*="fail"]')
  383. if await error_elements.count() > 0:
  384. error_text = await error_elements.first.text_content()
  385. if error_text and len(error_text.strip()) > 0:
  386. raise Exception(f"发布失败: {error_text.strip()}")
  387. except Exception as e:
  388. if "发布失败" in str(e):
  389. raise
  390. # 如果没有明确的成功标志,返回截图供 AI 分析
  391. if not publish_success:
  392. final_url = self.page.url
  393. print(f"[{self.platform_name}] 发布结果不确定,当前 URL: {final_url}")
  394. screenshot_base64 = await self.capture_screenshot()
  395. print(f"[{self.platform_name}] 已获取截图供 AI 分析")
  396. # 如果 URL 还是发布页面,可能需要继续操作
  397. if "publish/publish" in final_url:
  398. return PublishResult(
  399. success=False,
  400. platform=self.platform_name,
  401. error="发布结果待确认,请查看截图",
  402. screenshot_base64=screenshot_base64,
  403. page_url=final_url,
  404. status='need_action'
  405. )
  406. self.report_progress(100, "发布完成")
  407. print(f"[{self.platform_name}] Playwright 方式发布完成!")
  408. screenshot_base64 = await self.capture_screenshot()
  409. page_url = await self.get_page_url()
  410. return PublishResult(
  411. success=True,
  412. platform=self.platform_name,
  413. message="发布完成",
  414. screenshot_base64=screenshot_base64,
  415. page_url=page_url,
  416. status='success'
  417. )
  418. async def get_works(self, cookies: str, page: int = 0, page_size: int = 20) -> WorksResult:
  419. """获取小红书作品列表 - 通过监听页面网络响应获取数据"""
  420. print(f"\n{'='*60}", flush=True)
  421. print(f"[{self.platform_name}] 获取作品列表", flush=True)
  422. print(f"[{self.platform_name}] page={page}, page_size={page_size}", flush=True)
  423. print(f"{'='*60}", flush=True)
  424. works: List[WorkItem] = []
  425. total = 0
  426. has_more = False
  427. captured_data = {}
  428. try:
  429. await self.init_browser()
  430. cookie_list = self.parse_cookies(cookies)
  431. # 打印 cookies 信息用于调试
  432. print(f"[{self.platform_name}] 解析到 {len(cookie_list)} 个 cookies", flush=True)
  433. await self.set_cookies(cookie_list)
  434. if not self.page:
  435. raise Exception("Page not initialized")
  436. # 定义响应监听器 - 捕获页面自动发起的 API 请求
  437. async def handle_response(response):
  438. nonlocal captured_data
  439. url = response.url
  440. # 监听作品列表 API
  441. if 'creator/note/user/posted' in url or 'creator/note_list' in url:
  442. try:
  443. json_data = await response.json()
  444. print(f"[{self.platform_name}] 捕获到 API 响应: {url[:80]}...", flush=True)
  445. if json_data.get('success') or json_data.get('code') == 0:
  446. captured_data = json_data
  447. print(f"[{self.platform_name}] API 响应成功,data keys: {list(json_data.get('data', {}).keys())}", flush=True)
  448. except Exception as e:
  449. print(f"[{self.platform_name}] 解析响应失败: {e}", flush=True)
  450. # 注册响应监听器
  451. self.page.on('response', handle_response)
  452. print(f"[{self.platform_name}] 已注册 API 响应监听器", flush=True)
  453. # 访问笔记管理页面 - 页面会自动发起 API 请求
  454. print(f"[{self.platform_name}] 访问笔记管理页面...", flush=True)
  455. try:
  456. await self.page.goto("https://creator.xiaohongshu.com/new/note-manager", wait_until="domcontentloaded", timeout=30000)
  457. except Exception as nav_error:
  458. print(f"[{self.platform_name}] 导航超时,但继续尝试: {nav_error}", flush=True)
  459. # 等待 API 响应被捕获
  460. await asyncio.sleep(5)
  461. # 检查登录状态
  462. current_url = self.page.url
  463. print(f"[{self.platform_name}] 当前页面: {current_url}", flush=True)
  464. if "login" in current_url:
  465. raise Exception("Cookie 已过期,请重新登录")
  466. # 如果还没有捕获到数据,等待更长时间
  467. if not captured_data:
  468. print(f"[{self.platform_name}] 等待 API 响应...", flush=True)
  469. await asyncio.sleep(5)
  470. # 移除监听器
  471. self.page.remove_listener('response', handle_response)
  472. # 处理捕获到的数据
  473. import json
  474. if captured_data:
  475. print(f"[{self.platform_name}] 成功捕获到 API 数据", flush=True)
  476. data = captured_data.get('data', {})
  477. notes = data.get('notes', [])
  478. print(f"[{self.platform_name}] notes 数量: {len(notes)}", flush=True)
  479. # 从 tags 获取总数
  480. tags = data.get('tags', [])
  481. for tag in tags:
  482. if tag.get('id') == 'special.note_time_desc':
  483. total = tag.get('notes_count', 0)
  484. break
  485. has_more = data.get('page', -1) != -1
  486. for note in notes:
  487. note_id = note.get('id', '')
  488. if not note_id:
  489. continue
  490. # 获取封面
  491. cover_url = ''
  492. images_list = note.get('images_list', [])
  493. if images_list:
  494. cover_url = images_list[0].get('url', '')
  495. if cover_url.startswith('http://'):
  496. cover_url = cover_url.replace('http://', 'https://')
  497. # 获取时长
  498. duration = note.get('video_info', {}).get('duration', 0)
  499. # 解析状态
  500. status = 'published'
  501. tab_status = note.get('tab_status', 1)
  502. if tab_status == 0:
  503. status = 'draft'
  504. elif tab_status == 2:
  505. status = 'reviewing'
  506. elif tab_status == 3:
  507. status = 'rejected'
  508. works.append(WorkItem(
  509. work_id=note_id,
  510. title=note.get('display_title', '') or '无标题',
  511. cover_url=cover_url,
  512. duration=duration,
  513. status=status,
  514. publish_time=note.get('time', ''),
  515. play_count=note.get('view_count', 0),
  516. like_count=note.get('likes', 0),
  517. comment_count=note.get('comments_count', 0),
  518. share_count=note.get('shared_count', 0),
  519. collect_count=note.get('collected_count', 0),
  520. ))
  521. print(f"[{self.platform_name}] 解析到 {len(works)} 个作品,总计: {total}", flush=True)
  522. else:
  523. print(f"[{self.platform_name}] 未能捕获到 API 数据", flush=True)
  524. except Exception as e:
  525. import traceback
  526. print(f"[{self.platform_name}] 发生异常: {e}", flush=True)
  527. traceback.print_exc()
  528. return WorksResult(
  529. success=False,
  530. platform=self.platform_name,
  531. error=str(e)
  532. )
  533. finally:
  534. # 确保关闭浏览器
  535. await self.close_browser()
  536. return WorksResult(
  537. success=True,
  538. platform=self.platform_name,
  539. works=works,
  540. total=total or len(works),
  541. has_more=has_more
  542. )
  543. async def get_comments(self, cookies: str, work_id: str, cursor: str = "") -> CommentsResult:
  544. """获取小红书作品评论 - 通过创作者后台评论管理页面"""
  545. print(f"\n{'='*60}")
  546. print(f"[{self.platform_name}] 获取作品评论")
  547. print(f"[{self.platform_name}] work_id={work_id}, cursor={cursor}")
  548. print(f"{'='*60}")
  549. comments: List[CommentItem] = []
  550. total = 0
  551. has_more = False
  552. next_cursor = ""
  553. captured_data = {}
  554. try:
  555. await self.init_browser()
  556. cookie_list = self.parse_cookies(cookies)
  557. await self.set_cookies(cookie_list)
  558. if not self.page:
  559. raise Exception("Page not initialized")
  560. # 设置 API 响应监听器
  561. async def handle_response(response):
  562. nonlocal captured_data
  563. url = response.url
  564. # 监听评论相关 API - 创作者后台和普通页面的 API
  565. if '/comment/' in url and ('page' in url or 'list' in url):
  566. try:
  567. json_data = await response.json()
  568. print(f"[{self.platform_name}] 捕获到评论 API: {url[:100]}...", flush=True)
  569. if json_data.get('success') or json_data.get('code') == 0:
  570. data = json_data.get('data', {})
  571. comment_list = data.get('comments') or data.get('list') or []
  572. if comment_list:
  573. captured_data = json_data
  574. print(f"[{self.platform_name}] 评论 API 响应成功,comments={len(comment_list)}", flush=True)
  575. else:
  576. print(f"[{self.platform_name}] 评论 API 响应成功但无评论", flush=True)
  577. except Exception as e:
  578. print(f"[{self.platform_name}] 解析评论响应失败: {e}", flush=True)
  579. self.page.on('response', handle_response)
  580. print(f"[{self.platform_name}] 已注册评论 API 响应监听器", flush=True)
  581. # 访问创作者后台评论管理页面
  582. comment_url = "https://creator.xiaohongshu.com/creator/comment"
  583. print(f"[{self.platform_name}] 访问评论管理页面: {comment_url}", flush=True)
  584. await self.page.goto(comment_url, wait_until="domcontentloaded", timeout=30000)
  585. await asyncio.sleep(5)
  586. # 检查是否被重定向到登录页
  587. current_url = self.page.url
  588. print(f"[{self.platform_name}] 当前页面 URL: {current_url}", flush=True)
  589. if "login" in current_url:
  590. raise Exception("Cookie 已过期,请重新登录")
  591. # 等待评论加载
  592. if not captured_data:
  593. print(f"[{self.platform_name}] 等待评论 API 响应...", flush=True)
  594. # 尝试滚动页面触发评论加载
  595. await self.page.evaluate('window.scrollBy(0, 500)')
  596. await asyncio.sleep(3)
  597. if not captured_data:
  598. # 再等待一会,可能评论 API 加载较慢
  599. print(f"[{self.platform_name}] 继续等待评论加载...", flush=True)
  600. await asyncio.sleep(5)
  601. # 移除监听器
  602. self.page.remove_listener('response', handle_response)
  603. # 解析评论数据
  604. if captured_data:
  605. data = captured_data.get('data', {})
  606. comment_list = data.get('comments') or data.get('list') or []
  607. has_more = data.get('has_more', False)
  608. next_cursor = data.get('cursor', '')
  609. print(f"[{self.platform_name}] 解析评论: has_more={has_more}, comments={len(comment_list)}", flush=True)
  610. for comment in comment_list:
  611. cid = comment.get('id', '')
  612. if not cid:
  613. continue
  614. user_info = comment.get('user_info', {})
  615. # 解析子评论
  616. replies = []
  617. sub_comments = comment.get('sub_comments', []) or []
  618. for sub in sub_comments:
  619. sub_user = sub.get('user_info', {})
  620. replies.append(CommentItem(
  621. comment_id=sub.get('id', ''),
  622. work_id=work_id,
  623. content=sub.get('content', ''),
  624. author_id=sub_user.get('user_id', ''),
  625. author_name=sub_user.get('nickname', ''),
  626. author_avatar=sub_user.get('image', ''),
  627. like_count=sub.get('like_count', 0),
  628. create_time=sub.get('create_time', ''),
  629. ))
  630. comments.append(CommentItem(
  631. comment_id=cid,
  632. work_id=work_id,
  633. content=comment.get('content', ''),
  634. author_id=user_info.get('user_id', ''),
  635. author_name=user_info.get('nickname', ''),
  636. author_avatar=user_info.get('image', ''),
  637. like_count=comment.get('like_count', 0),
  638. reply_count=comment.get('sub_comment_count', 0),
  639. create_time=comment.get('create_time', ''),
  640. replies=replies,
  641. ))
  642. total = len(comments)
  643. print(f"[{self.platform_name}] 解析到 {total} 条评论", flush=True)
  644. else:
  645. print(f"[{self.platform_name}] 未捕获到评论 API 响应", flush=True)
  646. except Exception as e:
  647. import traceback
  648. traceback.print_exc()
  649. return CommentsResult(
  650. success=False,
  651. platform=self.platform_name,
  652. work_id=work_id,
  653. error=str(e)
  654. )
  655. finally:
  656. await self.close_browser()
  657. result = CommentsResult(
  658. success=True,
  659. platform=self.platform_name,
  660. work_id=work_id,
  661. comments=comments,
  662. total=total,
  663. has_more=has_more
  664. )
  665. result.__dict__['cursor'] = next_cursor
  666. return result
  667. async def get_all_comments(self, cookies: str) -> dict:
  668. """获取所有作品的评论 - 通过评论管理页面"""
  669. print(f"\n{'='*60}")
  670. print(f"[{self.platform_name}] 获取所有作品评论")
  671. print(f"{'='*60}")
  672. all_work_comments = []
  673. captured_comments = []
  674. captured_notes = {} # note_id -> note_info
  675. try:
  676. await self.init_browser()
  677. cookie_list = self.parse_cookies(cookies)
  678. await self.set_cookies(cookie_list)
  679. if not self.page:
  680. raise Exception("Page not initialized")
  681. # 设置 API 响应监听器
  682. async def handle_response(response):
  683. nonlocal captured_comments, captured_notes
  684. url = response.url
  685. try:
  686. # 监听评论列表 API - 多种格式
  687. if '/comment/' in url and ('page' in url or 'list' in url):
  688. json_data = await response.json()
  689. print(f"[{self.platform_name}] 捕获到评论 API: {url[:100]}...", flush=True)
  690. if json_data.get('success') or json_data.get('code') == 0:
  691. data = json_data.get('data', {})
  692. comments = data.get('comments', []) or data.get('list', [])
  693. # 从 URL 中提取 note_id
  694. import re
  695. note_id_match = re.search(r'note_id=([^&]+)', url)
  696. note_id = note_id_match.group(1) if note_id_match else ''
  697. if comments:
  698. for comment in comments:
  699. # 添加 note_id 到评论中
  700. if note_id and 'note_id' not in comment:
  701. comment['note_id'] = note_id
  702. captured_comments.append(comment)
  703. print(f"[{self.platform_name}] 捕获到 {len(comments)} 条评论 (note_id={note_id}),总计: {len(captured_comments)}", flush=True)
  704. # 监听笔记列表 API
  705. if '/note/' in url and ('list' in url or 'posted' in url or 'manager' in url):
  706. json_data = await response.json()
  707. if json_data.get('success') or json_data.get('code') == 0:
  708. data = json_data.get('data', {})
  709. notes = data.get('notes', []) or data.get('list', [])
  710. print(f"[{self.platform_name}] 捕获到笔记列表 API: {len(notes)} 个笔记", flush=True)
  711. for note in notes:
  712. note_id = note.get('note_id', '') or note.get('id', '')
  713. if note_id:
  714. cover_url = ''
  715. cover = note.get('cover', {})
  716. if isinstance(cover, dict):
  717. cover_url = cover.get('url', '') or cover.get('url_default', '')
  718. elif isinstance(cover, str):
  719. cover_url = cover
  720. captured_notes[note_id] = {
  721. 'title': note.get('title', '') or note.get('display_title', ''),
  722. 'cover': cover_url,
  723. }
  724. except Exception as e:
  725. print(f"[{self.platform_name}] 解析响应失败: {e}", flush=True)
  726. self.page.on('response', handle_response)
  727. print(f"[{self.platform_name}] 已注册 API 响应监听器", flush=True)
  728. # 访问评论管理页面
  729. print(f"[{self.platform_name}] 访问评论管理页面...", flush=True)
  730. await self.page.goto("https://creator.xiaohongshu.com/creator/comment", wait_until="domcontentloaded", timeout=30000)
  731. await asyncio.sleep(5)
  732. # 检查登录状态
  733. current_url = self.page.url
  734. if "login" in current_url:
  735. raise Exception("Cookie 已过期,请重新登录")
  736. print(f"[{self.platform_name}] 页面加载完成,当前捕获: {len(captured_comments)} 条评论, {len(captured_notes)} 个笔记", flush=True)
  737. # 滚动加载更多评论
  738. for i in range(5):
  739. await self.page.evaluate('window.scrollBy(0, 500)')
  740. await asyncio.sleep(1)
  741. await asyncio.sleep(3)
  742. # 移除监听器
  743. self.page.remove_listener('response', handle_response)
  744. print(f"[{self.platform_name}] 最终捕获: {len(captured_comments)} 条评论, {len(captured_notes)} 个笔记", flush=True)
  745. # 按作品分组评论
  746. work_comments_map = {} # note_id -> work_comments
  747. for comment in captured_comments:
  748. # 获取笔记信息
  749. note_info = comment.get('note_info', {}) or comment.get('note', {})
  750. note_id = comment.get('note_id', '') or note_info.get('note_id', '') or note_info.get('id', '')
  751. if not note_id:
  752. continue
  753. if note_id not in work_comments_map:
  754. saved_note = captured_notes.get(note_id, {})
  755. cover_url = ''
  756. cover = note_info.get('cover', {})
  757. if isinstance(cover, dict):
  758. cover_url = cover.get('url', '') or cover.get('url_default', '')
  759. elif isinstance(cover, str):
  760. cover_url = cover
  761. if not cover_url:
  762. cover_url = saved_note.get('cover', '')
  763. work_comments_map[note_id] = {
  764. 'work_id': note_id,
  765. 'title': note_info.get('title', '') or note_info.get('display_title', '') or saved_note.get('title', ''),
  766. 'cover_url': cover_url,
  767. 'comments': []
  768. }
  769. cid = comment.get('id', '') or comment.get('comment_id', '')
  770. if not cid:
  771. continue
  772. user_info = comment.get('user_info', {}) or comment.get('user', {})
  773. work_comments_map[note_id]['comments'].append({
  774. 'comment_id': cid,
  775. 'author_id': user_info.get('user_id', '') or user_info.get('id', ''),
  776. 'author_name': user_info.get('nickname', '') or user_info.get('name', ''),
  777. 'author_avatar': user_info.get('image', '') or user_info.get('avatar', ''),
  778. 'content': comment.get('content', ''),
  779. 'like_count': comment.get('like_count', 0),
  780. 'create_time': comment.get('create_time', ''),
  781. })
  782. all_work_comments = list(work_comments_map.values())
  783. total_comments = sum(len(w['comments']) for w in all_work_comments)
  784. print(f"[{self.platform_name}] 获取到 {len(all_work_comments)} 个作品的 {total_comments} 条评论", flush=True)
  785. except Exception as e:
  786. import traceback
  787. traceback.print_exc()
  788. return {
  789. 'success': False,
  790. 'platform': self.platform_name,
  791. 'error': str(e),
  792. 'work_comments': []
  793. }
  794. finally:
  795. await self.close_browser()
  796. return {
  797. 'success': True,
  798. 'platform': self.platform_name,
  799. 'work_comments': all_work_comments,
  800. 'total': len(all_work_comments)
  801. }