usa_plugin.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. import time
  2. import json
  3. import random
  4. import re
  5. import os
  6. import uuid
  7. from datetime import datetime
  8. from typing import List, Dict, Optional, Any, Callable
  9. from DrissionPage import ChromiumPage
  10. from plugin import IPlugin
  11. from core_types import (
  12. PluginConfig,
  13. AppointmentType,
  14. QueryResult,
  15. BookResult,
  16. AvailabilityStatus,
  17. DateAvailability,
  18. NotFoundError,
  19. PermissionDeniedError,
  20. RateLimiteddError,
  21. SessionExpiredOrInvalidError,
  22. BizLogicError,
  23. PluginStatus,
  24. utc_now_str
  25. )
  26. from utils.cloudflare_bypass_for_scraping import CloudflareBypasser
  27. from utils.mouse import HumanMouse
  28. from utils.keyboard import HumanKeyboard
  29. from utils.flow_engine import BaseFlow, on, guard, done, fatal
  30. from utils.plugin_utils import (
  31. init_chromium_options,
  32. filter_dates_in_range,
  33. cleanup_browser_workspace,
  34. save_screenshot
  35. )
  36. class UsaLoginFlow(BaseFlow):
  37. """
  38. 基于 BaseFlow 注解状态机驱动的美签 USVisaScheduling 会话创建与自动导航流
  39. """
  40. def __init__(self, page, plugin: 'UsaPlugin', interval: float = 1.0):
  41. super().__init__(
  42. page=page,
  43. config=plugin.config,
  44. context={"plugin": plugin},
  45. interval=interval,
  46. logger=plugin._log,
  47. cf_bypasser=CloudflareBypasser(page, log=True, logger=plugin._log),
  48. max_stuck_cycles=30
  49. )
  50. self.plugin = plugin
  51. self.login_submit_attempts = 0
  52. self.last_login_submit_time = 0.0
  53. # --- 异常守卫与自愈 (Priority 5~7) ---
  54. @guard(text="502 Bad Gateway", priority=5)
  55. @guard(text="503 Service Temporarily Unavailable", priority=5)
  56. def on_server_network_error(self):
  57. self.log("[WARN] 检测到服务器网络错误 (502/503),等待并刷新自愈...", level="warning")
  58. self.plugin._random_sleep(30, 40)
  59. self.page.refresh()
  60. try:
  61. self.page.wait.doc_loaded()
  62. except Exception:
  63. pass
  64. @guard(text="Sorry, you have been blocked", priority=6)
  65. @guard(text="You are being rate limited", priority=6)
  66. @guard(text="Cloudflare Ray ID", priority=6)
  67. @fatal("Blocked by Cloudflare WAF. Need to change IP or browser fingerprint.")
  68. def on_cloudflare_blocked(self):
  69. pass
  70. @guard(title="just a moment", priority=7)
  71. @guard(title="请稍候", priority=7)
  72. @guard(predicate=lambda flow: flow.cf_bypasser and not flow.cf_bypasser.is_bypassed(), priority=7)
  73. def on_cloudflare_5s_challenge(self):
  74. self.log("🛡️ 检测到 Cloudflare 5秒盾,正在尝试穿透...")
  75. if self.cf_bypasser.bypass(max_retry=8):
  76. self.log("✅ Cloudflare 挑战通过!")
  77. self.plugin._random_sleep(2, 4)
  78. self.cf_bypasser.handle_waiting_room()
  79. else:
  80. self.log("[WARN] 本周期未完全通过 Cloudflare,将在下一周期继续尝试...")
  81. @guard(title="waitting room", priority=7)
  82. @guard(title="waiting room", priority=7)
  83. def on_cloudflare_waiting_room(self):
  84. self.log("🛡️ 检测到 Cloudflare 等候室,正在等待自动排队刷新...")
  85. self.cf_bypasser.handle_waiting_room()
  86. time.sleep(2)
  87. # --- 状态流转与表单处理 (Priority 35~50) ---
  88. @on(ele="#signInName", priority=50)
  89. def handle_login_form(self):
  90. # 1. 冷却保护:若距离上次提交不足 15 秒,避免高频点击,静候跳转
  91. if time.time() - self.last_login_submit_time < 15.0:
  92. self.log("Login form recently submitted, waiting for redirect...")
  93. time.sleep(2)
  94. return
  95. # 2. 检查是否有页面报错(如凭证无效、被锁等)
  96. err_ele = (
  97. self.page.ele('.pageLevel', timeout=0.2)
  98. or self.page.ele('.claimVerificationServerError', timeout=0.2)
  99. or self.page.ele('.error:not([aria-hidden="true"])', timeout=0.2)
  100. )
  101. if err_ele and err_ele.text:
  102. err_msg = err_ele.text.strip().replace('\n', ' ')
  103. self.log(f"[WARN] 登录页面提示错误: '{err_msg}'", level="warning")
  104. if any(k in err_msg.lower() for k in ["invalid", "无效", "locked", "锁定"]):
  105. if self.login_submit_attempts >= 2:
  106. raise BizLogicError(f"美签登录凭据无效或账号被锁定: {err_msg}")
  107. # 3. 最大重试上限防护
  108. if self.login_submit_attempts >= 5:
  109. raise BizLogicError("登录失败:已重试 5 次登录仍停留在登录页面,请检查账号密码或代理网络。")
  110. self.login_submit_attempts += 1
  111. self.log(f"[State] Login page detected. Submitting credentials (attempt {self.login_submit_attempts}/5)...")
  112. username = self.plugin.config.account.username
  113. password = self.plugin.config.account.password
  114. username_input = self.page.ele('#signInName')
  115. self.plugin.mouse.human_click_ele(username_input)
  116. username_input.clear()
  117. self.plugin.keyboard.type_text(username, humanize=True)
  118. self.plugin._random_sleep(2, 4)
  119. password_input = self.page.ele('#password')
  120. self.plugin.mouse.human_click_ele(password_input)
  121. password_input.clear()
  122. self.plugin.keyboard.type_text(password, humanize=True)
  123. self.plugin._random_sleep(2, 4)
  124. continue_btn = self.page.ele('#continue')
  125. if continue_btn:
  126. self.plugin.mouse.human_click_ele(continue_btn)
  127. self.last_login_submit_time = time.time()
  128. self.log("Login form submitted. Waiting for the next step to load...")
  129. self.plugin._random_sleep(10, 15)
  130. def _extract_security_answer(self, security: Any, q_num: str) -> Optional[str]:
  131. if not security:
  132. return None
  133. # 1. 列表格式: ["ans1", "ans2", "ans3"]
  134. if isinstance(security, list):
  135. idx = int(q_num) - 1
  136. if 0 <= idx < len(security):
  137. return str(security[idx])
  138. return None
  139. # 2. 字典格式
  140. if isinstance(security, dict):
  141. # 极简键格式: {"1": "ans1", "2": "ans2"} 或 {1: "ans1", 2: "ans2"}
  142. if q_num in security:
  143. val = security[q_num]
  144. return str(val.get('a', '')) if isinstance(val, dict) else str(val)
  145. if q_num.isdigit() and int(q_num) in security:
  146. val = security[int(q_num)]
  147. return str(val.get('a', '')) if isinstance(val, dict) else str(val)
  148. # 旧版嵌套键格式: {"1_quest": {"a": "ans1"}} 或 {"1_quest": "ans1"}
  149. config_key = f"{q_num}_quest"
  150. if config_key in security:
  151. val = security[config_key]
  152. return str(val.get('a', '')) if isinstance(val, dict) else str(val)
  153. return None
  154. @on(ele="xpath://input[starts-with(@id, 'kba') and contains(@id, '_response')]", priority=45)
  155. def handle_kba_security_questions(self):
  156. self.log("[State] Security question verification detected. Filling in answers...")
  157. security = self.plugin.free_config.get('security')
  158. answer_eles = self.page.eles('xpath://input[starts-with(@id, "kba") and contains(@id, "_response")]')
  159. if not security:
  160. save_screenshot(self.page, self.plugin.instance_id, "kba_security_missing", logger=self.log)
  161. err_msg = "美签登录触发了 KBA 密保问题验证,但在 free_config 中未配置 'security' 答案,无法继续登录。"
  162. self.log(f"[ERROR] {err_msg}", level="error")
  163. raise BizLogicError(err_msg)
  164. for ans_ele in answer_eles:
  165. ele_id = ans_ele.attr('id')
  166. match = re.search(r'kba(\d+)_response', ele_id)
  167. if match:
  168. q_num = match.group(1)
  169. ans_text = self._extract_security_answer(security, q_num)
  170. if ans_text is None:
  171. save_screenshot(self.page, self.plugin.instance_id, f"kba_q{q_num}_missing", logger=self.log)
  172. err_msg = f"美签登录触发了第 {q_num} 道密保问题,但配置中未提供对应答案(当前配置: {security})。"
  173. self.log(f"[ERROR] {err_msg}", level="error")
  174. raise BizLogicError(err_msg)
  175. self.plugin.mouse.human_click_ele(ans_ele)
  176. ans_ele.clear()
  177. self.plugin.keyboard.type_text(ans_text, humanize=True)
  178. self.log(f"-> Found input {ele_id}, successfully filled in answer for question {q_num}.")
  179. continue_btn = self.page.ele('#continue')
  180. if continue_btn:
  181. self.plugin.mouse.human_click_ele(continue_btn)
  182. self.log("Security answers submitted. Waiting for redirection...")
  183. self.plugin._random_sleep(30, 40)
  184. @on(ele="#atlas-sidebar", priority=35)
  185. def handle_dashboard_sidebar(self):
  186. self.log("[State] At the main booking dashboard. Looking for navigation button...")
  187. # 1. 优先检查重新安排预约按钮 (#reschedule_appointment,针对已有预约账号的改约)
  188. reschedule_btn = self.page.ele('#reschedule_appointment', timeout=0.5)
  189. if reschedule_btn:
  190. self.log("Currently in rescheduling mode ('#reschedule_appointment'), clicking to proceed...")
  191. self.plugin.mouse.human_click_ele(reschedule_btn)
  192. self.plugin._random_sleep(15, 25)
  193. return
  194. # 2. 检查首次预约按钮 (#schedule_appointment)
  195. schedule_btn = self.page.ele('#schedule_appointment', timeout=0.5)
  196. if schedule_btn:
  197. self.log("Currently in first-time booking mode ('#schedule_appointment'), clicking to proceed...")
  198. self.plugin.mouse.human_click_ele(schedule_btn)
  199. self.plugin._random_sleep(15, 25)
  200. return
  201. # 3. 检查动态注入的 admin_link 安排预约
  202. admin_link = self.page.ele('#admin_link', timeout=0.5)
  203. if admin_link:
  204. self.log("Found '#admin_link' (安排预约), clicking to proceed...")
  205. self.plugin.mouse.human_click_ele(admin_link)
  206. self.plugin._random_sleep(15, 25)
  207. return
  208. # 4. 兜底匹配侧边栏内部包含 href='/schedule/' 或文本匹配预约/改约的链接
  209. fallback_link = (
  210. self.page.ele('css:#atlas-sidebar a[href*="schedule"]', timeout=0.5)
  211. or self.page.ele('xpath://*[@id="atlas-sidebar"]//a[contains(text(), "安排预约") or contains(text(), "重新安排预约") or contains(text(), "Schedule") or contains(text(), "Reschedule")]', timeout=0.5)
  212. )
  213. if fallback_link:
  214. self.log(f"Found sidebar booking link (text='{fallback_link.text}'), clicking to proceed...")
  215. self.plugin.mouse.human_click_ele(fallback_link)
  216. self.plugin._random_sleep(15, 25)
  217. return
  218. self.log("Not found schedule or reschedule button. The page may still be loading...")
  219. time.sleep(2)
  220. # --- 成功终态到达 (Priority 10) ---
  221. @on(ele="#post_select", priority=10)
  222. @done
  223. def on_reached_slot_search_page(self):
  224. self.plugin.session_create_time = time.time()
  225. self.log("🎉 Successfully reached the Slot Search page (Target Page). Session created successfully!")
  226. class UsaPlugin(IPlugin):
  227. name = "usa_plugin"
  228. version = "1.0.0"
  229. description = "美签USVisaScheduling预约插件 (B2C认证、流控调度与自动占位)"
  230. LOCATIONS = {
  231. "BEIJING": {"name": "BEIJING", "id": "106bf614-b0db-ec11-a7b4-001dd80234f6"},
  232. "GUANGZHOU": {"name": "GUANGZHOU", "id": "a26af614-b0db-ec11-a7b4-001dd80234f6"},
  233. "SHANGHAI": {"name": "SHANGHAI", "id": "096bf614-b0db-ec11-a7b4-001dd80234f6"},
  234. "WUHAN": {"name": "WUHAN", "id": "7b6af614-b0db-ec11-a7b4-001dd80234f6"},
  235. "SHENYANG": {"name": "SHENYANG", "id": "0f6bf614-b0db-ec11-a7b4-001dd80234f6"},
  236. }
  237. def __init__(self, group_id: str):
  238. self.group_id = group_id
  239. self.config: Optional[PluginConfig] = None
  240. self.free_config: Dict[str, Any] = {}
  241. self.is_healthy = True
  242. self.logger = None
  243. self.mouse = None
  244. self.keyboard = None
  245. self.page: Optional[ChromiumPage] = None
  246. self.instance_id = uuid.uuid4().hex[:8]
  247. self.root_workspace = os.path.abspath(os.path.join("data/temp_browser_data", f"{self.group_id}.{self.instance_id}"))
  248. self.user_data_path = os.path.join(self.root_workspace, "user_data")
  249. if not os.path.exists(self.root_workspace):
  250. os.makedirs(self.root_workspace)
  251. self.tunnel = None
  252. self.session_create_time: float = 0
  253. def _log(self, message):
  254. if self.logger:
  255. self.logger(f'[UsaPlugin] [{self.group_id}] [{self.instance_id}] {message}')
  256. else:
  257. print(f'[UsaPlugin] [{self.group_id}] [{self.instance_id}] {message}')
  258. def _random_sleep(self, min_sec=30, max_sec=60):
  259. """
  260. 核心防限速控制:模拟人类阅读和操作的长时间停顿,确保网络请求间隔在30-60秒
  261. """
  262. sleep_time = random.uniform(min_sec, max_sec)
  263. self._log(f"Anti-Rate-Limit: Sleeping for {sleep_time:.2f} seconds...")
  264. time.sleep(sleep_time)
  265. def set_config(self, config: PluginConfig):
  266. """设置 API 的配置信息"""
  267. self.config = config
  268. self.free_config = config.free_config or {}
  269. def set_log(self, logger: Callable[[str], None]) -> None:
  270. """设置日志输出工具"""
  271. self.logger = logger
  272. def keep_alive(self):
  273. """
  274. 通过页面内原生轻量 fetch 请求进行保活(每10秒调用一次):
  275. 1. 保持底层 HTTP/2 和代理长连接活跃,防止连接中断或代理切换 IP
  276. 2. 维持 Cloudflare 会话与 Session 活跃状态
  277. 3. 轻量请求 /favicon.ico,不增加后端业务负担
  278. """
  279. if not self.page or not self.is_healthy:
  280. return
  281. try:
  282. # 在浏览器页面上下文内发起轻量 fetch 请求 (/favicon.ico)
  283. js_code = """
  284. return fetch('/favicon.ico?_t=' + Date.now(), {
  285. method: 'GET',
  286. cache: 'no-store',
  287. credentials: 'include'
  288. }).then(r => ({ status: r.status, ok: r.ok }))
  289. .catch(e => ({ status: 0, error: e.message }));
  290. """
  291. result = self.page.run_js(js_code)
  292. status = result.get("status", 0) if isinstance(result, dict) else 0
  293. self._log(f"🟡 [KeepAlive] Ping 响应状态: {status}")
  294. except Exception as e:
  295. self._log(f"⚠️ [KeepAlive] 保活调用异常: {e}")
  296. def health_check(self) -> bool:
  297. if not self.is_healthy:
  298. return False
  299. if self.page is None:
  300. return False
  301. try:
  302. if not self.page.run_js("return 1;"):
  303. return False
  304. except Exception:
  305. return False
  306. if self.config.session_max_life > 0:
  307. current_time = time.time()
  308. elapsed_time = current_time - self.session_create_time
  309. if elapsed_time > self.config.session_max_life:
  310. self._log("Session expired.")
  311. return False
  312. return True
  313. def _bypass_cf_via_new_tab(self) -> bool:
  314. """
  315. 打开新标签页访问首页,绕过 Cloudflare 机器人验证页面并获取更新的 clearance 凭据,
  316. 完成后关闭新标签页并切换回原排期页面。
  317. """
  318. root_url = f"{self.free_config.get('usa_url', 'https://www.usvisascheduling.com').rstrip('/')}/"
  319. self._log(f"Opening new tab to '{root_url}' to bypass Cloudflare verification...")
  320. tab = None
  321. original_tab_id = self.page.tab_id
  322. try:
  323. # 1. 打开新标签页
  324. tab = self.page.new_tab(root_url)
  325. self._random_sleep(2, 3)
  326. # 2. 检查并穿透 Cloudflare 机器人验证 / 5秒盾
  327. cf_bypasser = CloudflareBypasser(tab, log=True, logger=self._log)
  328. page_title = (getattr(tab, "title", "") or "").lower()
  329. if not cf_bypasser.is_bypassed() or any(w in page_title for w in ["just a moment", "请稍候", "attention required", "cloudflare"]):
  330. self._log("🛡️ 检测到新标签页触发 Cloudflare 挑战,正在穿透...")
  331. if not cf_bypasser.bypass(max_retry=8):
  332. self._log("[WARN] 新标签页 Cloudflare 穿透未完全成功")
  333. self._random_sleep(2, 4)
  334. cf_bypasser.handle_waiting_room()
  335. else:
  336. self._log("新标签页未触发挑战或已通过 Cloudflare 验证。")
  337. self._random_sleep(1, 2)
  338. cf_bypasser.handle_waiting_room()
  339. self._log("✅ 新标签页 Cloudflare 验证处理完成。")
  340. return True
  341. except Exception as e:
  342. self._log(f"[ERROR] 新标签页穿透 Cloudflare 发生异常: {e}")
  343. return False
  344. finally:
  345. if tab:
  346. try:
  347. tab.close()
  348. self._log("新标签页已关闭。")
  349. except Exception as e:
  350. self._log(f"[WARN] 关闭新标签页失败: {e}")
  351. try:
  352. self.page.activate_tab(original_tab_id)
  353. except Exception as e:
  354. self._log(f"[DEBUG] 激活原排期标签页失败: {e}")
  355. self._random_sleep(1, 2)
  356. def create_session(self) -> None:
  357. """创建一个新的会话 (包含初始化浏览器、过CF验证和执行登录)"""
  358. self._log(f"Initializing Session (ID: {self.instance_id})...")
  359. self._log(f"Account id={self.config.account.id}, proxy id={self.config.proxy.id if self.config and self.config.proxy else 'None'}")
  360. co, self.tunnel = init_chromium_options(
  361. user_data_path=self.user_data_path,
  362. username_seed=self.config.account.username if self.config and self.config.account else None,
  363. proxy=self.config.proxy if self.config else None,
  364. headless=False,
  365. window_size="1920,1080",
  366. logger=self._log
  367. )
  368. self.page = ChromiumPage(co)
  369. self._log("Init humanize tools...")
  370. self.mouse = HumanMouse(self.page, debug=False)
  371. self.keyboard = HumanKeyboard(self.page)
  372. viewport_width = self.page.rect.viewport_size[0]
  373. viewport_height = self.page.rect.viewport_size[1]
  374. init_x = random.randint(10, viewport_width - 10)
  375. init_y = random.randint(10, viewport_height - 10)
  376. self.mouse.move(init_x, init_y)
  377. usa_url = self.free_config.get('usa_url', '')
  378. if usa_url:
  379. self._log(f"Navigating: {usa_url}")
  380. self.page.get(usa_url)
  381. # 启动 UsaLoginFlow 注解流状态机
  382. login_flow = UsaLoginFlow(
  383. self.page,
  384. plugin=self,
  385. interval=1.0
  386. )
  387. login_flow.run(max_steps=100, timeout=600.0)
  388. self.session_create_time = time.time()
  389. self._log("🎉 UsaLoginFlow completed successfully. Session created.")
  390. def query(self, apt_type: AppointmentType) -> QueryResult:
  391. """查询可用的签证预约信息"""
  392. self._log("Querying available slots...")
  393. res = QueryResult()
  394. res.success = False
  395. location_name = (self.free_config.get('location') or "").strip()
  396. # 最多尝试 3 次:首次查询 + 若遇 PSE0501/CF质询/超时则在新标签页过盾重试
  397. for attempt in range(3):
  398. current_url = (getattr(self.page, "url", "") or "").lower()
  399. if "schedule" not in current_url:
  400. self._log(f"Current url '{current_url}' not on schedule page, navigating...")
  401. schedule_url = f"{self.free_config.get('usa_url', 'https://www.usvisascheduling.com').rstrip('/')}/zh-CN/schedule/"
  402. self.page.get(schedule_url)
  403. self._random_sleep(3, 5)
  404. else:
  405. self._log("Already on schedule page, waiting for dynamic elements to finish loading...")
  406. if "just a moment" in (getattr(self.page, "title", "") or "").lower():
  407. cf_bypasser = CloudflareBypasser(self.page, log=self.config.debug if self.config else False, logger=self._log)
  408. if not cf_bypasser.bypass(max_retry=5):
  409. raise BizLogicError("Cloudflare bypass timeout")
  410. self._random_sleep(3, 5)
  411. cf_bypasser.handle_waiting_room()
  412. current_url = (getattr(self.page, "url", "") or "").lower()
  413. if 'auth' in current_url or 'login' in current_url:
  414. self.is_healthy = False
  415. raise SessionExpiredOrInvalidError()
  416. # 1. 等待申请人列表加载(从 page1 空列表异步加载到 page2)
  417. self._log("Waiting for applicant list (#gm_select) to populate...")
  418. self.page.ele("css:#gm_select label", timeout=30)
  419. # 2. 等待领事馆下拉框选项加载完成
  420. self._log("Waiting for consular post options to populate in #post_select...")
  421. post_select = self.page.ele('#post_select', timeout=30)
  422. if not post_select:
  423. if attempt < 2:
  424. self._log("[WARN] 未找到 #post_select,尝试在新标签页绕过人机验证重试...")
  425. self._bypass_cf_via_new_tab()
  426. continue
  427. raise BizLogicError("Element #post_select not found.")
  428. # 等待至少出现一个有效选项
  429. self.page.ele("css:#post_select option:not([value=''])", timeout=30)
  430. # 获取所有已加载的领事馆 options
  431. all_options = post_select.eles("tag:option")
  432. available_options = [opt for opt in all_options if (getattr(opt, "attrs", {}).get("value") or "").strip()]
  433. opts_summary = [f"{opt.text} ({getattr(opt, 'attrs', {}).get('value')})" for opt in available_options]
  434. self._log(f"Available consular posts: {opts_summary}")
  435. # 3. 匹配目标领事馆
  436. matched_id = None
  437. # 策略 A: 优先检查 LOCATIONS 字典
  438. known_info = self.LOCATIONS.get(location_name.upper())
  439. if known_info:
  440. target_id = known_info["id"]
  441. if any(opt.attrs.get("value") == target_id for opt in available_options):
  442. matched_id = target_id
  443. # 策略 B: 按文本包含匹配(例如 "GUANGZHOU" 或 "广州")
  444. if not matched_id:
  445. for opt in available_options:
  446. opt_text = (getattr(opt, "text", "") or "").upper()
  447. if location_name.upper() in opt_text:
  448. matched_id = opt.attrs.get("value")
  449. break
  450. # 策略 C: 针对当前申请锁定特定城市(如账号当前仅有广州选项)自适应兜底
  451. if not matched_id and available_options:
  452. fallback_opt = available_options[0]
  453. matched_id = fallback_opt.attrs.get("value")
  454. self._log(f"[WARN] Configured location '{location_name}' not available. Auto-selecting active consular post: '{fallback_opt.text}' ({matched_id})")
  455. if not matched_id:
  456. raise BizLogicError(f"No matching consular post found for location '{location_name}'. Available: {[opt.text for opt in available_options]}")
  457. # 开启监听目标 API
  458. target_api = 'get-family-consular-schedule-days'
  459. self.page.listen.start(target_api)
  460. # 4. 选择领事馆并触发 change 事件 (此操作会触发上述 API 的 AJAX 请求)
  461. self._log(f"Selecting consular post ID: {matched_id}...")
  462. post_select.select.by_value(matched_id)
  463. try:
  464. self.page.run_js('$("#post_select").val(arguments[0]).trigger("change");', matched_id)
  465. except Exception as e:
  466. self._log(f"[DEBUG] Triggering change event via JS: {e}")
  467. # 5. 等待拦截 API 响应 (设置超时时间)
  468. self._log("Waiting for schedule dates API response...")
  469. packet = None
  470. try:
  471. packet = self.page.listen.wait(timeout=20)
  472. finally:
  473. self.page.listen.stop()
  474. # 仅在未收到响应或状态码非 200 时检查是否有错误弹窗(如 PSE0501)
  475. if not packet or packet.response.status != 200:
  476. alert_msg = None
  477. try:
  478. alert_msg = self.page.handle_alert(accept=True, timeout=0.2)
  479. except Exception:
  480. pass
  481. if alert_msg:
  482. self._log(f"[WARN] 页面弹出提示: '{alert_msg}'")
  483. is_pse0501 = bool(alert_msg and ("PSE0501" in alert_msg or "无法加载可预约日期" in alert_msg))
  484. # 场景 1: 遇到 PSE0501 或 403 (CF 质询拦截)
  485. if is_pse0501 or (packet and packet.response.status == 403):
  486. if attempt < 2:
  487. self._log("[WARN] 检测到 PSE0501 或 HTTP 403 质询,通过新标签页访问首页绕过人机验证...")
  488. self._bypass_cf_via_new_tab()
  489. continue
  490. if alert_msg:
  491. raise BizLogicError(f"页面弹出提示: {alert_msg}")
  492. err_msg = str(packet.response.body)[:256] if packet else "Forbidden"
  493. raise PermissionDeniedError(f"HTTP 403: {err_msg}")
  494. # 场景 2: 未收到响应数据包 (超时)
  495. if not packet:
  496. if attempt < 2:
  497. self._log("[WARN] 未收到排期响应数据包,尝试通过新标签页绕过人机验证重试...")
  498. self._bypass_cf_via_new_tab()
  499. continue
  500. if alert_msg:
  501. raise BizLogicError(f"页面弹出提示: {alert_msg}")
  502. raise BizLogicError("Timeout waiting for schedule API response")
  503. # 场景 3: 其他 HTTP 异常状态码 (如 429)
  504. status_code = packet.response.status
  505. body = packet.response.body
  506. if isinstance(body, bytes):
  507. body = body.decode("utf-8", errors="ignore")
  508. err_text = str(body)[:512]
  509. if status_code == 429:
  510. self.is_healthy = False
  511. raise RateLimiteddError(f"HTTP 429: {err_text}")
  512. raise BizLogicError(f"HTTP {status_code} error. resp={err_text}")
  513. status_code = packet.response.status
  514. body = packet.response.body
  515. if isinstance(body, bytes):
  516. body = body.decode("utf-8", errors="ignore")
  517. self._log(f"API Response Status: {status_code}")
  518. # 6. 解析返回的数据
  519. available_dates = []
  520. data = body if isinstance(body, dict) else None
  521. if not data:
  522. match = re.search(r'(\{.*\})', str(body), re.DOTALL)
  523. data = json.loads(match.group(1)) if match else json.loads(str(body))
  524. schedule_days = data.get("ScheduleDays", [])
  525. if schedule_days:
  526. for day_obj in schedule_days:
  527. date_str = day_obj.get("Date")
  528. if date_str:
  529. available_dates.append(date_str)
  530. # 6. 处理最终结果
  531. if available_dates:
  532. available_dates.sort()
  533. res.success = True
  534. res.availability_status = AvailabilityStatus.Available
  535. earliest_date = available_dates[0]
  536. res.earliest_date = datetime.strptime(earliest_date, "%Y-%m-%d")
  537. res.availability = [
  538. DateAvailability(date=datetime.strptime(d, "%Y-%m-%d"), times=[])
  539. for d in available_dates
  540. ]
  541. self._log(f"Slot Found! earliest_date={earliest_date}, size={len(available_dates)}")
  542. else:
  543. res.success = False
  544. res.availability_status = AvailabilityStatus.NoneAvailable
  545. self._log("No slots available.")
  546. return res
  547. def book(self, slot_info: QueryResult, user_inputs) -> BookResult:
  548. """进行预约操作"""
  549. res = BookResult()
  550. res.success = False
  551. exp_start = user_inputs.get('expected_date_start', '')
  552. exp_end = user_inputs.get('expected_date_end', '')
  553. available_dates_str = [
  554. da.date.strftime("%Y-%m-%d")
  555. for da in slot_info.availability if da.date
  556. ]
  557. valid_dates_list = filter_dates_in_range(available_dates_str, exp_start, exp_end, shuffle=True)
  558. if not valid_dates_list:
  559. raise NotFoundError(message="No dates match user constraints")
  560. selected_slot_date = random.choice(valid_dates_list)
  561. book_date_obj = datetime.strptime(selected_slot_date, "%Y-%m-%d").date()
  562. # jQuery UI Datepicker 月份是 0-11
  563. target_day = str(book_date_obj.day)
  564. target_month = str(book_date_obj.month - 1)
  565. target_year = str(book_date_obj.year)
  566. self._log(f"Target booking date: {selected_slot_date}. Navigating calendar...")
  567. # 1. 适配不在当前日历的情况:通过下拉框选择年份和月份
  568. year_select = self.page.ele('.ui-datepicker-year', timeout=10) # 查找年份下拉框
  569. if year_select and year_select.value != target_year:
  570. self._log(f"Changing year to {target_year}")
  571. year_select.select.by_value(target_year)
  572. self._random_sleep(0.5, 1)
  573. month_select = self.page.ele('.ui-datepicker-month', timeout=10) # 查找月份下拉框
  574. if month_select and month_select.value != target_month:
  575. self._log(f"Changing month to {target_month}")
  576. month_select.select.by_value(target_month)
  577. self._random_sleep(0.5, 1)
  578. # 2. 点击目标日期
  579. self._log(f"Clicking date {selected_slot_date}...")
  580. target_cell_selector = f"xpath://td[@data-year='{target_year}' and @data-month='{target_month}']//a[text()='{target_day}']"
  581. target_date_cell = self.page.ele(target_cell_selector, timeout=10)
  582. if not target_date_cell:
  583. raise BizLogicError(f"Target date element not found for {selected_slot_date} after navigating.")
  584. self.mouse.human_click_ele(target_date_cell)
  585. self._log("Waiting for available times to load...")
  586. self._random_sleep(3, 5)
  587. # 3. 等待并选择时间
  588. self._log("Selecting earliest available time...")
  589. slot_time_selector = "css:#time_select input[name='schedule-entries']"
  590. first_time_radio = self.page.ele(slot_time_selector, timeout=15)
  591. if not first_time_radio:
  592. raise BizLogicError("Failed to load time slots after clicking date. Might be rate limited or slots gone.")
  593. selected_slot_time = first_time_radio.parent().text.strip()
  594. self.mouse.human_click_ele(first_time_radio)
  595. # 4. 点击提交
  596. self._random_sleep(1, 2)
  597. self._log("Submitting booking...")
  598. submit_appointment_selector = "#submitbtn"
  599. submit_button = self.page.ele(submit_appointment_selector, timeout=10)
  600. self.mouse.human_click_ele(submit_button)
  601. self._log("Booking submitted successfully!")
  602. # 构造返回结果
  603. res = BookResult()
  604. res.success = True
  605. res.book_date = selected_slot_date
  606. res.book_time = selected_slot_time
  607. res.account = self.config.account.username
  608. return res
  609. def cleanup(self):
  610. """销毁浏览器并彻底删除临时文件"""
  611. if getattr(self, "keep_browser_on_error", False):
  612. self._log("[DEBUG] keep_browser_on_error 生效,跳过浏览器清理并保留窗口供分析。")
  613. return
  614. cleanup_browser_workspace(self.page, self.root_workspace, self.tunnel, self._log)
  615. self.page = None
  616. self.tunnel = None
  617. def get_plugin_status(self) -> PluginStatus:
  618. account_name = self.config.account.username if self.config and self.config.account else ""
  619. proxy_info = f"{self.config.proxy.ip}:{self.config.proxy.port}" if self.config and self.config.proxy and self.config.proxy.ip else ""
  620. current_url = ""
  621. if self.page:
  622. try:
  623. current_url = self.page.url
  624. except Exception:
  625. pass
  626. session_age = int(time.time() - self.session_create_time) if self.session_create_time > 0 else 0
  627. state = "running" if self.is_healthy else "unhealthy"
  628. return PluginStatus(
  629. instance_id=self.instance_id,
  630. is_healthy=self.is_healthy,
  631. state=state,
  632. current_url=current_url,
  633. account=account_name,
  634. proxy=proxy_info,
  635. session_age_sec=session_age,
  636. last_active_at=utc_now_str(),
  637. )
  638. def __del__(self):
  639. """
  640. 析构函数:当对象被垃圾回收时自动调用
  641. """
  642. self.cleanup()