base.py 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  1. # -*- coding: utf-8 -*-
  2. """
  3. 平台发布基类
  4. 提供通用的发布接口和工具方法
  5. """
  6. import asyncio
  7. import json
  8. import os
  9. import uuid
  10. from abc import ABC, abstractmethod
  11. from dataclasses import dataclass, field
  12. from datetime import datetime
  13. from typing import List, Optional, Callable, Dict, Any
  14. from playwright.async_api import async_playwright, Browser, BrowserContext, Page
  15. @dataclass
  16. class PublishParams:
  17. """发布参数"""
  18. title: str
  19. video_path: str
  20. description: str = ""
  21. cover_path: Optional[str] = None
  22. tags: List[str] = field(default_factory=list)
  23. publish_date: Optional[datetime] = None
  24. location: str = "重庆市"
  25. def __post_init__(self):
  26. if not self.description:
  27. self.description = self.title
  28. @dataclass
  29. class PublishResult:
  30. """发布结果"""
  31. success: bool
  32. platform: str
  33. video_id: str = ""
  34. video_url: str = ""
  35. message: str = ""
  36. error: str = ""
  37. need_captcha: bool = False
  38. captcha_type: str = ""
  39. screenshot_base64: str = ""
  40. page_url: str = ""
  41. status: str = ""
  42. screenshot_path: str = ""
  43. @dataclass
  44. class WorkItem:
  45. """作品数据"""
  46. work_id: str
  47. title: str
  48. cover_url: str = ""
  49. video_url: str = ""
  50. duration: int = 0 # 秒
  51. status: str = "published" # published, reviewing, rejected, draft
  52. publish_time: str = ""
  53. play_count: int = 0
  54. like_count: int = 0
  55. comment_count: int = 0
  56. share_count: int = 0
  57. collect_count: int = 0
  58. def to_dict(self) -> Dict[str, Any]:
  59. return {
  60. "work_id": self.work_id,
  61. "title": self.title,
  62. "cover_url": self.cover_url,
  63. "video_url": self.video_url,
  64. "duration": self.duration,
  65. "status": self.status,
  66. "publish_time": self.publish_time,
  67. "play_count": self.play_count,
  68. "like_count": self.like_count,
  69. "comment_count": self.comment_count,
  70. "share_count": self.share_count,
  71. "collect_count": self.collect_count,
  72. }
  73. @dataclass
  74. class CommentItem:
  75. """评论数据"""
  76. comment_id: str
  77. parent_comment_id: str
  78. work_id: str
  79. content: str
  80. author_id: str = ""
  81. author_name: str = ""
  82. author_avatar: str = ""
  83. like_count: int = 0
  84. reply_count: int = 0
  85. create_time: str = ""
  86. is_author: bool = False # 是否是作者的评论
  87. replies: List['CommentItem'] = field(default_factory=list)
  88. def to_dict(self) -> Dict[str, Any]:
  89. return {
  90. "comment_id": self.comment_id,
  91. "parent_comment_id": self.parent_comment_id,
  92. "work_id": self.work_id,
  93. "content": self.content,
  94. "author_id": self.author_id,
  95. "author_name": self.author_name,
  96. "author_avatar": self.author_avatar,
  97. "like_count": self.like_count,
  98. "reply_count": self.reply_count,
  99. "create_time": self.create_time,
  100. "is_author": self.is_author,
  101. "replies": [r.to_dict() for r in self.replies],
  102. }
  103. @dataclass
  104. class WorksResult:
  105. """作品列表结果"""
  106. success: bool
  107. platform: str
  108. works: List[WorkItem] = field(default_factory=list)
  109. total: int = 0
  110. has_more: bool = False
  111. next_page: Any = ""
  112. error: str = ""
  113. debug_info: str = "" # 调试信息
  114. def to_dict(self) -> Dict[str, Any]:
  115. return {
  116. "success": self.success,
  117. "platform": self.platform,
  118. "works": [w.to_dict() for w in self.works],
  119. "total": self.total,
  120. "has_more": self.has_more,
  121. "next_page": self.next_page,
  122. "error": self.error,
  123. "debug_info": self.debug_info,
  124. }
  125. @dataclass
  126. class CommentsResult:
  127. """评论列表结果"""
  128. success: bool
  129. platform: str
  130. work_id: str
  131. comments: List[CommentItem] = field(default_factory=list)
  132. total: int = 0
  133. has_more: bool = False
  134. error: str = ""
  135. def to_dict(self) -> Dict[str, Any]:
  136. return {
  137. "success": self.success,
  138. "platform": self.platform,
  139. "work_id": self.work_id,
  140. "comments": [c.to_dict() for c in self.comments],
  141. "total": self.total,
  142. "has_more": self.has_more,
  143. "error": self.error,
  144. }
  145. class BasePublisher(ABC):
  146. """
  147. 平台发布基类
  148. 所有平台发布器都需要继承此类
  149. """
  150. platform_name: str = "base"
  151. login_url: str = ""
  152. publish_url: str = ""
  153. cookie_domain: str = ""
  154. def __init__(self, headless: bool = True):
  155. self.headless = headless
  156. self.browser: Optional[Browser] = None
  157. self.context: Optional[BrowserContext] = None
  158. self.page: Optional[Page] = None
  159. self.on_progress: Optional[Callable[[int, str], None]] = None
  160. self.user_id: Optional[int] = None
  161. self.publish_task_id: Optional[int] = None
  162. self.publish_account_id: Optional[int] = None
  163. self.proxy_config: Optional[Dict[str, Any]] = None
  164. def set_progress_callback(self, callback: Callable[[int, str], None]):
  165. """设置进度回调"""
  166. self.on_progress = callback
  167. def report_progress(self, progress: int, message: str):
  168. """报告进度"""
  169. print(f"[{self.platform_name}] [{progress}%] {message}")
  170. if self.on_progress:
  171. self.on_progress(progress, message)
  172. @staticmethod
  173. def parse_cookies(cookies_str: str) -> list:
  174. """解析 cookie 字符串为列表"""
  175. try:
  176. cookies = json.loads(cookies_str)
  177. if isinstance(cookies, list):
  178. return cookies
  179. except json.JSONDecodeError:
  180. pass
  181. # 字符串格式: name=value; name2=value2
  182. cookies = []
  183. for item in cookies_str.split(';'):
  184. item = item.strip()
  185. if '=' in item:
  186. name, value = item.split('=', 1)
  187. cookies.append({
  188. 'name': name.strip(),
  189. 'value': value.strip(),
  190. 'domain': '',
  191. 'path': '/'
  192. })
  193. return cookies
  194. @staticmethod
  195. def _normalize_same_site(value: Any) -> Optional[str]:
  196. """将不同来源的 sameSite 值转换为 Playwright 接受的值。"""
  197. if value is None:
  198. return None
  199. v = str(value).strip().lower()
  200. if not v:
  201. return None
  202. mapping = {
  203. "strict": "Strict",
  204. "lax": "Lax",
  205. "none": "None",
  206. "no_restriction": "None", # Electron
  207. "unspecified": None, # Electron: 不传该字段
  208. }
  209. return mapping.get(v, None)
  210. @classmethod
  211. def _sanitize_cookie_for_playwright(cls, cookie: Dict[str, Any], default_domain: str) -> Optional[Dict[str, Any]]:
  212. """清洗 cookie 字段,避免 BrowserContext.add_cookies 参数校验失败。"""
  213. if not isinstance(cookie, dict):
  214. return None
  215. name = str(cookie.get('name') or '').strip()
  216. if not name:
  217. return None
  218. cleaned: Dict[str, Any] = {
  219. 'name': name,
  220. 'value': str(cookie.get('value') or ''),
  221. }
  222. url = str(cookie.get('url') or '').strip()
  223. if url:
  224. cleaned['url'] = url
  225. else:
  226. domain = str(cookie.get('domain') or '').strip() or default_domain
  227. if not domain:
  228. return None
  229. cleaned['domain'] = domain
  230. cleaned['path'] = str(cookie.get('path') or '/')
  231. if 'httpOnly' in cookie:
  232. cleaned['httpOnly'] = bool(cookie.get('httpOnly'))
  233. if 'secure' in cookie:
  234. cleaned['secure'] = bool(cookie.get('secure'))
  235. expires_raw = cookie.get('expires', cookie.get('expirationDate'))
  236. if expires_raw not in (None, '', 0):
  237. try:
  238. expires_val = float(expires_raw)
  239. if expires_val > 0:
  240. cleaned['expires'] = expires_val
  241. except Exception:
  242. pass
  243. same_site = cls._normalize_same_site(cookie.get('sameSite', cookie.get('same_site')))
  244. if same_site:
  245. cleaned['sameSite'] = same_site
  246. return cleaned
  247. @staticmethod
  248. def cookies_to_string(cookies: list) -> str:
  249. """将 cookie 列表转换为字符串"""
  250. return '; '.join([f"{c['name']}={c['value']}" for c in cookies])
  251. async def init_browser(self, storage_state: str = None, proxy_config: Dict[str, Any] = None):
  252. """初始化浏览器"""
  253. print(f"[{self.platform_name}] init_browser: headless={self.headless}", flush=True)
  254. playwright = await async_playwright().start()
  255. proxy = proxy_config or self.proxy_config
  256. if proxy and isinstance(proxy, dict) and proxy.get('server'):
  257. print(f"[{self.platform_name}] 使用代理: {proxy.get('server')}", flush=True)
  258. self.browser = await playwright.chromium.launch(headless=self.headless, proxy=proxy)
  259. else:
  260. self.browser = await playwright.chromium.launch(headless=self.headless)
  261. if storage_state and os.path.exists(storage_state):
  262. self.context = await self.browser.new_context(storage_state=storage_state)
  263. else:
  264. self.context = await self.browser.new_context()
  265. self.page = await self.context.new_page()
  266. return self.page
  267. async def set_cookies(self, cookies: list):
  268. """设置 cookies"""
  269. if not self.context:
  270. raise Exception("Browser context not initialized")
  271. sanitized: List[Dict[str, Any]] = []
  272. for cookie in cookies:
  273. cleaned = self._sanitize_cookie_for_playwright(cookie, self.cookie_domain)
  274. if cleaned:
  275. sanitized.append(cleaned)
  276. if not sanitized:
  277. raise Exception("没有可用的 Cookie(清洗后为空)")
  278. await self.context.add_cookies(sanitized)
  279. async def close_browser(self):
  280. """关闭浏览器"""
  281. if self.context:
  282. await self.context.close()
  283. if self.browser:
  284. await self.browser.close()
  285. async def save_cookies(self, file_path: str):
  286. """保存 cookies 到文件"""
  287. if self.context:
  288. await self.context.storage_state(path=file_path)
  289. async def capture_screenshot(self) -> str:
  290. """截取当前页面截图,返回 Base64 编码"""
  291. import base64
  292. if not self.page:
  293. return ""
  294. try:
  295. screenshot_bytes = await self.page.screenshot(type="jpeg", quality=80)
  296. return base64.b64encode(screenshot_bytes).decode('utf-8')
  297. except Exception as e:
  298. print(f"[{self.platform_name}] 截图失败: {e}")
  299. return ""
  300. async def save_screenshot_to_file(self, directory: str = None, filename_prefix: str = "publish_failed") -> str:
  301. """
  302. 保存截图到指定目录,返回文件路径
  303. Args:
  304. directory: 截图保存目录,默认为 uploads/screenshots
  305. filename_prefix: 文件名前缀
  306. Returns:
  307. str: 保存的文件路径,失败返回空字符串
  308. """
  309. if not self.page:
  310. return ""
  311. try:
  312. if directory is None:
  313. current_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  314. directory = os.path.join(current_dir, '..', '..', 'uploads', 'screenshots')
  315. directory = os.path.abspath(directory)
  316. os.makedirs(directory, exist_ok=True)
  317. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  318. task_info = f"_task{self.publish_task_id}" if self.publish_task_id else ""
  319. account_info = f"_acc{self.publish_account_id}" if self.publish_account_id else ""
  320. filename = f"{filename_prefix}_{self.platform_name}{task_info}{account_info}_{timestamp}.png"
  321. filepath = os.path.join(directory, filename)
  322. await self.page.screenshot(path=filepath, type="png")
  323. print(f"[{self.platform_name}] 截图已保存: {filepath}")
  324. return filepath
  325. except Exception as e:
  326. print(f"[{self.platform_name}] 保存截图失败: {e}")
  327. return ""
  328. async def request_sms_code_from_frontend(self, phone: str = "", timeout_seconds: int = 120, message: str = "") -> str:
  329. node_api_url = os.environ.get('NODEJS_API_URL', 'http://localhost:3000').rstrip('/')
  330. internal_api_key = os.environ.get('INTERNAL_API_KEY', 'internal-api-key-default')
  331. if not self.user_id:
  332. raise Exception("缺少 user_id,无法请求前端输入验证码")
  333. captcha_task_id = f"py_{self.platform_name}_{uuid.uuid4().hex}"
  334. payload = {
  335. "user_id": self.user_id,
  336. "captcha_task_id": captcha_task_id,
  337. "type": "sms",
  338. "phone": phone or "",
  339. "message": message or "请输入短信验证码",
  340. "timeout_seconds": timeout_seconds,
  341. "publish_task_id": self.publish_task_id,
  342. "publish_account_id": self.publish_account_id,
  343. }
  344. import requests
  345. try:
  346. resp = requests.post(
  347. f"{node_api_url}/api/internal/captcha/request",
  348. headers={
  349. "Content-Type": "application/json",
  350. "X-Internal-API-Key": internal_api_key,
  351. },
  352. json=payload,
  353. timeout=timeout_seconds + 30,
  354. )
  355. except Exception as e:
  356. raise Exception(f"请求前端验证码失败: {e}")
  357. try:
  358. data = resp.json()
  359. except Exception:
  360. raise Exception(f"请求前端验证码失败: HTTP {resp.status_code}")
  361. if resp.status_code >= 400 or not data.get("success"):
  362. raise Exception(data.get("error") or data.get("message") or f"请求前端验证码失败: HTTP {resp.status_code}")
  363. code = data.get("code") or ""
  364. if not code:
  365. raise Exception("未收到验证码")
  366. return str(code)
  367. async def ai_analyze_sms_send_state(self, screenshot_base64: str = None) -> dict:
  368. import os
  369. import requests
  370. import json
  371. import re
  372. try:
  373. if not screenshot_base64:
  374. screenshot_base64 = await self.capture_screenshot()
  375. if not screenshot_base64:
  376. return {
  377. "has_sms_modal": False,
  378. "send_button_state": "unknown",
  379. "sent_likely": False,
  380. "block_reason": "unknown",
  381. "suggested_action": "manual_send",
  382. "confidence": 0,
  383. "notes": "无法获取截图",
  384. }
  385. ai_api_key = os.environ.get('DASHSCOPE_API_KEY', '')
  386. ai_base_url = os.environ.get('DASHSCOPE_BASE_URL', 'https://dashscope.aliyuncs.com/compatible-mode/v1')
  387. ai_vision_model = os.environ.get('AI_VISION_MODEL', 'qwen-vl-plus')
  388. if not ai_api_key:
  389. return {
  390. "has_sms_modal": True,
  391. "send_button_state": "unknown",
  392. "sent_likely": False,
  393. "block_reason": "no_ai_key",
  394. "suggested_action": "manual_send",
  395. "confidence": 0,
  396. "notes": "未配置 AI API Key",
  397. }
  398. prompt = """请分析这张网页截图,判断是否处于“短信验证码”验证弹窗/页面,并判断“发送验证码/获取验证码”是否已经触发成功。
  399. 你需要重点识别:
  400. 1) 是否存在短信验证码弹窗(包含“请输入验证码/短信验证码/手机号验证/获取验证码/发送验证码”等)
  401. 2) 发送按钮状态:enabled / disabled / countdown(出现xx秒) / hidden / unknown
  402. 3) 是否已发送成功:例如出现倒计时、按钮禁用、出现“已发送/重新发送/xx秒后重试”等
  403. 4) 是否被阻塞:例如出现滑块/人机验证、频繁发送、风控提示、网络异常等
  404. 请以 JSON 返回:
  405. ```json
  406. {
  407. "has_sms_modal": true,
  408. "send_button_state": "enabled|disabled|countdown|hidden|unknown",
  409. "sent_likely": true,
  410. "block_reason": "none|need_click_send|slider|risk|rate_limit|network|unknown",
  411. "suggested_action": "wait|click_send|solve_slider|manual_send",
  412. "confidence": 0-100,
  413. "notes": "一句话说明你看到的证据"
  414. }
  415. ```"""
  416. headers = {
  417. 'Authorization': f'Bearer {ai_api_key}',
  418. 'Content-Type': 'application/json'
  419. }
  420. payload = {
  421. "model": ai_vision_model,
  422. "messages": [
  423. {
  424. "role": "user",
  425. "content": [
  426. {
  427. "type": "image_url",
  428. "image_url": {
  429. "url": f"data:image/jpeg;base64,{screenshot_base64}"
  430. }
  431. },
  432. {
  433. "type": "text",
  434. "text": prompt
  435. }
  436. ]
  437. }
  438. ],
  439. "max_tokens": 500
  440. }
  441. response = requests.post(
  442. f"{ai_base_url}/chat/completions",
  443. headers=headers,
  444. json=payload,
  445. timeout=30
  446. )
  447. if response.status_code != 200:
  448. return {
  449. "has_sms_modal": True,
  450. "send_button_state": "unknown",
  451. "sent_likely": False,
  452. "block_reason": "network",
  453. "suggested_action": "manual_send",
  454. "confidence": 0,
  455. "notes": f"AI API 返回错误 {response.status_code}",
  456. }
  457. result = response.json()
  458. ai_response = result.get('choices', [{}])[0].get('message', {}).get('content', '')
  459. json_match = re.search(r'```json\\s*([\\s\\S]*?)\\s*```', ai_response)
  460. if json_match:
  461. json_str = json_match.group(1)
  462. else:
  463. json_match = re.search(r'\\{[\\s\\S]*\\}', ai_response)
  464. json_str = json_match.group(0) if json_match else '{}'
  465. try:
  466. data = json.loads(json_str)
  467. except Exception:
  468. data = {}
  469. return {
  470. "has_sms_modal": bool(data.get("has_sms_modal", True)),
  471. "send_button_state": data.get("send_button_state", "unknown"),
  472. "sent_likely": bool(data.get("sent_likely", False)),
  473. "block_reason": data.get("block_reason", "unknown"),
  474. "suggested_action": data.get("suggested_action", "manual_send"),
  475. "confidence": int(data.get("confidence", 0) or 0),
  476. "notes": data.get("notes", ""),
  477. }
  478. except Exception as e:
  479. return {
  480. "has_sms_modal": True,
  481. "send_button_state": "unknown",
  482. "sent_likely": False,
  483. "block_reason": "unknown",
  484. "suggested_action": "manual_send",
  485. "confidence": 0,
  486. "notes": f"AI 分析异常: {e}",
  487. }
  488. async def sync_cookies_to_node(self, cookies: list) -> bool:
  489. import os
  490. import json
  491. import requests
  492. if not self.user_id or not self.publish_account_id:
  493. return False
  494. node_api_url = os.environ.get('NODEJS_API_URL', 'http://localhost:3000').rstrip('/')
  495. internal_api_key = os.environ.get('INTERNAL_API_KEY', 'internal-api-key-default')
  496. try:
  497. payload = {
  498. "user_id": int(self.user_id),
  499. "account_id": int(self.publish_account_id),
  500. "cookies": json.dumps(cookies, ensure_ascii=False),
  501. }
  502. resp = requests.post(
  503. f"{node_api_url}/api/internal/accounts/update-cookies",
  504. headers={
  505. "Content-Type": "application/json",
  506. "X-Internal-API-Key": internal_api_key,
  507. },
  508. json=payload,
  509. timeout=30,
  510. )
  511. if resp.status_code >= 400:
  512. return False
  513. data = resp.json() if resp.content else {}
  514. return bool(data.get("success", True))
  515. except Exception:
  516. return False
  517. async def ai_suggest_playwright_selector(self, goal: str, screenshot_base64: str = None) -> dict:
  518. import os
  519. import requests
  520. import json
  521. import re
  522. try:
  523. if not screenshot_base64:
  524. screenshot_base64 = await self.capture_screenshot()
  525. if not screenshot_base64:
  526. return {"has_selector": False, "selector": "", "confidence": 0, "notes": "无法获取截图"}
  527. ai_api_key = os.environ.get('DASHSCOPE_API_KEY', '')
  528. ai_base_url = os.environ.get('DASHSCOPE_BASE_URL', 'https://dashscope.aliyuncs.com/compatible-mode/v1')
  529. ai_vision_model = os.environ.get('AI_VISION_MODEL', 'qwen-vl-plus')
  530. if not ai_api_key:
  531. return {"has_selector": False, "selector": "", "confidence": 0, "notes": "未配置 AI API Key"}
  532. prompt = f"""请分析这张网页截图,给出一个 Playwright Python 可用的 selector(用于 page.locator(selector))来完成目标操作。
  533. 目标:{goal}
  534. 要求:
  535. 1) selector 尽量稳定(优先 role/text/aria,其次 class,避免过度依赖随机 class)
  536. 2) selector 必须是 Playwright 支持的选择器语法(如:text="发布"、button:has-text("发布")、[role="button"]:has-text("发布") 等)
  537. 3) 只返回一个最优 selector
  538. 以 JSON 返回:
  539. ```json
  540. {{
  541. "has_selector": true,
  542. "selector": "button:has-text(\\"发布\\")",
  543. "confidence": 0-100,
  544. "notes": "你依据的页面证据"
  545. }}
  546. ```"""
  547. headers = {
  548. 'Authorization': f'Bearer {ai_api_key}',
  549. 'Content-Type': 'application/json'
  550. }
  551. payload = {
  552. "model": ai_vision_model,
  553. "messages": [
  554. {
  555. "role": "user",
  556. "content": [
  557. {
  558. "type": "image_url",
  559. "image_url": {
  560. "url": f"data:image/jpeg;base64,{screenshot_base64}"
  561. }
  562. },
  563. {
  564. "type": "text",
  565. "text": prompt
  566. }
  567. ]
  568. }
  569. ],
  570. "max_tokens": 300
  571. }
  572. response = requests.post(
  573. f"{ai_base_url}/chat/completions",
  574. headers=headers,
  575. json=payload,
  576. timeout=30
  577. )
  578. if response.status_code != 200:
  579. return {"has_selector": False, "selector": "", "confidence": 0, "notes": f"AI API 错误 {response.status_code}"}
  580. result = response.json()
  581. ai_response = result.get('choices', [{}])[0].get('message', {}).get('content', '')
  582. json_match = re.search(r'```json\\s*([\\s\\S]*?)\\s*```', ai_response)
  583. if json_match:
  584. json_str = json_match.group(1)
  585. else:
  586. json_match = re.search(r'\\{[\\s\\S]*\\}', ai_response)
  587. json_str = json_match.group(0) if json_match else '{}'
  588. try:
  589. data = json.loads(json_str)
  590. except Exception:
  591. data = {}
  592. selector = str(data.get("selector", "") or "").strip()
  593. has_selector = bool(data.get("has_selector", False)) and bool(selector)
  594. confidence = int(data.get("confidence", 0) or 0)
  595. notes = str(data.get("notes", "") or "")
  596. if not has_selector:
  597. return {"has_selector": False, "selector": "", "confidence": confidence, "notes": notes or "未给出 selector"}
  598. return {"has_selector": True, "selector": selector, "confidence": confidence, "notes": notes}
  599. except Exception as e:
  600. return {"has_selector": False, "selector": "", "confidence": 0, "notes": f"AI selector 异常: {e}"}
  601. async def ai_check_captcha(self, screenshot_base64: str = None) -> dict:
  602. """
  603. 使用 AI 分析截图检测验证码
  604. Args:
  605. screenshot_base64: 截图的 Base64 编码,如果为空则自动获取当前页面截图
  606. Returns:
  607. dict: {
  608. "has_captcha": bool, # 是否有验证码
  609. "captcha_type": str, # 验证码类型: slider, image, phone, rotate, puzzle
  610. "captcha_description": str, # 验证码描述
  611. "confidence": float, # 置信度 0-100
  612. "need_headful": bool # 是否需要切换到有头浏览器
  613. }
  614. """
  615. import os
  616. import requests
  617. try:
  618. # 获取截图
  619. if not screenshot_base64:
  620. screenshot_base64 = await self.capture_screenshot()
  621. if not screenshot_base64:
  622. print(f"[{self.platform_name}] AI验证码检测: 无法获取截图")
  623. return {
  624. "has_captcha": False,
  625. "captcha_type": "",
  626. "captcha_description": "",
  627. "confidence": 0,
  628. "need_headful": False
  629. }
  630. # 获取 AI 配置
  631. ai_api_key = os.environ.get('DASHSCOPE_API_KEY', '')
  632. ai_base_url = os.environ.get('DASHSCOPE_BASE_URL', 'https://dashscope.aliyuncs.com/compatible-mode/v1')
  633. ai_vision_model = os.environ.get('AI_VISION_MODEL', 'qwen-vl-plus')
  634. if not ai_api_key:
  635. print(f"[{self.platform_name}] AI验证码检测: 未配置 AI API Key,使用传统方式检测")
  636. return await self._traditional_captcha_check()
  637. # 构建 AI 请求
  638. prompt = """请分析这张网页截图,判断页面上是否存在验证码。
  639. 请检查以下类型的验证码:
  640. 1. 滑块验证码(需要滑动滑块到指定位置)
  641. 2. 图片验证码(需要选择正确的图片、点击图片上的文字等)
  642. 3. 旋转验证码(需要旋转图片到正确角度)
  643. 4. 拼图验证码(需要拖动拼图块到正确位置)
  644. 5. 手机验证码(需要输入手机收到的验证码)
  645. 6. 计算验证码(需要输入计算结果)
  646. 请以 JSON 格式返回结果:
  647. ```json
  648. {
  649. "has_captcha": true/false,
  650. "captcha_type": "slider/image/phone/rotate/puzzle/calculate/none",
  651. "captcha_description": "验证码的具体描述",
  652. "confidence": 0-100
  653. }
  654. ```
  655. 注意:
  656. - 如果页面有明显的验证码弹窗或验证区域,has_captcha 为 true
  657. - 如果只是普通的登录页面或表单,没有特殊的验证步骤,has_captcha 为 false
  658. - confidence 表示你对判断结果的信心,100 表示非常确定"""
  659. headers = {
  660. 'Authorization': f'Bearer {ai_api_key}',
  661. 'Content-Type': 'application/json'
  662. }
  663. payload = {
  664. "model": ai_vision_model,
  665. "messages": [
  666. {
  667. "role": "user",
  668. "content": [
  669. {
  670. "type": "image_url",
  671. "image_url": {
  672. "url": f"data:image/jpeg;base64,{screenshot_base64}"
  673. }
  674. },
  675. {
  676. "type": "text",
  677. "text": prompt
  678. }
  679. ]
  680. }
  681. ],
  682. "max_tokens": 500
  683. }
  684. print(f"[{self.platform_name}] AI验证码检测: 正在分析截图...")
  685. response = requests.post(
  686. f"{ai_base_url}/chat/completions",
  687. headers=headers,
  688. json=payload,
  689. timeout=30
  690. )
  691. if response.status_code != 200:
  692. print(f"[{self.platform_name}] AI验证码检测: API 返回错误 {response.status_code}")
  693. return await self._traditional_captcha_check()
  694. result = response.json()
  695. ai_response = result.get('choices', [{}])[0].get('message', {}).get('content', '')
  696. print(f"[{self.platform_name}] AI验证码检测响应: {ai_response[:200]}...")
  697. # 解析 AI 响应
  698. import re
  699. json_match = re.search(r'```json\s*([\s\S]*?)\s*```', ai_response)
  700. if json_match:
  701. json_str = json_match.group(1)
  702. else:
  703. # 尝试直接解析
  704. json_match = re.search(r'\{[\s\S]*\}', ai_response)
  705. if json_match:
  706. json_str = json_match.group(0)
  707. else:
  708. json_str = '{}'
  709. try:
  710. ai_result = json.loads(json_str)
  711. except:
  712. ai_result = {}
  713. has_captcha = ai_result.get('has_captcha', False)
  714. captcha_type = ai_result.get('captcha_type', '')
  715. captcha_description = ai_result.get('captcha_description', '')
  716. confidence = ai_result.get('confidence', 0)
  717. # 如果检测到验证码,需要切换到有头浏览器
  718. need_headful = has_captcha and captcha_type not in ['none', '']
  719. print(f"[{self.platform_name}] AI验证码检测结果: has_captcha={has_captcha}, type={captcha_type}, confidence={confidence}")
  720. return {
  721. "has_captcha": has_captcha,
  722. "captcha_type": captcha_type if captcha_type != 'none' else '',
  723. "captcha_description": captcha_description,
  724. "confidence": confidence,
  725. "need_headful": need_headful
  726. }
  727. except Exception as e:
  728. print(f"[{self.platform_name}] AI验证码检测异常: {e}")
  729. import traceback
  730. traceback.print_exc()
  731. return await self._traditional_captcha_check()
  732. async def _traditional_captcha_check(self) -> dict:
  733. """传统方式检测验证码(基于 DOM 元素)"""
  734. if not self.page:
  735. return {
  736. "has_captcha": False,
  737. "captcha_type": "",
  738. "captcha_description": "",
  739. "confidence": 0,
  740. "need_headful": False
  741. }
  742. try:
  743. # 检查常见的验证码选择器
  744. captcha_selectors = [
  745. # 滑块验证码
  746. ('[class*="slider"]', 'slider', '滑块验证码'),
  747. ('[class*="slide-verify"]', 'slider', '滑块验证码'),
  748. ('text="滑动"', 'slider', '滑块验证码'),
  749. ('text="拖动"', 'slider', '滑块验证码'),
  750. # 图片验证码
  751. ('[class*="captcha"]', 'image', '图片验证码'),
  752. ('[class*="verify-img"]', 'image', '图片验证码'),
  753. ('text="点击"', 'image', '图片验证码'),
  754. ('text="选择"', 'image', '图片验证码'),
  755. # 手机验证码
  756. ('text="验证码"', 'phone', '手机验证码'),
  757. ('text="获取验证码"', 'phone', '手机验证码'),
  758. ('[class*="sms-code"]', 'phone', '手机验证码'),
  759. # 旋转验证码
  760. ('text="旋转"', 'rotate', '旋转验证码'),
  761. ('[class*="rotate"]', 'rotate', '旋转验证码'),
  762. ]
  763. for selector, captcha_type, description in captcha_selectors:
  764. try:
  765. count = await self.page.locator(selector).count()
  766. if count > 0:
  767. # 检查是否可见
  768. element = self.page.locator(selector).first
  769. if await element.is_visible():
  770. print(f"[{self.platform_name}] 传统检测: 发现验证码 - {selector}")
  771. return {
  772. "has_captcha": True,
  773. "captcha_type": captcha_type,
  774. "captcha_description": description,
  775. "confidence": 80,
  776. "need_headful": True
  777. }
  778. except:
  779. pass
  780. return {
  781. "has_captcha": False,
  782. "captcha_type": "",
  783. "captcha_description": "",
  784. "confidence": 80,
  785. "need_headful": False
  786. }
  787. except Exception as e:
  788. print(f"[{self.platform_name}] 传统验证码检测异常: {e}")
  789. return {
  790. "has_captcha": False,
  791. "captcha_type": "",
  792. "captcha_description": "",
  793. "confidence": 0,
  794. "need_headful": False
  795. }
  796. async def get_page_url(self) -> str:
  797. """获取当前页面 URL"""
  798. if not self.page:
  799. return ""
  800. try:
  801. return self.page.url
  802. except:
  803. return ""
  804. async def check_publish_status(self) -> dict:
  805. """
  806. 检查发布状态
  807. 返回: {status, screenshot_base64, page_url, message}
  808. """
  809. if not self.page:
  810. return {"status": "error", "message": "页面未初始化"}
  811. try:
  812. screenshot = await self.capture_screenshot()
  813. page_url = await self.get_page_url()
  814. # 检查常见的成功/失败标志
  815. page_content = await self.page.content()
  816. # 检查成功标志
  817. success_keywords = ['发布成功', '上传成功', '发表成功', '提交成功']
  818. for keyword in success_keywords:
  819. if keyword in page_content:
  820. return {
  821. "status": "success",
  822. "screenshot_base64": screenshot,
  823. "page_url": page_url,
  824. "message": "发布成功"
  825. }
  826. # 检查验证码标志
  827. captcha_keywords = ['验证码', '身份验证', '请完成验证', '滑动验证', '图形验证']
  828. for keyword in captcha_keywords:
  829. if keyword in page_content:
  830. return {
  831. "status": "need_captcha",
  832. "screenshot_base64": screenshot,
  833. "page_url": page_url,
  834. "message": f"检测到{keyword}"
  835. }
  836. # 检查失败标志
  837. fail_keywords = ['发布失败', '上传失败', '提交失败', '操作失败']
  838. for keyword in fail_keywords:
  839. if keyword in page_content:
  840. return {
  841. "status": "failed",
  842. "screenshot_base64": screenshot,
  843. "page_url": page_url,
  844. "message": keyword
  845. }
  846. # 默认返回处理中
  847. return {
  848. "status": "processing",
  849. "screenshot_base64": screenshot,
  850. "page_url": page_url,
  851. "message": "处理中"
  852. }
  853. except Exception as e:
  854. return {
  855. "status": "error",
  856. "screenshot_base64": "",
  857. "page_url": "",
  858. "message": str(e)
  859. }
  860. async def wait_for_upload_complete(self, success_selector: str, timeout: int = 300):
  861. """等待上传完成"""
  862. if not self.page:
  863. raise Exception("Page not initialized")
  864. for _ in range(timeout // 3):
  865. try:
  866. count = await self.page.locator(success_selector).count()
  867. if count > 0:
  868. return True
  869. except:
  870. pass
  871. await asyncio.sleep(3)
  872. self.report_progress(30, "正在上传视频...")
  873. return False
  874. @abstractmethod
  875. async def publish(self, cookies: str, params: PublishParams) -> PublishResult:
  876. """
  877. 发布视频 - 子类必须实现
  878. Args:
  879. cookies: cookie 字符串或 JSON
  880. params: 发布参数
  881. Returns:
  882. PublishResult: 发布结果
  883. """
  884. pass
  885. async def get_works(self, cookies: str, page: int = 0, page_size: int = 20) -> WorksResult:
  886. """
  887. 获取作品列表 - 子类可覆盖实现
  888. Args:
  889. cookies: cookie 字符串或 JSON
  890. page: 页码(从0开始)
  891. page_size: 每页数量
  892. Returns:
  893. WorksResult: 作品列表结果
  894. """
  895. return WorksResult(
  896. success=False,
  897. platform=self.platform_name,
  898. error="该平台暂不支持获取作品列表"
  899. )
  900. async def get_comments(self, cookies: str, work_id: str, cursor: str = "") -> CommentsResult:
  901. """
  902. 获取作品评论 - 子类可覆盖实现
  903. Args:
  904. cookies: cookie 字符串或 JSON
  905. work_id: 作品ID
  906. cursor: 分页游标
  907. Returns:
  908. CommentsResult: 评论列表结果
  909. """
  910. return CommentsResult(
  911. success=False,
  912. platform=self.platform_name,
  913. work_id=work_id,
  914. error="该平台暂不支持获取评论"
  915. )
  916. async def run(self, cookies: str, params: PublishParams) -> PublishResult:
  917. """
  918. 运行发布任务
  919. 包装了 publish 方法,添加了异常处理和资源清理
  920. 发布失败时自动保存截图到 uploads/screenshots 目录
  921. """
  922. try:
  923. result = await self.publish(cookies, params)
  924. if not result.success and self.page:
  925. screenshot_path = await self.save_screenshot_to_file()
  926. if screenshot_path:
  927. result.screenshot_path = screenshot_path
  928. return result
  929. except Exception as e:
  930. import traceback
  931. traceback.print_exc()
  932. screenshot_path = ""
  933. if self.page:
  934. screenshot_path = await self.save_screenshot_to_file()
  935. return PublishResult(
  936. success=False,
  937. platform=self.platform_name,
  938. error=str(e),
  939. screenshot_path=screenshot_path
  940. )
  941. finally:
  942. await self.close_browser()
  943. async def run_get_works(self, cookies: str, page: int = 0, page_size: int = 20) -> WorksResult:
  944. """
  945. 运行获取作品任务
  946. """
  947. try:
  948. return await self.get_works(cookies, page, page_size)
  949. except Exception as e:
  950. import traceback
  951. traceback.print_exc()
  952. return WorksResult(
  953. success=False,
  954. platform=self.platform_name,
  955. error=str(e)
  956. )
  957. finally:
  958. await self.close_browser()
  959. async def run_get_comments(self, cookies: str, work_id: str, cursor: str = "") -> CommentsResult:
  960. """
  961. 运行获取评论任务
  962. """
  963. try:
  964. return await self.get_comments(cookies, work_id, cursor)
  965. except Exception as e:
  966. import traceback
  967. traceback.print_exc()
  968. return CommentsResult(
  969. success=False,
  970. platform=self.platform_name,
  971. work_id=work_id,
  972. error=str(e)
  973. )
  974. finally:
  975. await self.close_browser()
  976. async def check_login_status(self, cookies: str) -> dict:
  977. """
  978. 检查 Cookie 登录状态(通过浏览器访问后台页面检测)
  979. Args:
  980. cookies: cookie 字符串或 JSON
  981. Returns:
  982. dict: {
  983. "success": True,
  984. "valid": True/False,
  985. "need_login": True/False,
  986. "message": "状态描述"
  987. }
  988. """
  989. try:
  990. await self.init_browser()
  991. cookie_list = self.parse_cookies(cookies)
  992. await self.set_cookies(cookie_list)
  993. if not self.page:
  994. raise Exception("Page not initialized")
  995. # 访问平台后台首页
  996. home_url = self.login_url
  997. print(f"[{self.platform_name}] 访问后台页面: {home_url}")
  998. await self.page.goto(home_url, wait_until='domcontentloaded', timeout=30000)
  999. await asyncio.sleep(3)
  1000. # 检查当前 URL 是否被重定向到登录页
  1001. current_url = self.page.url
  1002. print(f"[{self.platform_name}] 当前 URL: {current_url}")
  1003. # 登录页特征
  1004. login_indicators = ['login', 'passport', 'signin', 'auth']
  1005. is_login_page = any(indicator in current_url.lower() for indicator in login_indicators)
  1006. # 检查页面是否有登录弹窗
  1007. need_login = is_login_page
  1008. # 风控/验证码特征
  1009. risk_indicators = ['captcha', 'verify', 'challenge', 'risk', 'security', 'safe', 'protect', 'slider']
  1010. need_verification = any(indicator in current_url.lower() for indicator in risk_indicators)
  1011. if not need_login:
  1012. # 检查页面内容是否有登录提示
  1013. login_selectors = [
  1014. 'text="请先登录"',
  1015. 'text="登录后继续"',
  1016. 'text="请登录"',
  1017. '[class*="login-modal"]',
  1018. '[class*="login-dialog"]',
  1019. '[class*="login-popup"]',
  1020. ]
  1021. for selector in login_selectors:
  1022. try:
  1023. if await self.page.locator(selector).count() > 0:
  1024. need_login = True
  1025. print(f"[{self.platform_name}] 检测到登录弹窗: {selector}")
  1026. break
  1027. except:
  1028. pass
  1029. if not need_login and not need_verification:
  1030. verification_selectors = [
  1031. 'text="安全验证"',
  1032. 'text="验证码"',
  1033. 'text="人机验证"',
  1034. 'text="滑块"',
  1035. 'text="请完成验证"',
  1036. 'text="系统检测到异常"',
  1037. 'text="访问受限"',
  1038. 'text="行为异常"',
  1039. ]
  1040. for selector in verification_selectors:
  1041. try:
  1042. if await self.page.locator(selector).count() > 0:
  1043. need_verification = True
  1044. print(f"[{self.platform_name}] 检测到风控/验证码提示: {selector}")
  1045. break
  1046. except:
  1047. pass
  1048. if need_login:
  1049. return {
  1050. "success": True,
  1051. "valid": False,
  1052. "need_login": True,
  1053. "message": "Cookie 已过期,需要重新登录"
  1054. }
  1055. elif need_verification:
  1056. return {
  1057. "success": True,
  1058. "valid": False,
  1059. "need_login": True,
  1060. "message": "触发风控/需要验证"
  1061. }
  1062. else:
  1063. return {
  1064. "success": True,
  1065. "valid": True,
  1066. "need_login": False,
  1067. "message": "登录状态有效"
  1068. }
  1069. except Exception as e:
  1070. import traceback
  1071. traceback.print_exc()
  1072. return {
  1073. "success": False,
  1074. "valid": False,
  1075. "need_login": True,
  1076. "error": str(e)
  1077. }
  1078. finally:
  1079. await self.close_browser()