baijiahao.py 183 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772
  1. # -*- coding: utf-8 -*-
  2. """
  3. 百家号视频发布器
  4. """
  5. import asyncio
  6. import json
  7. from typing import List
  8. from datetime import datetime
  9. from .base import (
  10. BasePublisher, PublishParams, PublishResult,
  11. WorkItem, WorksResult, CommentItem, CommentsResult
  12. )
  13. class BaijiahaoPublisher(BasePublisher):
  14. """
  15. 百家号视频发布器
  16. 使用 Playwright 自动化操作百家号创作者中心
  17. """
  18. platform_name = "baijiahao"
  19. login_url = "https://baijiahao.baidu.com/"
  20. publish_url = "https://baijiahao.baidu.com/builder/rc/edit?type=video"
  21. cookie_domain = ".baidu.com"
  22. # 登录检测配置
  23. login_check_url = "https://baijiahao.baidu.com/builder/rc/home"
  24. login_indicators = ["passport.baidu.com", "/login", "wappass.baidu.com"]
  25. login_selectors = ['text="登录"', 'text="请登录"', '[class*="login-btn"]']
  26. async def get_account_info(self, cookies: str) -> dict:
  27. """
  28. 获取百家号账号信息
  29. 使用直接 HTTP API 调用,不使用浏览器
  30. """
  31. import aiohttp
  32. print(f"\n{'='*60}")
  33. print(f"[{self.platform_name}] 获取账号信息 (使用 API)")
  34. print(f"{'='*60}")
  35. try:
  36. # 解析 cookies
  37. cookie_list = self.parse_cookies(cookies)
  38. cookie_dict = {c['name']: c['value'] for c in cookie_list}
  39. # 重要:百家号需要先访问主页建立会话上下文
  40. print(f"[{self.platform_name}] 第一步:访问主页建立会话...")
  41. session_headers = {
  42. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
  43. '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',
  44. # Cookie 由 session 管理,不手动设置
  45. 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
  46. 'Accept-Encoding': 'gzip, deflate, br',
  47. 'Connection': 'keep-alive',
  48. 'Upgrade-Insecure-Requests': '1',
  49. 'Sec-Fetch-Dest': 'document',
  50. 'Sec-Fetch-Mode': 'navigate',
  51. 'Sec-Fetch-Site': 'none',
  52. 'Sec-Fetch-User': '?1',
  53. 'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
  54. 'sec-ch-ua-mobile': '?0',
  55. 'sec-ch-ua-platform': '"Windows"'
  56. }
  57. headers = {
  58. 'Accept': 'application/json, text/plain, */*',
  59. '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',
  60. # Cookie 由 session 管理,不手动设置
  61. 'Referer': 'https://baijiahao.baidu.com/builder/rc/home',
  62. 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
  63. 'Accept-Encoding': 'gzip, deflate, br',
  64. 'Connection': 'keep-alive',
  65. 'Sec-Fetch-Dest': 'empty',
  66. 'Sec-Fetch-Mode': 'cors',
  67. 'Sec-Fetch-Site': 'same-origin',
  68. 'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
  69. 'sec-ch-ua-mobile': '?0',
  70. 'sec-ch-ua-platform': '"Windows"'
  71. }
  72. # 使用 cookies 参数初始化 session,让 aiohttp 自动管理 cookie 更新
  73. async with aiohttp.ClientSession(cookies=cookie_dict) as session:
  74. # 步骤 0: 先访问主页建立会话上下文(关键步骤!)
  75. print(f"[{self.platform_name}] [0/4] 访问主页建立会话上下文...")
  76. async with session.get(
  77. 'https://baijiahao.baidu.com/builder/rc/home',
  78. headers=session_headers,
  79. timeout=aiohttp.ClientTimeout(total=30)
  80. ) as home_response:
  81. home_status = home_response.status
  82. print(f"[{self.platform_name}] 主页访问状态: {home_status}")
  83. # 获取响应头中的新cookies(如果有)
  84. if 'Set-Cookie' in home_response.headers:
  85. new_cookies = home_response.headers['Set-Cookie']
  86. print(f"[{self.platform_name}] 获取到新的会话Cookie")
  87. # 这里可以处理新的cookies,但暂时跳过复杂处理
  88. # 短暂等待确保会话建立
  89. await asyncio.sleep(1)
  90. # 步骤 1: 获取账号基本信息
  91. print(f"[{self.platform_name}] [1/4] 调用 appinfo API...")
  92. async with session.get(
  93. 'https://baijiahao.baidu.com/builder/app/appinfo',
  94. headers=headers,
  95. timeout=aiohttp.ClientTimeout(total=30)
  96. ) as response:
  97. appinfo_result = await response.json()
  98. print(f"[{self.platform_name}] appinfo API 完整响应: {json.dumps(appinfo_result, ensure_ascii=False)[:500]}")
  99. print(f"[{self.platform_name}] appinfo API 响应: errno={appinfo_result.get('errno')}")
  100. # 检查登录状态
  101. if appinfo_result.get('errno') != 0:
  102. error_msg = appinfo_result.get('errmsg', '未知错误')
  103. errno = appinfo_result.get('errno')
  104. print(f"[{self.platform_name}] API 返回错误: errno={errno}, msg={error_msg}")
  105. # errno 110 表示未登录
  106. if errno == 110:
  107. return {
  108. "success": False,
  109. "error": "Cookie 已失效,需要重新登录",
  110. "need_login": True
  111. }
  112. # errno 10001402 表示分散认证问题,尝试重新访问主页后重试
  113. if errno == 10001402:
  114. print(f"[{self.platform_name}] 检测到分散认证问题,尝试重新访问主页...")
  115. await asyncio.sleep(2)
  116. # 重新访问主页
  117. async with session.get(
  118. 'https://baijiahao.baidu.com/builder/rc/home',
  119. headers=session_headers,
  120. timeout=aiohttp.ClientTimeout(total=30)
  121. ) as retry_home_response:
  122. print(f"[{self.platform_name}] 重新访问主页状态: {retry_home_response.status}")
  123. await asyncio.sleep(1)
  124. # 重试 API 调用
  125. async with session.get(
  126. 'https://baijiahao.baidu.com/builder/app/appinfo',
  127. headers=headers,
  128. timeout=aiohttp.ClientTimeout(total=30)
  129. ) as retry_response:
  130. retry_result = await retry_response.json()
  131. if retry_result.get('errno') == 0:
  132. print(f"[{self.platform_name}] 分散认证问题已解决")
  133. # 使用重试成功的结果继续处理
  134. appinfo_result = retry_result
  135. else:
  136. print(f"[{self.platform_name}] 重试仍然失败")
  137. return {
  138. "success": False,
  139. "error": f"分散认证问题: {error_msg}",
  140. "need_login": True
  141. }
  142. return {
  143. "success": False,
  144. "error": error_msg,
  145. "need_login": True
  146. }
  147. # 获取用户数据
  148. user_data = appinfo_result.get('data', {}).get('user', {})
  149. if not user_data:
  150. return {
  151. "success": False,
  152. "error": "无法获取用户信息",
  153. "need_login": True
  154. }
  155. # 检查账号状态
  156. status = user_data.get('status', '')
  157. # 有效的账号状态:audit(审核中), pass(已通过), normal(正常), newbie(新手)
  158. valid_statuses = ['audit', 'pass', 'normal', 'newbie']
  159. if status not in valid_statuses:
  160. print(f"[{self.platform_name}] 账号状态异常: {status}")
  161. # 提取基本信息
  162. account_name = user_data.get('name') or user_data.get('uname') or '百家号账号'
  163. app_id = user_data.get('app_id') or user_data.get('id', 0)
  164. account_id = str(app_id) if app_id else f"baijiahao_{int(datetime.now().timestamp() * 1000)}"
  165. # 处理头像 URL
  166. avatar_url = user_data.get('avatar') or user_data.get('avatar_unify', '')
  167. if avatar_url and avatar_url.startswith('//'):
  168. avatar_url = 'https:' + avatar_url
  169. print(f"[{self.platform_name}] 账号名称: {account_name}, ID: {account_id}")
  170. # 步骤 2: 获取粉丝数(非关键,失败不影响整体)
  171. fans_count = 0
  172. try:
  173. print(f"[{self.platform_name}] [2/3] 调用 growth/get_info API 获取粉丝数...")
  174. async with session.get(
  175. 'https://baijiahao.baidu.com/cms-ui/rights/growth/get_info',
  176. headers=headers,
  177. timeout=aiohttp.ClientTimeout(total=10)
  178. ) as response:
  179. growth_result = await response.json()
  180. if growth_result.get('errno') == 0:
  181. growth_data = growth_result.get('data', {})
  182. fans_count = int(growth_data.get('fans_num', 0))
  183. print(f"[{self.platform_name}] 粉丝数: {fans_count}")
  184. else:
  185. print(f"[{self.platform_name}] 获取粉丝数失败: {growth_result.get('errmsg')}")
  186. except Exception as e:
  187. print(f"[{self.platform_name}] 获取粉丝数异常(非关键): {e}")
  188. # 步骤 3: 获取作品数量(使用与 Node 端一致的 API)
  189. works_count = 0
  190. try:
  191. print(f"[{self.platform_name}] [3/3] 调用 article/lists API 获取作品数...")
  192. # 使用与 Node 端一致的 API 参数
  193. list_url = 'https://baijiahao.baidu.com/pcui/article/lists?currentPage=1&pageSize=20&search=&type=&collection=&startDate=&endDate=&clearBeforeFetch=false&dynamic=0'
  194. async with session.get(
  195. list_url,
  196. headers={
  197. 'accept': '*/*',
  198. 'user-agent': 'PostmanRuntime/7.51.0',
  199. # cookie 由 session 管理
  200. 'referer': 'https://baijiahao.baidu.com/builder/rc/content',
  201. 'connection': 'keep-alive',
  202. 'accept-encoding': 'gzip, deflate, br',
  203. },
  204. timeout=aiohttp.ClientTimeout(total=30)
  205. ) as response:
  206. response_text = await response.text()
  207. print(f"[{self.platform_name}] ========== Works API Response ==========")
  208. print(f"[{self.platform_name}] Full response: {response_text[:1000]}...") # 只打印前1000字符
  209. print(f"[{self.platform_name}] =========================================")
  210. works_result = json.loads(response_text)
  211. # 处理分散认证问题 (errno=10001402),重试一次
  212. if works_result.get('errno') == 10001402:
  213. print(f"[{self.platform_name}] 分散认证问题 (errno=10001402),3秒后重试...")
  214. await asyncio.sleep(3)
  215. # 重试一次,使用更完整的请求头
  216. retry_headers = headers.copy()
  217. retry_headers.update({
  218. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
  219. 'Cache-Control': 'max-age=0',
  220. 'Upgrade-Insecure-Requests': '1',
  221. })
  222. async with session.get(
  223. list_url,
  224. headers=retry_headers,
  225. timeout=aiohttp.ClientTimeout(total=30)
  226. ) as retry_response:
  227. retry_text = await retry_response.text()
  228. print(f"[{self.platform_name}] ========== Works API Retry Response ==========")
  229. print(f"[{self.platform_name}] Full retry response: {retry_text[:1000]}...")
  230. print(f"[{self.platform_name}] ===============================================")
  231. works_result = json.loads(retry_text)
  232. if works_result.get('errno') == 10001402:
  233. print(f"[{self.platform_name}] 重试仍然失败,返回已获取的账号信息")
  234. works_result = None
  235. if works_result and works_result.get('errno') == 0:
  236. works_data = works_result.get('data', {})
  237. # 优先使用 data.page.totalCount,如果没有则使用 data.total(兼容旧格式)
  238. page_info = works_data.get('page', {})
  239. works_count = int(page_info.get('totalCount', works_data.get('total', 0)))
  240. print(f"[{self.platform_name}] 作品数: {works_count} (from page.totalCount: {page_info.get('totalCount')}, from total: {works_data.get('total')})")
  241. else:
  242. errno = works_result.get('errno') if works_result else 'unknown'
  243. errmsg = works_result.get('errmsg', 'unknown error') if works_result else 'no response'
  244. print(f"[{self.platform_name}] 获取作品数失败: errno={errno}, errmsg={errmsg}")
  245. except Exception as e:
  246. import traceback
  247. print(f"[{self.platform_name}] 获取作品数异常(非关键): {e}")
  248. traceback.print_exc()
  249. # 返回账号信息
  250. account_info = {
  251. "success": True,
  252. "account_id": account_id,
  253. "account_name": account_name,
  254. "avatar_url": avatar_url,
  255. "fans_count": fans_count,
  256. "works_count": works_count,
  257. }
  258. print(f"[{self.platform_name}] ✓ 获取成功: {account_name} (粉丝: {fans_count}, 作品: {works_count})")
  259. return account_info
  260. except Exception as e:
  261. import traceback
  262. traceback.print_exc()
  263. return {
  264. "success": False,
  265. "error": str(e)
  266. }
  267. async def check_captcha(self) -> dict:
  268. """检查页面是否需要验证码"""
  269. if not self.page:
  270. return {'need_captcha': False, 'captcha_type': ''}
  271. try:
  272. # 检查各种验证码
  273. captcha_selectors = [
  274. 'text="请输入验证码"',
  275. 'text="滑动验证"',
  276. '[class*="captcha"]',
  277. '[class*="verify"]',
  278. ]
  279. for selector in captcha_selectors:
  280. try:
  281. if await self.page.locator(selector).count() > 0:
  282. print(f"[{self.platform_name}] 检测到验证码: {selector}")
  283. return {'need_captcha': True, 'captcha_type': 'image'}
  284. except:
  285. pass
  286. # 检查登录弹窗
  287. login_selectors = [
  288. 'text="请登录"',
  289. 'text="登录后继续"',
  290. '[class*="login-dialog"]',
  291. ]
  292. for selector in login_selectors:
  293. try:
  294. if await self.page.locator(selector).count() > 0:
  295. print(f"[{self.platform_name}] 检测到需要登录: {selector}")
  296. return {'need_captcha': True, 'captcha_type': 'login'}
  297. except:
  298. pass
  299. except Exception as e:
  300. print(f"[{self.platform_name}] 验证码检测异常: {e}")
  301. return {'need_captcha': False, 'captcha_type': ''}
  302. async def _ai_analyze_upload_state(self, screenshot_base64: str = None) -> dict:
  303. """
  304. 使用 AI 识别当前上传状态,返回:
  305. {
  306. status: completed|uploading|failed|unknown,
  307. progress: int|None,
  308. confidence: int,
  309. reason: str,
  310. should_enter_publish_form: bool
  311. }
  312. """
  313. import os
  314. import ast
  315. import re
  316. import requests
  317. result = {
  318. "status": "unknown",
  319. "progress": None,
  320. "confidence": 0,
  321. "reason": "",
  322. "should_enter_publish_form": False,
  323. }
  324. try:
  325. if not screenshot_base64:
  326. screenshot_base64 = await self.capture_screenshot()
  327. if not screenshot_base64:
  328. result["reason"] = "no-screenshot"
  329. return result
  330. ai_api_key = os.environ.get('DASHSCOPE_API_KEY', '')
  331. ai_base_url = os.environ.get('DASHSCOPE_BASE_URL', 'https://dashscope.aliyuncs.com/compatible-mode/v1')
  332. ai_vision_model = os.environ.get('AI_VISION_MODEL', 'qwen-vl-plus')
  333. if not ai_api_key:
  334. result["reason"] = "no-ai-key"
  335. return result
  336. prompt = """请分析这张“百家号视频发布页”截图,判断视频上传状态。
  337. 请只返回 JSON:
  338. {
  339. "status": "completed|uploading|failed|unknown",
  340. "progress": 0-100 或 null,
  341. "confidence": 0-100,
  342. "reason": "一句话证据",
  343. "should_enter_publish_form": true/false
  344. }
  345. 判定规则:
  346. 1) status=completed:
  347. - 出现“上传完成/处理完成/可发布/可填写标题描述/发布按钮可用”等信号
  348. - 或者明显已进入可填写发布信息的阶段
  349. 2) status=uploading:
  350. - 出现“上传中/处理中/转码中/xx%/请稍候”等
  351. 3) status=failed:
  352. - 出现“上传失败/处理失败/格式不支持/文件异常”等明确失败文案
  353. 4) should_enter_publish_form=true:
  354. - 画面显示“去发布/下一步/继续/完成编辑”等入口,且看起来应点击进入正式发布表单
  355. """
  356. headers = {
  357. 'Authorization': f'Bearer {ai_api_key}',
  358. 'Content-Type': 'application/json'
  359. }
  360. payload = {
  361. "model": ai_vision_model,
  362. "messages": [
  363. {
  364. "role": "user",
  365. "content": [
  366. {
  367. "type": "image_url",
  368. "image_url": {
  369. "url": f"data:image/jpeg;base64,{screenshot_base64}"
  370. }
  371. },
  372. {
  373. "type": "text",
  374. "text": prompt
  375. }
  376. ]
  377. }
  378. ],
  379. "max_tokens": 400
  380. }
  381. response = requests.post(
  382. f"{ai_base_url}/chat/completions",
  383. headers=headers,
  384. json=payload,
  385. timeout=30
  386. )
  387. if response.status_code != 200:
  388. result["reason"] = f"ai-http-{response.status_code}"
  389. return result
  390. response_json = response.json()
  391. ai_response = response_json.get('choices', [{}])[0].get('message', {}).get('content', '')
  392. json_match = re.search(r'```json\\s*([\\s\\S]*?)\\s*```', ai_response)
  393. if json_match:
  394. json_str = json_match.group(1)
  395. else:
  396. json_match = re.search(r'\\{[\\s\\S]*\\}', ai_response)
  397. json_str = json_match.group(0) if json_match else '{}'
  398. try:
  399. data = json.loads(json_str)
  400. except Exception:
  401. try:
  402. data = ast.literal_eval(json_str) if json_str and json_str != '{}' else {}
  403. if not isinstance(data, dict):
  404. data = {}
  405. except Exception:
  406. data = {}
  407. # 兼容中文 key / 非标准结构
  408. status_hint = str(
  409. data.get("status")
  410. or data.get("状态")
  411. or ""
  412. ).strip()
  413. status_raw = status_hint.lower()
  414. if (
  415. status_raw in ["complete", "completed", "success", "done", "finished", "ready"]
  416. or any(k in status_hint for k in ["完成", "成功", "可发布", "已上传"])
  417. ):
  418. status = "completed"
  419. elif (
  420. status_raw in ["uploading", "processing", "in_progress", "progress", "running"]
  421. or any(k in status_hint for k in ["上传中", "处理中", "转码", "进行中", "上传"])
  422. ):
  423. status = "uploading"
  424. elif (
  425. status_raw in ["failed", "error", "fail"]
  426. or any(k in status_hint for k in ["失败", "错误", "异常"])
  427. ):
  428. status = "failed"
  429. else:
  430. status = "unknown"
  431. progress = data.get("progress", data.get("进度", None))
  432. parsed_progress = None
  433. try:
  434. if progress is not None and str(progress).strip() != "":
  435. parsed_progress = max(0, min(100, int(float(progress))))
  436. except Exception:
  437. parsed_progress = None
  438. if parsed_progress is None:
  439. try:
  440. p_match = re.search(r'(\d{1,3})\s*%', ai_response or '')
  441. if p_match:
  442. parsed_progress = max(0, min(100, int(p_match.group(1))))
  443. except Exception:
  444. parsed_progress = None
  445. confidence = 0
  446. try:
  447. confidence = max(0, min(100, int(float(data.get("confidence", data.get("置信度", 0)) or 0))))
  448. except Exception:
  449. confidence = 0
  450. reason = str(data.get("reason", data.get("原因", "")) or "").strip()
  451. should_enter_raw = data.get(
  452. "should_enter_publish_form",
  453. data.get("是否进入发布表单", False)
  454. )
  455. if isinstance(should_enter_raw, bool):
  456. should_enter = should_enter_raw
  457. else:
  458. should_enter_text = str(should_enter_raw or "").strip().lower()
  459. should_enter = should_enter_text in ["true", "1", "yes", "y", "是"]
  460. # 当 AI 响应不是严格 JSON 时,按全文关键词推断
  461. response_text = str(ai_response or "")
  462. response_lower = response_text.lower()
  463. if status == "unknown":
  464. if any(k in response_text for k in ["上传完成", "处理完成", "上传成功", "可发布", "已完成"]):
  465. status = "completed"
  466. elif any(k in response_text for k in ["上传失败", "处理失败", "格式不支持", "文件异常", "失败"]):
  467. status = "failed"
  468. elif any(k in response_text for k in ["上传中", "处理中", "转码中", "请稍候"]) or re.search(r'(\d{1,3})\s*%', response_text):
  469. status = "uploading"
  470. if not should_enter and any(k in response_text for k in ["去发布", "下一步", "继续", "完成编辑"]):
  471. should_enter = True
  472. if not reason and response_text:
  473. reason = response_text.replace("\n", " ").strip()[:120]
  474. if confidence <= 0 and status != "unknown":
  475. confidence = 60
  476. # 二次语义修正
  477. if status == "uploading" and parsed_progress is not None and parsed_progress >= 100:
  478. status = "completed"
  479. should_enter = True
  480. # AI 有时会把 99/100 仍写成 uploading,这里做语义修正
  481. if status == "uploading" and parsed_progress is not None and parsed_progress >= 99 and confidence >= 60:
  482. status = "completed"
  483. should_enter = True
  484. return {
  485. "status": status,
  486. "progress": parsed_progress,
  487. "confidence": confidence,
  488. "reason": reason,
  489. "should_enter_publish_form": should_enter,
  490. }
  491. except Exception as e:
  492. result["reason"] = f"ai-exception:{e}"
  493. return result
  494. async def _extract_bjh_token(self) -> str:
  495. """从页面上下文提取百家号接口 token。"""
  496. if not self.page:
  497. return ""
  498. try:
  499. token = await self.page.evaluate(
  500. """
  501. () => {
  502. const isJwtLike = (v) => {
  503. if (!v || typeof v !== 'string') return false;
  504. const s = v.trim();
  505. if (s.length < 60) return false;
  506. const parts = s.split('.');
  507. if (parts.length !== 3) return false;
  508. return parts.every(p => /^[A-Za-z0-9_-]+$/.test(p) && p.length > 10);
  509. };
  510. const pickFromStorage = (storage) => {
  511. try {
  512. const keys = Object.keys(storage || {});
  513. for (const k of keys) {
  514. const v = storage.getItem(k);
  515. if (isJwtLike(v)) return v;
  516. }
  517. } catch {}
  518. return "";
  519. };
  520. let t = pickFromStorage(window.localStorage);
  521. if (t) return t;
  522. t = pickFromStorage(window.sessionStorage);
  523. if (t) return t;
  524. const meta = document.querySelector('meta[name="token"], meta[name="bjh-token"]');
  525. const metaToken = meta && meta.getAttribute('content');
  526. if (isJwtLike(metaToken)) return metaToken;
  527. const candidates = [
  528. (window.__INITIAL_STATE__ && window.__INITIAL_STATE__.token) || "",
  529. (window.__PRELOADED_STATE__ && window.__PRELOADED_STATE__.token) || "",
  530. (window.__NUXT__ && window.__NUXT__.state && window.__NUXT__.state.token) || "",
  531. ];
  532. for (const c of candidates) {
  533. if (isJwtLike(c)) return c;
  534. }
  535. return "";
  536. }
  537. """
  538. )
  539. if token:
  540. return str(token)
  541. except Exception:
  542. pass
  543. try:
  544. import re
  545. html = await self.page.content()
  546. m = re.search(r'([A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,})', html)
  547. if m:
  548. return m.group(1)
  549. except Exception:
  550. pass
  551. return ""
  552. async def _verify_publish_from_content_page(self, expected_title: str, page_size: int = 20) -> bool:
  553. """
  554. 到内容管理页调用列表接口,按标题二次确认是否已发布。
  555. """
  556. if not self.page:
  557. return False
  558. try:
  559. content_url = (
  560. "https://baijiahao.baidu.com/builder/rc/content"
  561. f"?currentPage=1&pageSize={int(page_size)}"
  562. "&search=&type=&collection=&startDate=&endDate="
  563. )
  564. await self.page.goto(content_url, wait_until="domcontentloaded", timeout=60000)
  565. await asyncio.sleep(2)
  566. token = await self._extract_bjh_token()
  567. expected = (expected_title or "").strip()
  568. if not expected:
  569. return False
  570. fetch_result = await self.page.evaluate(
  571. """
  572. async ({ token, pageSize }) => {
  573. const url =
  574. "https://baijiahao.baidu.com/pcui/article/lists" +
  575. "?currentPage=1" +
  576. `&pageSize=${pageSize}` +
  577. "&search=&type=&collection=&startDate=&endDate=" +
  578. "&clearBeforeFetch=false&dynamic=1";
  579. const r = await fetch(url, {
  580. method: "GET",
  581. credentials: "include",
  582. headers: {
  583. "accept": "application/json, text/plain, */*",
  584. ...(token ? { token } : {}),
  585. },
  586. });
  587. const text = await r.text();
  588. return { ok: r.ok, status: r.status, text };
  589. }
  590. """,
  591. {"token": token, "pageSize": int(page_size)}
  592. )
  593. if not fetch_result or not fetch_result.get("ok"):
  594. status = fetch_result.get("status") if isinstance(fetch_result, dict) else "unknown"
  595. print(f"[{self.platform_name}] 内容页校验接口失败: HTTP {status}")
  596. return False
  597. data = json.loads(fetch_result.get("text") or "{}")
  598. if data.get("errno") != 0:
  599. print(f"[{self.platform_name}] 内容页校验接口错误: errno={data.get('errno')}, msg={data.get('errmsg')}")
  600. return False
  601. items = ((data.get("data") or {}).get("list") or [])
  602. if not isinstance(items, list) or not items:
  603. print(f"[{self.platform_name}] 内容页校验:当前列表为空")
  604. return False
  605. # 标题匹配采用“全量相等 + 前缀包含”双策略,兼容平台侧自动截断。
  606. expected_variants = {expected}
  607. if len(expected) > 12:
  608. expected_variants.add(expected[:12])
  609. if len(expected) > 20:
  610. expected_variants.add(expected[:20])
  611. for item in items:
  612. title = str(item.get("title") or "").strip()
  613. if not title:
  614. continue
  615. for needle in expected_variants:
  616. if needle and (title == needle or needle in title):
  617. print(f"[{self.platform_name}] 内容页校验命中标题: {title}")
  618. return True
  619. print(f"[{self.platform_name}] 内容页校验未命中标题,expected={expected}")
  620. return False
  621. except Exception as e:
  622. print(f"[{self.platform_name}] 内容页校验异常: {e}")
  623. return False
  624. async def publish(self, cookies: str, params: PublishParams) -> PublishResult:
  625. """发布视频到百家号"""
  626. import os
  627. import re
  628. import shutil
  629. print(f"\n{'='*60}")
  630. print(f"[{self.platform_name}] 开始发布视频")
  631. print(f"[{self.platform_name}] 视频路径: {params.video_path}")
  632. print(f"[{self.platform_name}] 标题: {params.title}")
  633. print(f"[{self.platform_name}] 描述: {(params.description or '')[:120]}")
  634. print(f"[{self.platform_name}] Headless: {self.headless}")
  635. print(f"{'='*60}")
  636. self.report_progress(5, "正在初始化浏览器...")
  637. # 初始化浏览器
  638. await self.init_browser()
  639. print(f"[{self.platform_name}] 浏览器初始化完成")
  640. # 解析并设置 cookies
  641. cookie_list = self.parse_cookies(cookies)
  642. print(f"[{self.platform_name}] 解析到 {len(cookie_list)} 个 cookies")
  643. await self.set_cookies(cookie_list)
  644. if not self.page:
  645. raise Exception("Page not initialized")
  646. # 检查视频文件
  647. if not os.path.exists(params.video_path):
  648. raise Exception(f"视频文件不存在: {params.video_path}")
  649. print(f"[{self.platform_name}] 视频文件存在,大小: {os.path.getsize(params.video_path)} bytes")
  650. # 关键兜底:百家号在标题框不可编辑时会将“文件名主干”作为默认标题。
  651. # 因此上传前为视频创建“标题别名文件”(优先硬链接,失败再复制),确保默认标题可控。
  652. upload_video_path = params.video_path
  653. try:
  654. raw_title = (params.title or "").strip()
  655. if raw_title:
  656. safe_title = re.sub(r'[<>:"/\\\\|?*\\x00-\\x1F]', '', raw_title)
  657. safe_title = re.sub(r'\\s+', ' ', safe_title).strip().rstrip('.')
  658. if not safe_title:
  659. safe_title = "video"
  660. safe_title = safe_title[:30]
  661. src_ext = os.path.splitext(params.video_path)[1] or ".mp4"
  662. alias_dir = os.path.join(os.path.dirname(params.video_path), "_bjh_upload_alias")
  663. os.makedirs(alias_dir, exist_ok=True)
  664. # 轻量清理:删除 24h 前的旧别名文件,避免长期累积
  665. try:
  666. now_ts = datetime.now().timestamp()
  667. for fn in os.listdir(alias_dir):
  668. full = os.path.join(alias_dir, fn)
  669. if not os.path.isfile(full):
  670. continue
  671. if now_ts - os.path.getmtime(full) > 24 * 3600:
  672. try:
  673. os.remove(full)
  674. except Exception:
  675. pass
  676. except Exception:
  677. pass
  678. alias_name = f"{safe_title}{src_ext}"
  679. alias_path = os.path.join(alias_dir, alias_name)
  680. if os.path.abspath(alias_path) != os.path.abspath(params.video_path):
  681. if os.path.exists(alias_path):
  682. try:
  683. os.remove(alias_path)
  684. except Exception:
  685. pass
  686. try:
  687. os.link(params.video_path, alias_path)
  688. upload_video_path = alias_path
  689. print(f"[{self.platform_name}] 上传别名已创建(硬链接): {upload_video_path}")
  690. except Exception:
  691. shutil.copy2(params.video_path, alias_path)
  692. upload_video_path = alias_path
  693. print(f"[{self.platform_name}] 上传别名已创建(复制): {upload_video_path}")
  694. except Exception as e:
  695. upload_video_path = params.video_path
  696. print(f"[{self.platform_name}] 创建上传别名失败,回退原文件: {e}")
  697. self.report_progress(10, "正在打开上传页面...")
  698. # 访问视频发布页面(使用新视频发布界面)
  699. video_publish_url = "https://baijiahao.baidu.com/builder/rc/edit?type=videoV2&is_from_cms=1"
  700. await self.page.goto(video_publish_url, wait_until="domcontentloaded", timeout=60000)
  701. await asyncio.sleep(3)
  702. # 检查是否跳转到登录页
  703. current_url = self.page.url
  704. print(f"[{self.platform_name}] 当前页面: {current_url}")
  705. for indicator in self.login_indicators:
  706. if indicator in current_url:
  707. screenshot_base64 = await self.capture_screenshot()
  708. return PublishResult(
  709. success=False,
  710. platform=self.platform_name,
  711. error="Cookie 已过期,需要重新登录",
  712. need_captcha=True,
  713. captcha_type='login',
  714. screenshot_base64=screenshot_base64,
  715. page_url=current_url,
  716. status='need_captcha'
  717. )
  718. # 使用 AI 检查验证码
  719. ai_captcha = await self.ai_check_captcha()
  720. if ai_captcha['has_captcha']:
  721. print(f"[{self.platform_name}] AI检测到验证码: {ai_captcha['captcha_type']}", flush=True)
  722. screenshot_base64 = await self.capture_screenshot()
  723. return PublishResult(
  724. success=False,
  725. platform=self.platform_name,
  726. error=f"检测到{ai_captcha['captcha_type']}验证码,需要使用有头浏览器完成验证",
  727. need_captcha=True,
  728. captcha_type=ai_captcha['captcha_type'],
  729. screenshot_base64=screenshot_base64,
  730. page_url=current_url,
  731. status='need_captcha'
  732. )
  733. # 传统方式检查验证码
  734. captcha_result = await self.check_captcha()
  735. if captcha_result['need_captcha']:
  736. screenshot_base64 = await self.capture_screenshot()
  737. return PublishResult(
  738. success=False,
  739. platform=self.platform_name,
  740. error=f"需要{captcha_result['captcha_type']}验证码,请使用有头浏览器完成验证",
  741. need_captcha=True,
  742. captcha_type=captcha_result['captcha_type'],
  743. screenshot_base64=screenshot_base64,
  744. page_url=current_url,
  745. status='need_captcha'
  746. )
  747. self.report_progress(15, "正在选择视频文件...")
  748. # 等待页面加载完成
  749. await asyncio.sleep(2)
  750. # 关闭可能的弹窗
  751. try:
  752. close_buttons = [
  753. 'button:has-text("我知道了")',
  754. 'button:has-text("知道了")',
  755. '[class*="close"]',
  756. '[class*="modal-close"]',
  757. ]
  758. for btn_selector in close_buttons:
  759. try:
  760. btn = self.page.locator(btn_selector).first
  761. if await btn.count() > 0 and await btn.is_visible():
  762. await btn.click()
  763. await asyncio.sleep(0.5)
  764. except:
  765. pass
  766. except:
  767. pass
  768. # 上传视频 - 尝试多种方式
  769. upload_triggered = False
  770. # 方法1: 直接通过 file input 上传
  771. try:
  772. file_inputs = await self.page.query_selector_all('input[type="file"]')
  773. print(f"[{self.platform_name}] 找到 {len(file_inputs)} 个文件输入")
  774. for file_input in file_inputs:
  775. try:
  776. await file_input.set_input_files(upload_video_path)
  777. upload_triggered = True
  778. print(f"[{self.platform_name}] 通过 file input 上传成功")
  779. break
  780. except Exception as e:
  781. print(f"[{self.platform_name}] file input 上传失败: {e}")
  782. except Exception as e:
  783. print(f"[{self.platform_name}] 查找 file input 失败: {e}")
  784. # 方法2: 点击上传区域
  785. if not upload_triggered:
  786. upload_selectors = [
  787. 'div[class*="upload-box"]',
  788. 'div[class*="drag-upload"]',
  789. 'div[class*="uploader"]',
  790. 'div:has-text("点击上传")',
  791. 'div:has-text("选择文件")',
  792. '[class*="upload-area"]',
  793. ]
  794. for selector in upload_selectors:
  795. if upload_triggered:
  796. break
  797. try:
  798. upload_area = self.page.locator(selector).first
  799. if await upload_area.count() > 0:
  800. print(f"[{self.platform_name}] 尝试点击上传区域: {selector}")
  801. async with self.page.expect_file_chooser(timeout=10000) as fc_info:
  802. await upload_area.click()
  803. file_chooser = await fc_info.value
  804. await file_chooser.set_files(upload_video_path)
  805. upload_triggered = True
  806. print(f"[{self.platform_name}] 通过点击上传区域成功")
  807. break
  808. except Exception as e:
  809. print(f"[{self.platform_name}] 选择器 {selector} 失败: {e}")
  810. if not upload_triggered:
  811. screenshot_base64 = await self.capture_screenshot()
  812. return PublishResult(
  813. success=False,
  814. platform=self.platform_name,
  815. error="未找到上传入口",
  816. screenshot_base64=screenshot_base64,
  817. page_url=await self.get_page_url(),
  818. status='failed'
  819. )
  820. self.report_progress(20, "等待视频上传...")
  821. # 等待视频上传完成(百家号大文件+处理可能较慢)
  822. upload_timeout = 900
  823. start_time = asyncio.get_event_loop().time()
  824. last_heartbeat_time = start_time
  825. last_signal_time = start_time
  826. last_stall_log_time = start_time
  827. last_ai_upload_check_time = start_time - 60
  828. ai_upload_check_interval = 20
  829. ai_upload_poll_count = 0
  830. ai_upload_unknown_streak = 0
  831. last_pct = -1
  832. forced_continue_after = 180 # 无进度信号时,3 分钟后执行兜底继续
  833. processing_since = None
  834. processing_selector_hit = ""
  835. processing_stale_continue_after = 300 # 处理态持续 5 分钟仍无明确变化,执行兜底继续
  836. has_progress_signal = False
  837. progress_signal_lost_continue_after = 90 # 已看到进度后,若信号中断 90s,直接进入下一步
  838. hard_cutover_signal_gap_after = 120 # 已出现过进度后,信号中断超过该值则硬切下一阶段
  839. hard_cutover_elapsed_after = 210 # 上传总耗时超过该值时,硬切下一阶段
  840. async def _attempt_enter_publish_form_from_upload(stage: str) -> bool:
  841. enter_selectors = [
  842. 'button:has-text("去发布")',
  843. '[role="button"]:has-text("去发布")',
  844. 'button:has-text("发布视频")',
  845. '[role="button"]:has-text("发布视频")',
  846. 'button:has-text("下一步")',
  847. '[role="button"]:has-text("下一步")',
  848. 'button:has-text("继续")',
  849. '[role="button"]:has-text("继续")',
  850. 'button:has-text("完成编辑")',
  851. '[role="button"]:has-text("完成编辑")',
  852. '[class*="next"] button',
  853. '[class*="step"] button',
  854. ]
  855. blocked_exact = {"发布", "定时发布", "立即发布", "取消", "返回", "关闭"}
  856. blocked_contains = ["定时发布", "立即发布", "取消", "返回", "关闭", "删除", "重传", "重新上传", "清空"]
  857. for selector in enter_selectors:
  858. try:
  859. btns = self.page.locator(selector)
  860. count = await btns.count()
  861. for idx in range(min(count, 6)):
  862. btn = btns.nth(idx)
  863. if not await btn.is_visible():
  864. continue
  865. text = (await btn.text_content() or "").strip()
  866. compact = re.sub(r"\s+", "", text)
  867. if compact in blocked_exact or any(w in compact for w in blocked_contains):
  868. continue
  869. disabled_attr = await btn.get_attribute('disabled')
  870. aria_disabled = (await btn.get_attribute('aria-disabled') or '').lower()
  871. if disabled_attr is not None or aria_disabled == 'true':
  872. continue
  873. try:
  874. await btn.scroll_into_view_if_needed(timeout=1200)
  875. except Exception:
  876. pass
  877. try:
  878. await btn.click(timeout=2500)
  879. except Exception:
  880. await btn.click(force=True, timeout=2500)
  881. print(f"[{self.platform_name}] 上传阶段尝试切换到发布表单: stage={stage}, selector={selector}, text={compact or text}, idx={idx}")
  882. await asyncio.sleep(1)
  883. return True
  884. except Exception:
  885. pass
  886. # 深层 DOM 兜底(含 shadowRoot),应对常规选择器无法命中
  887. try:
  888. deep_clicked = await self.page.evaluate(
  889. """
  890. () => {
  891. const wanted = ['去发布', '发布视频', '下一步', '继续', '完成编辑'];
  892. const blockedExact = new Set(['发布', '定时发布', '立即发布', '取消', '返回', '关闭']);
  893. const blockedContains = ['定时发布', '立即发布', '取消', '返回', '关闭', '删除', '重传', '重新上传', '清空'];
  894. const roots = [document];
  895. const visited = new Set();
  896. const allNodes = [];
  897. while (roots.length) {
  898. const root = roots.pop();
  899. if (!root || visited.has(root)) continue;
  900. visited.add(root);
  901. const nodes = root.querySelectorAll('*');
  902. for (const n of nodes) {
  903. allNodes.push(n);
  904. if (n && n.shadowRoot) roots.push(n.shadowRoot);
  905. }
  906. }
  907. const isVisible = (el) => {
  908. try {
  909. const style = window.getComputedStyle(el);
  910. if (style.display === 'none' || style.visibility === 'hidden' || style.pointerEvents === 'none') return false;
  911. const rect = el.getBoundingClientRect();
  912. return !!rect && rect.width > 8 && rect.height > 8;
  913. } catch {
  914. return false;
  915. }
  916. };
  917. for (const el of allNodes) {
  918. const text = String(el.innerText || el.textContent || '').replace(/\\s+/g, '').trim();
  919. if (!text) continue;
  920. if (blockedExact.has(text)) continue;
  921. if (blockedContains.some(x => text.includes(x))) continue;
  922. if (!wanted.some(x => text.includes(x))) continue;
  923. if (!isVisible(el)) continue;
  924. const tag = String(el.tagName || '').toLowerCase();
  925. const role = String(el.getAttribute && el.getAttribute('role') || '').toLowerCase();
  926. const cls = String(el.className || '').toLowerCase();
  927. const clickable = tag === 'button' || tag === 'a' || role === 'button' || /btn|button|next|step/.test(cls);
  928. if (!clickable) continue;
  929. try {
  930. el.click();
  931. return { ok: true, text };
  932. } catch {}
  933. }
  934. return { ok: false, text: '' };
  935. }
  936. """
  937. )
  938. if deep_clicked and deep_clicked.get("ok"):
  939. clicked_text = str(deep_clicked.get("text") or "").strip()
  940. print(f"[{self.platform_name}] 上传阶段深层DOM切换发布表单成功: stage={stage}, text={clicked_text}")
  941. await asyncio.sleep(1.2)
  942. return True
  943. except Exception:
  944. pass
  945. return False
  946. while asyncio.get_event_loop().time() - start_time < upload_timeout:
  947. now = asyncio.get_event_loop().time()
  948. elapsed = int(now - start_time)
  949. status_parts = []
  950. # 检查上传进度
  951. pct = None
  952. try:
  953. progress_nodes = self.page.locator('[class*="progress"], [class*="percent"], div:has-text("%"), span:has-text("%")')
  954. node_count = await progress_nodes.count()
  955. for idx in range(min(node_count, 6)):
  956. text = await progress_nodes.nth(idx).text_content()
  957. if not text:
  958. continue
  959. match = re.search(r'(\d{1,3})\s*%', text)
  960. if match:
  961. pct = max(0, min(100, int(match.group(1))))
  962. break
  963. except Exception:
  964. pass
  965. if pct is not None:
  966. status_parts.append(f"progress={pct}%")
  967. last_signal_time = now
  968. has_progress_signal = True
  969. if pct != last_pct:
  970. self.report_progress(20 + min(35, int(pct * 0.35)), f"视频上传中 {pct}%...")
  971. last_pct = pct
  972. if pct >= 100:
  973. print(f"[{self.platform_name}] 上传完成(进度达到 100%)")
  974. break
  975. # 明确的上传完成提示
  976. upload_done = False
  977. upload_done_selectors = [
  978. 'div:has-text("上传完成")',
  979. 'div:has-text("处理完成")',
  980. 'div:has-text("上传成功")',
  981. 'span:has-text("上传完成")',
  982. '[class*="upload-success"]',
  983. ]
  984. try:
  985. for selector in upload_done_selectors:
  986. loc = self.page.locator(selector).first
  987. if await loc.count() > 0 and await loc.is_visible():
  988. upload_done = True
  989. print(f"[{self.platform_name}] 检测到上传完成提示: {selector}")
  990. break
  991. except Exception:
  992. pass
  993. if upload_done:
  994. last_signal_time = now
  995. break
  996. # 检查处理态
  997. is_processing = False
  998. processing_selectors = [
  999. 'div:has-text("上传中")',
  1000. 'span:has-text("上传中")',
  1001. 'div:has-text("处理中")',
  1002. 'span:has-text("处理中")',
  1003. 'div:has-text("转码中")',
  1004. 'span:has-text("转码中")',
  1005. 'div:has-text("请稍候")',
  1006. 'span:has-text("请稍候")',
  1007. 'div:has-text("正在上传")',
  1008. 'div:has-text("正在处理")',
  1009. 'text="上传中"',
  1010. 'text="处理中"',
  1011. ]
  1012. try:
  1013. for selector in processing_selectors:
  1014. loc = self.page.locator(selector).first
  1015. if await loc.count() > 0 and await loc.is_visible():
  1016. is_processing = True
  1017. processing_selector_hit = selector
  1018. break
  1019. except Exception:
  1020. pass
  1021. if is_processing:
  1022. if processing_since is None:
  1023. processing_since = now
  1024. processing_elapsed = int(now - processing_since)
  1025. status_parts.append(f"processing={processing_elapsed}s")
  1026. if processing_selector_hit:
  1027. status_parts.append(f"by={processing_selector_hit}")
  1028. # 处理态短时间内视为有效信号;超过阈值后不再持续刷新 signal_gap,避免卡死
  1029. if processing_elapsed <= 180:
  1030. last_signal_time = now
  1031. else:
  1032. processing_since = None
  1033. processing_selector_hit = ""
  1034. # 检查是否出现标题输入框(部分页面会在上传阶段就显示,需结合时间/处理态判断)
  1035. title_input_visible = False
  1036. try:
  1037. title_input = self.page.locator('input[placeholder*="标题"], textarea[placeholder*="标题"], [class*="title-input"] input').first
  1038. title_input_visible = await title_input.count() > 0 and await title_input.is_visible()
  1039. except Exception:
  1040. title_input_visible = False
  1041. if title_input_visible and (
  1042. (not is_processing and elapsed >= 45) or
  1043. (processing_since is not None and (now - processing_since) >= 180) or
  1044. elapsed >= 360
  1045. ):
  1046. print(f"[{self.platform_name}] 检测到可编辑标题,继续后续步骤")
  1047. break
  1048. # 检查是否有错误提示
  1049. error_text = ''
  1050. try:
  1051. error_nodes = self.page.locator('[class*="error"], [class*="fail"], div:has-text("上传失败"), div:has-text("处理失败")')
  1052. err_count = await error_nodes.count()
  1053. for idx in range(min(err_count, 6)):
  1054. txt = (await error_nodes.nth(idx).text_content() or '').strip()
  1055. if txt and any(k in txt for k in ['失败', '错误', '异常', '中断']):
  1056. error_text = txt
  1057. break
  1058. except Exception:
  1059. error_text = ''
  1060. if error_text:
  1061. screenshot_base64 = await self.capture_screenshot()
  1062. return PublishResult(
  1063. success=False,
  1064. platform=self.platform_name,
  1065. error=f"上传失败: {error_text}",
  1066. screenshot_base64=screenshot_base64,
  1067. page_url=await self.get_page_url(),
  1068. status='failed'
  1069. )
  1070. # AI 上传状态判定(节流),用于弥补 DOM/文案信号缺失
  1071. should_run_ai_upload_check = (now - last_ai_upload_check_time) >= ai_upload_check_interval
  1072. if should_run_ai_upload_check:
  1073. ai_upload_poll_count += 1
  1074. ai_upload_state = await self._ai_analyze_upload_state()
  1075. last_ai_upload_check_time = now
  1076. ai_status = str(ai_upload_state.get("status") or "unknown").strip().lower()
  1077. ai_progress = ai_upload_state.get("progress")
  1078. ai_confidence = int(ai_upload_state.get("confidence") or 0)
  1079. ai_reason = str(ai_upload_state.get("reason") or "").strip()
  1080. ai_should_enter_form = bool(ai_upload_state.get("should_enter_publish_form"))
  1081. print(
  1082. f"[{self.platform_name}] AI上传轮询#{ai_upload_poll_count}: elapsed={elapsed}s, "
  1083. f"status={ai_status}, progress={ai_progress}, confidence={ai_confidence}, "
  1084. f"enter_form={ai_should_enter_form}, reason={ai_reason or '-'}"
  1085. )
  1086. if ai_status == "unknown":
  1087. ai_upload_unknown_streak += 1
  1088. else:
  1089. ai_upload_unknown_streak = 0
  1090. if ai_status == "failed":
  1091. screenshot_base64 = await self.capture_screenshot()
  1092. return PublishResult(
  1093. success=False,
  1094. platform=self.platform_name,
  1095. error=f"上传失败(AI判定): {ai_reason or '检测到上传失败信号'}",
  1096. screenshot_base64=screenshot_base64,
  1097. page_url=await self.get_page_url(),
  1098. status='failed'
  1099. )
  1100. if ai_status == "completed":
  1101. if ai_should_enter_form:
  1102. await _attempt_enter_publish_form_from_upload("ai-completed")
  1103. print(f"[{self.platform_name}] AI判定上传已完成,进入下一阶段")
  1104. last_signal_time = now
  1105. break
  1106. if ai_status == "uploading":
  1107. has_progress_signal = True
  1108. last_signal_time = now
  1109. if isinstance(ai_progress, (int, float)):
  1110. ai_pct = max(0, min(100, int(ai_progress)))
  1111. status_parts.append(f"ai-progress={ai_pct}%")
  1112. if ai_pct != last_pct and ai_pct > 0:
  1113. self.report_progress(20 + min(35, int(ai_pct * 0.35)), f"视频上传中 {ai_pct}%...")
  1114. last_pct = ai_pct
  1115. if ai_pct >= 99 and ai_confidence >= 60:
  1116. if ai_should_enter_form:
  1117. await _attempt_enter_publish_form_from_upload("ai-upload-99")
  1118. print(f"[{self.platform_name}] AI判定上传接近完成,进入下一阶段")
  1119. break
  1120. else:
  1121. status_parts.append("ai=uploading")
  1122. if ai_should_enter_form and elapsed >= 60:
  1123. await _attempt_enter_publish_form_from_upload("ai-uploading-enter-form")
  1124. elif ai_status == "unknown" and ai_should_enter_form and elapsed >= 60:
  1125. await _attempt_enter_publish_form_from_upload("ai-unknown-enter-form")
  1126. elif ai_status == "unknown" and ai_upload_unknown_streak >= 3 and elapsed >= 90:
  1127. await _attempt_enter_publish_form_from_upload("ai-unknown-streak")
  1128. # 心跳日志,便于定位“卡住”
  1129. if now - last_heartbeat_time >= 15:
  1130. signal_gap = int(now - last_signal_time)
  1131. extra = ", ".join(status_parts) if status_parts else "no-visible-signal"
  1132. print(f"[{self.platform_name}] 上传等待中: elapsed={elapsed}s, signal_gap={signal_gap}s, {extra}")
  1133. last_heartbeat_time = now
  1134. # 已经出现过进度后,如果进度信号中断较久,进入下一步兜底
  1135. dynamic_signal_lost_after = progress_signal_lost_continue_after
  1136. if last_pct >= 95:
  1137. # 95%+ 阶段可能有短暂静默,适度放宽
  1138. dynamic_signal_lost_after = max(progress_signal_lost_continue_after, 150)
  1139. elif last_pct >= 80:
  1140. # 中后段进度(80%+)可能进入转码/校验静默期,但不应无限等待
  1141. dynamic_signal_lost_after = max(progress_signal_lost_continue_after, 150)
  1142. elif last_pct >= 60:
  1143. dynamic_signal_lost_after = max(progress_signal_lost_continue_after, 120)
  1144. if has_progress_signal and (now - last_signal_time) >= dynamic_signal_lost_after:
  1145. signal_gap = int(now - last_signal_time)
  1146. if last_pct >= 95 or title_input_visible or elapsed >= max(780, upload_timeout - 60):
  1147. print(f"[{self.platform_name}] 上传进度信号中断过久({signal_gap}s>={dynamic_signal_lost_after}s),继续后续步骤(兜底)")
  1148. break
  1149. if (last_pct >= 70 and signal_gap >= hard_cutover_signal_gap_after) or elapsed >= hard_cutover_elapsed_after:
  1150. await _attempt_enter_publish_form_from_upload("hard-cutover-signal")
  1151. print(f"[{self.platform_name}] 上传长时间无新信号,执行硬切换到标题阶段: elapsed={elapsed}s, signal_gap={signal_gap}s, last_pct={last_pct}")
  1152. break
  1153. if now - last_stall_log_time >= 30:
  1154. print(f"[{self.platform_name}] 上传信号中断({signal_gap}s)但进度不足/标题未就绪,继续等待上传完成...")
  1155. last_stall_log_time = now
  1156. # 额外硬切策略:出现过中后段进度但长时间无新增信号时,不再继续卡住
  1157. if has_progress_signal and last_pct >= 70 and (now - last_signal_time) >= hard_cutover_signal_gap_after:
  1158. signal_gap = int(now - last_signal_time)
  1159. await _attempt_enter_publish_form_from_upload("hard-cutover-progress")
  1160. print(f"[{self.platform_name}] 中后段上传信号停滞,强制切换到标题阶段: elapsed={elapsed}s, signal_gap={signal_gap}s, last_pct={last_pct}")
  1161. break
  1162. # 从未出现可见进度信号时,不再长时间卡在 20%
  1163. if (not has_progress_signal) and elapsed >= forced_continue_after and (now - last_signal_time) >= 120:
  1164. if title_input_visible or elapsed >= max(600, upload_timeout - 90):
  1165. print(f"[{self.platform_name}] 上传阶段长时间无可见进度信号,继续后续步骤(兜底)")
  1166. break
  1167. if elapsed >= 480:
  1168. await _attempt_enter_publish_form_from_upload("hard-cutover-no-signal")
  1169. print(f"[{self.platform_name}] 上传持续无可见信号,执行硬切换到标题阶段: elapsed={elapsed}s")
  1170. break
  1171. if now - last_stall_log_time >= 30:
  1172. print(f"[{self.platform_name}] 上传暂无可见信号且标题未就绪,继续等待...")
  1173. last_stall_log_time = now
  1174. # 处理态持续过久时兜底继续,避免固定 DOM 文案导致无限等待
  1175. if processing_since is not None and (now - processing_since) >= processing_stale_continue_after:
  1176. if last_pct >= 95 or title_input_visible or elapsed >= max(780, upload_timeout - 60):
  1177. print(f"[{self.platform_name}] 上传阶段处理态持续过久,继续后续步骤(兜底)")
  1178. break
  1179. if elapsed >= hard_cutover_elapsed_after:
  1180. await _attempt_enter_publish_form_from_upload("hard-cutover-processing")
  1181. print(f"[{self.platform_name}] 处理态持续过久且总耗时较长,执行硬切换到标题阶段: elapsed={elapsed}s")
  1182. break
  1183. if now - last_stall_log_time >= 30:
  1184. print(f"[{self.platform_name}] 处理态持续较久但标题未就绪,继续等待上传收尾...")
  1185. last_stall_log_time = now
  1186. await asyncio.sleep(3)
  1187. self.report_progress(60, "正在填写标题...")
  1188. await asyncio.sleep(2)
  1189. # 填写标题(严格校验写入结果,避免填错输入框)
  1190. desired_title = (params.title or "").strip()[:30] # 百家号标题限制 30 字
  1191. video_stem = os.path.splitext(os.path.basename(params.video_path or ""))[0].strip().lower()
  1192. def _normalize_title_for_match(value: str) -> str:
  1193. v = re.sub(r"\s+", "", str(value or "")).strip().lower()
  1194. v = re.sub(r"[`~!@#$%^&*()_+=\[\]{}\\|;:'\",.<>/?,。!?;:、()【】《》\-\u3000]", "", v)
  1195. return v
  1196. def _looks_like_non_title_value(value: str) -> bool:
  1197. raw = str(value or "").strip()
  1198. if not raw:
  1199. return True
  1200. compact = raw.lower()
  1201. # 典型 UUID(平台内部资源ID/文件名)
  1202. if re.fullmatch(r"[0-9a-f]{8}-[0-9a-f]{4}-[1-5]?[0-9a-f]{3}-[89ab]?[0-9a-f]{3}-[0-9a-f]{12}", compact):
  1203. return True
  1204. # 纯英文数字/连接符且较长,通常是资源ID而不是标题
  1205. if len(compact) >= 24 and re.fullmatch(r"[a-z0-9_-]+", compact):
  1206. return True
  1207. # 与视频文件名主干一致时,视为误填
  1208. if video_stem and compact == video_stem:
  1209. return True
  1210. # 文件路径或带扩展名文本,视为误填
  1211. if "\\" in raw or "/" in raw:
  1212. return True
  1213. if re.search(r"\.(mp4|mov|avi|mkv|wmv|flv|m4v)$", compact):
  1214. return True
  1215. return False
  1216. def _title_matches_expected(current_value: str) -> bool:
  1217. if not desired_title:
  1218. return False
  1219. current = str(current_value or "").strip()
  1220. if not current:
  1221. return False
  1222. if _looks_like_non_title_value(current):
  1223. return False
  1224. expected_norm = _normalize_title_for_match(desired_title)
  1225. current_norm = _normalize_title_for_match(current)
  1226. if not expected_norm or not current_norm:
  1227. return False
  1228. if expected_norm == current_norm:
  1229. return True
  1230. if len(expected_norm) >= 4 and (expected_norm in current_norm or current_norm in expected_norm):
  1231. return True
  1232. prefix_len = min(8, len(expected_norm))
  1233. if prefix_len >= 4 and expected_norm[:prefix_len] in current_norm:
  1234. return True
  1235. return False
  1236. title_filled = False
  1237. title_verified_value = ""
  1238. title_failure_reason = ""
  1239. title_selectors = [
  1240. 'input[placeholder*="标题"]',
  1241. 'textarea[placeholder*="标题"]',
  1242. 'input[aria-label*="标题"]',
  1243. 'textarea[aria-label*="标题"]',
  1244. 'input[data-placeholder*="标题"]',
  1245. 'textarea[data-placeholder*="标题"]',
  1246. 'input[name*="title"]',
  1247. 'textarea[name*="title"]',
  1248. 'input[id*="title"]',
  1249. 'textarea[id*="title"]',
  1250. '[class*="title-input"] input',
  1251. '[class*="title"] textarea',
  1252. '[class*="title"] input',
  1253. '[class*="headline"] input',
  1254. '[class*="headline"] textarea',
  1255. '[class*="name"] input',
  1256. '[contenteditable="true"][placeholder*="标题"]',
  1257. '[contenteditable="true"][aria-label*="标题"]',
  1258. '[contenteditable="plaintext-only"][placeholder*="标题"]',
  1259. '[data-placeholder*="标题"][contenteditable="true"]',
  1260. '[class*="title"] [contenteditable="true"]',
  1261. '[role="textbox"][aria-label*="标题"]',
  1262. '[role="textbox"][placeholder*="标题"]',
  1263. ]
  1264. async def _has_editable_title_input() -> bool:
  1265. for frame in self.page.frames:
  1266. for selector in title_selectors:
  1267. try:
  1268. nodes = frame.locator(selector)
  1269. count = await nodes.count()
  1270. for idx in range(min(count, 10)):
  1271. node = nodes.nth(idx)
  1272. if not await node.is_visible():
  1273. continue
  1274. node_type = (await node.get_attribute('type') or '').strip().lower()
  1275. if node_type in ['file', 'hidden', 'checkbox', 'radio', 'button', 'submit']:
  1276. continue
  1277. try:
  1278. if await node.is_disabled():
  1279. continue
  1280. except Exception:
  1281. pass
  1282. return True
  1283. except Exception:
  1284. pass
  1285. # 深层 DOM 检查(含 shadowRoot)
  1286. for frame in self.page.frames:
  1287. try:
  1288. deep_found = await frame.evaluate(
  1289. """
  1290. () => {
  1291. const roots = [document];
  1292. const visited = new Set();
  1293. while (roots.length) {
  1294. const root = roots.pop();
  1295. if (!root || visited.has(root)) continue;
  1296. visited.add(root);
  1297. const nodes = root.querySelectorAll('*');
  1298. for (const n of nodes) {
  1299. if (n && n.shadowRoot) roots.push(n.shadowRoot);
  1300. const tag = String(n.tagName || '').toLowerCase();
  1301. if (!['input', 'textarea'].includes(tag) && String(n.getAttribute && n.getAttribute('contenteditable') || '').toLowerCase() !== 'true' && String(n.getAttribute && n.getAttribute('role') || '').toLowerCase() !== 'textbox') {
  1302. continue;
  1303. }
  1304. const type = String(n.getAttribute && n.getAttribute('type') || '').toLowerCase();
  1305. if (tag === 'input' && ['file', 'hidden', 'checkbox', 'radio', 'button', 'submit'].includes(type)) continue;
  1306. if (n.disabled || n.readOnly) continue;
  1307. const style = window.getComputedStyle(n);
  1308. if (style.display === 'none' || style.visibility === 'hidden') continue;
  1309. const rect = n.getBoundingClientRect();
  1310. if (!rect || rect.width < 8 || rect.height < 8) continue;
  1311. return true;
  1312. }
  1313. }
  1314. return false;
  1315. }
  1316. """
  1317. )
  1318. if deep_found:
  1319. return True
  1320. except Exception:
  1321. pass
  1322. return False
  1323. async def _try_enter_publish_form(stage: str) -> bool:
  1324. action_selectors = [
  1325. 'button:has-text("去发布")',
  1326. '[role="button"]:has-text("去发布")',
  1327. 'button:has-text("发布视频")',
  1328. '[role="button"]:has-text("发布视频")',
  1329. 'button:has-text("下一步")',
  1330. '[role="button"]:has-text("下一步")',
  1331. 'button:has-text("继续")',
  1332. '[role="button"]:has-text("继续")',
  1333. 'button:has-text("完成编辑")',
  1334. '[role="button"]:has-text("完成编辑")',
  1335. '[class*="next"] button',
  1336. '[class*="step"] button',
  1337. ]
  1338. blocked_exact = {"发布", "定时发布", "立即发布", "取消", "返回", "关闭"}
  1339. blocked_contains = ["定时发布", "立即发布", "取消", "返回", "关闭", "删除", "重传", "重新上传", "清空"]
  1340. for frame in self.page.frames:
  1341. frame_url = frame.url or "about:blank"
  1342. for selector in action_selectors:
  1343. try:
  1344. btns = frame.locator(selector)
  1345. btn_count = await btns.count()
  1346. for idx in range(min(btn_count, 6)):
  1347. btn = btns.nth(idx)
  1348. if not await btn.is_visible():
  1349. continue
  1350. text = (await btn.text_content() or "").strip()
  1351. compact = re.sub(r"\s+", "", text)
  1352. if compact in blocked_exact or any(t in compact for t in blocked_contains):
  1353. continue
  1354. disabled_attr = await btn.get_attribute('disabled')
  1355. aria_disabled = (await btn.get_attribute('aria-disabled') or '').lower()
  1356. if disabled_attr is not None or aria_disabled == 'true':
  1357. continue
  1358. try:
  1359. await btn.scroll_into_view_if_needed(timeout=1500)
  1360. except Exception:
  1361. pass
  1362. try:
  1363. await btn.click(timeout=3000)
  1364. except Exception:
  1365. await btn.click(force=True, timeout=3000)
  1366. print(f"[{self.platform_name}] 尝试进入发布表单: stage={stage}, frame={frame_url}, selector={selector}, text={compact or text}, idx={idx}")
  1367. await asyncio.sleep(1.2)
  1368. if await _has_editable_title_input():
  1369. print(f"[{self.platform_name}] 已进入可编辑发布表单: stage={stage}")
  1370. return True
  1371. except Exception:
  1372. pass
  1373. # 深层 DOM 兜底(含 shadowRoot)
  1374. try:
  1375. deep_clicked = await self.page.evaluate(
  1376. """
  1377. () => {
  1378. const wanted = ['去发布', '发布视频', '下一步', '继续', '完成编辑'];
  1379. const blockedExact = new Set(['发布', '定时发布', '立即发布', '取消', '返回', '关闭']);
  1380. const blockedContains = ['定时发布', '立即发布', '取消', '返回', '关闭', '删除', '重传', '重新上传', '清空'];
  1381. const roots = [document];
  1382. const visited = new Set();
  1383. const allNodes = [];
  1384. while (roots.length) {
  1385. const root = roots.pop();
  1386. if (!root || visited.has(root)) continue;
  1387. visited.add(root);
  1388. const nodes = root.querySelectorAll('*');
  1389. for (const n of nodes) {
  1390. allNodes.push(n);
  1391. if (n && n.shadowRoot) roots.push(n.shadowRoot);
  1392. }
  1393. }
  1394. const isVisible = (el) => {
  1395. try {
  1396. const style = window.getComputedStyle(el);
  1397. if (style.display === 'none' || style.visibility === 'hidden' || style.pointerEvents === 'none') return false;
  1398. const rect = el.getBoundingClientRect();
  1399. return !!rect && rect.width > 8 && rect.height > 8;
  1400. } catch {
  1401. return false;
  1402. }
  1403. };
  1404. for (const el of allNodes) {
  1405. const text = String(el.innerText || el.textContent || '').replace(/\\s+/g, '').trim();
  1406. if (!text) continue;
  1407. if (blockedExact.has(text)) continue;
  1408. if (blockedContains.some(x => text.includes(x))) continue;
  1409. if (!wanted.some(x => text.includes(x))) continue;
  1410. if (!isVisible(el)) continue;
  1411. const tag = String(el.tagName || '').toLowerCase();
  1412. const role = String(el.getAttribute && el.getAttribute('role') || '').toLowerCase();
  1413. const cls = String(el.className || '').toLowerCase();
  1414. const clickable = tag === 'button' || tag === 'a' || role === 'button' || /btn|button|next|step/.test(cls);
  1415. if (!clickable) continue;
  1416. try {
  1417. el.click();
  1418. return { ok: true, text };
  1419. } catch {}
  1420. }
  1421. return { ok: false, text: '' };
  1422. }
  1423. """
  1424. )
  1425. if deep_clicked and deep_clicked.get("ok"):
  1426. print(f"[{self.platform_name}] 深层DOM进入发布表单成功: stage={stage}, text={str(deep_clicked.get('text') or '').strip()}")
  1427. await asyncio.sleep(1.2)
  1428. if await _has_editable_title_input():
  1429. print(f"[{self.platform_name}] 已进入可编辑发布表单(深层DOM): stage={stage}")
  1430. return True
  1431. except Exception:
  1432. pass
  1433. return False
  1434. # 先等待可编辑标题框出现,避免上传兜底后立即进入导致误命中 file input
  1435. await _try_enter_publish_form("pre-title")
  1436. title_ready = False
  1437. title_wait_deadline = asyncio.get_event_loop().time() + 180
  1438. last_title_wait_log = 0.0
  1439. last_enter_publish_try = 0.0
  1440. while asyncio.get_event_loop().time() < title_wait_deadline and not title_ready:
  1441. try:
  1442. if await _has_editable_title_input():
  1443. title_ready = True
  1444. break
  1445. except Exception:
  1446. pass
  1447. for frame in self.page.frames:
  1448. if title_ready:
  1449. break
  1450. for selector in title_selectors:
  1451. if title_ready:
  1452. break
  1453. try:
  1454. title_nodes = frame.locator(selector)
  1455. node_count = await title_nodes.count()
  1456. for idx in range(min(node_count, 8)):
  1457. node = title_nodes.nth(idx)
  1458. if not await node.is_visible():
  1459. continue
  1460. node_type = (await node.get_attribute('type') or '').strip().lower()
  1461. if node_type in ['file', 'hidden', 'checkbox', 'radio', 'button', 'submit']:
  1462. continue
  1463. try:
  1464. if await node.is_disabled():
  1465. continue
  1466. except Exception:
  1467. pass
  1468. title_ready = True
  1469. break
  1470. except Exception:
  1471. pass
  1472. if title_ready:
  1473. break
  1474. now_wait = asyncio.get_event_loop().time()
  1475. if now_wait - last_title_wait_log >= 10:
  1476. print(f"[{self.platform_name}] 等待可编辑标题输入框... frames={len(self.page.frames)}")
  1477. last_title_wait_log = now_wait
  1478. if now_wait - last_enter_publish_try >= 15:
  1479. await _try_enter_publish_form("title-wait")
  1480. last_enter_publish_try = now_wait
  1481. await asyncio.sleep(2)
  1482. if not title_ready:
  1483. title_failure_reason = "title-not-ready"
  1484. print(f"[{self.platform_name}] 未检测到明确标题输入框,进入兜底识别模式")
  1485. for frame in self.page.frames:
  1486. if title_filled:
  1487. break
  1488. frame_url = frame.url or "about:blank"
  1489. for selector in title_selectors:
  1490. if title_filled:
  1491. break
  1492. try:
  1493. title_nodes = frame.locator(selector)
  1494. node_count = await title_nodes.count()
  1495. for idx in range(min(node_count, 8)):
  1496. node = title_nodes.nth(idx)
  1497. if not await node.is_visible():
  1498. continue
  1499. node_type = (await node.get_attribute('type') or '').strip().lower()
  1500. if node_type in ['file', 'hidden', 'checkbox', 'radio', 'button', 'submit']:
  1501. continue
  1502. try:
  1503. if await node.is_disabled():
  1504. continue
  1505. except Exception:
  1506. pass
  1507. node_tag = ""
  1508. try:
  1509. node_tag = ((await node.evaluate("el => (el.tagName || '').toLowerCase()")) or "").strip()
  1510. except Exception:
  1511. node_tag = ""
  1512. contenteditable_attr = (await node.get_attribute('contenteditable') or '').strip().lower()
  1513. role_attr = (await node.get_attribute('role') or '').strip().lower()
  1514. is_text_input = node_tag in ['input', 'textarea']
  1515. is_editable_block = contenteditable_attr == 'true' or role_attr == 'textbox'
  1516. try:
  1517. await node.click(timeout=2000)
  1518. except Exception:
  1519. pass
  1520. if is_text_input:
  1521. try:
  1522. await node.fill(desired_title, timeout=5000)
  1523. except Exception:
  1524. try:
  1525. await self.page.keyboard.press("Control+KeyA")
  1526. await self.page.keyboard.press("Backspace")
  1527. await self.page.keyboard.type(desired_title)
  1528. except Exception:
  1529. continue
  1530. elif is_editable_block:
  1531. try:
  1532. await self.page.keyboard.press("Control+KeyA")
  1533. await self.page.keyboard.press("Backspace")
  1534. await self.page.keyboard.type(desired_title)
  1535. except Exception:
  1536. try:
  1537. await node.evaluate(
  1538. """
  1539. (el, title) => {
  1540. el.focus();
  1541. el.textContent = title;
  1542. el.dispatchEvent(new Event('input', { bubbles: true }));
  1543. el.dispatchEvent(new Event('change', { bubbles: true }));
  1544. }
  1545. """,
  1546. desired_title
  1547. )
  1548. except Exception:
  1549. continue
  1550. else:
  1551. continue
  1552. await asyncio.sleep(0.2)
  1553. current_value = ""
  1554. if is_text_input:
  1555. try:
  1556. current_value = (await node.input_value() or "").strip()
  1557. except Exception:
  1558. current_value = ""
  1559. else:
  1560. try:
  1561. current_value = ((await node.evaluate("el => (el.innerText || el.textContent || '')")) or "").strip()
  1562. except Exception:
  1563. current_value = ""
  1564. if _title_matches_expected(current_value):
  1565. title_filled = True
  1566. title_verified_value = current_value
  1567. print(f"[{self.platform_name}] 标题填写成功: frame={frame_url}, selector={selector}, idx={idx}, value={current_value}")
  1568. break
  1569. elif current_value:
  1570. title_failure_reason = "candidate-mismatch"
  1571. # 对同一节点再做一次 JS 强制赋值,处理键盘输入未生效的情况
  1572. forced_value = ""
  1573. try:
  1574. forced_value = (
  1575. (await node.evaluate(
  1576. """
  1577. (el, title) => {
  1578. const tag = String(el.tagName || '').toLowerCase();
  1579. const type = String((el.getAttribute('type') || '')).toLowerCase();
  1580. if (tag === 'input' && ['file', 'hidden', 'checkbox', 'radio', 'button', 'submit'].includes(type)) return '';
  1581. const ce = String(el.getAttribute('contenteditable') || '').toLowerCase();
  1582. const role = String(el.getAttribute('role') || '').toLowerCase();
  1583. const isTextInput = tag === 'input' || tag === 'textarea';
  1584. const isEditableBlock = ce === 'true' || role === 'textbox';
  1585. const emit = () => {
  1586. el.dispatchEvent(new Event('input', { bubbles: true }));
  1587. el.dispatchEvent(new Event('change', { bubbles: true }));
  1588. };
  1589. try { el.focus(); } catch {}
  1590. if (isTextInput) {
  1591. try {
  1592. const proto = tag === 'textarea' ? window.HTMLTextAreaElement.prototype : window.HTMLInputElement.prototype;
  1593. const setter = Object.getOwnPropertyDescriptor(proto, 'value')?.set;
  1594. if (setter) {
  1595. setter.call(el, '');
  1596. emit();
  1597. setter.call(el, title);
  1598. emit();
  1599. } else {
  1600. el.value = '';
  1601. emit();
  1602. el.value = title;
  1603. emit();
  1604. }
  1605. } catch {
  1606. el.value = title;
  1607. emit();
  1608. }
  1609. return String(el.value || '').trim();
  1610. }
  1611. if (isEditableBlock) {
  1612. el.textContent = '';
  1613. emit();
  1614. el.textContent = title;
  1615. emit();
  1616. return String(el.innerText || el.textContent || '').trim();
  1617. }
  1618. return '';
  1619. }
  1620. """,
  1621. desired_title
  1622. )) or ""
  1623. ).strip()
  1624. except Exception:
  1625. forced_value = ""
  1626. if _title_matches_expected(forced_value):
  1627. title_filled = True
  1628. title_verified_value = forced_value
  1629. print(f"[{self.platform_name}] 标题强制写入成功: frame={frame_url}, selector={selector}, idx={idx}, value={forced_value}")
  1630. break
  1631. print(f"[{self.platform_name}] 标题候选值不匹配,已忽略: frame={frame_url}, selector={selector}, idx={idx}, value={current_value}")
  1632. except Exception as e:
  1633. print(f"[{self.platform_name}] 标题选择器失败: frame={frame_url}, selector={selector}, err={e}")
  1634. # 深层 DOM 兜底(含 shadowRoot)
  1635. if not title_filled and desired_title:
  1636. for frame in self.page.frames:
  1637. if title_filled:
  1638. break
  1639. frame_url = frame.url or "about:blank"
  1640. try:
  1641. deep_result = await frame.evaluate(
  1642. """
  1643. (title) => {
  1644. const roots = [document];
  1645. const visited = new Set();
  1646. const candidates = [];
  1647. while (roots.length) {
  1648. const root = roots.pop();
  1649. if (!root || visited.has(root)) continue;
  1650. visited.add(root);
  1651. const nodes = root.querySelectorAll('*');
  1652. for (const n of nodes) {
  1653. if (n && n.shadowRoot) roots.push(n.shadowRoot);
  1654. const tag = String(n.tagName || '').toLowerCase();
  1655. const type = String(n.getAttribute && n.getAttribute('type') || '').toLowerCase();
  1656. const ce = String(n.getAttribute && n.getAttribute('contenteditable') || '').toLowerCase();
  1657. const role = String(n.getAttribute && n.getAttribute('role') || '').toLowerCase();
  1658. const isTextInput = tag === 'input' || tag === 'textarea';
  1659. const isEditableBlock = ce === 'true' || role === 'textbox';
  1660. if (!isTextInput && !isEditableBlock) continue;
  1661. if (tag === 'input' && ['file', 'hidden', 'checkbox', 'radio', 'button', 'submit'].includes(type)) continue;
  1662. if (n.disabled || n.readOnly) continue;
  1663. const style = window.getComputedStyle(n);
  1664. if (style.display === 'none' || style.visibility === 'hidden') continue;
  1665. const rect = n.getBoundingClientRect();
  1666. if (!rect || rect.width < 8 || rect.height < 8) continue;
  1667. const ph = String(n.getAttribute && n.getAttribute('placeholder') || '');
  1668. const aria = String(n.getAttribute && n.getAttribute('aria-label') || '');
  1669. const name = String(n.getAttribute && n.getAttribute('name') || '');
  1670. const id = String(n.getAttribute && n.getAttribute('id') || '');
  1671. const cls = String(n.className || '');
  1672. const maxLen = parseInt(String(n.getAttribute && n.getAttribute('maxlength') || '0'), 10) || 0;
  1673. const container = n.closest && n.closest('label, [class*="form"], [class*="item"], [class*="field"], [class*="title"]');
  1674. const ctx = String((container && container.innerText) || '').slice(0, 80);
  1675. let score = 0;
  1676. if (/标题|title/i.test(ph)) score += 7;
  1677. if (/标题|title/i.test(aria)) score += 6;
  1678. if (/标题|title/i.test(name)) score += 5;
  1679. if (/标题|title/i.test(id)) score += 5;
  1680. if (/title|标题/i.test(cls)) score += 4;
  1681. if (/标题|title/i.test(ctx)) score += 5;
  1682. if (maxLen > 0 && maxLen <= 40) score += 3;
  1683. if (isTextInput) score += 2;
  1684. if (isEditableBlock) score += 1;
  1685. candidates.push({ n, score, isTextInput, isEditableBlock });
  1686. }
  1687. }
  1688. candidates.sort((a, b) => b.score - a.score);
  1689. if (!candidates.length) return { ok: false, value: '', reason: 'no-candidate' };
  1690. const emit = (el) => {
  1691. el.dispatchEvent(new Event('input', { bubbles: true }));
  1692. el.dispatchEvent(new Event('change', { bubbles: true }));
  1693. };
  1694. let lastError = '';
  1695. for (const item of candidates.slice(0, 12)) {
  1696. const el = item.n;
  1697. try {
  1698. el.focus();
  1699. if (item.isTextInput) {
  1700. const tag = String(el.tagName || '').toLowerCase();
  1701. const proto = tag === 'textarea' ? window.HTMLTextAreaElement.prototype : window.HTMLInputElement.prototype;
  1702. const setter = Object.getOwnPropertyDescriptor(proto, 'value')?.set;
  1703. if (setter) {
  1704. setter.call(el, '');
  1705. emit(el);
  1706. setter.call(el, title);
  1707. emit(el);
  1708. } else {
  1709. el.value = '';
  1710. emit(el);
  1711. el.value = title;
  1712. emit(el);
  1713. }
  1714. const v = String(el.value || '').trim();
  1715. if (v) return { ok: true, value: v, score: item.score };
  1716. } else if (item.isEditableBlock) {
  1717. el.textContent = '';
  1718. emit(el);
  1719. el.textContent = title;
  1720. emit(el);
  1721. const v = String(el.innerText || el.textContent || '').trim();
  1722. if (v) return { ok: true, value: v, score: item.score };
  1723. }
  1724. } catch (e) {
  1725. lastError = String(e || '');
  1726. }
  1727. }
  1728. return { ok: false, value: '', reason: lastError || 'set-value-failed' };
  1729. }
  1730. """,
  1731. desired_title
  1732. )
  1733. if deep_result and deep_result.get('ok'):
  1734. deep_written = str(deep_result.get('value') or '').strip()
  1735. if _title_matches_expected(deep_written):
  1736. title_filled = True
  1737. title_verified_value = deep_written
  1738. print(f"[{self.platform_name}] 标题深层DOM填写成功: frame={frame_url}, value={deep_written}")
  1739. break
  1740. elif deep_written:
  1741. title_failure_reason = "deep-dom-mismatch"
  1742. print(f"[{self.platform_name}] 标题深层DOM命中但值不匹配: frame={frame_url}, value={deep_written}")
  1743. except Exception:
  1744. pass
  1745. # JS 兜底写入标题
  1746. if not title_filled and desired_title:
  1747. fallback_reason = ""
  1748. for frame in self.page.frames:
  1749. if title_filled:
  1750. break
  1751. frame_url = frame.url or "about:blank"
  1752. try:
  1753. fallback = await frame.evaluate(
  1754. """
  1755. (title) => {
  1756. const nodes = Array.from(document.querySelectorAll(
  1757. 'input:not([type="file"]):not([type="hidden"]), textarea, [contenteditable="true"], [role="textbox"]'
  1758. ));
  1759. const scored = nodes
  1760. .map((el) => {
  1761. const tag = String(el.tagName || '').toLowerCase();
  1762. const type = String((el.getAttribute('type') || '')).toLowerCase();
  1763. if (tag === 'input' && ['file', 'hidden', 'checkbox', 'radio', 'button', 'submit'].includes(type)) return null;
  1764. if (el.disabled || el.readOnly) return null;
  1765. const style = window.getComputedStyle(el);
  1766. if (style.display === 'none' || style.visibility === 'hidden') return null;
  1767. const rect = el.getBoundingClientRect();
  1768. if (!rect || rect.width < 8 || rect.height < 8) return null;
  1769. const ph = String(el.getAttribute('placeholder') || '');
  1770. const aria = String(el.getAttribute('aria-label') || '');
  1771. const name = String(el.getAttribute('name') || '');
  1772. const id = String(el.getAttribute('id') || '');
  1773. const cls = String(el.className || '');
  1774. const ce = String(el.getAttribute('contenteditable') || '').toLowerCase();
  1775. const role = String(el.getAttribute('role') || '').toLowerCase();
  1776. const maxLen = parseInt(String(el.getAttribute('maxlength') || '0'), 10) || 0;
  1777. const container = el.closest('label, [class*="form"], [class*="item"], [class*="field"], [class*="title"]');
  1778. const ctx = String((container && container.innerText) || '').slice(0, 80);
  1779. let score = 0;
  1780. if (ph.includes('标题')) score += 6;
  1781. if (aria.includes('标题')) score += 5;
  1782. if (/title|标题/i.test(name)) score += 4;
  1783. if (/title|标题/i.test(id)) score += 4;
  1784. if (/title|标题/i.test(cls)) score += 3;
  1785. if (/标题|title/i.test(ctx)) score += 4;
  1786. if (maxLen > 0 && maxLen <= 40) score += 3;
  1787. if (tag === 'input' || tag === 'textarea') score += 1;
  1788. if (ce === 'true' || role === 'textbox') score += 2;
  1789. return { el, score, maxLen };
  1790. })
  1791. .filter(x => x && x.score > 0)
  1792. .sort((a, b) => b.score - a.score);
  1793. // 没有明显标题线索时,回退到短输入框(常见标题长度限制)
  1794. const candidates = scored.length
  1795. ? scored
  1796. : nodes
  1797. .map((el) => {
  1798. const tag = String(el.tagName || '').toLowerCase();
  1799. const type = String((el.getAttribute('type') || '')).toLowerCase();
  1800. if (tag === 'input' && ['file', 'hidden', 'checkbox', 'radio', 'button', 'submit'].includes(type)) return null;
  1801. if (el.disabled || el.readOnly) return null;
  1802. const style = window.getComputedStyle(el);
  1803. if (style.display === 'none' || style.visibility === 'hidden') return null;
  1804. const rect = el.getBoundingClientRect();
  1805. if (!rect || rect.width < 8 || rect.height < 8) return null;
  1806. const maxLen = parseInt(String(el.getAttribute('maxlength') || '0'), 10) || 0;
  1807. const score = (maxLen > 0 && maxLen <= 40 ? 3 : 0) + (tag === 'input' || tag === 'textarea' ? 1 : 0);
  1808. return score > 0 ? { el, score, maxLen } : null;
  1809. })
  1810. .filter(Boolean)
  1811. .sort((a, b) => b.score - a.score);
  1812. if (!candidates.length) return { ok: false, value: '', reason: 'no-scored-input' };
  1813. let lastError = '';
  1814. for (const item of candidates.slice(0, 10)) {
  1815. const target = item.el;
  1816. const tag = String(target.tagName || '').toLowerCase();
  1817. const ce = String(target.getAttribute('contenteditable') || '').toLowerCase();
  1818. const role = String(target.getAttribute('role') || '').toLowerCase();
  1819. const isTextInput = tag === 'input' || tag === 'textarea';
  1820. const isEditableBlock = ce === 'true' || role === 'textbox';
  1821. try {
  1822. target.focus();
  1823. if (isTextInput) {
  1824. target.value = '';
  1825. target.dispatchEvent(new Event('input', { bubbles: true }));
  1826. target.value = title;
  1827. target.dispatchEvent(new Event('input', { bubbles: true }));
  1828. target.dispatchEvent(new Event('change', { bubbles: true }));
  1829. const v = String(target.value || '').trim();
  1830. if (v) return { ok: true, value: v, score: item.score || 0 };
  1831. } else if (isEditableBlock) {
  1832. target.textContent = '';
  1833. target.dispatchEvent(new Event('input', { bubbles: true }));
  1834. target.textContent = title;
  1835. target.dispatchEvent(new Event('input', { bubbles: true }));
  1836. target.dispatchEvent(new Event('change', { bubbles: true }));
  1837. const v = String(target.innerText || target.textContent || '').trim();
  1838. if (v) return { ok: true, value: v, score: item.score || 0 };
  1839. }
  1840. } catch (e) {
  1841. lastError = String(e || '');
  1842. }
  1843. }
  1844. return { ok: false, value: '', reason: lastError || 'set-value-failed' };
  1845. }
  1846. """,
  1847. desired_title
  1848. )
  1849. if fallback and fallback.get('ok'):
  1850. written = str(fallback.get('value') or '').strip()
  1851. if _title_matches_expected(written):
  1852. title_filled = True
  1853. title_verified_value = written
  1854. print(f"[{self.platform_name}] 标题 JS 兜底填写成功: frame={frame_url}, value={written}")
  1855. break
  1856. elif written:
  1857. fallback_reason = f"fallback-value-not-match:{written}"
  1858. title_failure_reason = fallback_reason
  1859. print(f"[{self.platform_name}] 标题 JS 兜底命中疑似错误字段,已忽略: frame={frame_url}, value={written}")
  1860. elif fallback:
  1861. fallback_reason = str(fallback.get('reason') or '')
  1862. if fallback_reason:
  1863. title_failure_reason = fallback_reason
  1864. except Exception as e:
  1865. fallback_reason = str(e)
  1866. if fallback_reason:
  1867. title_failure_reason = fallback_reason
  1868. if not title_filled:
  1869. print(f"[{self.platform_name}] 标题 JS 兜底未命中: reason={fallback_reason or 'unknown'}")
  1870. # 强化重试:标题框可能在上传收尾阶段延迟可编辑,循环尝试写入一段时间
  1871. if not title_filled and desired_title:
  1872. print(f"[{self.platform_name}] 标题常规填写未命中,进入强化重试...")
  1873. # 百家号在上传 80%+ 后可能经历较长静默处理期,给更长窗口等待标题输入框真正可编辑
  1874. strong_retry_deadline = asyncio.get_event_loop().time() + 240
  1875. strong_retry_round = 0
  1876. last_retry_log = 0.0
  1877. while asyncio.get_event_loop().time() < strong_retry_deadline and not title_filled:
  1878. strong_retry_round += 1
  1879. retry_reason = ""
  1880. if strong_retry_round == 1 or strong_retry_round % 5 == 0:
  1881. await _try_enter_publish_form(f"title-retry-{strong_retry_round}")
  1882. for frame in self.page.frames:
  1883. if title_filled:
  1884. break
  1885. frame_url = frame.url or "about:blank"
  1886. try:
  1887. retry_result = await frame.evaluate(
  1888. """
  1889. (title) => {
  1890. const nodes = Array.from(document.querySelectorAll(
  1891. 'input:not([type="file"]):not([type="hidden"]), textarea, [contenteditable="true"], [role="textbox"]'
  1892. ));
  1893. const candidates = nodes
  1894. .map((el) => {
  1895. const tag = String(el.tagName || '').toLowerCase();
  1896. const type = String((el.getAttribute('type') || '')).toLowerCase();
  1897. if (tag === 'input' && ['file', 'hidden', 'checkbox', 'radio', 'button', 'submit'].includes(type)) return null;
  1898. if (el.disabled || el.readOnly) return null;
  1899. const style = window.getComputedStyle(el);
  1900. if (style.display === 'none' || style.visibility === 'hidden') return null;
  1901. const rect = el.getBoundingClientRect();
  1902. if (!rect || rect.width < 8 || rect.height < 8) return null;
  1903. const ph = String(el.getAttribute('placeholder') || '');
  1904. const aria = String(el.getAttribute('aria-label') || '');
  1905. const name = String(el.getAttribute('name') || '');
  1906. const id = String(el.getAttribute('id') || '');
  1907. const cls = String(el.className || '');
  1908. const ce = String(el.getAttribute('contenteditable') || '').toLowerCase();
  1909. const role = String(el.getAttribute('role') || '').toLowerCase();
  1910. const maxLen = parseInt(String(el.getAttribute('maxlength') || '0'), 10) || 0;
  1911. const container = el.closest('label, [class*="form"], [class*="item"], [class*="field"], [class*="title"]');
  1912. const ctx = String((container && container.innerText) || '').slice(0, 80);
  1913. let score = 0;
  1914. if (/标题|title/i.test(ph)) score += 7;
  1915. if (/标题|title/i.test(aria)) score += 6;
  1916. if (/标题|title/i.test(name)) score += 5;
  1917. if (/标题|title/i.test(id)) score += 5;
  1918. if (/title|标题/i.test(cls)) score += 4;
  1919. if (/标题|title/i.test(ctx)) score += 5;
  1920. if (maxLen > 0 && maxLen <= 40) score += 3;
  1921. if (tag === 'input' || tag === 'textarea') score += 2;
  1922. if (ce === 'true' || role === 'textbox') score += 1;
  1923. return { el, score };
  1924. })
  1925. .filter(Boolean)
  1926. .sort((a, b) => b.score - a.score);
  1927. if (!candidates.length) {
  1928. return { ok: false, value: '', score: -1, reason: 'no-candidate' };
  1929. }
  1930. let lastError = '';
  1931. for (const item of candidates.slice(0, 12)) {
  1932. const target = item.el;
  1933. const tag = String(target.tagName || '').toLowerCase();
  1934. const ce = String(target.getAttribute('contenteditable') || '').toLowerCase();
  1935. const role = String(target.getAttribute('role') || '').toLowerCase();
  1936. const isTextInput = tag === 'input' || tag === 'textarea';
  1937. const isEditableBlock = ce === 'true' || role === 'textbox';
  1938. const emit = () => {
  1939. target.dispatchEvent(new Event('input', { bubbles: true }));
  1940. target.dispatchEvent(new Event('change', { bubbles: true }));
  1941. };
  1942. try {
  1943. target.focus();
  1944. if (isTextInput) {
  1945. try {
  1946. const proto = tag === 'textarea' ? window.HTMLTextAreaElement.prototype : window.HTMLInputElement.prototype;
  1947. const setter = Object.getOwnPropertyDescriptor(proto, 'value')?.set;
  1948. if (setter) {
  1949. setter.call(target, '');
  1950. emit();
  1951. setter.call(target, title);
  1952. emit();
  1953. } else {
  1954. target.value = '';
  1955. emit();
  1956. target.value = title;
  1957. emit();
  1958. }
  1959. } catch {
  1960. target.value = title;
  1961. emit();
  1962. }
  1963. const v = String(target.value || '').trim();
  1964. if (v) return { ok: true, value: v, score: item.score || 0, reason: '' };
  1965. } else if (isEditableBlock) {
  1966. target.textContent = '';
  1967. emit();
  1968. target.textContent = title;
  1969. emit();
  1970. const v = String(target.innerText || target.textContent || '').trim();
  1971. if (v) return { ok: true, value: v, score: item.score || 0, reason: '' };
  1972. }
  1973. } catch (e) {
  1974. lastError = String(e || '');
  1975. }
  1976. }
  1977. return { ok: false, value: '', score: -1, reason: lastError || 'set-value-failed' };
  1978. }
  1979. """,
  1980. desired_title
  1981. )
  1982. if retry_result and retry_result.get('ok'):
  1983. written = str(retry_result.get('value') or '').strip()
  1984. score = int(retry_result.get('score') or 0)
  1985. # 强化重试仍要求“像标题”且可匹配,避免误写到其他文本框
  1986. if score >= 3 and _title_matches_expected(written):
  1987. title_filled = True
  1988. title_verified_value = written
  1989. print(f"[{self.platform_name}] 标题强化重试成功: round={strong_retry_round}, frame={frame_url}, score={score}, value={written}")
  1990. break
  1991. elif written:
  1992. retry_reason = f"value-not-match:{written},score={score}"
  1993. elif retry_result:
  1994. retry_reason = str(retry_result.get('reason') or '')
  1995. except Exception as e:
  1996. retry_reason = str(e)
  1997. if title_filled:
  1998. break
  1999. now_retry = asyncio.get_event_loop().time()
  2000. if retry_reason in ("no-candidate", "no-scored-input"):
  2001. has_title_input = await _has_editable_title_input()
  2002. if not has_title_input:
  2003. retry_reason = "no-candidate-and-form-not-ready"
  2004. if now_retry - last_retry_log >= 10:
  2005. print(f"[{self.platform_name}] 标题强化重试中: round={strong_retry_round}, reason={retry_reason or 'pending'}")
  2006. last_retry_log = now_retry
  2007. if retry_reason:
  2008. title_failure_reason = retry_reason
  2009. await asyncio.sleep(3)
  2010. # AI 兜底:页面结构变化时,通过视觉识别返回可用 selector
  2011. if not title_filled and desired_title:
  2012. print(f"[{self.platform_name}] 标题强化重试仍未命中,尝试 AI selector 兜底...")
  2013. try:
  2014. ai_goal = "找到页面中用于填写视频标题的输入框或可编辑区域,返回一个可直接输入标题的 Playwright selector"
  2015. ai_selector = await self.ai_suggest_playwright_selector(ai_goal)
  2016. if ai_selector.get("has_selector"):
  2017. selector = str(ai_selector.get("selector") or "").strip()
  2018. confidence = int(ai_selector.get("confidence") or 0)
  2019. print(f"[{self.platform_name}] AI 标题 selector: {selector}, confidence={confidence}")
  2020. for frame in self.page.frames:
  2021. if title_filled:
  2022. break
  2023. frame_url = frame.url or "about:blank"
  2024. try:
  2025. loc = frame.locator(selector).first
  2026. if await loc.count() <= 0 or not await loc.is_visible():
  2027. continue
  2028. try:
  2029. await loc.click(timeout=2500)
  2030. except Exception:
  2031. pass
  2032. node_tag = ""
  2033. try:
  2034. node_tag = ((await loc.evaluate("el => (el.tagName || '').toLowerCase()")) or "").strip()
  2035. except Exception:
  2036. node_tag = ""
  2037. is_text_input = node_tag in ["input", "textarea"]
  2038. if is_text_input:
  2039. try:
  2040. await loc.fill(desired_title, timeout=5000)
  2041. except Exception:
  2042. await self.page.keyboard.press("Control+KeyA")
  2043. await self.page.keyboard.press("Backspace")
  2044. await self.page.keyboard.type(desired_title)
  2045. else:
  2046. try:
  2047. await self.page.keyboard.press("Control+KeyA")
  2048. await self.page.keyboard.press("Backspace")
  2049. await self.page.keyboard.type(desired_title)
  2050. except Exception:
  2051. await loc.evaluate(
  2052. """
  2053. (el, title) => {
  2054. el.focus();
  2055. const tag = String(el.tagName || '').toLowerCase();
  2056. if (tag === 'input' || tag === 'textarea') {
  2057. el.value = title;
  2058. } else {
  2059. el.textContent = title;
  2060. }
  2061. el.dispatchEvent(new Event('input', { bubbles: true }));
  2062. el.dispatchEvent(new Event('change', { bubbles: true }));
  2063. }
  2064. """,
  2065. desired_title
  2066. )
  2067. await asyncio.sleep(0.3)
  2068. current_value = ""
  2069. try:
  2070. if is_text_input:
  2071. current_value = (await loc.input_value() or "").strip()
  2072. else:
  2073. current_value = ((await loc.evaluate("el => (el.innerText || el.textContent || '')")) or "").strip()
  2074. except Exception:
  2075. current_value = ""
  2076. if _title_matches_expected(current_value):
  2077. title_filled = True
  2078. title_verified_value = current_value
  2079. print(f"[{self.platform_name}] AI selector 标题填写成功: frame={frame_url}, value={current_value}")
  2080. break
  2081. else:
  2082. print(f"[{self.platform_name}] AI selector 命中但值不匹配: frame={frame_url}, value={current_value}")
  2083. except Exception as e:
  2084. print(f"[{self.platform_name}] AI selector 执行失败: frame={frame_url}, err={e}")
  2085. else:
  2086. print(f"[{self.platform_name}] AI 未返回可用标题 selector: {ai_selector.get('notes') or 'no-notes'}")
  2087. title_failure_reason = "ai-no-selector"
  2088. except Exception as e:
  2089. print(f"[{self.platform_name}] AI 标题兜底异常: {e}")
  2090. title_failure_reason = f"ai-exception:{e}"
  2091. if not title_filled:
  2092. # 某些版本页面在上传后长期不暴露可编辑标题框;不中断流程,尝试继续发布。
  2093. if any(k in (title_failure_reason or "") for k in ["no-candidate", "form-not-ready", "title-not-ready", "ai-no-selector"]):
  2094. print(f"[{self.platform_name}] 标题输入框未就绪({title_failure_reason}),继续后续发布流程(使用页面现有标题)")
  2095. else:
  2096. screenshot_base64 = await self.capture_screenshot()
  2097. return PublishResult(
  2098. success=False,
  2099. platform=self.platform_name,
  2100. error=f"标题填写失败,已终止发布: {title_failure_reason or 'unknown'}",
  2101. screenshot_base64=screenshot_base64,
  2102. page_url=await self.get_page_url(),
  2103. status='failed'
  2104. )
  2105. # 填写描述
  2106. if params.description:
  2107. self.report_progress(65, "正在填写描述...")
  2108. try:
  2109. desc_selectors = [
  2110. 'textarea[placeholder*="描述"]',
  2111. 'textarea[placeholder*="简介"]',
  2112. '[class*="desc"] textarea',
  2113. '[class*="description"] textarea',
  2114. ]
  2115. for selector in desc_selectors:
  2116. try:
  2117. desc_input = self.page.locator(selector).first
  2118. if await desc_input.count() > 0 and await desc_input.is_visible():
  2119. await desc_input.click()
  2120. await self.page.keyboard.type(params.description[:200])
  2121. print(f"[{self.platform_name}] 描述填写成功")
  2122. break
  2123. except:
  2124. pass
  2125. except Exception as e:
  2126. print(f"[{self.platform_name}] 描述填写失败: {e}")
  2127. self.report_progress(70, "正在发布...")
  2128. await asyncio.sleep(1.5)
  2129. # 点击发布按钮(等待按钮可点击,避免上传完成后直接误判失败)
  2130. publish_selectors = [
  2131. 'button:has-text("立即发布")',
  2132. '[role="button"]:has-text("立即发布")',
  2133. 'button:has-text("确认发布")',
  2134. '[role="button"]:has-text("确认发布")',
  2135. 'button:has-text("发布")',
  2136. '[role="button"]:has-text("发布")',
  2137. 'button:has-text("发表")',
  2138. 'button:has-text("提交")',
  2139. '[class*="publish"] button',
  2140. '[class*="submit"] button',
  2141. ]
  2142. publish_blocked_keywords = [
  2143. "定时发布",
  2144. "预约发布",
  2145. "存草稿",
  2146. "草稿",
  2147. "取消",
  2148. "返回",
  2149. "预览",
  2150. ]
  2151. publish_processing_indicators = [
  2152. 'div:has-text("发布中")',
  2153. 'div:has-text("提交中")',
  2154. 'span:has-text("发布中")',
  2155. 'span:has-text("提交中")',
  2156. 'div:has-text("正在上传")',
  2157. 'div:has-text("正在处理")',
  2158. 'span:has-text("正在上传")',
  2159. 'span:has-text("正在处理")',
  2160. 'div:has-text("请稍候")',
  2161. 'span:has-text("请稍候")',
  2162. 'div:has-text("审核中")',
  2163. 'span:has-text("审核中")',
  2164. ]
  2165. def _compact_btn_text(text: str) -> str:
  2166. return re.sub(r"\s+", "", str(text or "")).strip()
  2167. def _score_publish_button(btn_text_compact: str, prefer_confirm: bool = False) -> int:
  2168. if not btn_text_compact:
  2169. return -1
  2170. if any(k in btn_text_compact for k in publish_blocked_keywords):
  2171. return -1
  2172. if "发布中" in btn_text_compact or "提交中" in btn_text_compact:
  2173. return -1
  2174. score = -1
  2175. if "立即发布" in btn_text_compact:
  2176. score = 130
  2177. elif btn_text_compact == "确认发布":
  2178. score = 125
  2179. elif "确认发布" in btn_text_compact:
  2180. score = 120
  2181. elif btn_text_compact == "发布":
  2182. score = 115
  2183. elif "发布" in btn_text_compact:
  2184. score = 100
  2185. elif "发表" in btn_text_compact:
  2186. score = 80
  2187. elif "提交" in btn_text_compact:
  2188. score = 70
  2189. if score < 0:
  2190. return -1
  2191. if prefer_confirm and ("确认发布" in btn_text_compact or "立即发布" in btn_text_compact):
  2192. score += 20
  2193. return score
  2194. async def _collect_publish_candidates(prefer_confirm: bool = False):
  2195. candidates = []
  2196. found_visible_button = False
  2197. found_disabled_button = False
  2198. for frame in self.page.frames:
  2199. frame_url = frame.url or "about:blank"
  2200. for selector in publish_selectors:
  2201. try:
  2202. btns = frame.locator(selector)
  2203. btn_count = await btns.count()
  2204. for idx in range(min(btn_count, 6)):
  2205. btn = btns.nth(idx)
  2206. if not await btn.is_visible():
  2207. continue
  2208. found_visible_button = True
  2209. btn_text = (await btn.text_content() or "").strip()
  2210. btn_text_compact = _compact_btn_text(btn_text)
  2211. disabled_attr = await btn.get_attribute('disabled')
  2212. aria_disabled = (await btn.get_attribute('aria-disabled') or '').lower()
  2213. cls = (await btn.get_attribute('class') or '').lower()
  2214. is_disabled = bool(disabled_attr) or aria_disabled == 'true' or 'disabled' in cls
  2215. if is_disabled:
  2216. found_disabled_button = True
  2217. continue
  2218. score = _score_publish_button(btn_text_compact, prefer_confirm=prefer_confirm)
  2219. if score < 0:
  2220. continue
  2221. candidates.append({
  2222. "btn": btn,
  2223. "frame_url": frame_url,
  2224. "selector": selector,
  2225. "idx": idx,
  2226. "text": btn_text,
  2227. "score": score,
  2228. })
  2229. except Exception:
  2230. pass
  2231. candidates.sort(key=lambda x: x.get("score", 0), reverse=True)
  2232. return candidates, found_visible_button, found_disabled_button
  2233. async def _click_publish_candidate(candidate: dict):
  2234. btn = candidate.get("btn")
  2235. if not btn:
  2236. return False, "candidate-empty"
  2237. frame_url = str(candidate.get("frame_url") or "about:blank")
  2238. selector = str(candidate.get("selector") or "")
  2239. idx = int(candidate.get("idx") or 0)
  2240. btn_text = str(candidate.get("text") or "").strip()
  2241. before_url = self.page.url
  2242. try:
  2243. try:
  2244. await btn.scroll_into_view_if_needed(timeout=1500)
  2245. except Exception:
  2246. pass
  2247. try:
  2248. await btn.click(timeout=4000)
  2249. except Exception:
  2250. await btn.click(force=True, timeout=4000)
  2251. await asyncio.sleep(0.6)
  2252. after_url = self.page.url
  2253. state_flags = []
  2254. if after_url != before_url:
  2255. state_flags.append("url-changed")
  2256. try:
  2257. post_text = _compact_btn_text(await btn.text_content() or "")
  2258. if any(k in post_text for k in ["发布中", "提交中", "处理中"]):
  2259. state_flags.append("btn-processing")
  2260. except Exception:
  2261. pass
  2262. try:
  2263. for indicator in publish_processing_indicators:
  2264. loc = self.page.locator(indicator).first
  2265. if await loc.count() > 0 and await loc.is_visible():
  2266. state_flags.append("processing-indicator")
  2267. break
  2268. except Exception:
  2269. pass
  2270. state_desc = ",".join(state_flags) if state_flags else "no-immediate-signal"
  2271. print(f"[{self.platform_name}] 点击发布按钮成功: frame={frame_url}, selector={selector}, idx={idx}, text={btn_text}, state={state_desc}")
  2272. return True, ""
  2273. except Exception as e:
  2274. return False, str(e)
  2275. publish_clicked = False
  2276. publish_click_error = ""
  2277. publish_clicked_text = ""
  2278. click_deadline = asyncio.get_event_loop().time() + 180
  2279. last_publish_log = 0.0
  2280. while asyncio.get_event_loop().time() < click_deadline and not publish_clicked:
  2281. candidates, found_visible_button, found_disabled_button = await _collect_publish_candidates(prefer_confirm=False)
  2282. if candidates:
  2283. for candidate in candidates[:6]:
  2284. ok, err = await _click_publish_candidate(candidate)
  2285. if ok:
  2286. publish_clicked = True
  2287. publish_clicked_text = str(candidate.get("text") or "").strip()
  2288. break
  2289. if err:
  2290. publish_click_error = err
  2291. if publish_clicked:
  2292. break
  2293. now_click = asyncio.get_event_loop().time()
  2294. if now_click - last_publish_log >= 10:
  2295. if found_visible_button and found_disabled_button:
  2296. print(f"[{self.platform_name}] 发布按钮可见但不可点击,等待可用...")
  2297. elif found_visible_button:
  2298. print(f"[{self.platform_name}] 发布按钮可见,但点击失败,继续重试...")
  2299. else:
  2300. print(f"[{self.platform_name}] 尚未找到可见发布按钮,继续等待...")
  2301. last_publish_log = now_click
  2302. await asyncio.sleep(2)
  2303. # 某些页面会二次弹出“确认发布/立即发布”,补一次优先确认点击
  2304. if publish_clicked:
  2305. initial_text = _compact_btn_text(publish_clicked_text)
  2306. if initial_text and initial_text != "立即发布":
  2307. await asyncio.sleep(1)
  2308. confirm_candidates, _, _ = await _collect_publish_candidates(prefer_confirm=True)
  2309. for candidate in confirm_candidates[:4]:
  2310. candidate_text = _compact_btn_text(str(candidate.get("text") or ""))
  2311. if candidate_text == initial_text and ("确认发布" not in candidate_text and "立即发布" not in candidate_text):
  2312. continue
  2313. ok, err = await _click_publish_candidate(candidate)
  2314. if ok:
  2315. print(f"[{self.platform_name}] 检测到二次确认发布流程,已补点确认按钮: {candidate_text}")
  2316. break
  2317. if err:
  2318. publish_click_error = err
  2319. if not publish_clicked:
  2320. screenshot_base64 = await self.capture_screenshot()
  2321. return PublishResult(
  2322. success=False,
  2323. platform=self.platform_name,
  2324. error=f"发布按钮未找到或不可点击(可能仍在处理/必填项未通过)。title={title_verified_value or desired_title}; err={publish_click_error or 'none'}",
  2325. screenshot_base64=screenshot_base64,
  2326. page_url=await self.get_page_url(),
  2327. status='failed'
  2328. )
  2329. self.report_progress(80, "等待发布完成...")
  2330. # 记录点击发布前的 URL
  2331. publish_page_url = self.page.url
  2332. print(f"[{self.platform_name}] 发布前 URL: {publish_page_url}")
  2333. # 等待发布完成(百家号审核/处理链路可能较慢,默认等待 15 分钟)
  2334. publish_timeout = 900
  2335. start_time = asyncio.get_event_loop().time()
  2336. last_url = publish_page_url
  2337. republish_click_count = 0
  2338. republish_attempt_count = 0
  2339. last_republish_attempt_time = 0.0
  2340. republish_attempt_interval = 45 # 失败后至少间隔 45s 再尝试,避免刷屏和误操作
  2341. max_republish_attempts = 2
  2342. while asyncio.get_event_loop().time() - start_time < publish_timeout:
  2343. await asyncio.sleep(3)
  2344. current_url = self.page.url
  2345. # 检测 URL 是否发生变化
  2346. if current_url != last_url:
  2347. print(f"[{self.platform_name}] URL 变化: {last_url} -> {current_url}")
  2348. last_url = current_url
  2349. # 检查是否跳转到内容管理页面(真正的成功标志)
  2350. # 百家号发布成功后会跳转到 /builder/rc/content 页面
  2351. if '/builder/rc/content' in current_url and 'edit' not in current_url:
  2352. self.report_progress(100, "发布成功!")
  2353. print(f"[{self.platform_name}] 发布成功,已跳转到内容管理页: {current_url}")
  2354. screenshot_base64 = await self.capture_screenshot()
  2355. return PublishResult(
  2356. success=True,
  2357. platform=self.platform_name,
  2358. message="发布成功",
  2359. screenshot_base64=screenshot_base64,
  2360. page_url=current_url,
  2361. status='success'
  2362. )
  2363. # 检查是否有明确的成功提示弹窗
  2364. try:
  2365. # 百家号发布成功会显示"发布成功"弹窗
  2366. success_modal = self.page.locator('div:has-text("发布成功"), div:has-text("提交成功"), div:has-text("视频发布成功")').first
  2367. if await success_modal.count() > 0 and await success_modal.is_visible():
  2368. self.report_progress(100, "发布成功!")
  2369. print(f"[{self.platform_name}] 检测到发布成功弹窗")
  2370. screenshot_base64 = await self.capture_screenshot()
  2371. # 等待一下看是否会跳转
  2372. await asyncio.sleep(3)
  2373. return PublishResult(
  2374. success=True,
  2375. platform=self.platform_name,
  2376. message="发布成功",
  2377. screenshot_base64=screenshot_base64,
  2378. page_url=self.page.url,
  2379. status='success'
  2380. )
  2381. except Exception as e:
  2382. print(f"[{self.platform_name}] 检测成功提示异常: {e}")
  2383. # 检查是否有错误提示
  2384. try:
  2385. error_selectors = [
  2386. 'div.error-tip',
  2387. 'div[class*="error-msg"]',
  2388. 'span[class*="error"]',
  2389. 'div:has-text("发布失败")',
  2390. 'div:has-text("提交失败")',
  2391. ]
  2392. for error_selector in error_selectors:
  2393. error_el = self.page.locator(error_selector).first
  2394. if await error_el.count() > 0 and await error_el.is_visible():
  2395. error_text = await error_el.text_content()
  2396. if error_text and error_text.strip():
  2397. print(f"[{self.platform_name}] 检测到错误: {error_text}")
  2398. screenshot_base64 = await self.capture_screenshot()
  2399. return PublishResult(
  2400. success=False,
  2401. platform=self.platform_name,
  2402. error=f"发布失败: {error_text.strip()}",
  2403. screenshot_base64=screenshot_base64,
  2404. page_url=current_url,
  2405. status='failed'
  2406. )
  2407. except Exception as e:
  2408. print(f"[{self.platform_name}] 检测错误提示异常: {e}")
  2409. # 检查验证码
  2410. captcha_result = await self.check_captcha()
  2411. if captcha_result['need_captcha']:
  2412. screenshot_base64 = await self.capture_screenshot()
  2413. return PublishResult(
  2414. success=False,
  2415. platform=self.platform_name,
  2416. error=f"发布过程中需要{captcha_result['captcha_type']}验证码",
  2417. need_captcha=True,
  2418. captcha_type=captcha_result['captcha_type'],
  2419. screenshot_base64=screenshot_base64,
  2420. page_url=current_url,
  2421. status='need_captcha'
  2422. )
  2423. # 检查发布按钮状态(如果还在编辑页面)
  2424. if 'edit' in current_url:
  2425. try:
  2426. is_processing = False
  2427. for indicator in publish_processing_indicators:
  2428. loc = self.page.locator(indicator).first
  2429. if await loc.count() > 0 and await loc.is_visible():
  2430. is_processing = True
  2431. print(f"[{self.platform_name}] 正在处理中...")
  2432. break
  2433. if not is_processing:
  2434. # 如果不是在处理中,按节流策略尝试重新点击发布按钮
  2435. now_loop = asyncio.get_event_loop().time()
  2436. elapsed = now_loop - start_time
  2437. if (
  2438. elapsed > 60
  2439. and republish_attempt_count < max_republish_attempts
  2440. and (now_loop - last_republish_attempt_time) >= republish_attempt_interval
  2441. ):
  2442. last_republish_attempt_time = now_loop
  2443. republish_attempt_count += 1
  2444. print(f"[{self.platform_name}] 发布状态未变化,执行第 {republish_attempt_count}/{max_republish_attempts} 次补点发布...")
  2445. republish_done = False
  2446. republish_candidates, _, _ = await _collect_publish_candidates(prefer_confirm=True)
  2447. for candidate in republish_candidates[:6]:
  2448. ok, err = await _click_publish_candidate(candidate)
  2449. if ok:
  2450. republish_done = True
  2451. republish_click_count += 1
  2452. candidate_text = _compact_btn_text(str(candidate.get("text") or ""))
  2453. print(f"[{self.platform_name}] 重新点击发布按钮成功: text={candidate_text}, count={republish_click_count}")
  2454. break
  2455. if err:
  2456. publish_click_error = err
  2457. if not republish_done:
  2458. print(f"[{self.platform_name}] 本轮未找到可用的立即发布按钮,继续等待状态变化")
  2459. except Exception as e:
  2460. print(f"[{self.platform_name}] 检查处理状态异常: {e}")
  2461. # 超时,获取截图分析最终状态
  2462. print(f"[{self.platform_name}] 发布超时,最终 URL: {self.page.url}")
  2463. screenshot_base64 = await self.capture_screenshot()
  2464. # 最后一次检查是否在内容管理页
  2465. final_url = self.page.url
  2466. if '/builder/rc/content' in final_url and 'edit' not in final_url:
  2467. return PublishResult(
  2468. success=True,
  2469. platform=self.platform_name,
  2470. message="发布成功(延迟确认)",
  2471. screenshot_base64=screenshot_base64,
  2472. page_url=final_url,
  2473. status='success'
  2474. )
  2475. # 超时后兜底:跳转内容管理页按标题校验,避免“已发布但未跳转”误判失败
  2476. print(f"[{self.platform_name}] 超时后执行内容页二次校验,title={params.title}")
  2477. verify_deadline = asyncio.get_event_loop().time() + 120 # 最多再校验 2 分钟
  2478. while asyncio.get_event_loop().time() < verify_deadline:
  2479. if await self._verify_publish_from_content_page(params.title, page_size=20):
  2480. screenshot_base64 = await self.capture_screenshot()
  2481. return PublishResult(
  2482. success=True,
  2483. platform=self.platform_name,
  2484. message="发布成功(内容页校验)",
  2485. screenshot_base64=screenshot_base64,
  2486. page_url=self.page.url,
  2487. status='success'
  2488. )
  2489. await asyncio.sleep(8)
  2490. return PublishResult(
  2491. success=False,
  2492. platform=self.platform_name,
  2493. error="发布超时,请手动检查发布状态",
  2494. screenshot_base64=screenshot_base64,
  2495. page_url=final_url,
  2496. status='need_action'
  2497. )
  2498. async def get_works(self, cookies: str, page: int = 0, page_size: int = 20) -> WorksResult:
  2499. """
  2500. 获取百家号作品列表
  2501. 优先使用内容管理页的接口(pcui/article/lists)。
  2502. 说明:
  2503. - 该接口通常需要自定义请求头 token(JWT),仅靠 Cookie 可能会返回“未登录”
  2504. - 这里使用 Playwright 打开内容页,从 localStorage/sessionStorage/页面脚本中自动提取 token,
  2505. 再在页面上下文中发起 fetch(携带 cookie + token),以提高成功率
  2506. """
  2507. import re
  2508. print(f"\n{'='*60}")
  2509. print(f"[{self.platform_name}] 获取作品列表 (使用 API)")
  2510. print(f"[{self.platform_name}] page={page}, page_size={page_size}")
  2511. print(f"{'='*60}")
  2512. works: List[WorkItem] = []
  2513. total = 0
  2514. has_more = False
  2515. next_page = ""
  2516. try:
  2517. # 解析并设置 cookies(Playwright)
  2518. cookie_list = self.parse_cookies(cookies)
  2519. await self.init_browser()
  2520. await self.set_cookies(cookie_list)
  2521. if not self.page:
  2522. raise Exception("Page not initialized")
  2523. # 先打开内容管理页,确保本页 Referer/会话就绪
  2524. # Node 侧传 page=0,1,...;接口 currentPage 为 1,2,...
  2525. current_page = int(page) + 1
  2526. page_size = int(page_size)
  2527. content_url = (
  2528. "https://baijiahao.baidu.com/builder/rc/content"
  2529. f"?currentPage={current_page}&pageSize={page_size}"
  2530. "&search=&type=&collection=&startDate=&endDate="
  2531. )
  2532. await self.page.goto(content_url, wait_until="domcontentloaded", timeout=60000)
  2533. await asyncio.sleep(2)
  2534. # 1) 提取 token(JWT)
  2535. token = await self.page.evaluate(
  2536. """
  2537. () => {
  2538. const isJwtLike = (v) => {
  2539. if (!v || typeof v !== 'string') return false;
  2540. const s = v.trim();
  2541. if (s.length < 60) return false;
  2542. const parts = s.split('.');
  2543. if (parts.length !== 3) return false;
  2544. return parts.every(p => /^[A-Za-z0-9_-]+$/.test(p) && p.length > 10);
  2545. };
  2546. const pickFromStorage = (storage) => {
  2547. try {
  2548. const keys = Object.keys(storage || {});
  2549. for (const k of keys) {
  2550. const v = storage.getItem(k);
  2551. if (isJwtLike(v)) return v;
  2552. }
  2553. } catch {}
  2554. return "";
  2555. };
  2556. // localStorage / sessionStorage
  2557. let t = pickFromStorage(window.localStorage);
  2558. if (t) return t;
  2559. t = pickFromStorage(window.sessionStorage);
  2560. if (t) return t;
  2561. // meta 标签
  2562. const meta = document.querySelector('meta[name="token"], meta[name="bjh-token"]');
  2563. const metaToken = meta && meta.getAttribute('content');
  2564. if (isJwtLike(metaToken)) return metaToken;
  2565. // 简单从全局变量里找
  2566. const candidates = [
  2567. (window.__INITIAL_STATE__ && window.__INITIAL_STATE__.token) || "",
  2568. (window.__PRELOADED_STATE__ && window.__PRELOADED_STATE__.token) || "",
  2569. (window.__NUXT__ && window.__NUXT__.state && window.__NUXT__.state.token) || "",
  2570. ];
  2571. for (const c of candidates) {
  2572. if (isJwtLike(c)) return c;
  2573. }
  2574. return "";
  2575. }
  2576. """
  2577. )
  2578. # 2) 若仍未取到 token,再从页面 HTML 兜底提取
  2579. if not token:
  2580. html = await self.page.content()
  2581. m = re.search(r'([A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,})', html)
  2582. if m:
  2583. token = m.group(1)
  2584. if not token:
  2585. raise Exception("未能从页面提取 token(可能未登录或触发风控),请重新登录百家号账号后再试")
  2586. # 3) 调用接口(在页面上下文 fetch,自动携带 cookie)
  2587. api_url = (
  2588. "https://baijiahao.baidu.com/pcui/article/lists"
  2589. f"?currentPage={current_page}"
  2590. f"&pageSize={page_size}"
  2591. "&search=&type=&collection=&startDate=&endDate="
  2592. "&clearBeforeFetch=false"
  2593. "&dynamic=1"
  2594. )
  2595. resp = await self.page.evaluate(
  2596. """
  2597. async ({ url, token }) => {
  2598. const r = await fetch(url, {
  2599. method: 'GET',
  2600. credentials: 'include',
  2601. headers: {
  2602. 'accept': 'application/json, text/plain, */*',
  2603. ...(token ? { token } : {}),
  2604. },
  2605. });
  2606. const text = await r.text();
  2607. return { ok: r.ok, status: r.status, text };
  2608. }
  2609. """,
  2610. {"url": api_url, "token": token},
  2611. )
  2612. if not resp or not resp.get("ok"):
  2613. status = resp.get("status") if isinstance(resp, dict) else "unknown"
  2614. raise Exception(f"百家号接口请求失败: HTTP {status}")
  2615. api_result = json.loads(resp.get("text") or "{}")
  2616. print(f"[{self.platform_name}] pcui/article/lists 响应: errno={api_result.get('errno')}, errmsg={api_result.get('errmsg')}")
  2617. if api_result.get("errno") != 0:
  2618. errno = api_result.get("errno")
  2619. errmsg = api_result.get("errmsg", "unknown error")
  2620. # 20040001 常见为“未登录”
  2621. if errno in (110, 20040001):
  2622. raise Exception("百家号未登录或 Cookie/token 失效,请重新登录后再同步")
  2623. raise Exception(f"百家号接口错误: errno={errno}, errmsg={errmsg}")
  2624. data = api_result.get("data", {}) or {}
  2625. items = data.get("list", []) or []
  2626. page_info = data.get("page", {}) or {}
  2627. total = int(page_info.get("totalCount", 0) or 0)
  2628. total_page = int(page_info.get("totalPage", 0) or 0)
  2629. cur_page = int(page_info.get("currentPage", current_page) or current_page)
  2630. has_more = bool(total_page and cur_page < total_page)
  2631. next_page = cur_page + 1 if has_more else ""
  2632. print(f"[{self.platform_name}] 获取到 {len(items)} 个作品,总数: {total}, currentPage={cur_page}, totalPage={total_page}")
  2633. def _pick_cover(item: dict) -> str:
  2634. cover = item.get("crosswise_cover") or item.get("vertical_cover") or ""
  2635. if cover:
  2636. return cover
  2637. raw = item.get("cover_images") or ""
  2638. try:
  2639. # cover_images 可能是 JSON 字符串
  2640. parsed = json.loads(raw) if isinstance(raw, str) else raw
  2641. if isinstance(parsed, list) and parsed:
  2642. first = parsed[0]
  2643. if isinstance(first, dict):
  2644. return first.get("src") or first.get("ori_src") or ""
  2645. if isinstance(first, str):
  2646. return first
  2647. except Exception:
  2648. pass
  2649. return ""
  2650. def _pick_duration(item: dict) -> int:
  2651. for k in ("rmb_duration", "duration", "long"):
  2652. try:
  2653. v = int(item.get(k) or 0)
  2654. if v > 0:
  2655. return v
  2656. except Exception:
  2657. pass
  2658. # displaytype_exinfo 里可能有 ugcvideo.video_info.durationInSecond
  2659. ex = item.get("displaytype_exinfo") or ""
  2660. try:
  2661. exj = json.loads(ex) if isinstance(ex, str) and ex else (ex if isinstance(ex, dict) else {})
  2662. ugc = (exj.get("ugcvideo") or {}) if isinstance(exj, dict) else {}
  2663. vi = ugc.get("video_info") or {}
  2664. v = int(vi.get("durationInSecond") or ugc.get("long") or 0)
  2665. return v if v > 0 else 0
  2666. except Exception:
  2667. return 0
  2668. def _pick_status(item: dict) -> str:
  2669. qs = str(item.get("quality_status") or "").lower()
  2670. st = str(item.get("status") or "").lower()
  2671. if qs == "rejected" or "reject" in st:
  2672. return "rejected"
  2673. if st in ("draft", "unpublish", "unpublished"):
  2674. return "draft"
  2675. # 百家号常见 publish
  2676. return "published"
  2677. for item in items:
  2678. # 优先使用 nid(builder 预览链接使用这个)
  2679. work_id = str(item.get("nid") or item.get("feed_id") or item.get("article_id") or item.get("id") or "")
  2680. if not work_id:
  2681. continue
  2682. works.append(
  2683. WorkItem(
  2684. work_id=work_id,
  2685. title=str(item.get("title") or ""),
  2686. cover_url=_pick_cover(item),
  2687. video_url=str(item.get("url") or ""),
  2688. duration=_pick_duration(item),
  2689. status=_pick_status(item),
  2690. publish_time=str(item.get("publish_time") or item.get("publish_at") or item.get("created_at") or ""),
  2691. play_count=int(item.get("read_amount") or 0),
  2692. like_count=int(item.get("like_amount") or 0),
  2693. comment_count=int(item.get("comment_amount") or 0),
  2694. share_count=int(item.get("share_amount") or 0),
  2695. collect_count=int(item.get("collection_amount") or 0),
  2696. )
  2697. )
  2698. print(f"[{self.platform_name}] ✓ 成功解析 {len(works)} 个作品")
  2699. except Exception as e:
  2700. import traceback
  2701. traceback.print_exc()
  2702. return WorksResult(
  2703. success=False,
  2704. platform=self.platform_name,
  2705. error=str(e),
  2706. debug_info="baijiahao_get_works_failed"
  2707. )
  2708. return WorksResult(
  2709. success=True,
  2710. platform=self.platform_name,
  2711. works=works,
  2712. total=total,
  2713. has_more=has_more,
  2714. next_page=next_page
  2715. )
  2716. async def get_all_works(self, cookies: str) -> WorksResult:
  2717. """
  2718. 获取百家号全部作品列表(自动分页,复用浏览器实例)。
  2719. 避免每页都启动新浏览器导致的性能问题和风控触发。
  2720. """
  2721. import re
  2722. print(f"\n{'='*60}")
  2723. print(f"[{self.platform_name}] 获取全部作品列表(自动分页)")
  2724. print(f"{'='*60}")
  2725. all_works: List[WorkItem] = []
  2726. seen_ids = set()
  2727. total = 0
  2728. current_page = 1
  2729. page_size = 20
  2730. max_pages = 50 # 最多50页
  2731. try:
  2732. cookie_list = self.parse_cookies(cookies)
  2733. await self.init_browser()
  2734. await self.set_cookies(cookie_list)
  2735. if not self.page:
  2736. raise Exception("Page not initialized")
  2737. # 打开内容管理页以建立会话并提取 token
  2738. content_url = (
  2739. "https://baijiahao.baidu.com/builder/rc/content"
  2740. f"?currentPage={current_page}&pageSize={page_size}"
  2741. "&search=&type=&collection=&startDate=&endDate="
  2742. )
  2743. await self.page.goto(content_url, wait_until="domcontentloaded", timeout=60000)
  2744. await asyncio.sleep(3)
  2745. # 检查登录状态
  2746. current_url = self.page.url
  2747. if "passport.baidu.com" in current_url or "login" in current_url:
  2748. raise Exception("Cookie 已过期,请重新登录百家号账号")
  2749. # 提取 token
  2750. token = await self.page.evaluate(
  2751. """
  2752. () => {
  2753. const isJwtLike = (v) => {
  2754. if (!v || typeof v !== 'string') return false;
  2755. const s = v.trim();
  2756. if (s.length < 60) return false;
  2757. const parts = s.split('.');
  2758. if (parts.length !== 3) return false;
  2759. return parts.every(p => /^[A-Za-z0-9_-]+$/.test(p) && p.length > 10);
  2760. };
  2761. const pickFromStorage = (storage) => {
  2762. try {
  2763. const keys = Object.keys(storage || {});
  2764. for (const k of keys) {
  2765. const v = storage.getItem(k);
  2766. if (isJwtLike(v)) return v;
  2767. }
  2768. } catch {}
  2769. return "";
  2770. };
  2771. let t = pickFromStorage(window.localStorage);
  2772. if (t) return t;
  2773. t = pickFromStorage(window.sessionStorage);
  2774. if (t) return t;
  2775. const meta = document.querySelector('meta[name="token"], meta[name="bjh-token"]');
  2776. const metaToken = meta && meta.getAttribute('content');
  2777. if (isJwtLike(metaToken)) return metaToken;
  2778. const candidates = [
  2779. (window.__INITIAL_STATE__ && window.__INITIAL_STATE__.token) || "",
  2780. (window.__PRELOADED_STATE__ && window.__PRELOADED_STATE__.token) || "",
  2781. (window.__NUXT__ && window.__NUXT__.state && window.__NUXT__.state.token) || "",
  2782. ];
  2783. for (const c of candidates) {
  2784. if (isJwtLike(c)) return c;
  2785. }
  2786. return "";
  2787. }
  2788. """
  2789. )
  2790. if not token:
  2791. html = await self.page.content()
  2792. m = re.search(r'([A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,})', html)
  2793. if m:
  2794. token = m.group(1)
  2795. if not token:
  2796. raise Exception("未能从页面提取 token(可能未登录或触发风控),请重新登录百家号账号后再试")
  2797. print(f"[{self.platform_name}] ✓ Token 提取成功")
  2798. def _pick_cover(item: dict) -> str:
  2799. cover = item.get("crosswise_cover") or item.get("vertical_cover") or ""
  2800. if cover:
  2801. return cover
  2802. raw = item.get("cover_images") or ""
  2803. try:
  2804. parsed = json.loads(raw) if isinstance(raw, str) else raw
  2805. if isinstance(parsed, list) and parsed:
  2806. first = parsed[0]
  2807. if isinstance(first, dict):
  2808. return first.get("src") or first.get("ori_src") or ""
  2809. if isinstance(first, str):
  2810. return first
  2811. except Exception:
  2812. pass
  2813. return ""
  2814. def _pick_duration(item: dict) -> int:
  2815. for k in ("rmb_duration", "duration", "long"):
  2816. try:
  2817. v = int(item.get(k) or 0)
  2818. if v > 0:
  2819. return v
  2820. except Exception:
  2821. pass
  2822. ex = item.get("displaytype_exinfo") or ""
  2823. try:
  2824. exj = json.loads(ex) if isinstance(ex, str) and ex else (ex if isinstance(ex, dict) else {})
  2825. ugc = (exj.get("ugcvideo") or {}) if isinstance(exj, dict) else {}
  2826. vi = ugc.get("video_info") or {}
  2827. v = int(vi.get("durationInSecond") or ugc.get("long") or 0)
  2828. return v if v > 0 else 0
  2829. except Exception:
  2830. return 0
  2831. def _pick_status(item: dict) -> str:
  2832. qs = str(item.get("quality_status") or "").lower()
  2833. st = str(item.get("status") or "").lower()
  2834. if qs == "rejected" or "reject" in st:
  2835. return "rejected"
  2836. if st in ("draft", "unpublish", "unpublished"):
  2837. return "draft"
  2838. return "published"
  2839. # 分页循环
  2840. for page_iter in range(max_pages):
  2841. page_num = page_iter + 1 # 百家号 currentPage 从 1 开始
  2842. api_url = (
  2843. "https://baijiahao.baidu.com/pcui/article/lists"
  2844. f"?currentPage={page_num}"
  2845. f"&pageSize={page_size}"
  2846. "&search=&type=&collection=&startDate=&endDate="
  2847. "&clearBeforeFetch=false"
  2848. "&dynamic=1"
  2849. )
  2850. resp = await self.page.evaluate(
  2851. """
  2852. async ({ url, token }) => {
  2853. const r = await fetch(url, {
  2854. method: 'GET',
  2855. credentials: 'include',
  2856. headers: {
  2857. 'accept': 'application/json, text/plain, */*',
  2858. ...(token ? { token } : {}),
  2859. },
  2860. });
  2861. const text = await r.text();
  2862. return { ok: r.ok, status: r.status, text };
  2863. }
  2864. """,
  2865. {"url": api_url, "token": token},
  2866. )
  2867. if not resp or not resp.get("ok"):
  2868. print(f"[{self.platform_name}] 第 {page_num} 页请求失败: HTTP {resp.get('status') if isinstance(resp, dict) else 'unknown'}")
  2869. break
  2870. api_result = json.loads(resp.get("text") or "{}")
  2871. errno = api_result.get("errno", -1)
  2872. if errno != 0:
  2873. errmsg = api_result.get("errmsg", "unknown error")
  2874. print(f"[{self.platform_name}] 第 {page_num} 页接口错误: errno={errno}, errmsg={errmsg}")
  2875. if errno in (110, 20040001):
  2876. raise Exception("百家号未登录或 Cookie/token 失效,请重新登录后再同步")
  2877. # 非登录错误则停止分页
  2878. break
  2879. data = api_result.get("data", {}) or {}
  2880. items = data.get("list", []) or []
  2881. page_info = data.get("page", {}) or {}
  2882. if page_iter == 0:
  2883. total = int(page_info.get("totalCount", 0) or 0)
  2884. print(f"[{self.platform_name}] 作品总数: {total}")
  2885. new_count = 0
  2886. for item in items:
  2887. work_id = str(item.get("nid") or item.get("feed_id") or item.get("article_id") or item.get("id") or "")
  2888. if not work_id or work_id in seen_ids:
  2889. continue
  2890. seen_ids.add(work_id)
  2891. new_count += 1
  2892. all_works.append(
  2893. WorkItem(
  2894. work_id=work_id,
  2895. title=str(item.get("title") or ""),
  2896. cover_url=_pick_cover(item),
  2897. video_url=str(item.get("url") or ""),
  2898. duration=_pick_duration(item),
  2899. status=_pick_status(item),
  2900. publish_time=str(item.get("publish_time") or item.get("publish_at") or item.get("created_at") or ""),
  2901. play_count=int(item.get("read_amount") or 0),
  2902. like_count=int(item.get("like_amount") or 0),
  2903. comment_count=int(item.get("comment_amount") or 0),
  2904. share_count=int(item.get("share_amount") or 0),
  2905. collect_count=int(item.get("collection_amount") or 0),
  2906. )
  2907. )
  2908. total_page = int(page_info.get("totalPage", 0) or 0)
  2909. has_more = bool(total_page and page_num < total_page)
  2910. print(f"[{self.platform_name}] 第 {page_num}/{total_page or '?'} 页: 获取 {new_count} 个新作品, 累计 {len(all_works)}")
  2911. if not has_more or len(items) == 0 or new_count == 0:
  2912. break
  2913. # 页间短暂等待,避免过快触发风控
  2914. await asyncio.sleep(1)
  2915. print(f"[{self.platform_name}] ✓ 自动分页完成,共获取 {len(all_works)} 个作品")
  2916. except Exception as e:
  2917. import traceback
  2918. traceback.print_exc()
  2919. # 如果已获取到部分作品,仍然返回成功
  2920. if all_works:
  2921. print(f"[{self.platform_name}] 虽有异常但已获取 {len(all_works)} 个作品,正常返回")
  2922. return WorksResult(
  2923. success=True,
  2924. platform=self.platform_name,
  2925. works=all_works,
  2926. total=total or len(all_works),
  2927. has_more=False,
  2928. next_page="",
  2929. )
  2930. return WorksResult(
  2931. success=False,
  2932. platform=self.platform_name,
  2933. error=str(e),
  2934. debug_info="baijiahao_get_all_works_failed"
  2935. )
  2936. return WorksResult(
  2937. success=True,
  2938. platform=self.platform_name,
  2939. works=all_works,
  2940. total=total or len(all_works),
  2941. has_more=False,
  2942. next_page="",
  2943. )
  2944. async def get_article_stats(
  2945. self,
  2946. cookies: str,
  2947. start_day: str,
  2948. end_day: str,
  2949. stat_type: str,
  2950. num: int,
  2951. count: int,
  2952. ) -> dict:
  2953. """
  2954. 调用百家号 /author/eco/statistics/articleListStatistic 接口(不依赖浏览器 token),用于作品列表维度的每日数据。
  2955. """
  2956. import aiohttp
  2957. print(f"[{self.platform_name}] get_article_stats: {start_day}-{end_day}, type={stat_type}, num={num}, count={count}")
  2958. # 解析 cookies
  2959. cookie_list = self.parse_cookies(cookies)
  2960. cookie_dict = {c['name']: c['value'] for c in cookie_list}
  2961. session_headers = {
  2962. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
  2963. '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',
  2964. 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
  2965. 'Accept-Encoding': 'gzip, deflate, br',
  2966. 'Connection': 'keep-alive',
  2967. 'Upgrade-Insecure-Requests': '1',
  2968. }
  2969. headers = {
  2970. 'Accept': 'application/json, text/plain, */*',
  2971. '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',
  2972. 'Referer': 'https://baijiahao.baidu.com/builder/rc/analysiscontent/single',
  2973. 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
  2974. 'Accept-Encoding': 'gzip, deflate, br',
  2975. 'Connection': 'keep-alive',
  2976. }
  2977. async with aiohttp.ClientSession(cookies=cookie_dict) as session:
  2978. # 0) 先访问 single 页面建立会话上下文(与 Node 端 UI 打开的页面一致)
  2979. try:
  2980. await session.get(
  2981. 'https://baijiahao.baidu.com/builder/rc/analysiscontent/single',
  2982. headers=session_headers,
  2983. timeout=aiohttp.ClientTimeout(total=20),
  2984. )
  2985. except Exception as e:
  2986. print(f"[{self.platform_name}] warmup single page failed (non-fatal): {e}")
  2987. # 1) 调用 articleListStatistic
  2988. api_url = (
  2989. "https://baijiahao.baidu.com/author/eco/statistics/articleListStatistic"
  2990. f"?start_day={start_day}&end_day={end_day}&type={stat_type}&num={num}&count={count}"
  2991. )
  2992. async with session.get(
  2993. api_url,
  2994. headers=headers,
  2995. timeout=aiohttp.ClientTimeout(total=30),
  2996. ) as resp:
  2997. status = resp.status
  2998. try:
  2999. data = await resp.json()
  3000. except Exception:
  3001. text = await resp.text()
  3002. print(f"[{self.platform_name}] articleListStatistic non-JSON response: {text[:1000]}")
  3003. raise
  3004. errno = data.get('errno')
  3005. errmsg = data.get('errmsg')
  3006. print(f"[{self.platform_name}] articleListStatistic: http={status}, errno={errno}, msg={errmsg}")
  3007. return {
  3008. "success": status == 200 and errno == 0,
  3009. "status": status,
  3010. "errno": errno,
  3011. "errmsg": errmsg,
  3012. "data": data.get('data') if isinstance(data, dict) else None,
  3013. }
  3014. async def get_trend_data(
  3015. self,
  3016. cookies: str,
  3017. nid: str,
  3018. ) -> dict:
  3019. """
  3020. 调用百家号 /author/eco/statistic/gettrenddata 接口,获取单作品的按日统计数据(basic_list)。
  3021. """
  3022. import aiohttp
  3023. print(f"[{self.platform_name}] get_trend_data: nid={nid}")
  3024. cookie_list = self.parse_cookies(cookies)
  3025. cookie_dict = {c['name']: c['value'] for c in cookie_list}
  3026. session_headers = {
  3027. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
  3028. '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',
  3029. 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
  3030. 'Accept-Encoding': 'gzip, deflate, br',
  3031. 'Connection': 'keep-alive',
  3032. 'Upgrade-Insecure-Requests': '1',
  3033. }
  3034. headers = {
  3035. 'Accept': 'application/json, text/plain, */*',
  3036. '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',
  3037. 'Referer': 'https://baijiahao.baidu.com/builder/rc/analysiscontent/single',
  3038. 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
  3039. 'Accept-Encoding': 'gzip, deflate, br',
  3040. 'Connection': 'keep-alive',
  3041. }
  3042. async with aiohttp.ClientSession(cookies=cookie_dict) as session:
  3043. # 0) warmup
  3044. try:
  3045. await session.get(
  3046. 'https://baijiahao.baidu.com/builder/rc/analysiscontent/single',
  3047. headers=session_headers,
  3048. timeout=aiohttp.ClientTimeout(total=20),
  3049. )
  3050. except Exception as e:
  3051. print(f"[{self.platform_name}] warmup single page (trend) failed (non-fatal): {e}")
  3052. api_url = (
  3053. "https://baijiahao.baidu.com/author/eco/statistic/gettrenddata"
  3054. f"?nid={nid}&trend_type=all&data_type=addition"
  3055. )
  3056. async with session.get(
  3057. api_url,
  3058. headers=headers,
  3059. timeout=aiohttp.ClientTimeout(total=30),
  3060. ) as resp:
  3061. status = resp.status
  3062. try:
  3063. data = await resp.json()
  3064. except Exception:
  3065. text = await resp.text()
  3066. print(f"[{self.platform_name}] gettrenddata non-JSON response: {text[:1000]}")
  3067. raise
  3068. errno = data.get('errno')
  3069. errmsg = data.get('errmsg')
  3070. print(f"[{self.platform_name}] gettrenddata: http={status}, errno={errno}, msg={errmsg}")
  3071. return {
  3072. "success": status == 200 and errno == 0,
  3073. "status": status,
  3074. "errno": errno,
  3075. "errmsg": errmsg,
  3076. "data": data.get('data') if isinstance(data, dict) else None,
  3077. }
  3078. async def get_app_statistic_v3(
  3079. self,
  3080. cookies: str,
  3081. start_day: str,
  3082. end_day: str,
  3083. ) -> dict:
  3084. """
  3085. 调用百家号 appStatisticV3(账号维度近30天基础数据),用于用户每日数据同步。
  3086. 登录模式与打开后台一致:使用账号已存 Cookie,不启浏览器。
  3087. """
  3088. import aiohttp
  3089. print(f"[{self.platform_name}] get_app_statistic_v3: {start_day}-{end_day}")
  3090. cookie_list = self.parse_cookies(cookies)
  3091. cookie_dict = {c['name']: c['value'] for c in cookie_list}
  3092. session_headers = {
  3093. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  3094. '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',
  3095. 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
  3096. 'Accept-Encoding': 'gzip, deflate, br',
  3097. 'Connection': 'keep-alive',
  3098. }
  3099. headers = {
  3100. 'Accept': 'application/json, text/plain, */*',
  3101. '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',
  3102. 'Referer': 'https://baijiahao.baidu.com/builder/rc/analysiscontent',
  3103. 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
  3104. 'Accept-Encoding': 'gzip, deflate, br',
  3105. 'Connection': 'keep-alive',
  3106. }
  3107. async with aiohttp.ClientSession(cookies=cookie_dict) as session:
  3108. # warmup:与打开后台一致,先访问后台页面建立会话
  3109. try:
  3110. await session.get(
  3111. 'https://baijiahao.baidu.com/builder/rc/analysiscontent',
  3112. headers=session_headers,
  3113. timeout=aiohttp.ClientTimeout(total=20),
  3114. )
  3115. except Exception as e:
  3116. print(f"[{self.platform_name}] warmup analysiscontent failed (non-fatal): {e}")
  3117. api_url = (
  3118. "https://baijiahao.baidu.com/author/eco/statistics/appStatisticV3"
  3119. f"?type=all&start_day={start_day}&end_day={end_day}&stat=0&special_filter_days=30"
  3120. )
  3121. async with session.get(
  3122. api_url,
  3123. headers=headers,
  3124. timeout=aiohttp.ClientTimeout(total=30),
  3125. ) as resp:
  3126. status = resp.status
  3127. try:
  3128. data = await resp.json()
  3129. except Exception:
  3130. text = await resp.text()
  3131. print(f"[{self.platform_name}] appStatisticV3 non-JSON: {text[:1000]}")
  3132. raise
  3133. errno = data.get('errno') if isinstance(data, dict) else None
  3134. errmsg = data.get('errmsg') if isinstance(data, dict) else None
  3135. print(f"[{self.platform_name}] appStatisticV3: http={status}, errno={errno}, msg={errmsg}")
  3136. return data if isinstance(data, dict) else {"errno": -1, "errmsg": "invalid response", "data": None}
  3137. async def get_fans_basic_info(
  3138. self,
  3139. cookies: str,
  3140. start: str,
  3141. end: str,
  3142. ) -> dict:
  3143. """
  3144. 调用百家号 getFansBasicInfo(近30天粉丝数据),用于用户每日数据同步。
  3145. 登录模式与打开后台一致:使用账号已存 Cookie。
  3146. """
  3147. import aiohttp
  3148. print(f"[{self.platform_name}] get_fans_basic_info: {start}-{end}")
  3149. cookie_list = self.parse_cookies(cookies)
  3150. cookie_dict = {c['name']: c['value'] for c in cookie_list}
  3151. session_headers = {
  3152. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  3153. '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',
  3154. 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
  3155. 'Accept-Encoding': 'gzip, deflate, br',
  3156. 'Connection': 'keep-alive',
  3157. }
  3158. headers = {
  3159. 'Accept': 'application/json, text/plain, */*',
  3160. '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',
  3161. 'Referer': 'https://baijiahao.baidu.com/builder/rc/analysisfans/basedata',
  3162. 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
  3163. 'Accept-Encoding': 'gzip, deflate, br',
  3164. 'Connection': 'keep-alive',
  3165. }
  3166. async with aiohttp.ClientSession(cookies=cookie_dict) as session:
  3167. try:
  3168. await session.get(
  3169. 'https://baijiahao.baidu.com/builder/rc/analysisfans/basedata',
  3170. headers=session_headers,
  3171. timeout=aiohttp.ClientTimeout(total=20),
  3172. )
  3173. except Exception as e:
  3174. print(f"[{self.platform_name}] warmup analysisfans/basedata failed (non-fatal): {e}")
  3175. api_url = (
  3176. "https://baijiahao.baidu.com/author/eco/statistics/getFansBasicInfo"
  3177. f"?start={start}&end={end}&fans_type=new%2Csum&sort=asc&is_page=0&show_type=chart"
  3178. )
  3179. async with session.get(
  3180. api_url,
  3181. headers=headers,
  3182. timeout=aiohttp.ClientTimeout(total=30),
  3183. ) as resp:
  3184. status = resp.status
  3185. try:
  3186. data = await resp.json()
  3187. except Exception:
  3188. text = await resp.text()
  3189. print(f"[{self.platform_name}] getFansBasicInfo non-JSON: {text[:1000]}")
  3190. raise
  3191. errno = data.get('errno') if isinstance(data, dict) else None
  3192. errmsg = data.get('errmsg') if isinstance(data, dict) else None
  3193. print(f"[{self.platform_name}] getFansBasicInfo: http={status}, errno={errno}, msg={errmsg}")
  3194. return data if isinstance(data, dict) else {"errno": -1, "errmsg": "invalid response", "data": None}
  3195. async def check_login_status(self, cookies: str) -> dict:
  3196. """
  3197. 检查百家号 Cookie 登录状态
  3198. 现在与其他平台保持一致,直接复用 BasePublisher 的浏览器检测逻辑:
  3199. - 使用 Playwright 打开后台页面
  3200. - 根据是否跳转到登录页 / 是否出现登录弹窗或风控提示,判断登录是否有效
  3201. """
  3202. print(f"[{self.platform_name}] 检查登录状态 (使用通用浏览器逻辑)")
  3203. # 直接调用父类的实现,保持与抖音/小红书/视频号一致
  3204. return await super().check_login_status(cookies)
  3205. async def get_comments(self, cookies: str, work_id: str, cursor: str = "") -> CommentsResult:
  3206. """
  3207. 获取百家号作品评论
  3208. 参考 weixin.py 中同名逻辑,但百家号当前评论页仅提供「所有评论流」视图,
  3209. 且页面 DOM 结构为(外层关键节点):
  3210. div.client_pages_newComment_comment_all_list > div.cheetah-ui-pro-scroll-view >
  3211. div.list-container > div.client_pages_newComment_comment_all_listItem
  3212. 暂时按「按作品标题模糊匹配」的方式,筛选属于指定 work_id 对应作品标题的评论;
  3213. 若无法可靠匹配,则先返回该页面上的所有评论记录。
  3214. """
  3215. print(f"\n{'='*60}")
  3216. print(f"[{self.platform_name}] 获取作品评论")
  3217. print(f"[{self.platform_name}] work_id={work_id}")
  3218. print(f"{'='*60}")
  3219. comments: List[CommentItem] = []
  3220. total = 0
  3221. has_more = False
  3222. try:
  3223. # 启动浏览器并设置 Cookie(复用 BasePublisher 逻辑)
  3224. await self.init_browser()
  3225. cookie_list = self.parse_cookies(cookies)
  3226. await self.set_cookies(cookie_list)
  3227. if not self.page:
  3228. raise Exception("Page not initialized")
  3229. page = self.page
  3230. # 打开评论管理页(全量评论列表)
  3231. comment_url = "https://baijiahao.baidu.com/builder/rc/commentmanage/comment/all"
  3232. print(f"[{self.platform_name}] 正在打开评论页面: {comment_url}")
  3233. await page.goto(comment_url, timeout=30000)
  3234. await asyncio.sleep(3)
  3235. # 检查是否跳转到登录页
  3236. current_url = page.url
  3237. if "login" in current_url or "passport.baidu.com" in current_url:
  3238. raise Exception("Cookie 已过期,请重新登录")
  3239. # 等待评论列表容器加载
  3240. container_selector = ".client_pages_newComment_comment_all_list .cheetah-ui-pro-scroll-view .list-container"
  3241. print(f"[{self.platform_name}] 等待评论列表 DOM 加载...")
  3242. try:
  3243. await page.wait_for_selector(container_selector, timeout=15000)
  3244. except Exception as e:
  3245. print(f"[{self.platform_name}] 等待评论列表失败: {e}")
  3246. return CommentsResult(
  3247. success=False,
  3248. platform=self.platform_name,
  3249. work_id=work_id,
  3250. error=f"无法加载评论列表: {e}",
  3251. )
  3252. # 尝试多次下拉「加载更多」
  3253. try:
  3254. for i in range(5):
  3255. # 滚动评论列表区域
  3256. await page.evaluate(
  3257. """
  3258. () => {
  3259. const container = document.querySelector('.client_pages_newComment_comment_all_list .cheetah-ui-pro-scroll-view .list-container');
  3260. if (container) {
  3261. container.scrollTop = container.scrollHeight;
  3262. } else {
  3263. window.scrollBy(0, 600);
  3264. }
  3265. }
  3266. """
  3267. )
  3268. await asyncio.sleep(1)
  3269. # 点击「加载更多」按钮(如果存在)
  3270. await page.evaluate(
  3271. """
  3272. () => {
  3273. const more = document.querySelector('.client_pages_newComment_components_loadMore, .client_pages_newComment_comment_all_list .client_pages_newComment_components_loadMore');
  3274. if (more) {
  3275. (more.querySelector('.more-arrow') || more).click();
  3276. }
  3277. }
  3278. """
  3279. )
  3280. await asyncio.sleep(1.2)
  3281. except Exception as e:
  3282. print(f"[{self.platform_name}] 下拉/加载更多异常(非致命): {e}")
  3283. # 从 DOM 中提取评论数据
  3284. print(f"[{self.platform_name}] 从 DOM 提取评论数据...")
  3285. raw_comments = await page.evaluate(
  3286. """
  3287. () => {
  3288. const result = [];
  3289. const listRoot = document.querySelector('.client_pages_newComment_comment_all_list .cheetah-ui-pro-scroll-view .list-container');
  3290. if (!listRoot) {
  3291. return result;
  3292. }
  3293. const items = listRoot.querySelectorAll('.client_pages_newComment_comment_all_listItem');
  3294. items.forEach((item) => {
  3295. try {
  3296. const avatarImg = item.querySelector('.comment-card-avatar-wrapper-card-avatar img');
  3297. const nameEl = item.querySelector('.content-wrapper .user-container .name');
  3298. const titleContentEl = item.querySelector('.title-wrapper .title-content');
  3299. const contentEl = item.querySelector('.content-wrapper .content .content-w-highlight, .content-wrapper .content');
  3300. const infoWrapper = item.querySelector('.content-wrapper .info-wrapper');
  3301. const timeEl = infoWrapper ? infoWrapper.querySelector('span:nth-child(1)') : null;
  3302. const replyInfoEl = infoWrapper ? infoWrapper.querySelector('span:nth-child(2)') : null;
  3303. const workTitle = titleContentEl ? titleContentEl.textContent.trim() : '';
  3304. const authorName = nameEl ? nameEl.textContent.trim() : '';
  3305. const avatar = avatarImg ? avatarImg.src : '';
  3306. const content = contentEl ? contentEl.textContent.trim() : '';
  3307. const timeText = timeEl ? timeEl.textContent.trim() : '';
  3308. const replyText = replyInfoEl ? replyInfoEl.textContent.trim() : '';
  3309. const replyMatch = replyText.match(/(\\d+)/);
  3310. const replyCount = replyMatch ? parseInt(replyMatch[1], 10) : 0;
  3311. result.push({
  3312. comment_id: `bjh_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`,
  3313. parent_comment_id: "",
  3314. work_title: workTitle,
  3315. content,
  3316. username: authorName,
  3317. nickname: authorName,
  3318. avatar,
  3319. like_count: 0,
  3320. reply_count: replyCount,
  3321. create_time: timeText,
  3322. is_author: false,
  3323. create_time_unix: Date.now() / 1000,
  3324. });
  3325. } catch (e) {
  3326. // 单条解析失败忽略
  3327. }
  3328. });
  3329. return result;
  3330. }
  3331. """
  3332. )
  3333. if not isinstance(raw_comments, list):
  3334. raw_comments = []
  3335. print(f"[{self.platform_name}] 共抓取到原始评论条目: {len(raw_comments)}")
  3336. # TODO: 如果后续有更可靠的 work_id -> 标题映射,可以在此处根据 work_id 过滤
  3337. # 当前先不做强过滤,全部返回,避免漏数据
  3338. for c in raw_comments:
  3339. comment_item = CommentItem(
  3340. comment_id=c.get("comment_id", ""),
  3341. parent_comment_id=c.get("parent_comment_id", ""),
  3342. work_id=work_id,
  3343. content=c.get("content", ""),
  3344. author_id=c.get("username", ""),
  3345. author_name=c.get("nickname", ""),
  3346. author_avatar=c.get("avatar", ""),
  3347. like_count=c.get("like_count", 0),
  3348. reply_count=c.get("reply_count", 0),
  3349. create_time=c.get("create_time", ""),
  3350. )
  3351. comment_item.is_author = c.get("is_author", False)
  3352. comment_item.create_time_unix = c.get("create_time_unix", 0)
  3353. comment_item.work_title = c.get("work_title", "")
  3354. comments.append(comment_item)
  3355. total = len(comments)
  3356. # 是否还有更多暂无法从接口获知,先固定为 False
  3357. has_more = False
  3358. print(f"[{self.platform_name}] ✅ 最终整理出评论条数: {total}")
  3359. except Exception as e:
  3360. import traceback
  3361. traceback.print_exc()
  3362. return CommentsResult(
  3363. success=False,
  3364. platform=self.platform_name,
  3365. work_id=work_id,
  3366. error=str(e),
  3367. )
  3368. return CommentsResult(
  3369. success=True,
  3370. platform=self.platform_name,
  3371. work_id=work_id,
  3372. comments=comments,
  3373. total=total,
  3374. has_more=has_more,
  3375. )