| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957 |
- import time
- import json
- import random
- import re
- import os
- import uuid
- import shutil
- import socket
- from datetime import datetime
- from typing import List, Dict, Optional, Any, Callable
- from urllib.parse import urljoin, urlparse, urlencode, parse_qs
- # DrissionPage 核心
- from DrissionPage import ChromiumPage, ChromiumOptions
- from vs_plg import IVSPlg
- from vs_types import VSPlgConfig, AppointmentType, VSQueryResult, VSBookResult, AvailabilityStatus, TimeSlot, DateAvailability, NotFoundError, PermissionDeniedError, RateLimiteddError, SessionExpiredOrInvalidError, BizLogicError
- from utils.cloudflare_bypass_for_scraping import CloudflareBypasser
- from toolkit.proxy_tunnel import ProxyTunnel
- from utils.mouse import HumanMouse
- from utils.keyboard import HumanKeyboard
- from utils.fingerprint_utils import FingerprintGenerator
- class BrowserResponse:
- """模拟 requests.Response"""
- def __init__(self, result_dict):
- result_dict = result_dict or {}
- self.status_code = result_dict.get('status', 0)
- self.text = result_dict.get('body', '')
- self.headers = result_dict.get('headers', {})
- self.url = result_dict.get('url', '')
- self._json = None
- def json(self):
- if self._json is None:
- if not self.text:
- return {}
- try:
- self._json = json.loads(self.text)
- except:
- self._json = {}
- return self._json
- class TlsPlugin(IVSPlg):
- """
- TLSContact 签证预约插件 (DrissionPage 版)
- """
- def __init__(self, group_id: str):
- self.group_id = group_id
- self.config: Optional[VSPlgConfig] = 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 get_group_id(self) -> str:
- return self.group_id
-
- 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}] {message}')
- else:
- print(f'[TlsPlugin] [{self.group_id}] {message}')
- def set_config(self, config: VSPlgConfig):
- self.config = config
- self.free_config = config.free_config or {}
-
- def keep_alive(self):
- try:
- resp = self._perform_request("GET", self.page.url, retry_count=1)
- self._check_page_is_session_expired_or_invalid('Book your appointment', html = resp.text)
- except SessionExpiredOrInvalidError as e:
- self.is_healthy = False
- except Exception as e:
- pass
- 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 _save_screenshot(self, name_prefix):
- try:
- timestamp = int(time.time())
- filename = f"{self.instance_id}_{name_prefix}_{timestamp}.jpg"
- save_path = os.path.join("data", filename)
- os.makedirs("data", exist_ok=True)
- self.page.get_screenshot(path=save_path, full_page=False)
- self._log(f"Screenshot saved to {save_path}")
- except Exception as e:
- self._log(f"Failed to save screenshot: {e}")
- def create_session(self):
- """
- 全浏览器会话创建:过盾 -> JS注入登录 -> 状态机自动路由导航 -> 到达目标页
- """
- self._log(f"Initializing Session (ID: {self.instance_id})...")
- co = ChromiumOptions()
-
- def get_free_port():
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
- s.bind(('', 0))
- return s.getsockname()[1]
-
- debug_port = get_free_port()
- self._log(f"Assigned Debug Port: {debug_port}")
-
- co.set_local_port(debug_port)
- co.set_user_data_path(self.user_data_path)
-
- chrome_path = os.getenv("CHROME_BIN")
- if chrome_path and os.path.exists(chrome_path):
- co.set_paths(browser_path=chrome_path)
-
- if self.config.proxy and self.config.proxy.ip:
- p = self.config.proxy
- if p.username and p.password:
- self._log(f"Starting Proxy Tunnel for {p.ip}...")
- self.tunnel = ProxyTunnel(p.ip, p.port, p.username, p.password)
- local_proxy = self.tunnel.start()
- self._log(f"Tunnel started at {local_proxy}")
- co.set_argument(f'--proxy-server={local_proxy}')
- else:
- proxy_str = f"{p.proto}://{p.ip}:{p.port}"
- co.set_argument(f'--proxy-server={proxy_str}')
- else:
- self._log("[WARN] No proxy configured!")
-
- fingerprint_gen = FingerprintGenerator()
- specific_fp = fingerprint_gen.generate(self.config.account.username)
- self._log(f'browser fingerprint={specific_fp}')
- co.headless(False)
- co.set_argument('--no-sandbox')
- co.set_argument('--disable-dev-shm-usage')
- co.set_argument('--window-size=1920,1080')
- co.set_argument('--disable-blink-features=AutomationControlled')
- co.set_argument('--ignore-gpu-blocklist')
- co.set_argument('--enable-webgl')
- co.set_argument('--use-gl=angle')
- co.set_argument('--use-angle=swiftshader')
- co.set_argument(f"--fingerprint={specific_fp.get('seed')}")
- co.set_argument(f"--fingerprint-platform={specific_fp.get('platform')}")
- co.set_argument(f"--fingerprint-brand={specific_fp.get('brand')}")
-
- try:
- self.page = ChromiumPage(co)
-
- # --- 预检指纹信息 ---
- if self.config.debug:
- self.page.get('https://example.com')
- js_script = """
- function getFingerprint() {
- let webglVendor = 'Unknown';
- let webglRenderer = 'Unknown';
- try {
- let canvas = document.createElement('canvas');
- let gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
- if (gl) {
- let debugInfo = gl.getExtension('WEBGL_debug_renderer_info');
- if (debugInfo) {
- webglVendor = gl.getParameter(debugInfo.UNMASKED_VENDOR_WEBGL);
- webglRenderer = gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL);
- }
- }
- } catch(e) {}
- return {
- "User-Agent": navigator.userAgent,
- "Platform": navigator.userAgentData ? navigator.userAgentData.platform : navigator.platform,
- "Brands": navigator.userAgentData ? navigator.userAgentData.brands.map(b => b.brand).join(', ') : 'Not Supported',
- "CPU Cores": navigator.hardwareConcurrency,
- "Language": navigator.language,
- "Timezone": Intl.DateTimeFormat().resolvedOptions().timeZone,
- "WebGL Vendor": webglVendor,
- "WebGL Renderer": webglRenderer
- };
- }
- return getFingerprint();
- """
- fp_data = self.page.run_js(js_script)
- self._log("================ 预检浏览器指纹数据 ================")
- self._log(json.dumps(fp_data, indent=4, ensure_ascii=False))
- self._log("====================================================")
- # --- 初始化访问与过盾 ---
- tls_url = self.free_config.get('tls_url', '')
- self._log(f"Navigating: {tls_url}")
- self.page.get(tls_url)
- time.sleep(5)
-
- cf_bypasser = CloudflareBypasser(self.page, log=True)
- if not cf_bypasser.bypass(max_retry=15):
- raise BizLogicError("Cloudflare bypass timeout")
- time.sleep(3)
- cf_bypasser.handle_waiting_room()
-
- # --- 初始化人类行为模拟工具 ---
- self._log("Init humanize tools...")
- self.mouse = HumanMouse(self.page, debug=True)
- self.keyboard = HumanKeyboard(self.page)
- viewport_width = self.page.rect.viewport_size[0]
- viewport_height = self.page.rect.viewport_size[1]
- init_x = random.randint(10, viewport_width - 10)
- init_y = random.randint(10, viewport_height - 10)
- self.mouse.move(init_x, init_y)
- max_steps = 20
- session_created = False
- has_submitted_login = False
-
- for step in range(max_steps):
- self.page.wait.load_start()
- current_url = self.page.url
- self._log(f"--- [Router Step {step+1}] Current URL: {current_url} ---")
-
- # 状态 1:到达终极目标页面 (成功退出条件)
- if "appointment-booking" in current_url or self.page.ele('tag:button@text():Book your appointment', timeout=1):
- btn_selector = 'tag:button@text():Book your appointment'
- if self.page.wait.ele_displayed(btn_selector, timeout=10):
- self.session_create_time = time.time()
- self._log("✅ Login & Navigation Success! Reached appointment-booking.")
- session_created = True
- break
-
- # 状态 2:遇到没有申请人的拦截页 (致命错误退出条件)
- no_applicant_indicators = [
- "Add a new applicant" in self.page.html,
- "You have not yet added an applicant" in self.page.html,
- "applicants-information" in current_url
- ]
- if any(no_applicant_indicators):
- raise BizLogicError(message="No applicant added. Cannot proceed to booking.")
-
- # 状态 3:首页/登录入口页 -> 需要点击进入登录
- if self.page.ele("tag:a@@href:login", timeout=1) and not self.page.ele('tag:label@@text():Email', timeout=1):
- self._log("State: Login Portal. Clicking login link...")
- login_link = self.page.ele("tag:a@@href:login")
- self.mouse.human_click_ele(login_link)
- time.sleep(3)
- continue
-
- # 状态 4:真正的登录表单页
- if self.page.ele('tag:label@@text():Email', timeout=1) and not has_submitted_login:
- self._log("State: Login Form. Processing credentials and Captcha...")
-
- recaptchav2_token = ""
- if self.page.ele('.g-recaptcha') or self.page.ele('xpath://iframe[contains(@src, "recaptcha")]'):
- rec_iframe = self.page.ele('xpath://iframe[contains(@src, "recaptcha")]')
- rec_iframe_src = rec_iframe.attr('src')
- rec_parsed = urlparse(rec_iframe_src)
- rec_params = parse_qs(rec_parsed.query)
- rec_sitekey = rec_params.get("k", [None])[0]
- rec_size = rec_params.get("size", [None])[0]
-
- if 'normal' == rec_size:
- self._log(f"Solving ReCaptcha sitekey={rec_sitekey}...")
- rc_params = {
- "type": "ReCaptchaV2TaskProxyLess",
- "page": current_url,
- "siteKey": rec_sitekey,
- "apiToken": self.free_config.get("capsolver_key", "")
- }
- recaptchav2_token = self._solve_recaptcha(rc_params)
- username = self.config.account.username
- password = self.config.account.password
-
- input_ele = self.page.ele('tag:label@@text():Email').next()
- self.mouse.human_click_ele(input_ele)
- time.sleep(random.uniform(0.2, 0.6))
- self.keyboard.type_text(username, humanize=True)
- time.sleep(random.uniform(0.5, 1.2))
-
- input_ele = self.page.ele('tag:label@@text():Password').next()
- self.mouse.human_click_ele(input_ele)
- time.sleep(random.uniform(0.2, 0.6))
- self.keyboard.type_text(password, humanize=True)
-
- # 注入 Token
- if recaptchav2_token:
- inject_js = f"var g = document.getElementById('g-recaptcha-response'); if(g) {{ g.value = '{recaptchav2_token}'; }}"
- self.page.run_js(inject_js)
- time.sleep(random.uniform(0.5, 1.0))
-
- self._log("Submitting Login...")
- login_btn = self.page.ele('tag:button@@text():Login')
- self.mouse.human_click_ele(login_btn)
- has_submitted_login = True
- time.sleep(3)
- continue
-
- # 状态 5:Travel Groups 页面
- if "travel-groups" in current_url:
- self._log("State: Travel Groups. Selecting targeted group...")
- groups = self._parse_travel_groups(self.page.html)
- location = self.free_config.get('location')
- self.travel_group = next((g for g in groups if location in g['location']), None)
-
- if not self.travel_group or not self.travel_group.get("submitted"):
- self._save_screenshot("group_not_found")
- raise NotFoundError(f"Group not found for {location}")
-
- formgroup_id = self.travel_group.get('group_number')
- btn_selector = f'tag:button@@name=formGroupId@@value={formgroup_id}'
-
- 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 select_btn:
- time.sleep(random.uniform(0.5, 1.2))
- self.mouse.human_click_ele(select_btn)
- time.sleep(3)
- continue
- else:
- self._log("[WARN] Select button found but not visible.")
- else:
- self._log(f"[WARN] Wait timeout for group button {formgroup_id}")
-
- # 状态 6:中间过渡页,需点击 "Book Appointment" 继续往下走
- if self.page.ele('#book-appointment-btn', timeout=1):
- self._log("State: Intermediate Dashboard. Clicking Book Appointment button...")
- self.mouse.human_click_ele(self.page.ele('#book-appointment-btn'))
- time.sleep(3)
- continue
-
- # 状态 7:登录失败校验 或 未知加载状态
- if "login-actions" in current_url and has_submitted_login:
- self._log("Waiting on login-actions... (Might be authenticating or invalid credentials)")
- time.sleep(2)
- if self.page.ele('text:Invalid username or password', timeout=1): # 假设网页上有错误提示
- raise BizLogicError(message="Login Failed! Invalid credentials or Captcha rejected.")
- continue
-
- # 兜底:未匹配到明确状态,等待页面渲染或重定向
- self._log("State: Transitioning or Unknown. Waiting 2 seconds...")
- time.sleep(2)
-
- # 如果循环耗尽还没到达目标
- if not session_created:
- raise BizLogicError(f"Failed to reach appointment-booking after {max_steps} navigation steps. Stuck at: {self.page.url}")
- except Exception as e:
- self._log(f"Session Create Error: {e}")
- if self.config.debug:
- self._save_screenshot("create_session_except")
- self.cleanup()
- raise e
- def query(self, apt_type: AppointmentType) -> VSQueryResult:
- res = VSQueryResult()
- res.success = False
- interest_month = self.free_config.get("interest_month", time.strftime("%m-%Y"))
-
- target_date_obj = datetime.strptime(interest_month, "%m-%Y")
- target_month_text = target_date_obj.strftime("%B %Y")
- target_year = target_date_obj.year
- target_month_num = target_date_obj.month
-
- slots = []
- current_selected_ele = self.page.ele('@data-testid=btn-current-month-available')
- current_month_text = current_selected_ele.text.strip() if current_selected_ele else ""
- is_on_target_month = (current_month_text.lower() == target_month_text.lower())
- if not is_on_target_month:
- self._log(f"Current is '{current_month_text}', navigating to '{target_month_text}'...")
- reached_target = False
- for step in range(12):
- current_ele = self.page.ele('@data-testid=btn-current-month-available', timeout=2)
- if current_ele and current_ele.text.strip().lower() == target_month_text.lower():
- self._log(f"✅ Successfully navigated to target month: '{target_month_text}'!")
- reached_target = True
- break
-
- next_btn = self.page.ele('@data-testid=btn-next-month-available', timeout=2)
-
- if next_btn and next_btn.tag.lower() == 'button':
- self._log(f"Clicking next month: {next_btn.text.strip()} ...")
- next_btn.click(by_js=True)
- time.sleep(2.5)
- else:
- self._log("⚠️ Reached the end of the calendar or 'Next Month' is disabled.")
- break
- if not reached_target:
- self._log(f"❌ Could not navigate to target month: {target_month_text}. Stop parsing.")
- res.success = False
- res.availability_status = AvailabilityStatus.NoneAvailable
- return res
- self._log("Extracting slots from DOM using robust data-testid features...")
- slots = self._scan_dom_for_slots(target_year, target_month_num)
- else:
- self._log(f"Already on '{target_month_text}'. Executing silent JS fetch...")
- resp = self._perform_request("GET", self.page.url, retry_count=1)
- 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! -> {slots}")
- else:
- self._log("No slots available.")
- res.success = False
- res.availability_status = AvailabilityStatus.NoneAvailable
- return res
- def book(self, slot_info: VSQueryResult, user_inputs: Dict = None) -> VSBookResult:
- res = VSBookResult()
- 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 = self._filter_dates(available_dates_str, exp_start, exp_end)
- 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}")
-
- # ================== 新增:随机选择预订模式 ==================
- book_mode = random.choice([1, 2])
- self._log(f"Using booking mode: {book_mode}")
-
- if book_mode == 1:
- rand_x = random.randint(300, 800)
- rand_y = random.randint(400, 700)
- self._log(f"Mode 1: Moving mouse to ({rand_x}, {rand_y}) and clicking, select slot")
- self.mouse.click(rand_x, rand_y, humanize=True)
-
- 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;
- input.dispatchEvent(new Event('input', {{ bubbles: true }}));
- input.dispatchEvent(new Event('change', {{ bubbles: true }}));
- }}
- 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"Mode 1: Form update triggered: {update_res}")
-
- if update_res != 'form_updated':
- raise BizLogicError(message=f"Failed to update form in Mode 1: {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("Mode 1: Moving mouse to submit button and clicking")
- self.mouse.human_click_ele(submit_btn)
- inject_res = 'clicked'
- else:
- js_inject_and_click = 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;
- input.dispatchEvent(new Event('input', {{ bubbles: true }}));
- input.dispatchEvent(new Event('change', {{ bubbles: true }}));
- }}
- 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.click();
- return 'clicked';
- }} catch (e) {{
- return e.toString();
- }}
- """
- inject_res = self.page.run_js(js_inject_and_click)
- self._log(f"Mode 2: Form submission triggered: {inject_res}")
-
- if inject_res != 'clicked':
- raise BizLogicError(message="Failed to inject form or click the submit button")
- 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
- self._save_screenshot("book_slot_success")
- 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 _scan_dom_for_slots(self, target_year: int, target_month_num: int) -> list[dict]:
- """
- DOM-based slot scanning — 结合区块结构与类名/属性推断标签
- """
- slots = []
-
- day_blocks_xpath = '//div[p and div//button[contains(@data-testid, "slot")]]'
- day_blocks = self.page.eles(f'xpath:{day_blocks_xpath}')
-
- for block in day_blocks:
- p_ele = block.ele('tag:p')
- if not p_ele: continue
-
- day_match = re.search(r'\d+', p_ele.text)
- if not day_match: continue
- day_str = day_match.group()
- full_date = f"{target_year}-{target_month_num:02d}-{int(day_str):02d}"
-
- btn_selectors = [
- 'xpath:.//button[starts-with(@data-testid, "btn-available-slot")]',
- 'xpath:.//button[contains(@class, "available")]',
- 'xpath:.//div[contains(@class, "time-slot") and not(contains(@class, "unavailable"))]'
- ]
-
- available_elements = []
- for sel in btn_selectors:
- elements = block.eles(sel)
- if elements:
- available_elements.extend(elements)
- break
- seen_times = set()
- for el in available_elements:
- classes = (el.attr("class") or "").lower()
- test_id = (el.attr("data-testid") or "").lower()
- combined_attrs = f"{classes} {test_id}"
- if "disabled" in combined_attrs or "unavailable" in combined_attrs:
- continue
- time_match = re.search(r'\d{2}:\d{2}', el.html)
- if not time_match: continue
- time_str = time_match.group()
-
- if time_str in seen_times:
- continue
- seen_times.add(time_str)
- label = ''
- if 'prime' in combined_attrs and 'weekend' in combined_attrs:
- label = 'ptaw'
- elif 'prime' in combined_attrs or 'premium' in combined_attrs:
- label = 'pta'
- elif any(k in combined_attrs for k in ['regular', 'standard', 'default']):
- label = ''
- else:
- label = ''
-
- slots.append({
- 'date': full_date,
- 'time': time_str,
- 'label': label,
- 'source': 'dom'
- })
-
- return slots
- def _get_proxy_url(self):
- # 构造代理
- proxy_url = ""
- if self.config.proxy.ip:
- s = self.config.proxy
- if s.username:
- proxy_url = f"{s.proto}://{s.username}:{s.password}@{s.ip}:{s.port}"
- else:
- proxy_url = f"{s.proto}://{s.ip}:{s.port}"
- return proxy_url
- def _perform_request(self, method, url, headers=None, data=None, json_data=None, params=None, retry_count=0):
- """
- 在浏览器上下文中注入 JS 执行 Fetch
- """
- if not self.page:
- raise BizLogicError("Browser not initialized")
- if params:
- from urllib.parse import urlencode
- if '?' in url:
- url += '&' + urlencode(params)
- else:
- url += '?' + urlencode(params)
- fetch_options = {
- "method": method.upper(),
- "headers": headers or {},
- "credentials": "include"
- }
- # Body 处理
- if json_data:
- fetch_options['body'] = json.dumps(json_data)
- fetch_options['headers']['Content-Type'] = 'application/json'
- elif data:
- if isinstance(data, dict):
- from urllib.parse import urlencode
- fetch_options['body'] = urlencode(data)
- fetch_options['headers']['Content-Type'] = 'application/x-www-form-urlencoded'
- else:
- fetch_options['body'] = data
- js_script = f"""
- const url = "{url}";
- const options = {json.dumps(fetch_options)};
-
- return fetch(url, options)
- .then(async response => {{
- const text = await response.text();
- const headers = {{}};
- response.headers.forEach((value, key) => headers[key] = value);
-
- return {{
- status: response.status,
- body: text,
- headers: headers,
- url: response.url
- }};
- }})
- .catch(error => {{
- return {{
- status: 0,
- body: error.toString(),
- headers: {{}},
- url: url
- }};
- }});
- """
-
- res_dict = self.page.run_js(js_script, timeout=30)
- resp = BrowserResponse(res_dict)
-
- if resp.status_code == 200:
- return resp
- elif resp.status_code == 401:
- self.is_healthy = False
- raise SessionExpiredOrInvalidError()
- elif resp.status_code == 403:
- if retry_count < 2:
- self._log(f"HTTP 403 Detected. Cloudflare session expired? 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.")
- raise PermissionDeniedError(f"HTTP 403: {resp.text[:100]}")
- elif resp.status_code == 429:
- self.is_healthy = False
- raise RateLimiteddError()
- 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:
- # 1. 刷新当前页面 (通常 Dashboard 页)
- # 这会强制浏览器重新进行 HTTP 请求,从而触发 Cloudflare 拦截页
- self._log("Refreshing page to trigger Cloudflare...")
- self.page.refresh()
-
- # 2. 调用 CloudflareBypasser
- cf = CloudflareBypasser(self.page, log=self.config.debug)
-
- # 3. 尝试过盾 (尝试次数稍多一点,因为此时可能网络不稳定)
- success = cf.bypass(max_retry=10)
-
- if success:
- # 再次确认页面是否正常加载 (非 403 页面)
- title = self.page.title.lower()
- if "access denied" in title:
- return False
-
- # 等待 DOM 稍微稳定
- time.sleep(2)
- return True
-
- return False
- except Exception as e:
- self._log(f"Error during firewall refresh: {e}")
- return False
- def _solve_recaptcha(self, params) -> str:
- """调用 VSCloudApi 解决 ReCaptcha"""
- key = params.get("apiToken")
- if not key: raise NotFoundError("Api-token required")
-
- submit_url = "https://api.capsolver.com/createTask"
- task = {
- "type": params.get("type"),
- "websiteURL": params.get("page"),
- "websiteKey": params.get("siteKey"),
- }
- if params.get("action"):
- task["pageAction"] = params.get("action")
-
- # if params.get("proxy"):
- # p = urlparse(params.get("proxy"))
- # task["proxyType"] = p.proto
- # task["proxyAddress"] = p.hostname
- # task["proxyPort"] = p.port
- # if p.username:
- # task["proxyLogin"] = p.username
- # task["proxyPassword"] = p.password
-
- # 注意:使用 DrissionPage 后,通常是 ProxyLess 模式
- # 除非你想让 Capsolver 也用同样的代理(通常不需要,除非风控极严)
-
- payload = {"clientKey": key, "task": task}
- import requests as req # 局部引用,避免混淆
- r = req.post(submit_url, json=payload, timeout=20)
- if r.status_code != 200:
- raise BizLogicError(message="Failed to submit capsolver task")
-
- task_id = r.json().get("taskId")
- for _ in range(20):
- r = req.post("https://api.capsolver.com/getTaskResult", json={"clientKey": key, "taskId": task_id}, timeout=20)
- if r.status_code == 200:
- d = r.json()
- if d.get("status") == "ready":
- return d["solution"]["gRecaptchaResponse"]
- time.sleep(3)
- raise BizLogicError(message="Capsolver task timeout")
- def _parse_travel_groups(self, html_content) -> List[Dict]:
- groups = []
- js_pattern = r'\\"travelGroups\\":\s*(\[.*?\]),\\"availableCountriesToCreateGroups'
- js_match = re.search(js_pattern, html_content, re.DOTALL)
- if js_match:
- json_str = js_match.group(1).replace(r'\"', '"')
- data = json.loads(json_str)
- for g in data:
- groups.append({
- 'group_name': g.get('groupName'),
- 'group_number': g.get('formGroupId'),
- 'location': g.get('vacName'),
- 'submitted': g.get('submitted')
- })
- else:
- self._log('Parsed travel group page, but not found travelGroups')
- 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 = ""
- # 简化逻辑:TLS label 列表
- if 'pta' in labels: lbl = 'pta'
- elif 'ptaw' in labels: lbl = 'ptaw'
- elif '' in labels or not labels: lbl = ''
-
- 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()
-
- html_lower = html.lower()
- if keyword.lower() not in html_lower:
- session_expire_or_invalid_indicators = [
- 'redirected automatically' in html_lower,
- 'login' in html_lower and 'password' in html_lower,
- 'session expired' in html_lower
- ]
- if any(session_expire_or_invalid_indicators):
- self.is_healthy = False
- raise SessionExpiredOrInvalidError()
-
- def _filter_dates(self, dates: List[str], start_str: str, end_str: str) -> List[str]:
- if not start_str or not end_str:
- return dates
- valid_dates = []
- s_date = datetime.strptime(start_str[:10], "%Y-%m-%d")
- e_date = datetime.strptime(end_str[:10], "%Y-%m-%d")
- for date_str in dates:
- curr_date = datetime.strptime(date_str, "%Y-%m-%d")
- if s_date <= curr_date <= e_date:
- valid_dates.append(date_str)
- random.shuffle(valid_dates)
- return valid_dates
-
- # --- 资源清理核心方法 ---
- def cleanup(self):
- """
- 销毁浏览器并彻底删除临时文件
- """
- # 1. 关闭浏览器
- if self.page:
- try:
- self.page.quit() # 这会关闭 Chrome 进程
- except Exception:
- pass # 忽略已关闭的错误
- self.page = None
-
- # 2. 删除文件
- # 注意:Chrome 关闭后可能需要几百毫秒释放文件锁,稍微等待
- if os.path.exists(self.root_workspace):
- for _ in range(3):
- try:
- time.sleep(0.2)
- shutil.rmtree(self.root_workspace, ignore_errors=True)
- break
- except Exception as e:
- # 如果删除失败(通常是Windows文件占用),重试
- self._log(f"Cleanup retry: {e}")
- time.sleep(0.5)
-
- # 如果依然存在,打印警告(虽然 ignore_errors=True 会掩盖报错,但可以 check exists)
- if os.path.exists(self.root_workspace):
- self._log(f"[WARN] Failed to fully remove workspace: {self.root_workspace}")
- # 3. [新增] 关闭代理隧道
- if self.tunnel:
- try: self.tunnel.stop()
- except: pass
- self.tunnel = None
-
- def __del__(self):
- """
- 析构函数:当对象被垃圾回收时自动调用
- """
- self.cleanup()
|