base.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. # -*- coding: utf-8 -*-
  2. """
  3. 平台发布基类
  4. 提供通用的发布接口和工具方法
  5. """
  6. import asyncio
  7. import json
  8. import os
  9. from abc import ABC, abstractmethod
  10. from dataclasses import dataclass, field
  11. from datetime import datetime
  12. from typing import List, Optional, Callable, Dict, Any
  13. from playwright.async_api import async_playwright, Browser, BrowserContext, Page
  14. @dataclass
  15. class PublishParams:
  16. """发布参数"""
  17. title: str
  18. video_path: str
  19. description: str = ""
  20. cover_path: Optional[str] = None
  21. tags: List[str] = field(default_factory=list)
  22. publish_date: Optional[datetime] = None
  23. location: str = "重庆市"
  24. def __post_init__(self):
  25. if not self.description:
  26. self.description = self.title
  27. @dataclass
  28. class PublishResult:
  29. """发布结果"""
  30. success: bool
  31. platform: str
  32. video_id: str = ""
  33. video_url: str = ""
  34. message: str = ""
  35. error: str = ""
  36. need_captcha: bool = False # 是否需要验证码
  37. captcha_type: str = "" # 验证码类型: phone, slider, image
  38. @dataclass
  39. class WorkItem:
  40. """作品数据"""
  41. work_id: str
  42. title: str
  43. cover_url: str = ""
  44. video_url: str = ""
  45. duration: int = 0 # 秒
  46. status: str = "published" # published, reviewing, rejected, draft
  47. publish_time: str = ""
  48. play_count: int = 0
  49. like_count: int = 0
  50. comment_count: int = 0
  51. share_count: int = 0
  52. collect_count: int = 0
  53. def to_dict(self) -> Dict[str, Any]:
  54. return {
  55. "work_id": self.work_id,
  56. "title": self.title,
  57. "cover_url": self.cover_url,
  58. "video_url": self.video_url,
  59. "duration": self.duration,
  60. "status": self.status,
  61. "publish_time": self.publish_time,
  62. "play_count": self.play_count,
  63. "like_count": self.like_count,
  64. "comment_count": self.comment_count,
  65. "share_count": self.share_count,
  66. "collect_count": self.collect_count,
  67. }
  68. @dataclass
  69. class CommentItem:
  70. """评论数据"""
  71. comment_id: str
  72. work_id: str
  73. content: str
  74. author_id: str = ""
  75. author_name: str = ""
  76. author_avatar: str = ""
  77. like_count: int = 0
  78. reply_count: int = 0
  79. create_time: str = ""
  80. is_author: bool = False # 是否是作者的评论
  81. replies: List['CommentItem'] = field(default_factory=list)
  82. def to_dict(self) -> Dict[str, Any]:
  83. return {
  84. "comment_id": self.comment_id,
  85. "work_id": self.work_id,
  86. "content": self.content,
  87. "author_id": self.author_id,
  88. "author_name": self.author_name,
  89. "author_avatar": self.author_avatar,
  90. "like_count": self.like_count,
  91. "reply_count": self.reply_count,
  92. "create_time": self.create_time,
  93. "is_author": self.is_author,
  94. "replies": [r.to_dict() for r in self.replies],
  95. }
  96. @dataclass
  97. class WorksResult:
  98. """作品列表结果"""
  99. success: bool
  100. platform: str
  101. works: List[WorkItem] = field(default_factory=list)
  102. total: int = 0
  103. has_more: bool = False
  104. error: str = ""
  105. def to_dict(self) -> Dict[str, Any]:
  106. return {
  107. "success": self.success,
  108. "platform": self.platform,
  109. "works": [w.to_dict() for w in self.works],
  110. "total": self.total,
  111. "has_more": self.has_more,
  112. "error": self.error,
  113. }
  114. @dataclass
  115. class CommentsResult:
  116. """评论列表结果"""
  117. success: bool
  118. platform: str
  119. work_id: str
  120. comments: List[CommentItem] = field(default_factory=list)
  121. total: int = 0
  122. has_more: bool = False
  123. error: str = ""
  124. def to_dict(self) -> Dict[str, Any]:
  125. return {
  126. "success": self.success,
  127. "platform": self.platform,
  128. "work_id": self.work_id,
  129. "comments": [c.to_dict() for c in self.comments],
  130. "total": self.total,
  131. "has_more": self.has_more,
  132. "error": self.error,
  133. }
  134. class BasePublisher(ABC):
  135. """
  136. 平台发布基类
  137. 所有平台发布器都需要继承此类
  138. """
  139. platform_name: str = "base"
  140. login_url: str = ""
  141. publish_url: str = ""
  142. cookie_domain: str = ""
  143. def __init__(self, headless: bool = True):
  144. self.headless = headless
  145. self.browser: Optional[Browser] = None
  146. self.context: Optional[BrowserContext] = None
  147. self.page: Optional[Page] = None
  148. self.on_progress: Optional[Callable[[int, str], None]] = None
  149. def set_progress_callback(self, callback: Callable[[int, str], None]):
  150. """设置进度回调"""
  151. self.on_progress = callback
  152. def report_progress(self, progress: int, message: str):
  153. """报告进度"""
  154. print(f"[{self.platform_name}] [{progress}%] {message}")
  155. if self.on_progress:
  156. self.on_progress(progress, message)
  157. @staticmethod
  158. def parse_cookies(cookies_str: str) -> list:
  159. """解析 cookie 字符串为列表"""
  160. try:
  161. cookies = json.loads(cookies_str)
  162. if isinstance(cookies, list):
  163. return cookies
  164. except json.JSONDecodeError:
  165. pass
  166. # 字符串格式: name=value; name2=value2
  167. cookies = []
  168. for item in cookies_str.split(';'):
  169. item = item.strip()
  170. if '=' in item:
  171. name, value = item.split('=', 1)
  172. cookies.append({
  173. 'name': name.strip(),
  174. 'value': value.strip(),
  175. 'domain': '',
  176. 'path': '/'
  177. })
  178. return cookies
  179. @staticmethod
  180. def cookies_to_string(cookies: list) -> str:
  181. """将 cookie 列表转换为字符串"""
  182. return '; '.join([f"{c['name']}={c['value']}" for c in cookies])
  183. async def init_browser(self, storage_state: str = None):
  184. """初始化浏览器"""
  185. playwright = await async_playwright().start()
  186. self.browser = await playwright.chromium.launch(headless=self.headless)
  187. if storage_state and os.path.exists(storage_state):
  188. self.context = await self.browser.new_context(storage_state=storage_state)
  189. else:
  190. self.context = await self.browser.new_context()
  191. self.page = await self.context.new_page()
  192. return self.page
  193. async def set_cookies(self, cookies: list):
  194. """设置 cookies"""
  195. if not self.context:
  196. raise Exception("Browser context not initialized")
  197. # 设置默认域名
  198. for cookie in cookies:
  199. if 'domain' not in cookie or not cookie['domain']:
  200. cookie['domain'] = self.cookie_domain
  201. await self.context.add_cookies(cookies)
  202. async def close_browser(self):
  203. """关闭浏览器"""
  204. if self.context:
  205. await self.context.close()
  206. if self.browser:
  207. await self.browser.close()
  208. async def save_cookies(self, file_path: str):
  209. """保存 cookies 到文件"""
  210. if self.context:
  211. await self.context.storage_state(path=file_path)
  212. async def wait_for_upload_complete(self, success_selector: str, timeout: int = 300):
  213. """等待上传完成"""
  214. if not self.page:
  215. raise Exception("Page not initialized")
  216. for _ in range(timeout // 3):
  217. try:
  218. count = await self.page.locator(success_selector).count()
  219. if count > 0:
  220. return True
  221. except:
  222. pass
  223. await asyncio.sleep(3)
  224. self.report_progress(30, "正在上传视频...")
  225. return False
  226. @abstractmethod
  227. async def publish(self, cookies: str, params: PublishParams) -> PublishResult:
  228. """
  229. 发布视频 - 子类必须实现
  230. Args:
  231. cookies: cookie 字符串或 JSON
  232. params: 发布参数
  233. Returns:
  234. PublishResult: 发布结果
  235. """
  236. pass
  237. async def get_works(self, cookies: str, page: int = 0, page_size: int = 20) -> WorksResult:
  238. """
  239. 获取作品列表 - 子类可覆盖实现
  240. Args:
  241. cookies: cookie 字符串或 JSON
  242. page: 页码(从0开始)
  243. page_size: 每页数量
  244. Returns:
  245. WorksResult: 作品列表结果
  246. """
  247. return WorksResult(
  248. success=False,
  249. platform=self.platform_name,
  250. error="该平台暂不支持获取作品列表"
  251. )
  252. async def get_comments(self, cookies: str, work_id: str, cursor: str = "") -> CommentsResult:
  253. """
  254. 获取作品评论 - 子类可覆盖实现
  255. Args:
  256. cookies: cookie 字符串或 JSON
  257. work_id: 作品ID
  258. cursor: 分页游标
  259. Returns:
  260. CommentsResult: 评论列表结果
  261. """
  262. return CommentsResult(
  263. success=False,
  264. platform=self.platform_name,
  265. work_id=work_id,
  266. error="该平台暂不支持获取评论"
  267. )
  268. async def run(self, cookies: str, params: PublishParams) -> PublishResult:
  269. """
  270. 运行发布任务
  271. 包装了 publish 方法,添加了异常处理和资源清理
  272. """
  273. try:
  274. return await self.publish(cookies, params)
  275. except Exception as e:
  276. import traceback
  277. traceback.print_exc()
  278. return PublishResult(
  279. success=False,
  280. platform=self.platform_name,
  281. error=str(e)
  282. )
  283. finally:
  284. await self.close_browser()
  285. async def run_get_works(self, cookies: str, page: int = 0, page_size: int = 20) -> WorksResult:
  286. """
  287. 运行获取作品任务
  288. """
  289. try:
  290. return await self.get_works(cookies, page, page_size)
  291. except Exception as e:
  292. import traceback
  293. traceback.print_exc()
  294. return WorksResult(
  295. success=False,
  296. platform=self.platform_name,
  297. error=str(e)
  298. )
  299. finally:
  300. await self.close_browser()
  301. async def run_get_comments(self, cookies: str, work_id: str, cursor: str = "") -> CommentsResult:
  302. """
  303. 运行获取评论任务
  304. """
  305. try:
  306. return await self.get_comments(cookies, work_id, cursor)
  307. except Exception as e:
  308. import traceback
  309. traceback.print_exc()
  310. return CommentsResult(
  311. success=False,
  312. platform=self.platform_name,
  313. work_id=work_id,
  314. error=str(e)
  315. )
  316. finally:
  317. await self.close_browser()