weixin.py 79 KB

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