import time import json import random import re import os import uuid from datetime import datetime from typing import List, Dict, Optional, Any, Callable from DrissionPage import ChromiumPage, ChromiumOptions from plugin import IPlugin from core_types import ( PluginConfig, AppointmentType, QueryResult, BookResult, AvailabilityStatus, TimeSlot, DateAvailability, NotFoundError, PermissionDeniedError, RateLimiteddError, SessionExpiredOrInvalidError, BizLogicError, PluginStatus, utc_now_str ) from utils.cloudflare_bypass_for_scraping import CloudflareBypasser from utils.mouse import HumanMouse, MOUSE_PROFILES from utils.keyboard import HumanKeyboard from utils.flow_engine import BaseFlow, on, guard, done, fatal from utils.plugin_utils import ( BrowserResponse, init_chromium_options, perform_browser_fetch, save_screenshot, filter_dates_in_range, solve_capsolver_recaptcha, handle_cookie_consent, cleanup_browser_workspace ) class TlsLoginFlow(BaseFlow): """ 基于 BaseFlow 注解状态机驱动的 TLSContact 会话创建与登录流 """ def __init__(self, page, plugin: 'TlsPlugin', interval: float = 1.0): super().__init__( page=page, config=plugin.config, context={"plugin": plugin}, interval=interval, logger=plugin._log, cf_bypasser=CloudflareBypasser(page, log=plugin.config.debug if plugin.config else False) ) self.plugin = plugin self.has_submitted_login = False self.has_cleaned_residual_session = False self.cf_block_refreshed = False self.other_langs = ['fr-fr', 'ar-ar', 'cb-ph', 'id-id', 'km-kh', 'mk-mk', 'ru-ru', 'sq-al', 'th-th', 'tl-ph', 'uz-uz', 'vi-vn', 'zh-cn', 'hy-am', 'zh-hk'] # --- 异常守卫与自愈 (Priority 5~9) --- @guard(text="502 Bad Gateway", priority=5) @guard(text="503 Service Temporarily Unavailable", priority=5) @guard(text="504 Gateway Time-out", priority=5) def on_server_network_error(self): self.log("[WARN] 检测到服务器网络错误 (502/503/504),等待 2 秒后刷新重试...", level="warning") time.sleep(2) self.page.refresh() try: self.page.wait.load_start(timeout=5) except Exception: pass @guard(ele="xpath://p[contains(text(), 'It looks like something went wrong')]", priority=5) @guard(ele="xpath://div[contains(text(), 'Error code:')]", priority=5) @guard(text="a client-side exception has occurred", priority=5) def on_spa_render_error(self): self.log("[WARN] 检测到前端 SPA 渲染或应用异常页面,尝试刷新自愈...", level="warning") time.sleep(2) self.page.refresh() try: self.page.wait.load_start(timeout=5) except Exception: pass @guard(text="Sorry, you have been blocked", priority=6) @guard(text="You are being rate limited", priority=6) @guard(text="Cloudflare Ray ID", priority=6) def on_cloudflare_blocked(self): if not self.cf_block_refreshed: self.cf_block_refreshed = True self.log("[WARN] 🛡️ 检测到 Cloudflare 阻断或速率限制页面,尝试刷新自愈 (1/1)...", level="warning") time.sleep(2) self.page.refresh() try: self.page.wait.load_start(timeout=5) except Exception: pass return raise BizLogicError("Blocked by Cloudflare WAF. Need to change IP or browser fingerprint.") @guard(title="just a moment", priority=7) @guard(title="请稍候", priority=7) def on_cloudflare_turnstile_challenge(self): self.log("🛡️ 检测到 Cloudflare 5秒盾挑战,正在尝试穿透...") self.cf_bypasser.bypass(max_retry=3) time.sleep(2) @guard(title="waitting room", priority=7) def on_cloudflare_waiting_room(self): self.log("🛡️ 检测到 Cloudflare 等候室,正在等待自动排队刷新...") self.cf_bypasser.handle_waiting_room() time.sleep(2) @guard(text="You have not yet added an applicant", priority=8) @guard(text="Add a new applicant", priority=8) @guard(url="applicants-information", priority=8) @fatal("No applicant added. Cannot proceed to booking.") def on_no_applicant_error(self): pass @guard(url="login-actions", text="Invalid username or password", priority=9) @guard(text="Invalid username or password", priority=9) @guard(text="Please solve the captcha", priority=9) @guard(text="reCAPTCHA validation failed", priority=9) @fatal("Login Failed! Invalid credentials or Captcha rejected.") def on_login_credentials_invalid(self): pass # --- 状态流转与表单处理 (Priority 15~55) --- @on( predicate=lambda flow: any(lang in flow.page.url for lang in flow.other_langs), priority=15 ) def handle_language_redirect(self): """如果 URL 语言非英语,自动切回 en-us""" curr_url = self.page.url matched_lang = next((lang for lang in self.other_langs if lang in curr_url), None) if matched_lang: target_url = curr_url.replace(matched_lang, 'en-us') self.log(f"Detected non-English locale '{matched_lang}'. Redirecting to: {target_url}") self.page.get(target_url) try: self.page.wait.load_start(timeout=3) except Exception: pass @on( predicate=lambda flow: ( not getattr(flow, 'has_submitted_login', False) and not getattr(flow, 'has_cleaned_residual_session', False) and bool(flow.page.ele("tag:svg@@data-testid=user-button", timeout=0.2)) ), priority=20 ) def handle_already_login_state(self): self.log("Detected active session from previous run (prior to login). Logging out first to clean state...") user_btn = self.page.ele("tag:svg@@data-testid=user-button") if user_btn: self.plugin.mouse.human_click_ele(user_btn) time.sleep(1.5) logout_btn = self.page.ele("#logout") if logout_btn: self.plugin.mouse.human_click_ele(logout_btn) self.has_cleaned_residual_session = True tls_url = self.plugin.free_config.get('tls_url', '') if tls_url: self.page.get(tls_url) try: self.page.wait.load_start(timeout=3) except Exception: pass @on(url="/workflow/application-summary", ele="tag:button@@text():Change", priority=30) def handle_application_summary_change_flow(self): self.log("On application-summary page. Clicking 'Change' to enter slot booking...") change_btn = self.page.ele('tag:button@@text():Change') if change_btn: change_btn.click(by_js=True) time.sleep(0.5) confirm_btn = self.page.ele('tag:button@@text():Yes') if confirm_btn: confirm_btn.click(by_js=True) try: self.page.wait.load_start(timeout=5) except Exception: pass @on(url="travel-groups", priority=40) def handle_travel_groups(self): self.log("State: Travel Groups. Parsing and selecting targeted group...") location = self.plugin.free_config.get('location', '') # 1. 如果页面有城市 Tab,确保选中目标城市 (例如 Shanghai) if location: tab_ele = self.page.ele(f'xpath://div[contains(@class, "TlsTab")]//p[contains(text(), "{location}")]') or \ self.page.ele(f'tag:p@@text():{location}') if tab_ele: parent_tab = tab_ele.parent('tag:div') if parent_tab and 'TlsTab_--selected' not in (parent_tab.attr('class') or ''): self.log(f"Switching to VAC Tab: {location}...") self.plugin.mouse.human_click_ele(tab_ele) time.sleep(1.5) # 2. 解析旅行团组 current_html = self.page.html groups = self.plugin._parse_travel_groups(current_html) self.log(f"Parsed {len(groups)} travel group(s): {groups}") # 3. 匹配目标团组 target_group = None if location: target_group = next((g for g in groups if g.get('location') and location.lower() in g['location'].lower() and g.get('submitted')), None) if not target_group: target_group = next((g for g in groups if g.get('submitted')), None) if not target_group and groups: target_group = groups[0] self.plugin.travel_group = target_group if not self.plugin.travel_group: save_screenshot(self.page, self.plugin.instance_id, "group_not_found", logger=self.log) raise NotFoundError(f"Group not found for {location}") formgroup_id = self.plugin.travel_group.get('group_number') self.log(f"Target travel group selected: {formgroup_id} (submitted={self.plugin.travel_group.get('submitted')})") # 4. 点击 Select 按钮 btn_selector = f'tag:button@@name=formGroupId@@value={formgroup_id}' if not self.page.ele(btn_selector): btn_selector = 'tag:button@@name=formGroupId' if self.page.wait.eles_loaded(btn_selector, timeout=10): buttons = self.page.eles(btn_selector) select_btn = next((btn for btn in reversed(buttons) if btn.rect.size[0] > 0 and btn.rect.size[1] > 0), None) if not select_btn and buttons: select_btn = buttons[0] if select_btn: self.log(f"Clicking Select button for group {formgroup_id}...") time.sleep(random.uniform(0.5, 1.0)) self.plugin.mouse.human_click_ele(select_btn) try: self.page.wait.load_start(timeout=5) except Exception: pass else: self.log("[WARN] Select button found but not visible.") else: self.log(f"[WARN] Wait timeout for group button {formgroup_id}") @on(ele="#book-appointment-btn", priority=45) def handle_intermediate_book_appointment_btn(self): self.log("State: Intermediate Dashboard. Clicking '#book-appointment-btn'...") self.plugin.mouse.human_click_ele(self.page.ele('#book-appointment-btn')) try: self.page.wait.load_start(timeout=3) except Exception: pass @on(ele="tag:label@@text():Email", priority=50) def handle_login_form(self): if getattr(self, "is_submitting_login", False): return self.login_attempts = getattr(self, "login_attempts", 0) + 1 if self.login_attempts > 3: raise BizLogicError("Login submission failed: remained on login page after multiple attempts.") self.log(f"State: Login Form (Attempt {self.login_attempts}). Processing credentials and ReCaptcha...") login_captcha_cfg = self.plugin.free_config.get("login_captcha", {}) capsolver_key = self.plugin.free_config.get("capsolver_key") or os.getenv("CAPSOLVER_KEY", "") # 1. 求解 ReCaptcha recaptcha_token = None has_recaptcha = bool( self.page.ele('.g-recaptcha') or self.page.ele('xpath://iframe[contains(@src, "recaptcha")]') or self.page.ele('#g-recaptcha-response') or login_captcha_cfg ) if has_recaptcha and capsolver_key: site_key = login_captcha_cfg.get("site_key") if not site_key and self.page.ele('xpath://iframe[contains(@src, "recaptcha")]'): try: from urllib.parse import urlparse, parse_qs rec_iframe = self.page.ele('xpath://iframe[contains(@src, "recaptcha")]') rec_iframe_src = rec_iframe.attr('src') or "" rec_parsed = urlparse(rec_iframe_src) rec_params = parse_qs(rec_parsed.query) site_key = rec_params.get("k", [None])[0] except Exception: pass if not site_key: site_key = "6LcDpXcfAAAAAM7wOEsF_38DNsL20tTvPTKxpyn0" task_type = login_captcha_cfg.get("task", "ReCaptchaV2TaskProxyLess") login_page_url = login_captcha_cfg.get("page_url") or self.page.url self.log(f"Solving ReCaptcha with Capsolver (site_key={site_key})...") recaptcha_token = solve_capsolver_recaptcha( api_token=capsolver_key, site_key=site_key, page_url=login_page_url, proxy=self.plugin.config.proxy if self.plugin.config else None, task_type=task_type ) self.log(f"ReCaptcha successfully solved (token length: {len(recaptcha_token)}).") # 2. 填写账号密码 username = self.plugin.config.account.username password = self.plugin.config.account.password email_input = self.page.ele('tag:label@@text():Email').next() self.plugin.mouse.human_click_ele(email_input) time.sleep(random.uniform(0.2, 0.5)) self.plugin.keyboard.type_text(username, humanize=True) time.sleep(random.uniform(0.4, 0.8)) pwd_input = self.page.ele('tag:label@@text():Password').next() self.plugin.mouse.human_click_ele(pwd_input) time.sleep(random.uniform(0.2, 0.5)) self.plugin.keyboard.type_text(password, humanize=True) # 3. 注入 Token if recaptcha_token: self.log("Injecting ReCaptcha token via JS...") inject_js = f""" (function() {{ var token = "{recaptcha_token}"; var g = document.getElementById('g-recaptcha-response'); if (g) {{ g.value = token; g.innerHTML = token; g.dispatchEvent(new Event('input', {{ bubbles: true }})); g.dispatchEvent(new Event('change', {{ bubbles: true }})); }} var textareas = document.querySelectorAll('textarea[name="g-recaptcha-response"], [name="g-recaptcha-response"]'); textareas.forEach(function(el) {{ el.value = token; el.innerHTML = token; el.dispatchEvent(new Event('input', {{ bubbles: true }})); el.dispatchEvent(new Event('change', {{ bubbles: true }})); }}); try {{ if (typeof ___grecaptcha_cfg !== 'undefined') {{ for (var cid in ___grecaptcha_cfg.clients) {{ var client = ___grecaptcha_cfg.clients[cid]; for (var p in client) {{ if (client[p] && typeof client[p].callback === 'function') {{ client[p].callback(token); }} else if (client[p] && client[p].callback && typeof window[client[p].callback] === 'function') {{ window[client[p].callback](token); }} }} }} }} }} catch(e) {{}} }})(); """ self.page.run_js(inject_js) time.sleep(random.uniform(0.5, 1.0)) # 4. 点击登录 self.log("Submitting Login...") login_btn = self.page.ele('tag:button@@text():Login') if login_btn: self.plugin.mouse.human_click_ele(login_btn) self.has_submitted_login = True try: self.page.wait.url_change(self.page.url, timeout=12) except Exception: try: self.page.wait.load_start(timeout=5) except Exception: pass self.is_submitting_login = False @on(ele="tag:a@@href:login", not_ele="tag:label@@text():Email", priority=55) def handle_login_portal_link(self): self.log("State: Login Portal. Clicking login link...") login_link = self.page.ele("tag:a@@href:login") if login_link: self.plugin.mouse.human_click_ele(login_link) try: self.page.wait.load_start(timeout=3) except Exception: pass # --- 成功终态到达 (Done, Priority 10) --- @on(url="appointment-booking", priority=10) @on(ele="tag:button@text():Book your appointment", priority=10) @done def on_reached_appointment_booking(self): btn_selector = 'tag:button@text():Book your appointment' if self.page.wait.ele_displayed(btn_selector, timeout=10): handle_cookie_consent(self.page, mouse=self.plugin.mouse, logger=self.log) self.plugin.session_create_time = time.time() self.log("✅ Login & Navigation Success! Reached appointment-booking target page.") class TlsPlugin(IPlugin): """ TLSContact 签证预约插件 (DrissionPage 版) """ name = "tls_plugin" version = "1.0.0" description = "TLSContact欧洲多国签证预约插件 (DrissionPage拟人化与CF挑战破解)" def __init__(self, group_id: str): self.group_id = group_id self.config: Optional[PluginConfig] = None self.free_config: Dict[str, Any] = {} self.is_healthy = True self.logger = None self.mouse = None self.keyboard = None self.page: Optional[ChromiumPage] = None self.travel_group: Optional[Dict] = None self.instance_id = uuid.uuid4().hex[:8] self.root_workspace = os.path.abspath(os.path.join("data/temp_browser_data", f"{self.group_id}.{self.instance_id}")) self.user_data_path = os.path.join(self.root_workspace, "user_data") if not os.path.exists(self.root_workspace): os.makedirs(self.root_workspace) self.tunnel = None self.session_create_time: float = 0 def set_log(self, logger: Callable[[str], None]): self.logger = logger def _log(self, message): if self.logger: self.logger(f'[TlsPlugin] [{self.group_id}] [{self.instance_id}] {message}') else: print(f'[TlsPlugin] [{self.group_id}] [{self.instance_id}] {message}') def set_config(self, config: PluginConfig): self.config = config self.free_config = config.free_config or {} def keep_alive(self): try: self.page.refresh() self.page.wait.load_start(timeout=2) self.page.wait.doc_loaded() time.sleep(random.uniform(1, 3)) self._check_page_is_session_expired_or_invalid('Book your appointment', html = self.page.html) except SessionExpiredOrInvalidError as e: self.is_healthy = False except Exception as e: self._log(f"Unexpected error in keep_alive: {e}") def health_check(self) -> bool: if not self.is_healthy: return False if self.page is None: return False try: if not self.page.run_js("return 1;"): return False except: return False if self.config.session_max_life > 0: current_time = time.time() elapsed_time = current_time - self.session_create_time if elapsed_time > self.config.session_max_life: self._log(f"Session expired.") return False return True def create_session(self): """ 全浏览器会话创建:过盾 -> JS注入登录 -> 状态机自动路由导航 -> 到达目标页 """ self._log(f"Initializing Session (ID: {self.instance_id})...") self._log(f"Account id={self.config.account.id}, proxy id={self.config.proxy.id if self.config and self.config.proxy else 'None'}") co, self.tunnel = init_chromium_options( user_data_path=self.user_data_path, username_seed=self.config.account.username if self.config and self.config.account else None, proxy=self.config.proxy if self.config else None, headless=False, window_size="1920,1080", logger=self._log ) self.page = ChromiumPage(co) self._log("Init humanize tools...") profile_name = random.choice(list(MOUSE_PROFILES.keys())) self._log(f"[HumanMouse] current mouse profile: {profile_name}") self.mouse = HumanMouse(self.page, timing=MOUSE_PROFILES[profile_name], debug=self.config.debug) self.keyboard = HumanKeyboard(self.page) # 打开初始站点入口 tls_url = self.free_config.get('tls_url', '') if tls_url: self._log(f"Navigating to entry: {tls_url}") self.page.get(tls_url) # 启动 TlsLoginFlow 注解流状态机(首屏5秒盾、等候室、登录等全权由状态机响应式调度) login_flow = TlsLoginFlow( self.page, plugin=self, interval=1.0 ) login_flow.run(max_steps=20) self.session_create_time = time.time() self._log("🎉 TlsLoginFlow completed successfully. Session created.") def query(self, apt_type: AppointmentType) -> QueryResult: res = QueryResult() res.success = False slots = [] self._log(f"Executing silent JS fetch...") resp = self._perform_request("GET", self.page.url, retry_count=0) self._check_page_is_session_expired_or_invalid('Book your appointment', resp.text) slots = self._parse_appointment_slots(resp.text) if slots: res.success = True earliest_date = slots[0]["date"] earliest_dt = datetime.strptime(earliest_date, "%Y-%m-%d") res.availability_status = AvailabilityStatus.Available res.earliest_date = earliest_dt date_map: dict[datetime, list[TimeSlot]] = {} for s in slots: date_str = s["date"] dt = datetime.strptime(date_str, "%Y-%m-%d") date_map.setdefault(dt, []).append( TimeSlot(time=s["time"], label=str(s.get("label", ""))) ) res.availability = [DateAvailability(date=d, times=slots) for d, slots in date_map.items()] self._log(f"Slot Found! earliest_date={earliest_date}, size={len(slots)}") else: self._log("No slots available.") res.success = False res.availability_status = AvailabilityStatus.NoneAvailable return res def book(self, slot_info: QueryResult, user_inputs: Dict = None) -> BookResult: res = BookResult() res.success = False exp_start = user_inputs.get('expected_start_date', '') exp_end = user_inputs.get('expected_end_date', '') support_pta = user_inputs.get('support_pta', True) target_labels = [''] if support_pta: target_labels.append('pta') available_dates_str =[ da.date.strftime("%Y-%m-%d") for da in slot_info.availability if da.date ] valid_dates_list = filter_dates_in_range(available_dates_str, exp_start, exp_end, shuffle=True) if not valid_dates_list: raise NotFoundError(message="No dates match user constraints") all_possible_slots =[] for da in slot_info.availability: if not da.date: continue date_str = da.date.strftime("%Y-%m-%d") if date_str in valid_dates_list: for t in da.times: if t.label in target_labels: all_possible_slots.append({ "date": date_str, "time_obj": t, "label": t.label }) if not all_possible_slots: raise NotFoundError(message="No suitable slot found (after label filtering)") selected_slot = random.choice(all_possible_slots) selected_date = selected_slot["date"] selected_time = selected_slot["time_obj"] selected_label = selected_slot["label"] self._log(f"Found {len(all_possible_slots)} valid slots. selected slot: {selected_date} {selected_time.time} {selected_label}") self.page.listen.start('/workflow/appointment-booking', method='POST') js_update_form = f""" try {{ const buttons = Array.from(document.querySelectorAll('button[type="submit"]')); const submitBtn = buttons.find(btn => {{ return btn.textContent.trim().toLowerCase().includes('book your appointment'); }}); if (!submitBtn) return 'Submit button not found'; const form = submitBtn.closest('form'); if (!form) return 'Correct form not found'; function setReactValue(input, value) {{ if (!input) return; input.value = value; }} setReactValue(form.querySelector('input[name="date"]'), '{selected_date}'); setReactValue(form.querySelector('input[name="time"]'), '{selected_time.time}'); setReactValue(form.querySelector('input[name="appointmentLabel"]'), '{selected_label}'); submitBtn.removeAttribute('disabled'); submitBtn.classList.remove('opacity-50', 'cursor-not-allowed'); return 'form_updated'; }} catch (e) {{ return e.toString(); }} """ update_res = self.page.run_js(js_update_form) self._log(f"Form update triggered: {update_res}") if update_res != 'form_updated': raise BizLogicError(message=f"Failed to update form: {update_res}") submit_btn = self.page.ele('tag:button@@type=submit@@text():Book your appointment') if not submit_btn: raise BizLogicError(message="Submit button not found for mouse click") self._log("Moving mouse to submit button and clicking") self.mouse.human_click_ele(submit_btn) packet = self.page.listen.wait(timeout=10) if not packet: raise BizLogicError(message='Listening data failed') self.page.listen.stop() self._log(f"URL: {packet.url}") self._log(f"POST Body: {packet.request.postData}") self._log(f"POST Resp: {packet.response.body}") self._log("Waiting for Next.js to process the form submission...") for _ in range(10): try: current_page_url = self.page.url current_page_html = self.page.html appointment_confirmation_indicators = [ "order-summary" in current_page_url, "partner-services" in current_page_url, "appointment-confirmation" in current_page_url, "Change my appointment" in current_page_html, "Book a new appointment" in current_page_html, ] if any(appointment_confirmation_indicators): self._log(f"✅ BOOKING SUCCESS! Redirected to: {current_page_url}") res.success = True res.label = selected_label res.book_date = selected_date res.book_time = selected_time.time save_screenshot(self.page, self.instance_id, "book_slot_success", logger=self._log) break toast_selector = 'tag:div@role=alert' toast_ele = self.page.ele(toast_selector, timeout=0.5) if toast_ele: error_msg = toast_ele.text self._log(f"❌ BOOKING FAILED! Detected popup: {error_msg}") break time.sleep(0.5) except Exception: pass return res def _perform_request(self, method, url, headers=None, data=None, json_data=None, params=None, retry_count=0): """ 在浏览器上下文中注入 JS 执行 Fetch """ resp = perform_browser_fetch(self.page, method, url, headers=headers, data=data, json_data=json_data, params=params) if resp.status_code == 200: return resp elif resp.status_code == 401: self.is_healthy = False raise SessionExpiredOrInvalidError() # 将 403 和 429 合并,共享绕盾重试逻辑 elif resp.status_code in (403, 429): if retry_count < 2: # 动态打印是 403 还是 429 self._log(f"HTTP {resp.status_code} Detected. Cloudflare block or rate limit? Attempting refresh (Try {retry_count+1}/2)...") if self._refresh_firewall_session(): self._log("Firewall session refreshed. Retrying request...") return self._perform_request(method, url, headers, data, json_data, params, retry_count+1) else: self._log("Failed to refresh firewall session.") # 如果重试 2 次仍然失败,或者刷新防火墙失败,则按原来的逻辑抛出各自的异常 if resp.status_code == 403: raise PermissionDeniedError(f"HTTP 403: {resp.text[:100]}") else: self.is_healthy = False # 注意:保留了原代码中 RateLimiteddError 的拼写(双d) raise RateLimiteddError(f"HTTP 429: {resp.text[:100]}") else: if resp.status_code == 0: raise BizLogicError(f"Network Error: {resp.text}") raise BizLogicError(message=f"HTTP Error {resp.status_code}: {resp.text[:100]}") def _refresh_firewall_session(self) -> bool: """ 主动刷新页面以触发 Cloudflare 挑战并尝试通过 """ try: self._log("Refreshing page to trigger Cloudflare...") self.page.refresh() time.sleep(5) cf = CloudflareBypasser(self.page, log=self.config.debug) success = cf.bypass(max_retry=6) if success: title = self.page.title.lower() if "access denied" in title: return False time.sleep(2) return True return False except Exception as e: self._log(f"Error during firewall refresh: {e}") return False def _parse_travel_groups(self, html_content) -> List[Dict]: groups = [] # 1. 兼容新版 applications 与旧版 travelGroups 正则 patterns = [ r'(?:\\\\\"|\")?(?:travelGroups|applications)(?:\\\\\"|\")?:\s*(\[.*?\])\s*,\s*(?:\\\\\"|\")?availableCountriesToCreateGroups', r'\\"travelGroups\\":\s*(\[.*?\])', r'\\"applications\\":\s*(\[.*?\])', r'"applications":\s*(\[.*?\])' ] for pat in patterns: js_match = re.search(pat, html_content, re.DOTALL) if js_match: try: json_str = js_match.group(1).replace(r'\"', '"').replace(r'\\"', '"') data = json.loads(json_str) for g in data: groups.append({ 'group_name': g.get('groupName') or g.get('group_name') or 'My travel group', 'group_number': str(g.get('formGroupId') or g.get('group_number') or ''), 'location': g.get('vacName') or g.get('location') or '', 'submitted': bool(g.get('submitted', True)) }) if groups: break except Exception as e: self._log(f"Error parsing travelGroups JSON: {e}") if not groups and getattr(self, 'page', None): try: btn_eles = self.page.eles('tag:button@@name=formGroupId') for btn in btn_eles: val = btn.attr('value') if val and str(val) not in [g['group_number'] for g in groups]: parent_row = btn.parent('tag:tr') or btn.parent('tag:div') is_submitted = True if parent_row and 'created' in parent_row.text.lower() and 'submitted' not in parent_row.text.lower(): is_submitted = False groups.append({ 'group_name': 'My travel group', 'group_number': str(val), 'location': '', 'submitted': is_submitted }) except Exception as e: self._log(f"DOM fallback parsing travel groups failed: {e}") if not groups: self._log('Parsed travel group page, but found no travel groups in JSON or DOM') return groups def _parse_appointment_slots(self, html_content) -> List[Dict]: slots = [] pattern = r'"availableAppointments\\":\s*(\[.*\]),\\"showFlexiAppointment' match = re.search(pattern, html_content, re.DOTALL) if match: json_str = match.group(1).replace(r'\"', '"') data = json.loads(json_str) for day in data: d_str = day.get('day') for s in day.get('slots', []): labels = s.get('labels', []) lbl = None if 'pta' in labels: lbl = 'pta' elif 'ptaw' in labels: lbl = 'ptaw' elif '' in labels: lbl = '' if lbl is not None: slots.append({ 'date': d_str, 'time': s.get('time'), 'label': lbl }) return slots def _check_page_is_session_expired_or_invalid(self, keyword, html: str) -> bool: if not html: self.is_healthy = False raise SessionExpiredOrInvalidError("Empty page HTML content") html_lower = html.lower() if keyword.lower() not in html_lower: session_expire_or_invalid_indicators = [ 'redirected automatically' in html_lower, 'session expired' in html_lower, 'session timeout' in html_lower, ('login' in html_lower and 'password' in html_lower) ] if any(session_expire_or_invalid_indicators): self.is_healthy = False raise SessionExpiredOrInvalidError("Session expired or invalid state detected in page HTML") # --- 资源清理核心方法 --- def cleanup(self): """销毁浏览器并彻底删除临时文件""" cleanup_browser_workspace(self.page, self.root_workspace, self.tunnel, self._log) self.page = None self.tunnel = None def get_plugin_status(self) -> PluginStatus: account_name = self.config.account.username if self.config and self.config.account else "" proxy_info = f"{self.config.proxy.ip}:{self.config.proxy.port}" if self.config and self.config.proxy and self.config.proxy.ip else "" current_url = "" if self.page: try: current_url = self.page.url except Exception: pass session_age = int(time.time() - self.session_create_time) if self.session_create_time > 0 else 0 state = "running" if self.is_healthy else "unhealthy" return PluginStatus( instance_id=self.instance_id, is_healthy=self.is_healthy, state=state, current_url=current_url, account=account_name, proxy=proxy_info, session_age_sec=session_age, last_active_at=utc_now_str(), ) def __del__(self): """ 析构函数:当对象被垃圾回收时自动调用 """ self.cleanup()