xiaohongshu.py 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  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. async def wait_for_manual_login(timeout_seconds: int = 300) -> bool:
  225. if not self.page:
  226. return False
  227. self.report_progress(12, "检测到需要登录,请在浏览器窗口完成登录...")
  228. try:
  229. await self.page.bring_to_front()
  230. except:
  231. pass
  232. waited = 0
  233. while waited < timeout_seconds:
  234. try:
  235. url = self.page.url
  236. if "login" not in url and "passport" not in url and "creator.xiaohongshu.com" in url:
  237. return True
  238. await asyncio.sleep(2)
  239. waited += 2
  240. except:
  241. await asyncio.sleep(2)
  242. waited += 2
  243. return False
  244. async def wait_for_manual_captcha(timeout_seconds: int = 180) -> bool:
  245. waited = 0
  246. while waited < timeout_seconds:
  247. try:
  248. ai_captcha = await self.ai_check_captcha()
  249. if not ai_captcha.get("has_captcha"):
  250. return True
  251. except:
  252. pass
  253. await asyncio.sleep(3)
  254. waited += 3
  255. return False
  256. # 检查登录状态
  257. if "login" in current_url or "passport" in current_url:
  258. if not self.headless:
  259. logged_in = await wait_for_manual_login()
  260. if logged_in:
  261. try:
  262. if self.context:
  263. cookies_after = await self.context.cookies()
  264. await self.sync_cookies_to_node(cookies_after)
  265. except:
  266. pass
  267. await self.page.goto(publish_url)
  268. await asyncio.sleep(3)
  269. current_url = self.page.url
  270. else:
  271. screenshot_base64 = await self.capture_screenshot()
  272. return PublishResult(
  273. success=False,
  274. platform=self.platform_name,
  275. error="需要登录:请在浏览器窗口完成登录后重试",
  276. screenshot_base64=screenshot_base64,
  277. page_url=current_url,
  278. status='need_captcha',
  279. need_captcha=True,
  280. captcha_type='login'
  281. )
  282. else:
  283. screenshot_base64 = await self.capture_screenshot()
  284. return PublishResult(
  285. success=False,
  286. platform=self.platform_name,
  287. error="登录已过期,请重新登录",
  288. screenshot_base64=screenshot_base64,
  289. page_url=current_url,
  290. status='need_captcha',
  291. need_captcha=True,
  292. captcha_type='login'
  293. )
  294. # 使用 AI 检查验证码
  295. ai_captcha = await self.ai_check_captcha()
  296. if ai_captcha['has_captcha']:
  297. print(f"[{self.platform_name}] AI检测到验证码: {ai_captcha['captcha_type']}", flush=True)
  298. if not self.headless:
  299. solved = await wait_for_manual_captcha()
  300. if solved:
  301. try:
  302. if self.context:
  303. cookies_after = await self.context.cookies()
  304. await self.sync_cookies_to_node(cookies_after)
  305. except:
  306. pass
  307. else:
  308. screenshot_base64 = await self.capture_screenshot()
  309. return PublishResult(
  310. success=False,
  311. platform=self.platform_name,
  312. error=f"需要验证码:请在浏览器窗口完成验证后重试",
  313. screenshot_base64=screenshot_base64,
  314. page_url=current_url,
  315. status='need_captcha',
  316. need_captcha=True,
  317. captcha_type=ai_captcha['captcha_type']
  318. )
  319. else:
  320. screenshot_base64 = await self.capture_screenshot()
  321. return PublishResult(
  322. success=False,
  323. platform=self.platform_name,
  324. error=f"检测到{ai_captcha['captcha_type']}验证码,需要使用有头浏览器完成验证",
  325. screenshot_base64=screenshot_base64,
  326. page_url=current_url,
  327. status='need_captcha',
  328. need_captcha=True,
  329. captcha_type=ai_captcha['captcha_type']
  330. )
  331. self.report_progress(20, "正在上传视频...")
  332. # 等待页面加载
  333. await asyncio.sleep(2)
  334. # 上传视频
  335. upload_triggered = False
  336. # 方法1: 直接设置隐藏的 file input
  337. print(f"[{self.platform_name}] 尝试方法1: 设置 file input")
  338. file_inputs = self.page.locator('input[type="file"]')
  339. input_count = await file_inputs.count()
  340. print(f"[{self.platform_name}] 找到 {input_count} 个 file input")
  341. if input_count > 0:
  342. # 找到接受视频的 input
  343. for i in range(input_count):
  344. input_el = file_inputs.nth(i)
  345. accept = await input_el.get_attribute('accept') or ''
  346. print(f"[{self.platform_name}] Input {i} accept: {accept}")
  347. if 'video' in accept or '*' in accept or not accept:
  348. await input_el.set_input_files(params.video_path)
  349. upload_triggered = True
  350. print(f"[{self.platform_name}] 视频文件已设置到 input {i}")
  351. break
  352. # 方法2: 点击上传区域触发文件选择器
  353. if not upload_triggered:
  354. print(f"[{self.platform_name}] 尝试方法2: 点击上传区域")
  355. try:
  356. upload_area = self.page.locator('[class*="upload-wrapper"], [class*="upload-area"], .upload-input').first
  357. if await upload_area.count() > 0:
  358. async with self.page.expect_file_chooser(timeout=5000) as fc_info:
  359. await upload_area.click()
  360. file_chooser = await fc_info.value
  361. await file_chooser.set_files(params.video_path)
  362. upload_triggered = True
  363. print(f"[{self.platform_name}] 通过点击上传区域上传成功")
  364. except Exception as e:
  365. print(f"[{self.platform_name}] 方法2失败: {e}")
  366. if not upload_triggered:
  367. screenshot_base64 = await self.capture_screenshot()
  368. page_url = await self.get_page_url()
  369. return PublishResult(
  370. success=False,
  371. platform=self.platform_name,
  372. error="无法上传视频文件",
  373. screenshot_base64=screenshot_base64,
  374. page_url=page_url,
  375. status='need_action'
  376. )
  377. self.report_progress(40, "等待视频上传完成...")
  378. print(f"[{self.platform_name}] 等待视频上传和处理...")
  379. # 等待上传完成(检测页面变化)
  380. upload_complete = False
  381. for i in range(60): # 最多等待3分钟
  382. await asyncio.sleep(3)
  383. # 检查是否有标题输入框(上传完成后出现)
  384. title_input_count = await self.page.locator('input[placeholder*="标题"], input[placeholder*="填写标题"]').count()
  385. # 或者检查编辑器区域
  386. editor_count = await self.page.locator('[class*="ql-editor"], [contenteditable="true"]').count()
  387. # 检查发布按钮是否可见
  388. publish_btn_count = await self.page.locator('.publishBtn, button:has-text("发布")').count()
  389. print(f"[{self.platform_name}] 检测 {i+1}: 标题框={title_input_count}, 编辑器={editor_count}, 发布按钮={publish_btn_count}")
  390. if title_input_count > 0 or (editor_count > 0 and publish_btn_count > 0):
  391. upload_complete = True
  392. print(f"[{self.platform_name}] 视频上传完成!")
  393. break
  394. if not upload_complete:
  395. screenshot_base64 = await self.capture_screenshot()
  396. page_url = await self.get_page_url()
  397. return PublishResult(
  398. success=False,
  399. platform=self.platform_name,
  400. error="视频上传超时",
  401. screenshot_base64=screenshot_base64,
  402. page_url=page_url,
  403. status='need_action'
  404. )
  405. await asyncio.sleep(2)
  406. self.report_progress(60, "正在填写笔记信息...")
  407. print(f"[{self.platform_name}] 填写标题: {params.title[:20]}")
  408. # 填写标题
  409. title_filled = False
  410. title_selectors = [
  411. 'input[placeholder*="标题"]',
  412. 'input[placeholder*="填写标题"]',
  413. '[class*="title"] input',
  414. '.c-input_inner',
  415. ]
  416. for selector in title_selectors:
  417. title_input = self.page.locator(selector).first
  418. if await title_input.count() > 0:
  419. await title_input.click()
  420. await title_input.fill('') # 先清空
  421. await title_input.fill(params.title[:20])
  422. title_filled = True
  423. print(f"[{self.platform_name}] 标题已填写,使用选择器: {selector}")
  424. break
  425. if not title_filled:
  426. print(f"[{self.platform_name}] 警告: 未找到标题输入框")
  427. # 填写描述和标签
  428. if params.description or params.tags:
  429. desc_filled = False
  430. desc_selectors = [
  431. '[class*="ql-editor"]',
  432. '[class*="content-input"] [contenteditable="true"]',
  433. '[class*="editor"] [contenteditable="true"]',
  434. '.ql-editor',
  435. ]
  436. for selector in desc_selectors:
  437. desc_input = self.page.locator(selector).first
  438. if await desc_input.count() > 0:
  439. await desc_input.click()
  440. await asyncio.sleep(0.5)
  441. if params.description:
  442. await self.page.keyboard.type(params.description, delay=20)
  443. print(f"[{self.platform_name}] 描述已填写")
  444. if params.tags:
  445. # 添加标签
  446. await self.page.keyboard.press("Enter")
  447. for tag in params.tags[:5]: # 最多5个标签
  448. await self.page.keyboard.type(f"#{tag}", delay=20)
  449. await asyncio.sleep(0.3)
  450. await self.page.keyboard.press("Space")
  451. print(f"[{self.platform_name}] 标签已填写: {params.tags[:5]}")
  452. desc_filled = True
  453. break
  454. if not desc_filled:
  455. print(f"[{self.platform_name}] 警告: 未找到描述输入框")
  456. await asyncio.sleep(2)
  457. self.report_progress(80, "正在发布...")
  458. await asyncio.sleep(2)
  459. # 滚动到页面底部确保发布按钮可见
  460. await self.page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
  461. await asyncio.sleep(1)
  462. print(f"[{self.platform_name}] 查找发布按钮...")
  463. # 点击发布
  464. publish_selectors = [
  465. 'button.publishBtn',
  466. '.publishBtn',
  467. 'button.d-button.red',
  468. 'button:has-text("发布"):not(:has-text("定时发布"))',
  469. '[class*="publish"][class*="btn"]',
  470. ]
  471. publish_clicked = False
  472. for selector in publish_selectors:
  473. try:
  474. btn = self.page.locator(selector).first
  475. if await btn.count() > 0:
  476. is_visible = await btn.is_visible()
  477. is_enabled = await btn.is_enabled()
  478. print(f"[{self.platform_name}] 按钮 {selector}: visible={is_visible}, enabled={is_enabled}")
  479. if is_visible and is_enabled:
  480. box = await btn.bounding_box()
  481. if box:
  482. print(f"[{self.platform_name}] 点击发布按钮: {selector}, 位置: ({box['x']}, {box['y']})")
  483. # 使用真实鼠标点击
  484. await self.page.mouse.click(box['x'] + box['width']/2, box['y'] + box['height']/2)
  485. publish_clicked = True
  486. break
  487. except Exception as e:
  488. print(f"[{self.platform_name}] 选择器 {selector} 错误: {e}")
  489. if not publish_clicked:
  490. try:
  491. suggest = await self.ai_suggest_playwright_selector("点击小红书发布按钮")
  492. if suggest.get("has_selector") and suggest.get("selector"):
  493. sel = suggest.get("selector")
  494. btn = self.page.locator(sel).first
  495. if await btn.count() > 0 and await btn.is_visible() and await btn.is_enabled():
  496. try:
  497. await btn.click()
  498. except:
  499. box = await btn.bounding_box()
  500. if box:
  501. await self.page.mouse.click(box['x'] + box['width']/2, box['y'] + box['height']/2)
  502. publish_clicked = True
  503. except Exception as e:
  504. print(f"[{self.platform_name}] AI 点击发布按钮失败: {e}", flush=True)
  505. if not publish_clicked:
  506. # 保存截图用于调试
  507. screenshot_path = f"debug_publish_failed_{self.platform_name}.png"
  508. await self.page.screenshot(path=screenshot_path, full_page=True)
  509. print(f"[{self.platform_name}] 未找到发布按钮,截图保存到: {screenshot_path}")
  510. # 打印页面 HTML 结构用于调试
  511. buttons = await self.page.query_selector_all('button')
  512. print(f"[{self.platform_name}] 页面上共有 {len(buttons)} 个按钮")
  513. for i, btn in enumerate(buttons[:10]):
  514. text = await btn.text_content() or ''
  515. cls = await btn.get_attribute('class') or ''
  516. print(f" 按钮 {i}: text='{text.strip()[:30]}', class='{cls[:50]}'")
  517. raise Exception("未找到发布按钮")
  518. print(f"[{self.platform_name}] 已点击发布按钮,等待发布完成...")
  519. self.report_progress(90, "等待发布结果...")
  520. # 等待发布完成(检测 URL 变化或成功提示)
  521. publish_success = False
  522. for i in range(20): # 最多等待 20 秒
  523. await asyncio.sleep(1)
  524. current_url = self.page.url
  525. # 检查是否跳转到发布成功页面或内容管理页面
  526. if "published=true" in current_url or "success" in current_url or "content" in current_url:
  527. publish_success = True
  528. print(f"[{self.platform_name}] 发布成功! 跳转到: {current_url}")
  529. break
  530. # 检查是否有成功提示
  531. try:
  532. success_msg = await self.page.locator('[class*="success"], .toast-success, [class*="Toast"]').first.is_visible()
  533. if success_msg:
  534. publish_success = True
  535. print(f"[{self.platform_name}] 检测到成功提示!")
  536. break
  537. except:
  538. pass
  539. # 检查是否有错误提示
  540. try:
  541. error_elements = self.page.locator('[class*="error"], .toast-error, [class*="fail"]')
  542. if await error_elements.count() > 0:
  543. error_text = await error_elements.first.text_content()
  544. if error_text and len(error_text.strip()) > 0:
  545. raise Exception(f"发布失败: {error_text.strip()}")
  546. except Exception as e:
  547. if "发布失败" in str(e):
  548. raise
  549. # 如果没有明确的成功标志,返回截图供 AI 分析
  550. if not publish_success:
  551. final_url = self.page.url
  552. print(f"[{self.platform_name}] 发布结果不确定,当前 URL: {final_url}")
  553. screenshot_base64 = await self.capture_screenshot()
  554. print(f"[{self.platform_name}] 已获取截图供 AI 分析")
  555. # 如果 URL 还是发布页面,可能需要继续操作
  556. if "publish/publish" in final_url:
  557. return PublishResult(
  558. success=False,
  559. platform=self.platform_name,
  560. error="发布结果待确认,请查看截图",
  561. screenshot_base64=screenshot_base64,
  562. page_url=final_url,
  563. status='need_action'
  564. )
  565. self.report_progress(100, "发布完成")
  566. print(f"[{self.platform_name}] Playwright 方式发布完成!")
  567. screenshot_base64 = await self.capture_screenshot()
  568. page_url = await self.get_page_url()
  569. return PublishResult(
  570. success=True,
  571. platform=self.platform_name,
  572. message="发布完成",
  573. screenshot_base64=screenshot_base64,
  574. page_url=page_url,
  575. status='success'
  576. )
  577. async def get_account_info(self, cookies: str) -> dict:
  578. """获取账号信息"""
  579. print(f"\n{'='*60}")
  580. print(f"[{self.platform_name}] 获取账号信息")
  581. print(f"{'='*60}")
  582. captured_info = {}
  583. try:
  584. await self.init_browser()
  585. cookie_list = self.parse_cookies(cookies)
  586. await self.set_cookies(cookie_list)
  587. if not self.page:
  588. raise Exception("Page not initialized")
  589. # 监听个人信息 API
  590. async def handle_response(response):
  591. nonlocal captured_info
  592. if 'api/galaxy/creator/home/personal_info' in response.url:
  593. try:
  594. json_data = await response.json()
  595. print(f"[{self.platform_name}] 捕获个人信息 API", flush=True)
  596. if json_data.get('success') or json_data.get('code') == 0:
  597. data = json_data.get('data', {})
  598. captured_info = {
  599. "account_id": f"xhs_{data.get('red_num', '')}",
  600. "account_name": data.get('name', ''),
  601. "avatar_url": data.get('avatar', ''),
  602. "fans_count": data.get('fans_count', 0),
  603. "works_count": 0 # 暂时无法直接获取准确的作品数,需要从作品列表获取
  604. }
  605. except Exception as e:
  606. print(f"[{self.platform_name}] 解析个人信息失败: {e}", flush=True)
  607. self.page.on('response', handle_response)
  608. # 访问首页
  609. print(f"[{self.platform_name}] 访问创作者首页...", flush=True)
  610. await self.page.goto("https://creator.xiaohongshu.com/new/home", wait_until="domcontentloaded")
  611. # 等待 API 响应
  612. for _ in range(10):
  613. if captured_info:
  614. break
  615. await asyncio.sleep(1)
  616. if not captured_info:
  617. print(f"[{self.platform_name}] 未捕获到个人信息,尝试刷新...", flush=True)
  618. await self.page.reload()
  619. for _ in range(10):
  620. if captured_info:
  621. break
  622. await asyncio.sleep(1)
  623. if not captured_info:
  624. raise Exception("无法获取账号信息")
  625. # 尝试获取作品数(从首页或其他地方)
  626. # 或者简单地返回已获取的信息,作品数由 get_works 更新
  627. return {
  628. "success": True,
  629. **captured_info
  630. }
  631. except Exception as e:
  632. import traceback
  633. traceback.print_exc()
  634. return {
  635. "success": False,
  636. "error": str(e)
  637. }
  638. finally:
  639. await self.close_browser()
  640. async def get_works(self, cookies: str, page: int = 0, page_size: int = 20) -> WorksResult:
  641. """获取小红书作品列表 - 通过直接调用创作者笔记列表 API 获取"""
  642. print(f"\n{'='*60}", flush=True)
  643. print(f"[{self.platform_name}] 获取作品列表", flush=True)
  644. print(f"[{self.platform_name}] page={page}, page_size={page_size}", flush=True)
  645. print(f"{'='*60}", flush=True)
  646. works: List[WorkItem] = []
  647. total = 0
  648. has_more = False
  649. next_page = ""
  650. api_page_size = 20
  651. try:
  652. await self.init_browser()
  653. cookie_list = self.parse_cookies(cookies)
  654. # 打印 cookies 信息用于调试
  655. print(f"[{self.platform_name}] 解析到 {len(cookie_list)} 个 cookies", flush=True)
  656. await self.set_cookies(cookie_list)
  657. if not self.page:
  658. raise Exception("Page not initialized")
  659. # 访问笔记管理页面 - 页面会自动发起 API 请求
  660. print(f"[{self.platform_name}] 访问笔记管理页面...", flush=True)
  661. try:
  662. await self.page.goto("https://creator.xiaohongshu.com/new/note-manager", wait_until="domcontentloaded", timeout=30000)
  663. except Exception as nav_error:
  664. print(f"[{self.platform_name}] 导航超时,但继续尝试: {nav_error}", flush=True)
  665. # 检查登录状态
  666. current_url = self.page.url
  667. print(f"[{self.platform_name}] 当前页面: {current_url}", flush=True)
  668. if "login" in current_url:
  669. raise Exception("Cookie 已过期,请重新登录")
  670. async def fetch_notes_page(p):
  671. return await self.page.evaluate(
  672. """async (pageNum) => {
  673. try {
  674. const url = `https://edith.xiaohongshu.com/web_api/sns/v5/creator/note/user/posted?tab=0&page=${pageNum}`;
  675. const headers = { 'Accept': 'application/json' };
  676. if (typeof window !== 'undefined' && typeof window._webmsxyw === 'function') {
  677. try {
  678. const sign = window._webmsxyw(url, '');
  679. headers['x-s'] = sign['X-s'];
  680. headers['x-t'] = String(sign['X-t']);
  681. } catch (e) {
  682. // ignore sign errors and fallback
  683. }
  684. }
  685. const res = await fetch(url, {
  686. method: 'GET',
  687. credentials: 'include',
  688. headers
  689. });
  690. return await res.json();
  691. } catch (e) {
  692. return { success: false, error: e.toString() };
  693. }
  694. }""",
  695. p
  696. )
  697. def parse_notes(notes_list):
  698. parsed = []
  699. for note in notes_list:
  700. note_id = note.get('id', '')
  701. if not note_id:
  702. continue
  703. cover_url = ''
  704. images_list = note.get('images_list', [])
  705. if images_list:
  706. cover_url = images_list[0].get('url', '')
  707. if cover_url.startswith('http://'):
  708. cover_url = cover_url.replace('http://', 'https://')
  709. duration = note.get('video_info', {}).get('duration', 0)
  710. status = 'published'
  711. tab_status = note.get('tab_status', 1)
  712. if tab_status == 0:
  713. status = 'draft'
  714. elif tab_status == 2:
  715. status = 'reviewing'
  716. elif tab_status == 3:
  717. status = 'rejected'
  718. parsed.append(WorkItem(
  719. work_id=note_id,
  720. title=note.get('display_title', '') or '无标题',
  721. cover_url=cover_url,
  722. duration=duration,
  723. status=status,
  724. publish_time=note.get('time', ''),
  725. play_count=note.get('view_count', 0),
  726. like_count=note.get('likes', 0),
  727. comment_count=note.get('comments_count', 0),
  728. share_count=note.get('shared_count', 0),
  729. collect_count=note.get('collected_count', 0),
  730. ))
  731. return parsed
  732. resp = None
  733. for attempt in range(1, 4):
  734. resp = await fetch_notes_page(page)
  735. if resp and (resp.get('success') or resp.get('code') == 0) and resp.get('data'):
  736. break
  737. print(f"[{self.platform_name}] 拉取作品列表失败,重试 {attempt}/3: {str(resp)[:200]}", flush=True)
  738. await asyncio.sleep(1.2 * attempt)
  739. if not resp or not (resp.get('success') or resp.get('code') == 0) or not resp.get('data'):
  740. raise Exception(f"无法获取作品列表数据: {resp.get('msg') if isinstance(resp, dict) else resp}")
  741. data = resp.get('data', {}) or {}
  742. notes = data.get('notes', []) or []
  743. print(f"[{self.platform_name}] 第 {page} 页 notes 数量: {len(notes)}", flush=True)
  744. tags = data.get('tags', []) or []
  745. if tags:
  746. preferred = 0
  747. for tag in tags:
  748. if tag.get('id') == 'special.note_time_desc':
  749. preferred = tag.get('notes_count', 0) or tag.get('notesCount', 0) or tag.get('count', 0) or 0
  750. break
  751. if preferred:
  752. total = preferred
  753. else:
  754. 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])
  755. if not total:
  756. total = int(data.get('total', 0) or data.get('total_count', 0) or data.get('totalCount', 0) or 0)
  757. if not total and isinstance(data.get('page', {}), dict):
  758. total = int(data.get('page', {}).get('total', 0) or data.get('page', {}).get('totalCount', 0) or 0)
  759. next_page = data.get('page', "")
  760. if next_page == page:
  761. next_page = page + 1
  762. works.extend(parse_notes(notes))
  763. if total:
  764. has_more = (page * api_page_size + len(notes)) < total
  765. if has_more and (next_page == -1 or str(next_page) == "-1" or next_page == "" or next_page is None):
  766. next_page = page + 1
  767. else:
  768. if len(notes) == 0:
  769. has_more = False
  770. else:
  771. next_resp = await fetch_notes_page(page + 1)
  772. next_data = (next_resp or {}).get('data', {}) if isinstance(next_resp, dict) else {}
  773. next_notes = next_data.get('notes', []) or []
  774. has_more = len(next_notes) > 0
  775. next_page = next_data.get('page', next_page)
  776. except Exception as e:
  777. import traceback
  778. print(f"[{self.platform_name}] 发生异常: {e}", flush=True)
  779. traceback.print_exc()
  780. return WorksResult(
  781. success=False,
  782. platform=self.platform_name,
  783. error=str(e)
  784. )
  785. finally:
  786. # 确保关闭浏览器
  787. await self.close_browser()
  788. return WorksResult(
  789. success=True,
  790. platform=self.platform_name,
  791. works=works,
  792. total=total or (page * api_page_size + len(works)),
  793. has_more=has_more,
  794. next_page=next_page
  795. )
  796. async def get_all_works(self, cookies: str) -> WorksResult:
  797. """获取小红书全部作品(单次请求内自动翻页抓全量,避免 Node 侧分页不一致)"""
  798. print(f"\n{'='*60}", flush=True)
  799. print(f"[{self.platform_name}] 获取全部作品(auto paging)", flush=True)
  800. print(f"{'='*60}", flush=True)
  801. works: List[WorkItem] = []
  802. total = 0
  803. seen_ids = set()
  804. cursor: object = 0
  805. max_iters = 800
  806. api_page_size = 20
  807. try:
  808. await self.init_browser()
  809. cookie_list = self.parse_cookies(cookies)
  810. print(f"[{self.platform_name}] 解析到 {len(cookie_list)} 个 cookies", flush=True)
  811. await self.set_cookies(cookie_list)
  812. if not self.page:
  813. raise Exception("Page not initialized")
  814. print(f"[{self.platform_name}] 访问笔记管理页面...", flush=True)
  815. try:
  816. await self.page.goto("https://creator.xiaohongshu.com/new/note-manager", wait_until="domcontentloaded", timeout=30000)
  817. except Exception as nav_error:
  818. print(f"[{self.platform_name}] 导航超时,但继续尝试: {nav_error}", flush=True)
  819. current_url = self.page.url
  820. print(f"[{self.platform_name}] 当前页面: {current_url}", flush=True)
  821. if "login" in current_url:
  822. raise Exception("Cookie 已过期,请重新登录")
  823. async def fetch_notes_page(p):
  824. return await self.page.evaluate(
  825. """async (pageNum) => {
  826. try {
  827. const url = `https://edith.xiaohongshu.com/web_api/sns/v5/creator/note/user/posted?tab=0&page=${pageNum}`;
  828. const headers = { 'Accept': 'application/json' };
  829. if (typeof window !== 'undefined' && typeof window._webmsxyw === 'function') {
  830. try {
  831. const sign = window._webmsxyw(url, '');
  832. headers['x-s'] = sign['X-s'];
  833. headers['x-t'] = String(sign['X-t']);
  834. } catch (e) {
  835. // ignore sign errors and fallback
  836. }
  837. }
  838. const res = await fetch(url, {
  839. method: 'GET',
  840. credentials: 'include',
  841. headers
  842. });
  843. return await res.json();
  844. } catch (e) {
  845. return { success: false, error: e.toString() };
  846. }
  847. }""",
  848. p
  849. )
  850. def parse_notes(notes_list):
  851. parsed = []
  852. for note in notes_list:
  853. note_id = note.get('id', '')
  854. if not note_id:
  855. continue
  856. cover_url = ''
  857. images_list = note.get('images_list', [])
  858. if images_list:
  859. cover_url = images_list[0].get('url', '')
  860. if cover_url.startswith('http://'):
  861. cover_url = cover_url.replace('http://', 'https://')
  862. duration = note.get('video_info', {}).get('duration', 0)
  863. status = 'published'
  864. tab_status = note.get('tab_status', 1)
  865. if tab_status == 0:
  866. status = 'draft'
  867. elif tab_status == 2:
  868. status = 'reviewing'
  869. elif tab_status == 3:
  870. status = 'rejected'
  871. parsed.append(WorkItem(
  872. work_id=note_id,
  873. title=note.get('display_title', '') or '无标题',
  874. cover_url=cover_url,
  875. duration=duration,
  876. status=status,
  877. publish_time=note.get('time', ''),
  878. play_count=note.get('view_count', 0),
  879. like_count=note.get('likes', 0),
  880. comment_count=note.get('comments_count', 0),
  881. share_count=note.get('shared_count', 0),
  882. collect_count=note.get('collected_count', 0),
  883. ))
  884. return parsed
  885. async def collect_by_scrolling() -> WorksResult:
  886. print(f"[{self.platform_name}] 直连接口被拒绝,切换为滚动页面 + 监听 API 响应模式", flush=True)
  887. captured: List[WorkItem] = []
  888. captured_total = 0
  889. captured_seen = set()
  890. lock = asyncio.Lock()
  891. async def handle_response(response):
  892. nonlocal captured_total
  893. url = response.url
  894. if "edith.xiaohongshu.com" not in url or "creator/note/user/posted" not in url:
  895. return
  896. try:
  897. json_data = await response.json()
  898. except Exception:
  899. return
  900. if not isinstance(json_data, dict):
  901. return
  902. if not (json_data.get("success") or json_data.get("code") == 0) or not json_data.get("data"):
  903. return
  904. data = json_data.get("data", {}) or {}
  905. notes = data.get("notes", []) or []
  906. tags = data.get("tags", []) or []
  907. declared = 0
  908. if tags:
  909. preferred = 0
  910. for tag in tags:
  911. if tag.get("id") == "special.note_time_desc":
  912. preferred = tag.get("notes_count", 0) or tag.get("notesCount", 0) or tag.get("count", 0) or 0
  913. break
  914. if preferred:
  915. declared = int(preferred)
  916. else:
  917. 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])
  918. if not declared:
  919. declared = int(data.get("total", 0) or data.get("total_count", 0) or data.get("totalCount", 0) or 0)
  920. if not declared and isinstance(data.get("page", {}), dict):
  921. declared = int(data.get("page", {}).get("total", 0) or data.get("page", {}).get("totalCount", 0) or 0)
  922. async with lock:
  923. if declared:
  924. captured_total = max(captured_total, declared)
  925. parsed = parse_notes(notes)
  926. new_count = 0
  927. for w in parsed:
  928. if w.work_id and w.work_id not in captured_seen:
  929. captured_seen.add(w.work_id)
  930. captured.append(w)
  931. new_count += 1
  932. if new_count > 0:
  933. print(
  934. f"[{self.platform_name}] 捕获 notes 响应: notes={len(notes)}, new={new_count}, total_now={len(captured)}, declared_total={captured_total}",
  935. flush=True
  936. )
  937. self.page.on("response", handle_response)
  938. try:
  939. try:
  940. await self.page.goto("https://creator.xiaohongshu.com/new/note-manager", wait_until="networkidle", timeout=60000)
  941. except Exception as nav_error:
  942. print(f"[{self.platform_name}] 导航异常(继续):{nav_error}", flush=True)
  943. await asyncio.sleep(2.0)
  944. idle_rounds = 0
  945. last_count = 0
  946. last_height = 0
  947. for _ in range(1, 400):
  948. scroll_state = await self.page.evaluate(
  949. """() => {
  950. const isScrollable = (el) => {
  951. if (!el) return false;
  952. const style = window.getComputedStyle(el);
  953. const oy = style.overflowY;
  954. return (oy === 'auto' || oy === 'scroll') && (el.scrollHeight - el.clientHeight > 200);
  955. };
  956. const pickBest = () => {
  957. const nodes = Array.from(document.querySelectorAll('*'));
  958. let best = document.scrollingElement || document.documentElement || document.body;
  959. let bestScroll = (best.scrollHeight || 0) - (best.clientHeight || 0);
  960. for (const el of nodes) {
  961. if (!isScrollable(el)) continue;
  962. const diff = el.scrollHeight - el.clientHeight;
  963. if (diff > bestScroll) {
  964. best = el;
  965. bestScroll = diff;
  966. }
  967. }
  968. return best;
  969. };
  970. const el = pickBest();
  971. const beforeTop = el.scrollTop || 0;
  972. const beforeHeight = el.scrollHeight || 0;
  973. el.scrollTo(0, beforeHeight);
  974. return {
  975. beforeTop,
  976. afterTop: el.scrollTop || 0,
  977. height: el.scrollHeight || 0,
  978. client: el.clientHeight || 0,
  979. };
  980. }"""
  981. )
  982. await asyncio.sleep(1.2)
  983. async with lock:
  984. count_now = len(captured)
  985. total_now = captured_total
  986. if total_now and count_now >= total_now:
  987. break
  988. height_now = int(scroll_state.get("height", 0) or 0) if isinstance(scroll_state, dict) else 0
  989. if count_now == last_count and height_now == last_height:
  990. idle_rounds += 1
  991. else:
  992. idle_rounds = 0
  993. last_count = count_now
  994. last_height = height_now
  995. if idle_rounds >= 6:
  996. break
  997. async with lock:
  998. final_works = list(captured)
  999. final_total = captured_total or len(final_works)
  1000. return WorksResult(
  1001. success=True,
  1002. platform=self.platform_name,
  1003. works=final_works,
  1004. total=final_total,
  1005. has_more=False,
  1006. next_page=-1
  1007. )
  1008. finally:
  1009. try:
  1010. self.page.remove_listener("response", handle_response)
  1011. except Exception:
  1012. pass
  1013. iters = 0
  1014. while iters < max_iters:
  1015. iters += 1
  1016. resp = await fetch_notes_page(cursor)
  1017. if not resp or not isinstance(resp, dict):
  1018. print(f"[{self.platform_name}] 第 {iters} 次拉取无响应,cursor={cursor}", flush=True)
  1019. break
  1020. if not (resp.get('success') or resp.get('code') == 0) or not resp.get('data'):
  1021. print(f"[{self.platform_name}] 拉取失败 cursor={cursor}: {str(resp)[:200]}", flush=True)
  1022. if iters == 1:
  1023. return await collect_by_scrolling()
  1024. break
  1025. data = resp.get('data', {}) or {}
  1026. notes = data.get('notes', []) or []
  1027. if not notes:
  1028. print(f"[{self.platform_name}] cursor={cursor} 无作品,停止", flush=True)
  1029. break
  1030. tags = data.get('tags', []) or []
  1031. if tags:
  1032. preferred = 0
  1033. for tag in tags:
  1034. if tag.get('id') == 'special.note_time_desc':
  1035. preferred = tag.get('notes_count', 0) or tag.get('notesCount', 0) or tag.get('count', 0) or 0
  1036. break
  1037. if preferred:
  1038. total = max(total, int(preferred))
  1039. else:
  1040. total = max(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]))
  1041. if not total:
  1042. t2 = int(data.get('total', 0) or data.get('total_count', 0) or data.get('totalCount', 0) or 0)
  1043. if not t2 and isinstance(data.get('page', {}), dict):
  1044. t2 = int(data.get('page', {}).get('total', 0) or data.get('page', {}).get('totalCount', 0) or 0)
  1045. total = max(total, t2)
  1046. parsed = parse_notes(notes)
  1047. new_items = []
  1048. for w in parsed:
  1049. if w.work_id and w.work_id not in seen_ids:
  1050. seen_ids.add(w.work_id)
  1051. new_items.append(w)
  1052. works.extend(new_items)
  1053. print(f"[{self.platform_name}] cursor={cursor} got={len(notes)}, new={len(new_items)}, total_now={len(works)}, declared_total={total}", flush=True)
  1054. if total and len(works) >= total:
  1055. break
  1056. if len(new_items) == 0:
  1057. break
  1058. next_page = data.get('page', "")
  1059. if next_page == cursor:
  1060. next_page = ""
  1061. if next_page == -1 or str(next_page) == "-1":
  1062. next_page = ""
  1063. if next_page is None or next_page == "":
  1064. if isinstance(cursor, int):
  1065. cursor = cursor + 1
  1066. else:
  1067. cursor = len(works) // api_page_size
  1068. else:
  1069. cursor = next_page
  1070. await asyncio.sleep(0.5)
  1071. except Exception as e:
  1072. import traceback
  1073. print(f"[{self.platform_name}] 发生异常: {e}", flush=True)
  1074. traceback.print_exc()
  1075. return WorksResult(success=False, platform=self.platform_name, error=str(e))
  1076. finally:
  1077. await self.close_browser()
  1078. return WorksResult(
  1079. success=True,
  1080. platform=self.platform_name,
  1081. works=works,
  1082. total=total or len(works),
  1083. has_more=False,
  1084. next_page=-1
  1085. )
  1086. async def get_comments(self, cookies: str, work_id: str, cursor: str = "") -> CommentsResult:
  1087. """获取小红书作品评论 - 通过创作者后台评论管理页面"""
  1088. print(f"\n{'='*60}")
  1089. print(f"[{self.platform_name}] 获取作品评论")
  1090. print(f"[{self.platform_name}] work_id={work_id}, cursor={cursor}")
  1091. print(f"{'='*60}")
  1092. comments: List[CommentItem] = []
  1093. total = 0
  1094. has_more = False
  1095. next_cursor = ""
  1096. captured_data = {}
  1097. try:
  1098. await self.init_browser()
  1099. cookie_list = self.parse_cookies(cookies)
  1100. await self.set_cookies(cookie_list)
  1101. if not self.page:
  1102. raise Exception("Page not initialized")
  1103. # 设置 API 响应监听器
  1104. async def handle_response(response):
  1105. nonlocal captured_data
  1106. url = response.url
  1107. # 监听评论相关 API - 创作者后台和普通页面的 API
  1108. if '/comment/' in url and ('page' in url or 'list' in url):
  1109. try:
  1110. json_data = await response.json()
  1111. print(f"[{self.platform_name}] 捕获到评论 API: {url[:100]}...", flush=True)
  1112. if json_data.get('success') or json_data.get('code') == 0:
  1113. data = json_data.get('data', {})
  1114. comment_list = data.get('comments') or data.get('list') or []
  1115. if comment_list:
  1116. captured_data = json_data
  1117. print(f"[{self.platform_name}] 评论 API 响应成功,comments={len(comment_list)}", flush=True)
  1118. else:
  1119. print(f"[{self.platform_name}] 评论 API 响应成功但无评论", flush=True)
  1120. except Exception as e:
  1121. print(f"[{self.platform_name}] 解析评论响应失败: {e}", flush=True)
  1122. self.page.on('response', handle_response)
  1123. print(f"[{self.platform_name}] 已注册评论 API 响应监听器", flush=True)
  1124. # 访问创作者后台评论管理页面
  1125. comment_url = "https://creator.xiaohongshu.com/creator/comment"
  1126. print(f"[{self.platform_name}] 访问评论管理页面: {comment_url}", flush=True)
  1127. await self.page.goto(comment_url, wait_until="domcontentloaded", timeout=30000)
  1128. await asyncio.sleep(5)
  1129. # 检查是否被重定向到登录页
  1130. current_url = self.page.url
  1131. print(f"[{self.platform_name}] 当前页面 URL: {current_url}", flush=True)
  1132. if "login" in current_url:
  1133. raise Exception("Cookie 已过期,请重新登录")
  1134. # 等待评论加载
  1135. if not captured_data:
  1136. print(f"[{self.platform_name}] 等待评论 API 响应...", flush=True)
  1137. # 尝试滚动页面触发评论加载
  1138. await self.page.evaluate('window.scrollBy(0, 500)')
  1139. await asyncio.sleep(3)
  1140. if not captured_data:
  1141. # 再等待一会,可能评论 API 加载较慢
  1142. print(f"[{self.platform_name}] 继续等待评论加载...", flush=True)
  1143. await asyncio.sleep(5)
  1144. # 移除监听器
  1145. self.page.remove_listener('response', handle_response)
  1146. # 解析评论数据
  1147. if captured_data:
  1148. data = captured_data.get('data', {})
  1149. comment_list = data.get('comments') or data.get('list') or []
  1150. has_more = data.get('has_more', False)
  1151. next_cursor = data.get('cursor', '')
  1152. print(f"[{self.platform_name}] 解析评论: has_more={has_more}, comments={len(comment_list)}", flush=True)
  1153. for comment in comment_list:
  1154. cid = comment.get('id', '')
  1155. if not cid:
  1156. continue
  1157. user_info = comment.get('user_info', {})
  1158. # 解析子评论
  1159. replies = []
  1160. sub_comments = comment.get('sub_comments', []) or []
  1161. for sub in sub_comments:
  1162. sub_user = sub.get('user_info', {})
  1163. replies.append(CommentItem(
  1164. comment_id=sub.get('id', ''),
  1165. work_id=work_id,
  1166. content=sub.get('content', ''),
  1167. author_id=sub_user.get('user_id', ''),
  1168. author_name=sub_user.get('nickname', ''),
  1169. author_avatar=sub_user.get('image', ''),
  1170. like_count=sub.get('like_count', 0),
  1171. create_time=sub.get('create_time', ''),
  1172. ))
  1173. comments.append(CommentItem(
  1174. comment_id=cid,
  1175. work_id=work_id,
  1176. content=comment.get('content', ''),
  1177. author_id=user_info.get('user_id', ''),
  1178. author_name=user_info.get('nickname', ''),
  1179. author_avatar=user_info.get('image', ''),
  1180. like_count=comment.get('like_count', 0),
  1181. reply_count=comment.get('sub_comment_count', 0),
  1182. create_time=comment.get('create_time', ''),
  1183. replies=replies,
  1184. ))
  1185. total = len(comments)
  1186. print(f"[{self.platform_name}] 解析到 {total} 条评论", flush=True)
  1187. else:
  1188. print(f"[{self.platform_name}] 未捕获到评论 API 响应", flush=True)
  1189. except Exception as e:
  1190. import traceback
  1191. traceback.print_exc()
  1192. return CommentsResult(
  1193. success=False,
  1194. platform=self.platform_name,
  1195. work_id=work_id,
  1196. error=str(e)
  1197. )
  1198. finally:
  1199. await self.close_browser()
  1200. result = CommentsResult(
  1201. success=True,
  1202. platform=self.platform_name,
  1203. work_id=work_id,
  1204. comments=comments,
  1205. total=total,
  1206. has_more=has_more
  1207. )
  1208. result.__dict__['cursor'] = next_cursor
  1209. return result
  1210. async def get_all_comments(self, cookies: str) -> dict:
  1211. """获取所有作品的评论 - 通过评论管理页面"""
  1212. print(f"\n{'='*60}")
  1213. print(f"[{self.platform_name}] 获取所有作品评论")
  1214. print(f"{'='*60}")
  1215. all_work_comments = []
  1216. captured_comments = []
  1217. captured_notes = {} # note_id -> note_info
  1218. try:
  1219. await self.init_browser()
  1220. cookie_list = self.parse_cookies(cookies)
  1221. await self.set_cookies(cookie_list)
  1222. if not self.page:
  1223. raise Exception("Page not initialized")
  1224. # 设置 API 响应监听器
  1225. async def handle_response(response):
  1226. nonlocal captured_comments, captured_notes
  1227. url = response.url
  1228. try:
  1229. # 监听评论列表 API - 多种格式
  1230. if '/comment/' in url and ('page' in url or 'list' in url):
  1231. json_data = await response.json()
  1232. print(f"[{self.platform_name}] 捕获到评论 API: {url[:100]}...", flush=True)
  1233. if json_data.get('success') or json_data.get('code') == 0:
  1234. data = json_data.get('data', {})
  1235. comments = data.get('comments', []) or data.get('list', [])
  1236. # 从 URL 中提取 note_id
  1237. import re
  1238. note_id_match = re.search(r'note_id=([^&]+)', url)
  1239. note_id = note_id_match.group(1) if note_id_match else ''
  1240. if comments:
  1241. for comment in comments:
  1242. # 添加 note_id 到评论中
  1243. if note_id and 'note_id' not in comment:
  1244. comment['note_id'] = note_id
  1245. captured_comments.append(comment)
  1246. print(f"[{self.platform_name}] 捕获到 {len(comments)} 条评论 (note_id={note_id}),总计: {len(captured_comments)}", flush=True)
  1247. # 监听笔记列表 API
  1248. if '/note/' in url and ('list' in url or 'posted' in url or 'manager' in url):
  1249. json_data = await response.json()
  1250. if json_data.get('success') or json_data.get('code') == 0:
  1251. data = json_data.get('data', {})
  1252. notes = data.get('notes', []) or data.get('list', [])
  1253. print(f"[{self.platform_name}] 捕获到笔记列表 API: {len(notes)} 个笔记", flush=True)
  1254. for note in notes:
  1255. note_id = note.get('note_id', '') or note.get('id', '')
  1256. if note_id:
  1257. cover_url = ''
  1258. cover = note.get('cover', {})
  1259. if isinstance(cover, dict):
  1260. cover_url = cover.get('url', '') or cover.get('url_default', '')
  1261. elif isinstance(cover, str):
  1262. cover_url = cover
  1263. captured_notes[note_id] = {
  1264. 'title': note.get('title', '') or note.get('display_title', ''),
  1265. 'cover': cover_url,
  1266. }
  1267. except Exception as e:
  1268. print(f"[{self.platform_name}] 解析响应失败: {e}", flush=True)
  1269. self.page.on('response', handle_response)
  1270. print(f"[{self.platform_name}] 已注册 API 响应监听器", flush=True)
  1271. # 访问评论管理页面
  1272. print(f"[{self.platform_name}] 访问评论管理页面...", flush=True)
  1273. await self.page.goto("https://creator.xiaohongshu.com/creator/comment", wait_until="domcontentloaded", timeout=30000)
  1274. await asyncio.sleep(5)
  1275. # 检查登录状态
  1276. current_url = self.page.url
  1277. if "login" in current_url:
  1278. raise Exception("Cookie 已过期,请重新登录")
  1279. print(f"[{self.platform_name}] 页面加载完成,当前捕获: {len(captured_comments)} 条评论, {len(captured_notes)} 个笔记", flush=True)
  1280. # 滚动加载更多评论
  1281. for i in range(5):
  1282. await self.page.evaluate('window.scrollBy(0, 500)')
  1283. await asyncio.sleep(1)
  1284. await asyncio.sleep(3)
  1285. # 移除监听器
  1286. self.page.remove_listener('response', handle_response)
  1287. print(f"[{self.platform_name}] 最终捕获: {len(captured_comments)} 条评论, {len(captured_notes)} 个笔记", flush=True)
  1288. # 按作品分组评论
  1289. work_comments_map = {} # note_id -> work_comments
  1290. for comment in captured_comments:
  1291. # 获取笔记信息
  1292. note_info = comment.get('note_info', {}) or comment.get('note', {})
  1293. note_id = comment.get('note_id', '') or note_info.get('note_id', '') or note_info.get('id', '')
  1294. if not note_id:
  1295. continue
  1296. if note_id not in work_comments_map:
  1297. saved_note = captured_notes.get(note_id, {})
  1298. cover_url = ''
  1299. cover = note_info.get('cover', {})
  1300. if isinstance(cover, dict):
  1301. cover_url = cover.get('url', '') or cover.get('url_default', '')
  1302. elif isinstance(cover, str):
  1303. cover_url = cover
  1304. if not cover_url:
  1305. cover_url = saved_note.get('cover', '')
  1306. work_comments_map[note_id] = {
  1307. 'work_id': note_id,
  1308. 'title': note_info.get('title', '') or note_info.get('display_title', '') or saved_note.get('title', ''),
  1309. 'cover_url': cover_url,
  1310. 'comments': []
  1311. }
  1312. cid = comment.get('id', '') or comment.get('comment_id', '')
  1313. if not cid:
  1314. continue
  1315. user_info = comment.get('user_info', {}) or comment.get('user', {})
  1316. work_comments_map[note_id]['comments'].append({
  1317. 'comment_id': cid,
  1318. 'author_id': user_info.get('user_id', '') or user_info.get('id', ''),
  1319. 'author_name': user_info.get('nickname', '') or user_info.get('name', ''),
  1320. 'author_avatar': user_info.get('image', '') or user_info.get('avatar', ''),
  1321. 'content': comment.get('content', ''),
  1322. 'like_count': comment.get('like_count', 0),
  1323. 'create_time': comment.get('create_time', ''),
  1324. })
  1325. all_work_comments = list(work_comments_map.values())
  1326. total_comments = sum(len(w['comments']) for w in all_work_comments)
  1327. print(f"[{self.platform_name}] 获取到 {len(all_work_comments)} 个作品的 {total_comments} 条评论", flush=True)
  1328. except Exception as e:
  1329. import traceback
  1330. traceback.print_exc()
  1331. return {
  1332. 'success': False,
  1333. 'platform': self.platform_name,
  1334. 'error': str(e),
  1335. 'work_comments': []
  1336. }
  1337. finally:
  1338. await self.close_browser()
  1339. return {
  1340. 'success': True,
  1341. 'platform': self.platform_name,
  1342. 'work_comments': all_work_comments,
  1343. 'total': len(all_work_comments)
  1344. }