weixin.py 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790
  1. # -*- coding: utf-8 -*-
  2. """
  3. 微信视频号发布器
  4. 参考: matrix/tencent_uploader/main.py
  5. """
  6. import asyncio
  7. import json
  8. import os
  9. from datetime import datetime
  10. from typing import List
  11. from .base import (
  12. BasePublisher, PublishParams, PublishResult,
  13. WorkItem, WorksResult, CommentItem, CommentsResult
  14. )
  15. import os
  16. import time
  17. # 允许通过环境变量手动指定“上传视频入口”的选择器,便于在页面结构频繁变更时快速调整
  18. WEIXIN_UPLOAD_SELECTOR = os.environ.get("WEIXIN_UPLOAD_SELECTOR", "").strip()
  19. def format_short_title(origin_title: str) -> str:
  20. """
  21. 格式化短标题
  22. - 移除特殊字符
  23. - 长度限制在 6-16 字符
  24. """
  25. allowed_special_chars = "《》"":+?%°"
  26. filtered_chars = [
  27. char if char.isalnum() or char in allowed_special_chars
  28. else ' ' if char == ',' else ''
  29. for char in origin_title
  30. ]
  31. formatted_string = ''.join(filtered_chars)
  32. if len(formatted_string) > 16:
  33. formatted_string = formatted_string[:16]
  34. elif len(formatted_string) < 6:
  35. formatted_string += ' ' * (6 - len(formatted_string))
  36. return formatted_string
  37. class WeixinPublisher(BasePublisher):
  38. """
  39. 微信视频号发布器
  40. 使用 Playwright 自动化操作视频号创作者中心
  41. 注意: 需要使用 Chrome 浏览器,否则可能出现 H264 编码错误
  42. """
  43. platform_name = "weixin"
  44. login_url = "https://channels.weixin.qq.com/platform"
  45. publish_url = "https://channels.weixin.qq.com/platform/post/create"
  46. cookie_domain = ".weixin.qq.com"
  47. def _parse_count(self, count_str: str) -> int:
  48. """解析数字(支持带'万'的格式)"""
  49. try:
  50. count_str = count_str.strip()
  51. if '万' in count_str:
  52. return int(float(count_str.replace('万', '')) * 10000)
  53. return int(count_str)
  54. except:
  55. return 0
  56. async def ai_find_upload_selector(self, frame_html: str, frame_name: str = "main") -> str:
  57. """
  58. 使用 AI 从 HTML 中识别“上传视频/选择文件”相关元素的 CSS 选择器。
  59. 设计思路:
  60. - 仅在常规 DOM 选择器都失败时调用,避免频繁占用 AI 配额;
  61. - 通过 DashScope 文本模型(与验证码识别同一套配置)分析 HTML;
  62. - 返回一个适合用于 frame.locator(selector) 的 CSS 选择器。
  63. """
  64. import json
  65. import re
  66. import requests
  67. import os
  68. # 避免 HTML 过长导致 token 超限,只截取前 N 字符
  69. if not frame_html:
  70. return ""
  71. max_len = 20000
  72. if len(frame_html) > max_len:
  73. frame_html = frame_html[:max_len]
  74. ai_api_key = os.environ.get("DASHSCOPE_API_KEY", "")
  75. ai_base_url = os.environ.get("DASHSCOPE_BASE_URL", "https://dashscope.aliyuncs.com/compatible-mode/v1")
  76. ai_text_model = os.environ.get("AI_TEXT_MODEL", "qwen-plus")
  77. if not ai_api_key:
  78. print(f"[{self.platform_name}] AI上传入口识别: 未配置 AI API Key,跳过")
  79. return ""
  80. prompt = f"""
  81. 你是熟悉微信视频号后台的前端工程师,现在需要在一段 HTML 中找到“上传视频文件”的入口。
  82. 页面说明:
  83. - 平台:微信视频号(channels.weixin.qq.com)
  84. - 目标:用于上传视频文件的按钮或 input(一般会触发文件选择框)
  85. - 你会收到某个 frame 的完整 HTML 片段(不包含截图)。
  86. 请你根据下面的 HTML,推断最适合用于上传视频文件的元素,并输出一个可以被 Playwright 使用的 CSS 选择器。
  87. 要求:
  88. 1. 只考虑“上传/选择视频文件”的入口,不要返回“发布/发表/下一步”等按钮;
  89. 2. 选择器需要尽量稳定,不要使用自动生成的随机类名(例如带很多随机字母/数字的类名可以用前缀匹配);
  90. 3. 选择器必须是 CSS 选择器(不要返回 XPath);
  91. 4. 如果确实找不到合理的上传入口,返回 selector 为空字符串。
  92. 请以 JSON 格式输出,严格遵守以下结构(不要添加任何解释文字):
  93. ```json
  94. {{
  95. "selector": "CSS 选择器字符串,比如:input[type='file'] 或 div.upload-content input[type='file']"
  96. }}
  97. ```
  98. 下面是 frame=\"{frame_name}\" 的 HTML:
  99. ```html
  100. {frame_html}
  101. ```"""
  102. payload = {
  103. "model": ai_text_model,
  104. "messages": [
  105. {
  106. "role": "user",
  107. "content": prompt,
  108. }
  109. ],
  110. "max_tokens": 600,
  111. }
  112. headers = {
  113. "Authorization": f"Bearer {ai_api_key}",
  114. "Content-Type": "application/json",
  115. }
  116. try:
  117. print(f"[{self.platform_name}] AI上传入口识别: 正在分析 frame={frame_name} HTML...")
  118. resp = requests.post(
  119. f"{ai_base_url}/chat/completions",
  120. headers=headers,
  121. json=payload,
  122. timeout=40,
  123. )
  124. if resp.status_code != 200:
  125. print(f"[{self.platform_name}] AI上传入口识别: API 返回错误 {resp.status_code}")
  126. return ""
  127. data = resp.json()
  128. content = data.get("choices", [{}])[0].get("message", {}).get("content", "") or ""
  129. # 尝试从 ```json``` 代码块中解析
  130. json_match = re.search(r"```json\\s*([\\s\\S]*?)\\s*```", content)
  131. if json_match:
  132. json_str = json_match.group(1)
  133. else:
  134. json_match = re.search(r"\\{[\\s\\S]*\\}", content)
  135. json_str = json_match.group(0) if json_match else "{}"
  136. try:
  137. result = json.loads(json_str)
  138. except Exception:
  139. result = {}
  140. selector = (result.get("selector") or "").strip()
  141. print(f"[{self.platform_name}] AI上传入口识别结果: selector='{selector}'")
  142. return selector
  143. except Exception as e:
  144. print(f"[{self.platform_name}] AI上传入口识别异常: {e}")
  145. return ""
  146. async def ai_pick_selector_from_candidates(self, candidates: list, goal: str, frame_name: str = "main") -> str:
  147. """
  148. 将“候选元素列表(包含 css selector + 文本/属性)”发给 AI,让 AI 直接挑选最符合 goal 的元素。
  149. 适用于:HTML 里看不出上传入口、或页面大量动态渲染时。
  150. """
  151. import json
  152. import re
  153. import requests
  154. import os
  155. if not candidates:
  156. return ""
  157. ai_api_key = os.environ.get("DASHSCOPE_API_KEY", "")
  158. ai_base_url = os.environ.get("DASHSCOPE_BASE_URL", "https://dashscope.aliyuncs.com/compatible-mode/v1")
  159. ai_text_model = os.environ.get("AI_TEXT_MODEL", "qwen-plus")
  160. if not ai_api_key:
  161. print(f"[{self.platform_name}] AI候选选择器: 未配置 AI API Key,跳过")
  162. return ""
  163. # 控制长度,最多取前 120 个候选
  164. candidates = candidates[:120]
  165. prompt = f"""
  166. 你是自动化发布工程师。现在要在微信视频号(channels.weixin.qq.com)发布页面里找到“{goal}”相关的入口元素。
  167. 我会给你一组候选元素,每个候选都包含:
  168. - css: 可直接用于 Playwright 的 CSS 选择器
  169. - tag / type / role / ariaLabel / text / id / className(部分字段可能为空)
  170. 你的任务:
  171. - 从候选中选出最可能用于“{goal}”的元素,返回它的 css 选择器;
  172. - 如果没有任何候选符合,返回空字符串。
  173. 注意:
  174. - 如果 goal 是“上传视频入口”,优先选择 input[type=file] 或看起来会触发选择文件/上传的区域;
  175. - 不要选择“发布/发表/下一步”等按钮(除非 goal 明确是发布按钮)。
  176. 请严格按 JSON 输出(不要解释):
  177. ```json
  178. {{ "selector": "..." }}
  179. ```
  180. 候选列表(frame={frame_name}):
  181. ```json
  182. {json.dumps(candidates, ensure_ascii=False)}
  183. ```"""
  184. payload = {
  185. "model": ai_text_model,
  186. "messages": [{"role": "user", "content": prompt}],
  187. "max_tokens": 400,
  188. }
  189. headers = {
  190. "Authorization": f"Bearer {ai_api_key}",
  191. "Content-Type": "application/json",
  192. }
  193. try:
  194. print(f"[{self.platform_name}] AI候选选择器: 正在分析 frame={frame_name}, goal={goal} ...")
  195. resp = requests.post(
  196. f"{ai_base_url}/chat/completions",
  197. headers=headers,
  198. json=payload,
  199. timeout=40,
  200. )
  201. if resp.status_code != 200:
  202. print(f"[{self.platform_name}] AI候选选择器: API 返回错误 {resp.status_code}")
  203. return ""
  204. data = resp.json()
  205. content = data.get("choices", [{}])[0].get("message", {}).get("content", "") or ""
  206. json_match = re.search(r"```json\\s*([\\s\\S]*?)\\s*```", content)
  207. if json_match:
  208. json_str = json_match.group(1)
  209. else:
  210. json_match = re.search(r"\\{[\\s\\S]*\\}", content)
  211. json_str = json_match.group(0) if json_match else "{}"
  212. try:
  213. result = json.loads(json_str)
  214. except Exception:
  215. result = {}
  216. selector = (result.get("selector") or "").strip()
  217. print(f"[{self.platform_name}] AI候选选择器结果: selector='{selector}'")
  218. return selector
  219. except Exception as e:
  220. print(f"[{self.platform_name}] AI候选选择器异常: {e}")
  221. return ""
  222. async def _extract_relevant_html_snippets(self, html: str) -> str:
  223. """
  224. 从 HTML 中抽取与上传相关的片段,减少 token,提升 AI 命中率。
  225. - 优先抓取包含 upload/上传/file/input 等关键词的窗口片段
  226. - 若未命中关键词,返回“开头 + 结尾”的拼接
  227. """
  228. import re
  229. if not html:
  230. return ""
  231. patterns = [
  232. r"upload",
  233. r"uploader",
  234. r"file",
  235. r"type\\s*=\\s*['\\\"]file['\\\"]",
  236. r"input",
  237. r"drag",
  238. r"drop",
  239. r"选择",
  240. r"上传",
  241. r"添加",
  242. r"视频",
  243. ]
  244. regex = re.compile("|".join(patterns), re.IGNORECASE)
  245. snippets = []
  246. for m in regex.finditer(html):
  247. start = max(0, m.start() - 350)
  248. end = min(len(html), m.end() + 350)
  249. snippets.append(html[start:end])
  250. if len(snippets) >= 18:
  251. break
  252. if snippets:
  253. # 去重(粗略)
  254. unique = []
  255. seen = set()
  256. for s in snippets:
  257. key = hash(s)
  258. if key not in seen:
  259. seen.add(key)
  260. unique.append(s)
  261. return "\n\n<!-- SNIPPET -->\n\n".join(unique)[:20000]
  262. # fallback: head + tail
  263. head = html[:9000]
  264. tail = html[-9000:] if len(html) > 9000 else ""
  265. return (head + "\n\n<!-- TAIL -->\n\n" + tail)[:20000]
  266. async def init_browser(self, storage_state: str = None):
  267. """初始化浏览器 - 参考 matrix 使用 channel=chrome 避免 H264 编码错误"""
  268. from playwright.async_api import async_playwright
  269. playwright = await async_playwright().start()
  270. # 参考 matrix: 使用系统内的 Chrome 浏览器,避免 H264 编码错误
  271. # 如果没有安装 Chrome,则使用默认 Chromium
  272. try:
  273. self.browser = await playwright.chromium.launch(
  274. # headless=self.headless,
  275. headless=False,
  276. channel="chrome" # 使用系统 Chrome
  277. )
  278. print(f"[{self.platform_name}] 使用系统 Chrome 浏览器")
  279. except Exception as e:
  280. print(f"[{self.platform_name}] Chrome 不可用,使用 Chromium: {e}")
  281. self.browser = await playwright.chromium.launch(headless=self.headless)
  282. # 设置 HTTP Headers 防止重定向
  283. headers = {
  284. "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
  285. "Referer": "https://channels.weixin.qq.com/platform/post/list",
  286. }
  287. self.context = await self.browser.new_context(
  288. extra_http_headers=headers,
  289. ignore_https_errors=True,
  290. viewport={"width": 1920, "height": 1080},
  291. user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
  292. )
  293. self.page = await self.context.new_page()
  294. return self.page
  295. async def set_schedule_time(self, publish_date: datetime):
  296. """设置定时发布"""
  297. if not self.page:
  298. return
  299. print(f"[{self.platform_name}] 设置定时发布...")
  300. # 点击定时选项
  301. label_element = self.page.locator("label").filter(has_text="定时").nth(1)
  302. await label_element.click()
  303. # 选择日期
  304. await self.page.click('input[placeholder="请选择发表时间"]')
  305. publish_month = f"{publish_date.month:02d}"
  306. current_month = f"{publish_month}月"
  307. # 检查月份
  308. page_month = await self.page.inner_text('span.weui-desktop-picker__panel__label:has-text("月")')
  309. if page_month != current_month:
  310. await self.page.click('button.weui-desktop-btn__icon__right')
  311. # 选择日期
  312. elements = await self.page.query_selector_all('table.weui-desktop-picker__table a')
  313. for element in elements:
  314. class_name = await element.evaluate('el => el.className')
  315. if 'weui-desktop-picker__disabled' in class_name:
  316. continue
  317. text = await element.inner_text()
  318. if text.strip() == str(publish_date.day):
  319. await element.click()
  320. break
  321. # 输入时间
  322. await self.page.click('input[placeholder="请选择时间"]')
  323. await self.page.keyboard.press("Control+KeyA")
  324. await self.page.keyboard.type(str(publish_date.hour))
  325. # 点击其他地方确认
  326. await self.page.locator("div.input-editor").click()
  327. async def handle_upload_error(self, video_path: str):
  328. """处理上传错误"""
  329. if not self.page:
  330. return
  331. print(f"[{self.platform_name}] 视频出错了,重新上传中...")
  332. await self.page.locator('div.media-status-content div.tag-inner:has-text("删除")').click()
  333. await self.page.get_by_role('button', name="删除", exact=True).click()
  334. file_input = self.page.locator('input[type="file"]')
  335. await file_input.set_input_files(video_path)
  336. async def add_title_tags(self, params: PublishParams):
  337. """添加标题和话题"""
  338. if not self.page:
  339. return
  340. await self.page.locator("div.input-editor").click()
  341. await self.page.keyboard.type(params.title)
  342. if params.tags:
  343. await self.page.keyboard.press("Enter")
  344. for tag in params.tags:
  345. await self.page.keyboard.type("#" + tag)
  346. await self.page.keyboard.press("Space")
  347. print(f"[{self.platform_name}] 成功添加标题和 {len(params.tags)} 个话题")
  348. async def add_short_title(self):
  349. """添加短标题"""
  350. if not self.page:
  351. return
  352. try:
  353. short_title_element = self.page.get_by_text("短标题", exact=True).locator("..").locator(
  354. "xpath=following-sibling::div").locator('span input[type="text"]')
  355. if await short_title_element.count():
  356. # 获取已有内容作为短标题
  357. pass
  358. except:
  359. pass
  360. async def upload_cover(self, cover_path: str):
  361. """上传封面图"""
  362. if not self.page or not cover_path or not os.path.exists(cover_path):
  363. return
  364. try:
  365. await asyncio.sleep(2)
  366. preview_btn_info = await self.page.locator(
  367. 'div.finder-tag-wrap.btn:has-text("更换封面")').get_attribute('class')
  368. if "disabled" not in preview_btn_info:
  369. await self.page.locator('div.finder-tag-wrap.btn:has-text("更换封面")').click()
  370. await self.page.locator('div.single-cover-uploader-wrap > div.wrap').hover()
  371. # 删除现有封面
  372. if await self.page.locator(".del-wrap > .svg-icon").count():
  373. await self.page.locator(".del-wrap > .svg-icon").click()
  374. # 上传新封面
  375. preview_div = self.page.locator("div.single-cover-uploader-wrap > div.wrap")
  376. async with self.page.expect_file_chooser() as fc_info:
  377. await preview_div.click()
  378. preview_chooser = await fc_info.value
  379. await preview_chooser.set_files(cover_path)
  380. await asyncio.sleep(2)
  381. await self.page.get_by_role("button", name="确定").click()
  382. await asyncio.sleep(1)
  383. await self.page.get_by_role("button", name="确认").click()
  384. print(f"[{self.platform_name}] 封面上传成功")
  385. except Exception as e:
  386. print(f"[{self.platform_name}] 封面上传失败: {e}")
  387. async def check_captcha(self) -> dict:
  388. """检查页面是否需要验证码"""
  389. if not self.page:
  390. return {'need_captcha': False, 'captcha_type': ''}
  391. try:
  392. # 检查各种验证码
  393. captcha_selectors = [
  394. 'text="请输入验证码"',
  395. 'text="滑动验证"',
  396. '[class*="captcha"]',
  397. '[class*="verify"]',
  398. ]
  399. for selector in captcha_selectors:
  400. try:
  401. if await self.page.locator(selector).count() > 0:
  402. print(f"[{self.platform_name}] 检测到验证码: {selector}")
  403. return {'need_captcha': True, 'captcha_type': 'image'}
  404. except:
  405. pass
  406. # 检查登录弹窗
  407. login_selectors = [
  408. 'text="请登录"',
  409. 'text="扫码登录"',
  410. '[class*="login-dialog"]',
  411. ]
  412. for selector in login_selectors:
  413. try:
  414. if await self.page.locator(selector).count() > 0:
  415. print(f"[{self.platform_name}] 检测到需要登录: {selector}")
  416. return {'need_captcha': True, 'captcha_type': 'login'}
  417. except:
  418. pass
  419. except Exception as e:
  420. print(f"[{self.platform_name}] 验证码检测异常: {e}")
  421. return {'need_captcha': False, 'captcha_type': ''}
  422. async def publish(self, cookies: str, params: PublishParams) -> PublishResult:
  423. """发布视频到视频号"""
  424. print(f"\n{'='*60}")
  425. print(f"[{self.platform_name}] 开始发布视频")
  426. print(f"[{self.platform_name}] 视频路径: {params.video_path}")
  427. print(f"[{self.platform_name}] 标题: {params.title}")
  428. print(f"[{self.platform_name}] Headless: {self.headless}")
  429. print(f"{'='*60}")
  430. self.report_progress(5, "正在初始化浏览器...")
  431. # 初始化浏览器(使用 Chrome)
  432. await self.init_browser()
  433. print(f"[{self.platform_name}] 浏览器初始化完成")
  434. # 解析并设置 cookies
  435. cookie_list = self.parse_cookies(cookies)
  436. print(cookie_list)
  437. print(f"[{self.platform_name}] 解析到 {len(cookie_list)} 个 cookies")
  438. await self.set_cookies(cookie_list)
  439. if not self.page:
  440. raise Exception("Page not initialized")
  441. # 检查视频文件
  442. if not os.path.exists(params.video_path):
  443. raise Exception(f"视频文件不存在: {params.video_path}")
  444. print(f"[{self.platform_name}] 视频文件存在,大小: {os.path.getsize(params.video_path)} bytes")
  445. self.report_progress(10, "正在打开上传页面...")
  446. # 访问上传页面
  447. await self.page.goto(self.publish_url, wait_until="networkidle", timeout=60000)
  448. await asyncio.sleep(3)
  449. # 检查是否跳转到登录页
  450. current_url = self.page.url
  451. print(f"[{self.platform_name}] 当前页面: {current_url}")
  452. if "login" in current_url:
  453. screenshot_base64 = await self.capture_screenshot()
  454. return PublishResult(
  455. success=False,
  456. platform=self.platform_name,
  457. error="Cookie 已过期,需要重新登录",
  458. need_captcha=True,
  459. captcha_type='login',
  460. screenshot_base64=screenshot_base64,
  461. page_url=current_url,
  462. status='need_captcha'
  463. )
  464. # 使用 AI 检查验证码
  465. ai_captcha = await self.ai_check_captcha()
  466. if ai_captcha['has_captcha']:
  467. print(f"[{self.platform_name}] AI检测到验证码: {ai_captcha['captcha_type']}", flush=True)
  468. screenshot_base64 = await self.capture_screenshot()
  469. return PublishResult(
  470. success=False,
  471. platform=self.platform_name,
  472. error=f"检测到{ai_captcha['captcha_type']}验证码,需要使用有头浏览器完成验证",
  473. need_captcha=True,
  474. captcha_type=ai_captcha['captcha_type'],
  475. screenshot_base64=screenshot_base64,
  476. page_url=current_url,
  477. status='need_captcha'
  478. )
  479. # 传统方式检查验证码
  480. captcha_result = await self.check_captcha()
  481. if captcha_result['need_captcha']:
  482. screenshot_base64 = await self.capture_screenshot()
  483. return PublishResult(
  484. success=False,
  485. platform=self.platform_name,
  486. error=f"需要{captcha_result['captcha_type']}验证码,请使用有头浏览器完成验证",
  487. need_captcha=True,
  488. captcha_type=captcha_result['captcha_type'],
  489. screenshot_base64=screenshot_base64,
  490. page_url=current_url,
  491. status='need_captcha'
  492. )
  493. self.report_progress(15, "正在选择视频文件...")
  494. # 上传视频
  495. # 说明:视频号发布页在不同账号/地区/灰度下 DOM 结构差异较大,且上传组件可能在 iframe 中。
  496. # 因此这里按 matrix 的思路“点击触发 file chooser”,同时增加“遍历全部 frame + 精确挑选 video input”的兜底。
  497. upload_success = False
  498. if not self.page:
  499. raise Exception("Page not initialized")
  500. # 等待页面把上传区域渲染出来(避免过早判断)
  501. try:
  502. await self.page.wait_for_selector("div.upload-content, input[type='file'], iframe", timeout=20000)
  503. except Exception:
  504. pass
  505. async def _try_set_files_in_frame(frame, frame_name: str) -> bool:
  506. """在指定 frame 中尝试触发上传"""
  507. nonlocal upload_success
  508. if upload_success:
  509. return True
  510. # 方法0:如果用户通过环境变量显式配置了选择器,优先尝试这个
  511. if WEIXIN_UPLOAD_SELECTOR:
  512. try:
  513. el = frame.locator(WEIXIN_UPLOAD_SELECTOR).first
  514. if await el.count() > 0 and await el.is_visible():
  515. print(f"[{self.platform_name}] [{frame_name}] 使用环境变量 WEIXIN_UPLOAD_SELECTOR: {WEIXIN_UPLOAD_SELECTOR}")
  516. try:
  517. async with self.page.expect_file_chooser(timeout=5000) as fc_info:
  518. await el.click()
  519. chooser = await fc_info.value
  520. await chooser.set_files(params.video_path)
  521. upload_success = True
  522. print(f"[{self.platform_name}] [{frame_name}] 通过环境变量选择器上传成功")
  523. return True
  524. except Exception as e:
  525. print(f"[{self.platform_name}] [{frame_name}] 环境变量选择器点击失败,尝试直接 set_input_files: {e}")
  526. try:
  527. await el.set_input_files(params.video_path)
  528. upload_success = True
  529. print(f"[{self.platform_name}] [{frame_name}] 环境变量选择器 set_input_files 成功")
  530. return True
  531. except Exception as e2:
  532. print(f"[{self.platform_name}] [{frame_name}] 环境变量选择器 set_input_files 仍失败: {e2}")
  533. except Exception as e:
  534. print(f"[{self.platform_name}] [{frame_name}] 使用环境变量选择器定位元素失败: {e}")
  535. # 先尝试点击上传区域触发 chooser(最贴近 matrix)
  536. click_selectors = [
  537. "div.upload-content",
  538. "div[class*='upload-content']",
  539. "div[class*='upload']",
  540. "div.add-wrap",
  541. "[class*='uploader']",
  542. "text=点击上传",
  543. "text=上传视频",
  544. "text=选择视频",
  545. ]
  546. for selector in click_selectors:
  547. try:
  548. el = frame.locator(selector).first
  549. if await el.count() > 0 and await el.is_visible():
  550. print(f"[{self.platform_name}] [{frame_name}] 找到可点击上传区域: {selector}")
  551. try:
  552. async with self.page.expect_file_chooser(timeout=5000) as fc_info:
  553. await el.click()
  554. chooser = await fc_info.value
  555. await chooser.set_files(params.video_path)
  556. upload_success = True
  557. print(f"[{self.platform_name}] [{frame_name}] 通过 file chooser 上传成功")
  558. return True
  559. except Exception as e:
  560. print(f"[{self.platform_name}] [{frame_name}] 点击触发 chooser 失败: {e}")
  561. except Exception:
  562. pass
  563. # 再尝试直接设置 input[type=file](iframe/隐藏 input 常见)
  564. try:
  565. inputs = frame.locator("input[type='file']")
  566. cnt = await inputs.count()
  567. if cnt > 0:
  568. best_idx = 0
  569. best_score = -1
  570. for i in range(cnt):
  571. try:
  572. inp = inputs.nth(i)
  573. accept = (await inp.get_attribute("accept")) or ""
  574. multiple = (await inp.get_attribute("multiple")) or ""
  575. score = 0
  576. if "video" in accept:
  577. score += 10
  578. if "mp4" in accept:
  579. score += 3
  580. if multiple:
  581. score += 1
  582. if score > best_score:
  583. best_score = score
  584. best_idx = i
  585. except Exception:
  586. continue
  587. target = inputs.nth(best_idx)
  588. print(f"[{self.platform_name}] [{frame_name}] 尝试对 input[{best_idx}] set_input_files (score={best_score})")
  589. await target.set_input_files(params.video_path)
  590. upload_success = True
  591. print(f"[{self.platform_name}] [{frame_name}] 通过 file input 上传成功")
  592. return True
  593. except Exception as e:
  594. print(f"[{self.platform_name}] [{frame_name}] file input 上传失败: {e}")
  595. # 不直接返回,让后面的 AI 兜底有机会执行
  596. # 方法4: 兜底使用 AI 分析 HTML,猜测上传入口
  597. try:
  598. frame_url = getattr(frame, "url", "")
  599. html_full = await frame.content()
  600. html_for_ai = await self._extract_relevant_html_snippets(html_full)
  601. print(f"[{self.platform_name}] [{frame_name}] frame_url={frame_url}, html_len={len(html_full)}, html_for_ai_len={len(html_for_ai)}")
  602. ai_selector = await self.ai_find_upload_selector(html_for_ai, frame_name=frame_name)
  603. if ai_selector:
  604. try:
  605. el = frame.locator(ai_selector).first
  606. if await el.count() > 0:
  607. print(f"[{self.platform_name}] [{frame_name}] 使用 AI 选择器点击上传入口: {ai_selector}")
  608. try:
  609. async with self.page.expect_file_chooser(timeout=5000) as fc_info:
  610. await el.click()
  611. chooser = await fc_info.value
  612. await chooser.set_files(params.video_path)
  613. upload_success = True
  614. print(f"[{self.platform_name}] [{frame_name}] 通过 AI 选择器上传成功")
  615. return True
  616. except Exception as e:
  617. print(f"[{self.platform_name}] [{frame_name}] AI 选择器点击失败,改为直接 set_input_files: {e}")
  618. try:
  619. await el.set_input_files(params.video_path)
  620. upload_success = True
  621. print(f"[{self.platform_name}] [{frame_name}] AI 选择器直接 set_input_files 成功")
  622. return True
  623. except Exception as e2:
  624. print(f"[{self.platform_name}] [{frame_name}] AI 选择器 set_input_files 仍失败: {e2}")
  625. except Exception as e:
  626. print(f"[{self.platform_name}] [{frame_name}] 使用 AI 选择器定位元素失败: {e}")
  627. else:
  628. # 如果 AI 无法从 HTML 推断,退一步:构造候选元素列表交给 AI 选择
  629. try:
  630. candidates = await frame.evaluate("""
  631. () => {
  632. function cssEscape(s) {
  633. try { return CSS.escape(s); } catch (e) { return s.replace(/[^a-zA-Z0-9_-]/g, '\\\\$&'); }
  634. }
  635. function buildSelector(el) {
  636. if (!el || el.nodeType !== 1) return '';
  637. if (el.id) return `#${cssEscape(el.id)}`;
  638. let parts = [];
  639. let cur = el;
  640. for (let depth = 0; cur && cur.nodeType === 1 && depth < 5; depth++) {
  641. let part = cur.tagName.toLowerCase();
  642. const role = cur.getAttribute('role');
  643. const type = cur.getAttribute('type');
  644. if (type) part += `[type="${type}"]`;
  645. if (role) part += `[role="${role}"]`;
  646. const cls = (cur.className || '').toString().trim().split(/\\s+/).filter(Boolean);
  647. if (cls.length) part += '.' + cls.slice(0, 2).map(cssEscape).join('.');
  648. // nth-of-type
  649. let idx = 1;
  650. let sib = cur;
  651. while (sib && (sib = sib.previousElementSibling)) {
  652. if (sib.tagName === cur.tagName) idx++;
  653. }
  654. part += `:nth-of-type(${idx})`;
  655. parts.unshift(part);
  656. cur = cur.parentElement;
  657. }
  658. return parts.join(' > ');
  659. }
  660. const nodes = Array.from(document.querySelectorAll('input, button, a, div, span'))
  661. .filter(el => {
  662. const tag = el.tagName.toLowerCase();
  663. const type = (el.getAttribute('type') || '').toLowerCase();
  664. const role = (el.getAttribute('role') || '').toLowerCase();
  665. const aria = (el.getAttribute('aria-label') || '').toLowerCase();
  666. const txt = (el.innerText || '').trim().slice(0, 60);
  667. const cls = (el.className || '').toString().toLowerCase();
  668. const isFile = tag === 'input' && type === 'file';
  669. const looksClickable =
  670. tag === 'button' || tag === 'a' || role === 'button' || el.onclick ||
  671. cls.includes('upload') || cls.includes('uploader') || cls.includes('drag') ||
  672. aria.includes('上传') || aria.includes('选择') || aria.includes('添加') ||
  673. txt.includes('上传') || txt.includes('选择') || txt.includes('添加') || txt.includes('点击上传');
  674. if (!isFile && !looksClickable) return false;
  675. const r = el.getBoundingClientRect();
  676. const visible = r.width > 5 && r.height > 5;
  677. return visible;
  678. });
  679. const limited = nodes.slice(0, 120).map(el => ({
  680. css: buildSelector(el),
  681. tag: el.tagName.toLowerCase(),
  682. type: el.getAttribute('type') || '',
  683. role: el.getAttribute('role') || '',
  684. ariaLabel: el.getAttribute('aria-label') || '',
  685. text: (el.innerText || '').trim().slice(0, 80),
  686. id: el.id || '',
  687. className: (el.className || '').toString().slice(0, 120),
  688. accept: el.getAttribute('accept') || '',
  689. }));
  690. return limited;
  691. }
  692. """)
  693. ai_selector2 = await self.ai_pick_selector_from_candidates(
  694. candidates=candidates,
  695. goal="上传视频入口",
  696. frame_name=frame_name
  697. )
  698. if ai_selector2:
  699. el2 = frame.locator(ai_selector2).first
  700. if await el2.count() > 0:
  701. print(f"[{self.platform_name}] [{frame_name}] 使用 AI 候选选择器点击上传入口: {ai_selector2}")
  702. try:
  703. async with self.page.expect_file_chooser(timeout=5000) as fc_info:
  704. await el2.click()
  705. chooser2 = await fc_info.value
  706. await chooser2.set_files(params.video_path)
  707. upload_success = True
  708. print(f"[{self.platform_name}] [{frame_name}] 通过 AI 候选选择器上传成功")
  709. return True
  710. except Exception as e:
  711. print(f"[{self.platform_name}] [{frame_name}] AI 候选选择器点击失败,尝试 set_input_files: {e}")
  712. try:
  713. await el2.set_input_files(params.video_path)
  714. upload_success = True
  715. print(f"[{self.platform_name}] [{frame_name}] AI 候选选择器 set_input_files 成功")
  716. return True
  717. except Exception as e2:
  718. print(f"[{self.platform_name}] [{frame_name}] AI 候选选择器 set_input_files 仍失败: {e2}")
  719. except Exception as e:
  720. print(f"[{self.platform_name}] [{frame_name}] 构造候选并交给 AI 失败: {e}")
  721. except Exception as e:
  722. print(f"[{self.platform_name}] [{frame_name}] AI 上传入口识别整体失败: {e}")
  723. return False
  724. # 先尝试主 frame
  725. try:
  726. await _try_set_files_in_frame(self.page.main_frame, "main")
  727. except Exception as e:
  728. print(f"[{self.platform_name}] main frame 上传尝试异常: {e}")
  729. # 再遍历所有子 frame
  730. if not upload_success:
  731. try:
  732. frames = self.page.frames
  733. print(f"[{self.platform_name}] 发现 frames: {len(frames)}")
  734. for idx, fr in enumerate(frames):
  735. if upload_success:
  736. break
  737. # main_frame 已尝试过
  738. if fr == self.page.main_frame:
  739. continue
  740. name = fr.name or f"frame-{idx}"
  741. await _try_set_files_in_frame(fr, name)
  742. except Exception as e:
  743. print(f"[{self.platform_name}] 遍历 frames 异常: {e}")
  744. if not upload_success:
  745. screenshot_base64 = await self.capture_screenshot()
  746. return PublishResult(
  747. success=False,
  748. platform=self.platform_name,
  749. error="未找到上传入口(可能在 iframe 中或页面结构已变更)",
  750. screenshot_base64=screenshot_base64,
  751. page_url=await self.get_page_url(),
  752. status='failed'
  753. )
  754. self.report_progress(20, "正在填充标题和话题...")
  755. # 添加标题和话题
  756. await self.add_title_tags(params)
  757. self.report_progress(30, "等待视频上传完成...")
  758. # 等待上传完成
  759. for _ in range(120):
  760. try:
  761. button_info = await self.page.get_by_role("button", name="发表").get_attribute('class')
  762. if "weui-desktop-btn_disabled" not in button_info:
  763. print(f"[{self.platform_name}] 视频上传完毕")
  764. # 上传封面
  765. self.report_progress(50, "正在上传封面...")
  766. await self.upload_cover(params.cover_path)
  767. break
  768. else:
  769. # 检查上传错误
  770. if await self.page.locator('div.status-msg.error').count():
  771. if await self.page.locator('div.media-status-content div.tag-inner:has-text("删除")').count():
  772. await self.handle_upload_error(params.video_path)
  773. await asyncio.sleep(3)
  774. except:
  775. await asyncio.sleep(3)
  776. self.report_progress(60, "处理视频设置...")
  777. # 添加短标题
  778. try:
  779. short_title_el = self.page.get_by_text("短标题", exact=True).locator("..").locator(
  780. "xpath=following-sibling::div").locator('span input[type="text"]')
  781. if await short_title_el.count():
  782. short_title = format_short_title(params.title)
  783. await short_title_el.fill(short_title)
  784. except:
  785. pass
  786. # 定时发布
  787. if params.publish_date:
  788. self.report_progress(70, "设置定时发布...")
  789. await self.set_schedule_time(params.publish_date)
  790. self.report_progress(80, "正在发布...")
  791. # 点击发布 - 参考 matrix
  792. for i in range(30):
  793. try:
  794. # 参考 matrix: div.form-btns button:has-text("发表")
  795. publish_btn = self.page.locator('div.form-btns button:has-text("发表")')
  796. if await publish_btn.count():
  797. print(f"[{self.platform_name}] 点击发布按钮...")
  798. await publish_btn.click()
  799. # 等待跳转到作品列表页面 - 参考 matrix
  800. await self.page.wait_for_url(
  801. "https://channels.weixin.qq.com/platform/post/list",
  802. timeout=10000
  803. )
  804. self.report_progress(100, "发布成功")
  805. print(f"[{self.platform_name}] 视频发布成功!")
  806. screenshot_base64 = await self.capture_screenshot()
  807. return PublishResult(
  808. success=True,
  809. platform=self.platform_name,
  810. message="发布成功",
  811. screenshot_base64=screenshot_base64,
  812. page_url=self.page.url,
  813. status='success'
  814. )
  815. except Exception as e:
  816. current_url = self.page.url
  817. if "https://channels.weixin.qq.com/platform/post/list" in current_url:
  818. self.report_progress(100, "发布成功")
  819. print(f"[{self.platform_name}] 视频发布成功!")
  820. screenshot_base64 = await self.capture_screenshot()
  821. return PublishResult(
  822. success=True,
  823. platform=self.platform_name,
  824. message="发布成功",
  825. screenshot_base64=screenshot_base64,
  826. page_url=current_url,
  827. status='success'
  828. )
  829. else:
  830. print(f"[{self.platform_name}] 视频正在发布中... {i+1}/30, URL: {current_url}")
  831. await asyncio.sleep(1)
  832. # 发布超时
  833. screenshot_base64 = await self.capture_screenshot()
  834. page_url = await self.get_page_url()
  835. return PublishResult(
  836. success=False,
  837. platform=self.platform_name,
  838. error="发布超时,请检查发布状态",
  839. screenshot_base64=screenshot_base64,
  840. page_url=page_url,
  841. status='need_action'
  842. )
  843. async def _get_works_fallback_dom(self, page_size: int) -> tuple:
  844. """API 失败时从当前页面 DOM 抓取作品列表(兼容新账号/不同入口)"""
  845. works: List[WorkItem] = []
  846. total = 0
  847. has_more = False
  848. try:
  849. for selector in ["div.post-feed-item", "[class*='post-feed']", "[class*='feed-item']", "div[class*='post']"]:
  850. try:
  851. await self.page.wait_for_selector(selector, timeout=8000)
  852. break
  853. except Exception:
  854. continue
  855. post_items = self.page.locator("div.post-feed-item")
  856. item_count = await post_items.count()
  857. if item_count == 0:
  858. post_items = self.page.locator("[class*='post-feed']")
  859. item_count = await post_items.count()
  860. for i in range(min(item_count, page_size)):
  861. try:
  862. item = post_items.nth(i)
  863. cover_el = item.locator("div.media img.thumb").first
  864. cover_url = await cover_el.get_attribute("src") or "" if await cover_el.count() > 0 else ""
  865. if not cover_url:
  866. cover_el = item.locator("img").first
  867. cover_url = await cover_el.get_attribute("src") or "" if await cover_el.count() > 0 else ""
  868. title_el = item.locator("div.post-title").first
  869. title = (await title_el.text_content() or "").strip() if await title_el.count() > 0 else ""
  870. time_el = item.locator("div.post-time span").first
  871. publish_time = (await time_el.text_content() or "").strip() if await time_el.count() > 0 else ""
  872. play_count = like_count = comment_count = share_count = collect_count = 0
  873. data_items = item.locator("div.post-data div.data-item")
  874. for j in range(await data_items.count()):
  875. data_item = data_items.nth(j)
  876. count_text = (await data_item.locator("span.count").text_content() or "0").strip()
  877. if await data_item.locator("span.weui-icon-outlined-eyes-on").count() > 0:
  878. play_count = self._parse_count(count_text)
  879. elif await data_item.locator("span.weui-icon-outlined-like").count() > 0:
  880. like_count = self._parse_count(count_text)
  881. elif await data_item.locator("span.weui-icon-outlined-comment").count() > 0:
  882. comment_count = self._parse_count(count_text)
  883. elif await data_item.locator("use[xlink\\:href='#icon-share']").count() > 0:
  884. share_count = self._parse_count(count_text)
  885. elif await data_item.locator("use[xlink\\:href='#icon-thumb']").count() > 0:
  886. collect_count = self._parse_count(count_text)
  887. work_id = f"weixin_{i}_{hash(title)}_{hash(publish_time)}"
  888. works.append(WorkItem(
  889. work_id=work_id,
  890. title=title or "无标题",
  891. cover_url=cover_url,
  892. duration=0,
  893. status="published",
  894. publish_time=publish_time,
  895. play_count=play_count,
  896. like_count=like_count,
  897. comment_count=comment_count,
  898. share_count=share_count,
  899. collect_count=collect_count,
  900. ))
  901. except Exception as e:
  902. print(f"[{self.platform_name}] DOM 解析作品 {i} 失败: {e}", flush=True)
  903. continue
  904. total = len(works)
  905. has_more = item_count > page_size
  906. print(f"[{self.platform_name}] DOM 回退获取 {len(works)} 条", flush=True)
  907. except Exception as e:
  908. print(f"[{self.platform_name}] DOM 回退失败: {e}", flush=True)
  909. return (works, total, has_more, "")
  910. async def get_works(self, cookies: str, page: int = 0, page_size: int = 20) -> WorksResult:
  911. """获取视频号作品列表(调用 post_list 接口)
  912. page: 页码从 0 开始,或上一页返回的 rawKeyBuff/lastBuff 字符串
  913. """
  914. # 分页:首页 currentPage=1/rawKeyBuff=null,下一页用 currentPage 递增或 rawKeyBuff
  915. if page is None or page == "" or (isinstance(page, int) and page == 0):
  916. current_page = 1
  917. raw_key_buff = None
  918. elif isinstance(page, int):
  919. current_page = page + 1
  920. raw_key_buff = None
  921. else:
  922. current_page = 1
  923. raw_key_buff = str(page)
  924. ts_ms = str(int(time.time() * 1000))
  925. print(f"\n{'='*60}")
  926. print(f"[{self.platform_name}] 获取作品列表 currentPage={current_page}, pageSize={page_size}, rawKeyBuff={raw_key_buff[:40] if raw_key_buff else 'null'}...")
  927. print(f"{'='*60}")
  928. works: List[WorkItem] = []
  929. total = 0
  930. has_more = False
  931. next_page = ""
  932. try:
  933. await self.init_browser()
  934. cookie_list = self.parse_cookies(cookies)
  935. await self.set_cookies(cookie_list)
  936. if not self.page:
  937. raise Exception("Page not initialized")
  938. await self.page.goto("https://channels.weixin.qq.com/platform/post/list", timeout=30000)
  939. await asyncio.sleep(3)
  940. current_url = self.page.url
  941. if "login" in current_url:
  942. raise Exception("Cookie 已过期,请重新登录")
  943. api_url = "https://channels.weixin.qq.com/micro/content/cgi-bin/mmfinderassistant-bin/post/post_list"
  944. req_body = {
  945. "pageSize": page_size,
  946. "currentPage": current_page,
  947. "userpageType": 11,
  948. "stickyOrder": True,
  949. "timestamp": ts_ms,
  950. "_log_finder_uin": "",
  951. "_log_finder_id": "",
  952. "rawKeyBuff": raw_key_buff,
  953. "pluginSessionId": None,
  954. "scene": 7,
  955. "reqScene": 7,
  956. }
  957. body_str = json.dumps(req_body)
  958. response = await self.page.evaluate("""
  959. async ([url, bodyStr]) => {
  960. try {
  961. const resp = await fetch(url, {
  962. method: 'POST',
  963. credentials: 'include',
  964. headers: {
  965. 'Content-Type': 'application/json',
  966. 'Accept': '*/*',
  967. 'Referer': 'https://channels.weixin.qq.com/platform/post/list'
  968. },
  969. body: bodyStr
  970. });
  971. return await resp.json();
  972. } catch (e) {
  973. return { error: e.toString() };
  974. }
  975. }
  976. """, [api_url, body_str])
  977. is_first_page = current_page == 1 and raw_key_buff is None
  978. if response.get("error"):
  979. print(f"[{self.platform_name}] API 请求失败: {response.get('error')}", flush=True)
  980. if is_first_page:
  981. works, total, has_more, next_page = await self._get_works_fallback_dom(page_size)
  982. if works:
  983. return WorksResult(success=True, platform=self.platform_name, works=works, total=total, has_more=has_more, next_page=next_page)
  984. return WorksResult(success=False, platform=self.platform_name, error=response.get("error", "API 请求失败"))
  985. err_code = response.get("errCode", -1)
  986. if err_code != 0:
  987. err_msg = response.get("errMsg", "unknown")
  988. print(f"[{self.platform_name}] API errCode={err_code}, errMsg={err_msg}, 完整响应(前800字): {json.dumps(response, ensure_ascii=False)[:800]}", flush=True)
  989. if is_first_page:
  990. works, total, has_more, next_page = await self._get_works_fallback_dom(page_size)
  991. if works:
  992. return WorksResult(success=True, platform=self.platform_name, works=works, total=total, has_more=has_more, next_page=next_page)
  993. return WorksResult(success=False, platform=self.platform_name, error=f"errCode={err_code}, errMsg={err_msg}")
  994. data = response.get("data") or {}
  995. raw_list = data.get("list") or []
  996. total = int(data.get("totalCount") or 0)
  997. has_more = bool(data.get("continueFlag", False))
  998. next_page = (data.get("lastBuff") or "").strip()
  999. print(f"[{self.platform_name}] API 响应: list_len={len(raw_list)}, totalCount={total}, continueFlag={has_more}, lastBuff={next_page[:50] if next_page else ''}...")
  1000. if is_first_page and len(raw_list) == 0:
  1001. works_fb, total_fb, has_more_fb, _ = await self._get_works_fallback_dom(page_size)
  1002. if works_fb:
  1003. return WorksResult(success=True, platform=self.platform_name, works=works_fb, total=total_fb, has_more=has_more_fb, next_page="")
  1004. for item in raw_list:
  1005. try:
  1006. work_id = str(item.get("objectId") or item.get("id") or "").strip()
  1007. if not work_id:
  1008. work_id = f"weixin_{hash(item.get('createTime',0))}_{hash(item.get('desc', {}).get('description',''))}"
  1009. desc = item.get("desc") or {}
  1010. title = (desc.get("description") or "").strip() or "无标题"
  1011. cover_url = ""
  1012. duration = 0
  1013. media_list = desc.get("media") or []
  1014. if media_list and isinstance(media_list[0], dict):
  1015. m = media_list[0]
  1016. cover_url = (m.get("coverUrl") or m.get("thumbUrl") or "").strip()
  1017. duration = int(m.get("videoPlayLen") or 0)
  1018. create_ts = item.get("createTime") or 0
  1019. if isinstance(create_ts, (int, float)) and create_ts:
  1020. publish_time = datetime.fromtimestamp(create_ts).strftime("%Y-%m-%d %H:%M:%S")
  1021. else:
  1022. publish_time = str(create_ts) if create_ts else ""
  1023. read_count = int(item.get("readCount") or 0)
  1024. like_count = int(item.get("likeCount") or 0)
  1025. comment_count = int(item.get("commentCount") or 0)
  1026. forward_count = int(item.get("forwardCount") or 0)
  1027. fav_count = int(item.get("favCount") or 0)
  1028. works.append(WorkItem(
  1029. work_id=work_id,
  1030. title=title,
  1031. cover_url=cover_url,
  1032. duration=duration,
  1033. status="published",
  1034. publish_time=publish_time,
  1035. play_count=read_count,
  1036. like_count=like_count,
  1037. comment_count=comment_count,
  1038. share_count=forward_count,
  1039. collect_count=fav_count,
  1040. ))
  1041. except Exception as e:
  1042. print(f"[{self.platform_name}] 解析作品项失败: {e}", flush=True)
  1043. continue
  1044. if total == 0 and works:
  1045. total = len(works)
  1046. print(f"[{self.platform_name}] 本页获取 {len(works)} 条,totalCount={total}, next_page={bool(next_page)}")
  1047. except Exception as e:
  1048. import traceback
  1049. traceback.print_exc()
  1050. return WorksResult(success=False, platform=self.platform_name, error=str(e))
  1051. return WorksResult(success=True, platform=self.platform_name, works=works, total=total, has_more=has_more, next_page=next_page)
  1052. async def get_comments(self, cookies: str, work_id: str, cursor: str = "") -> CommentsResult:
  1053. """
  1054. 获取视频号作品评论(完全参考 get_weixin_work_comments.py 的接口监听逻辑)
  1055. 支持递归提取二级评论,正确处理 parent_comment_id
  1056. """
  1057. print(f"\n{'='*60}")
  1058. print(f"[{self.platform_name}] 获取作品评论")
  1059. print(f"[{self.platform_name}] work_id={work_id}")
  1060. print(f"{'='*60}")
  1061. comments: List[CommentItem] = []
  1062. total = 0
  1063. has_more = False
  1064. try:
  1065. await self.init_browser()
  1066. cookie_list = self.parse_cookies(cookies)
  1067. await self.set_cookies(cookie_list)
  1068. if not self.page:
  1069. raise Exception("Page not initialized")
  1070. # 访问评论管理页面
  1071. print(f"[{self.platform_name}] 正在打开评论页面...")
  1072. await self.page.goto("https://channels.weixin.qq.com/platform/interaction/comment", timeout=30000)
  1073. await asyncio.sleep(2)
  1074. # 检查登录状态
  1075. current_url = self.page.url
  1076. if "login" in current_url:
  1077. raise Exception("Cookie 已过期,请重新登录")
  1078. # === 步骤1: 监听 post_list 接口获取作品列表 ===
  1079. posts = []
  1080. try:
  1081. async with self.page.expect_response(
  1082. lambda res: "/post/post_list" in res.url,
  1083. timeout=20000
  1084. ) as post_resp_info:
  1085. await self.page.wait_for_selector('.scroll-list .comment-feed-wrap', timeout=15000)
  1086. post_resp = await post_resp_info.value
  1087. post_data = await post_resp.json()
  1088. if post_data.get("errCode") == 0:
  1089. posts = post_data.get("data", {}).get("list", [])
  1090. print(f"[{self.platform_name}] ✅ 获取 {len(posts)} 个作品")
  1091. else:
  1092. err_msg = post_data.get("errMsg", "未知错误")
  1093. print(f"[{self.platform_name}] ❌ post_list 业务错误: {err_msg}")
  1094. return CommentsResult(
  1095. success=False,
  1096. platform=self.platform_name,
  1097. work_id=work_id,
  1098. error=f"post_list 业务错误: {err_msg}"
  1099. )
  1100. except Exception as e:
  1101. print(f"[{self.platform_name}] ❌ 获取 post_list 失败: {e}")
  1102. return CommentsResult(
  1103. success=False,
  1104. platform=self.platform_name,
  1105. work_id=work_id,
  1106. error=f"获取 post_list 失败: {e}"
  1107. )
  1108. # === 步骤2: 在 DOM 中查找目标作品 ===
  1109. feed_wraps = await self.page.query_selector_all('.scroll-list .comment-feed-wrap')
  1110. target_feed = None
  1111. target_post = None
  1112. target_index = -1
  1113. for i, feed in enumerate(feed_wraps):
  1114. if i >= len(posts):
  1115. break
  1116. post = posts[i]
  1117. object_nonce = post.get("objectNonce", "")
  1118. post_work_id = post.get("objectId", "") or object_nonce
  1119. # 匹配 work_id(支持 objectId 或 objectNonce 匹配)
  1120. if work_id in [post_work_id, object_nonce] or post_work_id in work_id or object_nonce in work_id:
  1121. target_feed = feed
  1122. target_post = post
  1123. target_index = i
  1124. work_title = post.get("desc", {}).get("description", "无标题")
  1125. print(f"[{self.platform_name}] ✅ 找到目标作品: {work_title}")
  1126. break
  1127. if not target_feed or not target_post:
  1128. print(f"[{self.platform_name}] ❌ 未找到 work_id={work_id} 对应的作品")
  1129. return CommentsResult(
  1130. success=True,
  1131. platform=self.platform_name,
  1132. work_id=work_id,
  1133. comments=[],
  1134. total=0,
  1135. has_more=False
  1136. )
  1137. # 准备作品信息(用于递归函数)
  1138. object_nonce = target_post.get("objectNonce", f"nonce_{target_index}")
  1139. work_title = target_post.get("desc", {}).get("description", f"作品{target_index+1}")
  1140. work_info = {
  1141. "work_id": object_nonce,
  1142. "work_title": work_title
  1143. }
  1144. # === 步骤3: 点击作品触发 comment_list 接口 ===
  1145. content_wrap = await target_feed.query_selector('.feed-content') or target_feed
  1146. try:
  1147. async with self.page.expect_response(
  1148. lambda res: "/comment/comment_list" in res.url,
  1149. timeout=15000
  1150. ) as comment_resp_info:
  1151. await content_wrap.click()
  1152. await asyncio.sleep(0.8)
  1153. comment_resp = await comment_resp_info.value
  1154. comment_data = await comment_resp.json()
  1155. if comment_data.get("errCode") != 0:
  1156. err_msg = comment_data.get("errMsg", "未知错误")
  1157. print(f"[{self.platform_name}] ❌ 评论接口错误: {err_msg}")
  1158. return CommentsResult(
  1159. success=False,
  1160. platform=self.platform_name,
  1161. work_id=work_id,
  1162. error=f"评论接口错误: {err_msg}"
  1163. )
  1164. raw_comments = comment_data.get("data", {}).get("comment", [])
  1165. total = comment_data.get("data", {}).get("totalCount", len(raw_comments))
  1166. print(f"[{self.platform_name}] 📊 原始评论数: {len(raw_comments)}, 总数: {total}")
  1167. # === 步骤4: 递归提取所有评论(含子评论)===
  1168. extracted = self._extract_comments(raw_comments, parent_id="", work_info=work_info)
  1169. # === 步骤5: 转换为 CommentItem 列表(保留 weixin.py 的数据结构)===
  1170. for c in extracted:
  1171. # 使用接口返回的 comment_id
  1172. comment_id = c.get("comment_id", "")
  1173. parent_comment_id = c.get("parent_comment_id", "")
  1174. # 构建 CommentItem(保留原有数据结构用于数据库入库)
  1175. comment_item = CommentItem(
  1176. comment_id=comment_id,
  1177. parent_comment_id=parent_comment_id,
  1178. work_id=work_id,
  1179. content=c.get("content", ""),
  1180. author_id=c.get("username", ""), # 使用 username 作为 author_id
  1181. author_name=c.get("nickname", ""),
  1182. author_avatar=c.get("avatar", ""),
  1183. like_count=c.get("like_count", 0),
  1184. reply_count=0,
  1185. create_time=c.get("create_time", ""),
  1186. )
  1187. # 添加扩展字段(用于数据库存储和后续处理)
  1188. # comment_item.parent_comment_id = c.get("parent_comment_id", "")
  1189. comment_item.is_author = c.get("is_author", False)
  1190. comment_item.create_time_unix = c.get("create_time_unix", 0)
  1191. comment_item.work_title = c.get("work_title", "")
  1192. print(comment_item)
  1193. comments.append(comment_item)
  1194. # 打印日志
  1195. author_tag = " 👤(作者)" if c.get("is_author") else ""
  1196. parent_tag = f" [回复: {c.get('parent_comment_id', '')}]" if c.get("parent_comment_id") else ""
  1197. print(f"[{self.platform_name}] - [{c.get('nickname', '')}] {c.get('content', '')[:30]}... "
  1198. f"({c.get('create_time', '')}){author_tag}{parent_tag}")
  1199. # 判断是否还有更多(优先使用接口返回的 continueFlag,否则根据数量判断)
  1200. has_more = comment_data.get("data", {}).get("continueFlag", False) or len(extracted) < total
  1201. print(f"[{self.platform_name}] ✅ 共提取 {len(comments)} 条评论(含子评论)")
  1202. except Exception as e:
  1203. print(f"[{self.platform_name}] ❌ 获取评论失败: {e}")
  1204. import traceback
  1205. traceback.print_exc()
  1206. return CommentsResult(
  1207. success=False,
  1208. platform=self.platform_name,
  1209. work_id=work_id,
  1210. error=f"获取评论失败: {e}"
  1211. )
  1212. except Exception as e:
  1213. import traceback
  1214. traceback.print_exc()
  1215. return CommentsResult(
  1216. success=False,
  1217. platform=self.platform_name,
  1218. work_id=work_id,
  1219. error=str(e)
  1220. )
  1221. return CommentsResult(
  1222. success=True,
  1223. platform=self.platform_name,
  1224. work_id=work_id,
  1225. comments=comments,
  1226. total=total,
  1227. has_more=has_more
  1228. )
  1229. def _extract_comments(self, comment_list: list, parent_id: str = "", work_info: dict = None) -> list:
  1230. """
  1231. 递归提取一级和二级评论(完全参考 get_weixin_work_comments.py 的 extract_comments 函数)
  1232. Args:
  1233. comment_list: 评论列表(原始接口数据)
  1234. parent_id: 父评论ID(一级评论为空字符串"",二级评论为父级评论ID)
  1235. work_info: 作品信息字典
  1236. Returns:
  1237. list: 扁平化的评论列表,包含一级和二级评论
  1238. """
  1239. result = []
  1240. # 获取当前用户 username(用于判断是否为作者)
  1241. # 优先从环境变量获取,也可通过其他方式配置
  1242. my_username = getattr(self, 'my_username', '') or os.environ.get('WEIXIN_MY_USERNAME', '')
  1243. for cmt in comment_list:
  1244. # 处理时间戳
  1245. create_ts = int(cmt.get("commentCreatetime", 0) or 0)
  1246. readable_time = (
  1247. datetime.fromtimestamp(create_ts).strftime('%Y-%m-%d %H:%M:%S')
  1248. if create_ts > 0 else ""
  1249. )
  1250. # 判断是否作者(如果配置了 my_username)
  1251. username = cmt.get("username", "") or ""
  1252. is_author = (my_username != "") and (username == my_username)
  1253. # 构建评论条目 - 完全参考 get_weixin_work_comments.py 的字段
  1254. entry = {
  1255. "work_id": work_info.get("work_id", "") if work_info else "",
  1256. "work_title": work_info.get("work_title", "") if work_info else "",
  1257. "comment_id": cmt.get("commentId"),
  1258. "parent_comment_id": parent_id, # 关键:一级评论为空字符串"",二级评论为父评论ID
  1259. "username": username,
  1260. "nickname": cmt.get("commentNickname", ""),
  1261. "avatar": cmt.get("commentHeadurl", ""),
  1262. "content": cmt.get("commentContent", ""),
  1263. "create_time_unix": create_ts,
  1264. "create_time": readable_time,
  1265. "is_author": is_author,
  1266. "like_count": cmt.get("commentLikeCount", 0) or 0
  1267. }
  1268. result.append(entry)
  1269. # 递归处理二级评论(levelTwoComment)
  1270. # 关键:二级评论的 parent_id 应该是当前这条评论的 comment_id
  1271. level_two = cmt.get("levelTwoComment", []) or []
  1272. if level_two and isinstance(level_two, list) and len(level_two) > 0:
  1273. # 当前评论的 ID 作为其子评论的 parent_id
  1274. current_comment_id = cmt.get("commentId", "")
  1275. result.extend(
  1276. self._extract_comments(level_two, parent_id=current_comment_id, work_info=work_info)
  1277. )
  1278. return result
  1279. async def auto_reply_private_messages(self, cookies: str) -> dict:
  1280. """自动回复私信 - 集成自 pw3.py"""
  1281. print(f"\n{'='*60}")
  1282. print(f"[{self.platform_name}] 开始自动回复私信")
  1283. print(f"{'='*60}")
  1284. try:
  1285. await self.init_browser()
  1286. cookie_list = self.parse_cookies(cookies)
  1287. await self.set_cookies(cookie_list)
  1288. if not self.page:
  1289. raise Exception("Page not initialized")
  1290. # 访问私信页面
  1291. await self.page.goto("https://channels.weixin.qq.com/platform/private_msg", timeout=30000)
  1292. await asyncio.sleep(3)
  1293. # 检查登录状态
  1294. current_url = self.page.url
  1295. print(f"[{self.platform_name}] 当前 URL: {current_url}")
  1296. if "login" in current_url:
  1297. raise Exception("Cookie 已过期,请重新登录")
  1298. # 等待私信页面加载(使用多个选择器容错)
  1299. try:
  1300. await self.page.wait_for_selector('.private-msg-list-header', timeout=15000)
  1301. except:
  1302. # 尝试其他选择器
  1303. try:
  1304. await self.page.wait_for_selector('.weui-desktop-tab__navs__inner', timeout=10000)
  1305. print(f"[{self.platform_name}] 使用备用选择器加载成功")
  1306. except:
  1307. # 截图调试
  1308. screenshot_path = f"weixin_private_msg_{int(asyncio.get_event_loop().time())}.png"
  1309. await self.page.screenshot(path=screenshot_path)
  1310. print(f"[{self.platform_name}] 页面加载失败,截图: {screenshot_path}")
  1311. raise Exception(f"私信页面加载超时,当前 URL: {current_url}")
  1312. print(f"[{self.platform_name}] 私信页面加载完成")
  1313. # 处理两个 tab
  1314. total_replied = 0
  1315. for tab_name in ["打招呼消息", "私信"]:
  1316. replied_count = await self._process_tab_sessions(tab_name)
  1317. total_replied += replied_count
  1318. print(f"[{self.platform_name}] 自动回复完成,共回复 {total_replied} 条消息")
  1319. return {
  1320. 'success': True,
  1321. 'platform': self.platform_name,
  1322. 'replied_count': total_replied,
  1323. 'message': f'成功回复 {total_replied} 条私信'
  1324. }
  1325. except Exception as e:
  1326. import traceback
  1327. traceback.print_exc()
  1328. return {
  1329. 'success': False,
  1330. 'platform': self.platform_name,
  1331. 'error': str(e)
  1332. }
  1333. async def _process_tab_sessions(self, tab_name: str) -> int:
  1334. """处理指定 tab 下的所有会话"""
  1335. print(f"\n🔄 正在处理「{tab_name}」中的所有会话...")
  1336. if not self.page:
  1337. return 0
  1338. replied_count = 0
  1339. try:
  1340. # 点击 tab
  1341. if tab_name == "私信":
  1342. tab_link = self.page.locator('.weui-desktop-tab__navs__inner li').first.locator('a')
  1343. elif tab_name == "打招呼消息":
  1344. tab_link = self.page.locator('.weui-desktop-tab__navs__inner li').nth(1).locator('a')
  1345. else:
  1346. return 0
  1347. if await tab_link.is_visible():
  1348. await tab_link.click()
  1349. print(f" ➤ 已点击「{tab_name}」tab")
  1350. else:
  1351. print(f" ❌ 「{tab_name}」tab 不可见")
  1352. return 0
  1353. # 等待会话列表加载
  1354. try:
  1355. await self.page.wait_for_function("""
  1356. () => {
  1357. const hasSession = document.querySelectorAll('.session-wrap').length > 0;
  1358. const hasEmpty = !!document.querySelector('.empty-text');
  1359. return hasSession || hasEmpty;
  1360. }
  1361. """, timeout=8000)
  1362. print(" ✅ 会话列表区域已加载")
  1363. except:
  1364. print(" ⚠️ 等待会话列表超时,继续尝试读取...")
  1365. # 获取会话
  1366. session_wraps = self.page.locator('.session-wrap')
  1367. session_count = await session_wraps.count()
  1368. print(f" 💬 共找到 {session_count} 个会话")
  1369. if session_count == 0:
  1370. return 0
  1371. # 遍历每个会话
  1372. for idx in range(session_count):
  1373. try:
  1374. current_sessions = self.page.locator('.session-wrap')
  1375. if idx >= await current_sessions.count():
  1376. break
  1377. session = current_sessions.nth(idx)
  1378. user_name = await session.locator('.name').inner_text()
  1379. last_preview = await session.locator('.feed-info').inner_text()
  1380. print(f"\n ➤ [{idx+1}/{session_count}] 正在处理: {user_name} | 最后消息: {last_preview}")
  1381. await session.click()
  1382. await asyncio.sleep(2)
  1383. # 提取聊天历史
  1384. history = await self._extract_chat_history()
  1385. need_reply = (not history) or (not history[-1]["is_author"])
  1386. if need_reply:
  1387. reply_text = await self._generate_reply_with_ai(history)
  1388. if reply_text=="":
  1389. reply_text = self._generate_reply(history)
  1390. # # 生成回复
  1391. # if history and history[-1]["is_author"]:
  1392. # reply_text = await self._generate_reply_with_ai(history)
  1393. # else:
  1394. # reply_text = self._generate_reply(history)
  1395. if reply_text:
  1396. print(f" 📝 回复内容: {reply_text}")
  1397. try:
  1398. textarea = self.page.locator('.edit_area').first
  1399. send_btn = self.page.locator('button:has-text("发送")').first
  1400. if await textarea.is_visible() and await send_btn.is_visible():
  1401. await textarea.fill(reply_text)
  1402. await asyncio.sleep(0.5)
  1403. await send_btn.click()
  1404. print(" ✅ 已发送")
  1405. replied_count += 1
  1406. await asyncio.sleep(1.5)
  1407. else:
  1408. print(" ❌ 输入框或发送按钮不可见")
  1409. except Exception as e:
  1410. print(f" ❌ 发送失败: {e}")
  1411. else:
  1412. print(" ➤ 无需回复")
  1413. else:
  1414. print(" ➤ 最后一条是我发的,跳过回复")
  1415. except Exception as e:
  1416. print(f" ❌ 处理会话 {idx+1} 时出错: {e}")
  1417. continue
  1418. except Exception as e:
  1419. print(f"❌ 处理「{tab_name}」失败: {e}")
  1420. return replied_count
  1421. async def _extract_chat_history(self) -> list:
  1422. """精准提取聊天记录,区分作者(自己)和用户"""
  1423. if not self.page:
  1424. return []
  1425. history = []
  1426. message_wrappers = self.page.locator('.session-content-wrapper > div:not(.footer) > .text-wrapper')
  1427. count = await message_wrappers.count()
  1428. for i in range(count):
  1429. try:
  1430. wrapper = message_wrappers.nth(i)
  1431. # 判断方向
  1432. is_right = await wrapper.locator('.content-right').count() > 0
  1433. is_left = await wrapper.locator('.content-left').count() > 0
  1434. if not (is_left or is_right):
  1435. continue
  1436. # 提取消息文本
  1437. pre_el = wrapper.locator('pre.message-plain')
  1438. content = ''
  1439. if await pre_el.count() > 0:
  1440. content = await pre_el.inner_text()
  1441. content = content.strip()
  1442. if not content:
  1443. continue
  1444. # 获取头像
  1445. avatar_img = wrapper.locator('.avatar').first
  1446. avatar_src = ''
  1447. if await avatar_img.count() > 0:
  1448. avatar_src = await avatar_img.get_attribute("src") or ''
  1449. # 右侧 = 作者(自己)
  1450. is_author = is_right
  1451. # 获取用户名
  1452. if is_left:
  1453. name_el = wrapper.locator('.profile .name')
  1454. author_name = '用户'
  1455. if await name_el.count() > 0:
  1456. author_name = await name_el.inner_text()
  1457. else:
  1458. author_name = "我"
  1459. history.append({
  1460. "author": author_name,
  1461. "content": content,
  1462. "is_author": is_author,
  1463. "avatar": avatar_src
  1464. })
  1465. except Exception as e:
  1466. print(f" ⚠️ 解析第 {i+1} 条消息失败: {e}")
  1467. continue
  1468. return history
  1469. async def _generate_reply_with_ai(self, chat_history: list) -> str:
  1470. """使用 AI 生成智能回复"""
  1471. import requests
  1472. import json
  1473. try:
  1474. # 获取 AI 配置
  1475. ai_api_key = os.environ.get('DASHSCOPE_API_KEY', '')
  1476. ai_base_url = os.environ.get('DASHSCOPE_BASE_URL', 'https://dashscope.aliyuncs.com/compatible-mode/v1')
  1477. ai_model = os.environ.get('AI_MODEL', 'qwen-plus')
  1478. if not ai_api_key:
  1479. print("⚠️ 未配置 AI API Key,使用规则回复")
  1480. return self._generate_reply(chat_history)
  1481. # 构建对话上下文
  1482. messages = [{"role": "system", "content": "你是一个友好的微信视频号创作者助手,负责回复粉丝私信。请保持简洁、友好、专业的语气。回复长度不超过20字。"}]
  1483. for msg in chat_history:
  1484. role = "assistant" if msg["is_author"] else "user"
  1485. messages.append({
  1486. "role": role,
  1487. "content": msg["content"]
  1488. })
  1489. # 调用 AI API
  1490. headers = {
  1491. 'Authorization': f'Bearer {ai_api_key}',
  1492. 'Content-Type': 'application/json'
  1493. }
  1494. payload = {
  1495. "model": ai_model,
  1496. "messages": messages,
  1497. "max_tokens": 150,
  1498. "temperature": 0.8
  1499. }
  1500. print(" 🤖 正在调用 AI 生成回复...")
  1501. response = requests.post(
  1502. f"{ai_base_url}/chat/completions",
  1503. headers=headers,
  1504. json=payload,
  1505. timeout=30
  1506. )
  1507. if response.status_code != 200:
  1508. print(f" ⚠️ AI API 返回错误 {response.status_code},使用规则回复")
  1509. return self._generate_reply(chat_history)
  1510. result = response.json()
  1511. ai_reply = result.get('choices', [{}])[0].get('message', {}).get('content', '').strip()
  1512. if ai_reply:
  1513. print(f" ✅ AI 生成回复: {ai_reply}")
  1514. return ai_reply
  1515. else:
  1516. print(" ⚠️ AI 返回空内容,使用规则回复")
  1517. return self._generate_reply(chat_history)
  1518. except Exception as e:
  1519. print(f" ⚠️ AI 回复生成失败: {e},使用规则回复")
  1520. return self._generate_reply(chat_history)
  1521. def _generate_reply(self, chat_history: list) -> str:
  1522. """根据完整聊天历史生成回复(规则回复方式)"""
  1523. if not chat_history:
  1524. return "你好!感谢联系~"
  1525. # 检查最后一条是否是作者发的
  1526. if chat_history[-1]["is_author"]:
  1527. return "" # 不回复
  1528. # 找最后一条用户消息
  1529. last_user_msg = chat_history[-1]["content"]
  1530. # 简单规则回复
  1531. if "谢谢" in last_user_msg or "感谢" in last_user_msg:
  1532. return "不客气!欢迎常来交流~"
  1533. elif "你好" in last_user_msg or "在吗" in last_user_msg:
  1534. return "你好!请问有什么可以帮您的?"
  1535. elif "视频" in last_user_msg or "怎么拍" in last_user_msg:
  1536. return "视频是用手机拍摄的,注意光线和稳定哦!"
  1537. else:
  1538. return "收到!我会认真阅读您的留言~"