xiaohongshu.py 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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_path = f"debug_login_required_{self.platform_name}.png"
  178. await self.page.screenshot(path=screenshot_path)
  179. raise Exception(f"登录已过期,请重新登录(截图: {screenshot_path})")
  180. self.report_progress(20, "正在上传视频...")
  181. # 等待页面加载
  182. await asyncio.sleep(2)
  183. # 上传视频
  184. upload_triggered = False
  185. # 方法1: 直接设置隐藏的 file input
  186. print(f"[{self.platform_name}] 尝试方法1: 设置 file input")
  187. file_inputs = self.page.locator('input[type="file"]')
  188. input_count = await file_inputs.count()
  189. print(f"[{self.platform_name}] 找到 {input_count} 个 file input")
  190. if input_count > 0:
  191. # 找到接受视频的 input
  192. for i in range(input_count):
  193. input_el = file_inputs.nth(i)
  194. accept = await input_el.get_attribute('accept') or ''
  195. print(f"[{self.platform_name}] Input {i} accept: {accept}")
  196. if 'video' in accept or '*' in accept or not accept:
  197. await input_el.set_input_files(params.video_path)
  198. upload_triggered = True
  199. print(f"[{self.platform_name}] 视频文件已设置到 input {i}")
  200. break
  201. # 方法2: 点击上传区域触发文件选择器
  202. if not upload_triggered:
  203. print(f"[{self.platform_name}] 尝试方法2: 点击上传区域")
  204. try:
  205. upload_area = self.page.locator('[class*="upload-wrapper"], [class*="upload-area"], .upload-input').first
  206. if await upload_area.count() > 0:
  207. async with self.page.expect_file_chooser(timeout=5000) as fc_info:
  208. await upload_area.click()
  209. file_chooser = await fc_info.value
  210. await file_chooser.set_files(params.video_path)
  211. upload_triggered = True
  212. print(f"[{self.platform_name}] 通过点击上传区域上传成功")
  213. except Exception as e:
  214. print(f"[{self.platform_name}] 方法2失败: {e}")
  215. if not upload_triggered:
  216. screenshot_path = f"debug_upload_failed_{self.platform_name}.png"
  217. await self.page.screenshot(path=screenshot_path)
  218. raise Exception(f"无法上传视频文件(截图: {screenshot_path})")
  219. self.report_progress(40, "等待视频上传完成...")
  220. print(f"[{self.platform_name}] 等待视频上传和处理...")
  221. # 等待上传完成(检测页面变化)
  222. upload_complete = False
  223. for i in range(60): # 最多等待3分钟
  224. await asyncio.sleep(3)
  225. # 检查是否有标题输入框(上传完成后出现)
  226. title_input_count = await self.page.locator('input[placeholder*="标题"], input[placeholder*="填写标题"]').count()
  227. # 或者检查编辑器区域
  228. editor_count = await self.page.locator('[class*="ql-editor"], [contenteditable="true"]').count()
  229. # 检查发布按钮是否可见
  230. publish_btn_count = await self.page.locator('.publishBtn, button:has-text("发布")').count()
  231. print(f"[{self.platform_name}] 检测 {i+1}: 标题框={title_input_count}, 编辑器={editor_count}, 发布按钮={publish_btn_count}")
  232. if title_input_count > 0 or (editor_count > 0 and publish_btn_count > 0):
  233. upload_complete = True
  234. print(f"[{self.platform_name}] 视频上传完成!")
  235. break
  236. if not upload_complete:
  237. screenshot_path = f"debug_upload_timeout_{self.platform_name}.png"
  238. await self.page.screenshot(path=screenshot_path)
  239. raise Exception(f"视频上传超时(截图: {screenshot_path})")
  240. await asyncio.sleep(2)
  241. self.report_progress(60, "正在填写笔记信息...")
  242. print(f"[{self.platform_name}] 填写标题: {params.title[:20]}")
  243. # 填写标题
  244. title_filled = False
  245. title_selectors = [
  246. 'input[placeholder*="标题"]',
  247. 'input[placeholder*="填写标题"]',
  248. '[class*="title"] input',
  249. '.c-input_inner',
  250. ]
  251. for selector in title_selectors:
  252. title_input = self.page.locator(selector).first
  253. if await title_input.count() > 0:
  254. await title_input.click()
  255. await title_input.fill('') # 先清空
  256. await title_input.fill(params.title[:20])
  257. title_filled = True
  258. print(f"[{self.platform_name}] 标题已填写,使用选择器: {selector}")
  259. break
  260. if not title_filled:
  261. print(f"[{self.platform_name}] 警告: 未找到标题输入框")
  262. # 填写描述和标签
  263. if params.description or params.tags:
  264. desc_filled = False
  265. desc_selectors = [
  266. '[class*="ql-editor"]',
  267. '[class*="content-input"] [contenteditable="true"]',
  268. '[class*="editor"] [contenteditable="true"]',
  269. '.ql-editor',
  270. ]
  271. for selector in desc_selectors:
  272. desc_input = self.page.locator(selector).first
  273. if await desc_input.count() > 0:
  274. await desc_input.click()
  275. await asyncio.sleep(0.5)
  276. if params.description:
  277. await self.page.keyboard.type(params.description, delay=20)
  278. print(f"[{self.platform_name}] 描述已填写")
  279. if params.tags:
  280. # 添加标签
  281. await self.page.keyboard.press("Enter")
  282. for tag in params.tags[:5]: # 最多5个标签
  283. await self.page.keyboard.type(f"#{tag}", delay=20)
  284. await asyncio.sleep(0.3)
  285. await self.page.keyboard.press("Space")
  286. print(f"[{self.platform_name}] 标签已填写: {params.tags[:5]}")
  287. desc_filled = True
  288. break
  289. if not desc_filled:
  290. print(f"[{self.platform_name}] 警告: 未找到描述输入框")
  291. await asyncio.sleep(2)
  292. self.report_progress(80, "正在发布...")
  293. await asyncio.sleep(2)
  294. # 滚动到页面底部确保发布按钮可见
  295. await self.page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
  296. await asyncio.sleep(1)
  297. print(f"[{self.platform_name}] 查找发布按钮...")
  298. # 点击发布
  299. publish_selectors = [
  300. 'button.publishBtn',
  301. '.publishBtn',
  302. 'button.d-button.red',
  303. 'button:has-text("发布"):not(:has-text("定时发布"))',
  304. '[class*="publish"][class*="btn"]',
  305. ]
  306. publish_clicked = False
  307. for selector in publish_selectors:
  308. try:
  309. btn = self.page.locator(selector).first
  310. if await btn.count() > 0:
  311. is_visible = await btn.is_visible()
  312. is_enabled = await btn.is_enabled()
  313. print(f"[{self.platform_name}] 按钮 {selector}: visible={is_visible}, enabled={is_enabled}")
  314. if is_visible and is_enabled:
  315. box = await btn.bounding_box()
  316. if box:
  317. print(f"[{self.platform_name}] 点击发布按钮: {selector}, 位置: ({box['x']}, {box['y']})")
  318. # 使用真实鼠标点击
  319. await self.page.mouse.click(box['x'] + box['width']/2, box['y'] + box['height']/2)
  320. publish_clicked = True
  321. break
  322. except Exception as e:
  323. print(f"[{self.platform_name}] 选择器 {selector} 错误: {e}")
  324. if not publish_clicked:
  325. # 保存截图用于调试
  326. screenshot_path = f"debug_publish_failed_{self.platform_name}.png"
  327. await self.page.screenshot(path=screenshot_path, full_page=True)
  328. print(f"[{self.platform_name}] 未找到发布按钮,截图保存到: {screenshot_path}")
  329. # 打印页面 HTML 结构用于调试
  330. buttons = await self.page.query_selector_all('button')
  331. print(f"[{self.platform_name}] 页面上共有 {len(buttons)} 个按钮")
  332. for i, btn in enumerate(buttons[:10]):
  333. text = await btn.text_content() or ''
  334. cls = await btn.get_attribute('class') or ''
  335. print(f" 按钮 {i}: text='{text.strip()[:30]}', class='{cls[:50]}'")
  336. raise Exception("未找到发布按钮")
  337. print(f"[{self.platform_name}] 已点击发布按钮,等待发布完成...")
  338. self.report_progress(90, "等待发布结果...")
  339. # 等待发布完成(检测 URL 变化或成功提示)
  340. publish_success = False
  341. for i in range(20): # 最多等待 20 秒
  342. await asyncio.sleep(1)
  343. current_url = self.page.url
  344. # 检查是否跳转到发布成功页面或内容管理页面
  345. if "published=true" in current_url or "success" in current_url or "content" in current_url:
  346. publish_success = True
  347. print(f"[{self.platform_name}] 发布成功! 跳转到: {current_url}")
  348. break
  349. # 检查是否有成功提示
  350. try:
  351. success_msg = await self.page.locator('[class*="success"], .toast-success, [class*="Toast"]').first.is_visible()
  352. if success_msg:
  353. publish_success = True
  354. print(f"[{self.platform_name}] 检测到成功提示!")
  355. break
  356. except:
  357. pass
  358. # 检查是否有错误提示
  359. try:
  360. error_elements = self.page.locator('[class*="error"], .toast-error, [class*="fail"]')
  361. if await error_elements.count() > 0:
  362. error_text = await error_elements.first.text_content()
  363. if error_text and len(error_text.strip()) > 0:
  364. raise Exception(f"发布失败: {error_text.strip()}")
  365. except Exception as e:
  366. if "发布失败" in str(e):
  367. raise
  368. # 如果没有明确的成功标志,保存截图
  369. if not publish_success:
  370. final_url = self.page.url
  371. print(f"[{self.platform_name}] 发布结果不确定,当前 URL: {final_url}")
  372. screenshot_path = f"debug_publish_result_{self.platform_name}.png"
  373. await self.page.screenshot(path=screenshot_path, full_page=True)
  374. print(f"[{self.platform_name}] 截图保存到: {screenshot_path}")
  375. # 如果 URL 还是发布页面,可能发布失败
  376. if "publish/publish" in final_url:
  377. raise Exception(f"发布可能失败,仍停留在发布页面(截图: {screenshot_path})")
  378. self.report_progress(100, "发布完成")
  379. print(f"[{self.platform_name}] Playwright 方式发布完成!")
  380. return PublishResult(
  381. success=True,
  382. platform=self.platform_name,
  383. message="发布完成"
  384. )
  385. async def get_works(self, cookies: str, page: int = 0, page_size: int = 20) -> WorksResult:
  386. """获取小红书作品列表 - 通过监听页面网络响应获取数据"""
  387. print(f"\n{'='*60}", flush=True)
  388. print(f"[{self.platform_name}] 获取作品列表", flush=True)
  389. print(f"[{self.platform_name}] page={page}, page_size={page_size}", flush=True)
  390. print(f"{'='*60}", flush=True)
  391. works: List[WorkItem] = []
  392. total = 0
  393. has_more = False
  394. captured_data = {}
  395. try:
  396. await self.init_browser()
  397. cookie_list = self.parse_cookies(cookies)
  398. # 打印 cookies 信息用于调试
  399. print(f"[{self.platform_name}] 解析到 {len(cookie_list)} 个 cookies", flush=True)
  400. await self.set_cookies(cookie_list)
  401. if not self.page:
  402. raise Exception("Page not initialized")
  403. # 定义响应监听器 - 捕获页面自动发起的 API 请求
  404. async def handle_response(response):
  405. nonlocal captured_data
  406. url = response.url
  407. # 监听作品列表 API
  408. if 'creator/note/user/posted' in url or 'creator/note_list' in url:
  409. try:
  410. json_data = await response.json()
  411. print(f"[{self.platform_name}] 捕获到 API 响应: {url[:80]}...", flush=True)
  412. if json_data.get('success') or json_data.get('code') == 0:
  413. captured_data = json_data
  414. print(f"[{self.platform_name}] API 响应成功,data keys: {list(json_data.get('data', {}).keys())}", flush=True)
  415. except Exception as e:
  416. print(f"[{self.platform_name}] 解析响应失败: {e}", flush=True)
  417. # 注册响应监听器
  418. self.page.on('response', handle_response)
  419. print(f"[{self.platform_name}] 已注册 API 响应监听器", flush=True)
  420. # 访问笔记管理页面 - 页面会自动发起 API 请求
  421. print(f"[{self.platform_name}] 访问笔记管理页面...", flush=True)
  422. try:
  423. await self.page.goto("https://creator.xiaohongshu.com/new/note-manager", wait_until="domcontentloaded", timeout=30000)
  424. except Exception as nav_error:
  425. print(f"[{self.platform_name}] 导航超时,但继续尝试: {nav_error}", flush=True)
  426. # 等待 API 响应被捕获
  427. await asyncio.sleep(5)
  428. # 检查登录状态
  429. current_url = self.page.url
  430. print(f"[{self.platform_name}] 当前页面: {current_url}", flush=True)
  431. if "login" in current_url:
  432. raise Exception("Cookie 已过期,请重新登录")
  433. # 如果还没有捕获到数据,等待更长时间
  434. if not captured_data:
  435. print(f"[{self.platform_name}] 等待 API 响应...", flush=True)
  436. await asyncio.sleep(5)
  437. # 移除监听器
  438. self.page.remove_listener('response', handle_response)
  439. # 处理捕获到的数据
  440. import json
  441. if captured_data:
  442. print(f"[{self.platform_name}] 成功捕获到 API 数据", flush=True)
  443. data = captured_data.get('data', {})
  444. notes = data.get('notes', [])
  445. print(f"[{self.platform_name}] notes 数量: {len(notes)}", flush=True)
  446. # 从 tags 获取总数
  447. tags = data.get('tags', [])
  448. for tag in tags:
  449. if tag.get('id') == 'special.note_time_desc':
  450. total = tag.get('notes_count', 0)
  451. break
  452. has_more = data.get('page', -1) != -1
  453. for note in notes:
  454. note_id = note.get('id', '')
  455. if not note_id:
  456. continue
  457. # 获取封面
  458. cover_url = ''
  459. images_list = note.get('images_list', [])
  460. if images_list:
  461. cover_url = images_list[0].get('url', '')
  462. if cover_url.startswith('http://'):
  463. cover_url = cover_url.replace('http://', 'https://')
  464. # 获取时长
  465. duration = note.get('video_info', {}).get('duration', 0)
  466. # 解析状态
  467. status = 'published'
  468. tab_status = note.get('tab_status', 1)
  469. if tab_status == 0:
  470. status = 'draft'
  471. elif tab_status == 2:
  472. status = 'reviewing'
  473. elif tab_status == 3:
  474. status = 'rejected'
  475. works.append(WorkItem(
  476. work_id=note_id,
  477. title=note.get('display_title', '') or '无标题',
  478. cover_url=cover_url,
  479. duration=duration,
  480. status=status,
  481. publish_time=note.get('time', ''),
  482. play_count=note.get('view_count', 0),
  483. like_count=note.get('likes', 0),
  484. comment_count=note.get('comments_count', 0),
  485. share_count=note.get('shared_count', 0),
  486. collect_count=note.get('collected_count', 0),
  487. ))
  488. print(f"[{self.platform_name}] 解析到 {len(works)} 个作品,总计: {total}", flush=True)
  489. else:
  490. print(f"[{self.platform_name}] 未能捕获到 API 数据", flush=True)
  491. except Exception as e:
  492. import traceback
  493. print(f"[{self.platform_name}] 发生异常: {e}", flush=True)
  494. traceback.print_exc()
  495. return WorksResult(
  496. success=False,
  497. platform=self.platform_name,
  498. error=str(e)
  499. )
  500. finally:
  501. # 确保关闭浏览器
  502. await self.close_browser()
  503. return WorksResult(
  504. success=True,
  505. platform=self.platform_name,
  506. works=works,
  507. total=total or len(works),
  508. has_more=has_more
  509. )
  510. async def get_comments(self, cookies: str, work_id: str, cursor: str = "") -> CommentsResult:
  511. """获取小红书作品评论 - 通过创作者后台评论管理页面"""
  512. print(f"\n{'='*60}")
  513. print(f"[{self.platform_name}] 获取作品评论")
  514. print(f"[{self.platform_name}] work_id={work_id}, cursor={cursor}")
  515. print(f"{'='*60}")
  516. comments: List[CommentItem] = []
  517. total = 0
  518. has_more = False
  519. next_cursor = ""
  520. captured_data = {}
  521. try:
  522. await self.init_browser()
  523. cookie_list = self.parse_cookies(cookies)
  524. await self.set_cookies(cookie_list)
  525. if not self.page:
  526. raise Exception("Page not initialized")
  527. # 设置 API 响应监听器
  528. async def handle_response(response):
  529. nonlocal captured_data
  530. url = response.url
  531. # 监听评论相关 API - 创作者后台和普通页面的 API
  532. if '/comment/' in url and ('page' in url or 'list' in url):
  533. try:
  534. json_data = await response.json()
  535. print(f"[{self.platform_name}] 捕获到评论 API: {url[:100]}...", flush=True)
  536. if json_data.get('success') or json_data.get('code') == 0:
  537. data = json_data.get('data', {})
  538. comment_list = data.get('comments') or data.get('list') or []
  539. if comment_list:
  540. captured_data = json_data
  541. print(f"[{self.platform_name}] 评论 API 响应成功,comments={len(comment_list)}", flush=True)
  542. else:
  543. print(f"[{self.platform_name}] 评论 API 响应成功但无评论", flush=True)
  544. except Exception as e:
  545. print(f"[{self.platform_name}] 解析评论响应失败: {e}", flush=True)
  546. self.page.on('response', handle_response)
  547. print(f"[{self.platform_name}] 已注册评论 API 响应监听器", flush=True)
  548. # 访问创作者后台评论管理页面
  549. comment_url = "https://creator.xiaohongshu.com/creator/comment"
  550. print(f"[{self.platform_name}] 访问评论管理页面: {comment_url}", flush=True)
  551. await self.page.goto(comment_url, wait_until="domcontentloaded", timeout=30000)
  552. await asyncio.sleep(5)
  553. # 检查是否被重定向到登录页
  554. current_url = self.page.url
  555. print(f"[{self.platform_name}] 当前页面 URL: {current_url}", flush=True)
  556. if "login" in current_url:
  557. raise Exception("Cookie 已过期,请重新登录")
  558. # 等待评论加载
  559. if not captured_data:
  560. print(f"[{self.platform_name}] 等待评论 API 响应...", flush=True)
  561. # 尝试滚动页面触发评论加载
  562. await self.page.evaluate('window.scrollBy(0, 500)')
  563. await asyncio.sleep(3)
  564. if not captured_data:
  565. # 再等待一会,可能评论 API 加载较慢
  566. print(f"[{self.platform_name}] 继续等待评论加载...", flush=True)
  567. await asyncio.sleep(5)
  568. # 移除监听器
  569. self.page.remove_listener('response', handle_response)
  570. # 解析评论数据
  571. if captured_data:
  572. data = captured_data.get('data', {})
  573. comment_list = data.get('comments') or data.get('list') or []
  574. has_more = data.get('has_more', False)
  575. next_cursor = data.get('cursor', '')
  576. print(f"[{self.platform_name}] 解析评论: has_more={has_more}, comments={len(comment_list)}", flush=True)
  577. for comment in comment_list:
  578. cid = comment.get('id', '')
  579. if not cid:
  580. continue
  581. user_info = comment.get('user_info', {})
  582. # 解析子评论
  583. replies = []
  584. sub_comments = comment.get('sub_comments', []) or []
  585. for sub in sub_comments:
  586. sub_user = sub.get('user_info', {})
  587. replies.append(CommentItem(
  588. comment_id=sub.get('id', ''),
  589. work_id=work_id,
  590. content=sub.get('content', ''),
  591. author_id=sub_user.get('user_id', ''),
  592. author_name=sub_user.get('nickname', ''),
  593. author_avatar=sub_user.get('image', ''),
  594. like_count=sub.get('like_count', 0),
  595. create_time=sub.get('create_time', ''),
  596. ))
  597. comments.append(CommentItem(
  598. comment_id=cid,
  599. work_id=work_id,
  600. content=comment.get('content', ''),
  601. author_id=user_info.get('user_id', ''),
  602. author_name=user_info.get('nickname', ''),
  603. author_avatar=user_info.get('image', ''),
  604. like_count=comment.get('like_count', 0),
  605. reply_count=comment.get('sub_comment_count', 0),
  606. create_time=comment.get('create_time', ''),
  607. replies=replies,
  608. ))
  609. total = len(comments)
  610. print(f"[{self.platform_name}] 解析到 {total} 条评论", flush=True)
  611. else:
  612. print(f"[{self.platform_name}] 未捕获到评论 API 响应", flush=True)
  613. except Exception as e:
  614. import traceback
  615. traceback.print_exc()
  616. return CommentsResult(
  617. success=False,
  618. platform=self.platform_name,
  619. work_id=work_id,
  620. error=str(e)
  621. )
  622. finally:
  623. await self.close_browser()
  624. result = CommentsResult(
  625. success=True,
  626. platform=self.platform_name,
  627. work_id=work_id,
  628. comments=comments,
  629. total=total,
  630. has_more=has_more
  631. )
  632. result.__dict__['cursor'] = next_cursor
  633. return result
  634. async def get_all_comments(self, cookies: str) -> dict:
  635. """获取所有作品的评论 - 通过评论管理页面"""
  636. print(f"\n{'='*60}")
  637. print(f"[{self.platform_name}] 获取所有作品评论")
  638. print(f"{'='*60}")
  639. all_work_comments = []
  640. captured_comments = []
  641. captured_notes = {} # note_id -> note_info
  642. try:
  643. await self.init_browser()
  644. cookie_list = self.parse_cookies(cookies)
  645. await self.set_cookies(cookie_list)
  646. if not self.page:
  647. raise Exception("Page not initialized")
  648. # 设置 API 响应监听器
  649. async def handle_response(response):
  650. nonlocal captured_comments, captured_notes
  651. url = response.url
  652. try:
  653. # 监听评论列表 API - 多种格式
  654. if '/comment/' in url and ('page' in url or 'list' in url):
  655. json_data = await response.json()
  656. print(f"[{self.platform_name}] 捕获到评论 API: {url[:100]}...", flush=True)
  657. if json_data.get('success') or json_data.get('code') == 0:
  658. data = json_data.get('data', {})
  659. comments = data.get('comments', []) or data.get('list', [])
  660. # 从 URL 中提取 note_id
  661. import re
  662. note_id_match = re.search(r'note_id=([^&]+)', url)
  663. note_id = note_id_match.group(1) if note_id_match else ''
  664. if comments:
  665. for comment in comments:
  666. # 添加 note_id 到评论中
  667. if note_id and 'note_id' not in comment:
  668. comment['note_id'] = note_id
  669. captured_comments.append(comment)
  670. print(f"[{self.platform_name}] 捕获到 {len(comments)} 条评论 (note_id={note_id}),总计: {len(captured_comments)}", flush=True)
  671. # 监听笔记列表 API
  672. if '/note/' in url and ('list' in url or 'posted' in url or 'manager' in url):
  673. json_data = await response.json()
  674. if json_data.get('success') or json_data.get('code') == 0:
  675. data = json_data.get('data', {})
  676. notes = data.get('notes', []) or data.get('list', [])
  677. print(f"[{self.platform_name}] 捕获到笔记列表 API: {len(notes)} 个笔记", flush=True)
  678. for note in notes:
  679. note_id = note.get('note_id', '') or note.get('id', '')
  680. if note_id:
  681. cover_url = ''
  682. cover = note.get('cover', {})
  683. if isinstance(cover, dict):
  684. cover_url = cover.get('url', '') or cover.get('url_default', '')
  685. elif isinstance(cover, str):
  686. cover_url = cover
  687. captured_notes[note_id] = {
  688. 'title': note.get('title', '') or note.get('display_title', ''),
  689. 'cover': cover_url,
  690. }
  691. except Exception as e:
  692. print(f"[{self.platform_name}] 解析响应失败: {e}", flush=True)
  693. self.page.on('response', handle_response)
  694. print(f"[{self.platform_name}] 已注册 API 响应监听器", flush=True)
  695. # 访问评论管理页面
  696. print(f"[{self.platform_name}] 访问评论管理页面...", flush=True)
  697. await self.page.goto("https://creator.xiaohongshu.com/creator/comment", wait_until="domcontentloaded", timeout=30000)
  698. await asyncio.sleep(5)
  699. # 检查登录状态
  700. current_url = self.page.url
  701. if "login" in current_url:
  702. raise Exception("Cookie 已过期,请重新登录")
  703. print(f"[{self.platform_name}] 页面加载完成,当前捕获: {len(captured_comments)} 条评论, {len(captured_notes)} 个笔记", flush=True)
  704. # 滚动加载更多评论
  705. for i in range(5):
  706. await self.page.evaluate('window.scrollBy(0, 500)')
  707. await asyncio.sleep(1)
  708. await asyncio.sleep(3)
  709. # 移除监听器
  710. self.page.remove_listener('response', handle_response)
  711. print(f"[{self.platform_name}] 最终捕获: {len(captured_comments)} 条评论, {len(captured_notes)} 个笔记", flush=True)
  712. # 按作品分组评论
  713. work_comments_map = {} # note_id -> work_comments
  714. for comment in captured_comments:
  715. # 获取笔记信息
  716. note_info = comment.get('note_info', {}) or comment.get('note', {})
  717. note_id = comment.get('note_id', '') or note_info.get('note_id', '') or note_info.get('id', '')
  718. if not note_id:
  719. continue
  720. if note_id not in work_comments_map:
  721. saved_note = captured_notes.get(note_id, {})
  722. cover_url = ''
  723. cover = note_info.get('cover', {})
  724. if isinstance(cover, dict):
  725. cover_url = cover.get('url', '') or cover.get('url_default', '')
  726. elif isinstance(cover, str):
  727. cover_url = cover
  728. if not cover_url:
  729. cover_url = saved_note.get('cover', '')
  730. work_comments_map[note_id] = {
  731. 'work_id': note_id,
  732. 'title': note_info.get('title', '') or note_info.get('display_title', '') or saved_note.get('title', ''),
  733. 'cover_url': cover_url,
  734. 'comments': []
  735. }
  736. cid = comment.get('id', '') or comment.get('comment_id', '')
  737. if not cid:
  738. continue
  739. user_info = comment.get('user_info', {}) or comment.get('user', {})
  740. work_comments_map[note_id]['comments'].append({
  741. 'comment_id': cid,
  742. 'author_id': user_info.get('user_id', '') or user_info.get('id', ''),
  743. 'author_name': user_info.get('nickname', '') or user_info.get('name', ''),
  744. 'author_avatar': user_info.get('image', '') or user_info.get('avatar', ''),
  745. 'content': comment.get('content', ''),
  746. 'like_count': comment.get('like_count', 0),
  747. 'create_time': comment.get('create_time', ''),
  748. })
  749. all_work_comments = list(work_comments_map.values())
  750. total_comments = sum(len(w['comments']) for w in all_work_comments)
  751. print(f"[{self.platform_name}] 获取到 {len(all_work_comments)} 个作品的 {total_comments} 条评论", flush=True)
  752. except Exception as e:
  753. import traceback
  754. traceback.print_exc()
  755. return {
  756. 'success': False,
  757. 'platform': self.platform_name,
  758. 'error': str(e),
  759. 'work_comments': []
  760. }
  761. finally:
  762. await self.close_browser()
  763. return {
  764. 'success': True,
  765. 'platform': self.platform_name,
  766. 'work_comments': all_work_comments,
  767. 'total': len(all_work_comments)
  768. }