|
@@ -1,47 +1,161 @@
|
|
|
import time
|
|
import time
|
|
|
import json
|
|
import json
|
|
|
import random
|
|
import random
|
|
|
-import socket
|
|
|
|
|
-import uuid
|
|
|
|
|
-import shutil
|
|
|
|
|
import re
|
|
import re
|
|
|
import os
|
|
import os
|
|
|
|
|
+import uuid
|
|
|
import base64
|
|
import base64
|
|
|
-from concurrent.futures import ThreadPoolExecutor
|
|
|
|
|
from datetime import datetime
|
|
from datetime import datetime
|
|
|
from typing import List, Dict, Optional, Any, Callable
|
|
from typing import List, Dict, Optional, Any, Callable
|
|
|
-from urllib.parse import urlencode, urlparse
|
|
|
|
|
-
|
|
|
|
|
-from DrissionPage import ChromiumPage, ChromiumOptions
|
|
|
|
|
|
|
+from DrissionPage import ChromiumPage
|
|
|
|
|
|
|
|
-import configure
|
|
|
|
|
from vs_plg import IVSPlg
|
|
from vs_plg import IVSPlg
|
|
|
-from vs_types import VSPlgConfig, AppointmentType, VSQueryResult, VSBookResult, AvailabilityStatus, TimeSlot, DateAvailability, NotFoundError, PermissionDeniedError, RateLimiteddError, SessionExpiredOrInvalidError, BizLogicError, PluginStatus, utc_now_str
|
|
|
|
|
-from toolkit.mihomo_tunnel import MihomoTunnel
|
|
|
|
|
|
|
+from vs_types import (
|
|
|
|
|
+ VSPlgConfig,
|
|
|
|
|
+ AppointmentType,
|
|
|
|
|
+ VSQueryResult,
|
|
|
|
|
+ VSBookResult,
|
|
|
|
|
+ AvailabilityStatus,
|
|
|
|
|
+ TimeSlot,
|
|
|
|
|
+ DateAvailability,
|
|
|
|
|
+ NotFoundError,
|
|
|
|
|
+ PermissionDeniedError,
|
|
|
|
|
+ RateLimiteddError,
|
|
|
|
|
+ SessionExpiredOrInvalidError,
|
|
|
|
|
+ BizLogicError,
|
|
|
|
|
+ PluginStatus,
|
|
|
|
|
+ utc_now_str
|
|
|
|
|
+)
|
|
|
from toolkit.vs_cloud_api import VSCloudApi
|
|
from toolkit.vs_cloud_api import VSCloudApi
|
|
|
|
|
+from utils.cloudflare_bypass_for_scraping import CloudflareBypasser
|
|
|
from utils.mouse import HumanMouse
|
|
from utils.mouse import HumanMouse
|
|
|
from utils.keyboard import HumanKeyboard
|
|
from utils.keyboard import HumanKeyboard
|
|
|
-from utils.fingerprint_utils import FingerprintGenerator
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-class BrowserResponse:
|
|
|
|
|
- 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
|
|
|
|
|
-
|
|
|
|
|
-# ==========================================
|
|
|
|
|
-# 2. ItaPlugin 核心逻辑
|
|
|
|
|
-# ==========================================
|
|
|
|
|
|
|
+from utils.flow_engine import BaseFlow, on, guard, done, fatal
|
|
|
|
|
+from utils.plugin_utils import (
|
|
|
|
|
+ BrowserResponse,
|
|
|
|
|
+ init_chromium_options,
|
|
|
|
|
+ perform_browser_fetch,
|
|
|
|
|
+ filter_dates_in_range,
|
|
|
|
|
+ cleanup_browser_workspace
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class ItaLoginFlow(BaseFlow):
|
|
|
|
|
+ """
|
|
|
|
|
+ 基于 BaseFlow 注解状态机驱动的意大利 Prenot@Mi 会话创建与自动导航流
|
|
|
|
|
+ """
|
|
|
|
|
+ def __init__(self, page, plugin: 'ItaPlugin', 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
|
|
|
|
|
+
|
|
|
|
|
+ # --- 异常守卫与自愈 (Priority 5~7) ---
|
|
|
|
|
+ @guard(text="502 Bad Gateway", priority=5)
|
|
|
|
|
+ @guard(text="503 Service Temporarily Unavailable", priority=5)
|
|
|
|
|
+ @guard(text="error 1020", priority=5)
|
|
|
|
|
+ def on_server_network_error(self):
|
|
|
|
|
+ self.log("[WARN] 检测到服务器网络错误 (502/503/1020),等待并刷新自愈...", level="warning")
|
|
|
|
|
+ self.plugin._random_sleep(10, 15)
|
|
|
|
|
+ self.page.refresh()
|
|
|
|
|
+ try:
|
|
|
|
|
+ self.page.wait.doc_loaded()
|
|
|
|
|
+ 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)
|
|
|
|
|
+ @fatal("Blocked by Cloudflare WAF. Need to change IP or browser fingerprint.")
|
|
|
|
|
+ def on_cloudflare_blocked(self):
|
|
|
|
|
+ pass
|
|
|
|
|
+
|
|
|
|
|
+ @guard(title="just a moment", priority=7)
|
|
|
|
|
+ def on_cloudflare_turnstile_challenge(self):
|
|
|
|
|
+ self.log("🛡️ 检测到 Cloudflare 5秒盾挑战,正在尝试穿透...")
|
|
|
|
|
+ if self.cf_bypasser.bypass(max_retry=3):
|
|
|
|
|
+ self.plugin._random_sleep(3, 5)
|
|
|
|
|
+ self.cf_bypasser.handle_waiting_room()
|
|
|
|
|
+
|
|
|
|
|
+ @guard(title="waitting room", priority=7)
|
|
|
|
|
+ def on_cloudflare_waiting_room(self):
|
|
|
|
|
+ self.log("🛡️ 检测到 Cloudflare 等候室,正在等待自动排队刷新...")
|
|
|
|
|
+ self.cf_bypasser.handle_waiting_room()
|
|
|
|
|
+ time.sleep(2)
|
|
|
|
|
+
|
|
|
|
|
+ # --- 状态流转与表单处理 (Priority 35~45) ---
|
|
|
|
|
+ @on(ele="#pingid-button", priority=45)
|
|
|
|
|
+ def handle_initial_landing_page(self):
|
|
|
|
|
+ self.log("[State] Initial Landing Page detected. Clicking login button...")
|
|
|
|
|
+ login_btn = self.page.ele('#pingid-button')
|
|
|
|
|
+ if login_btn:
|
|
|
|
|
+ self.plugin.mouse.human_click_ele(login_btn)
|
|
|
|
|
+ self.log("Redirecting to PingID...")
|
|
|
|
|
+ self.plugin._random_sleep(3, 5)
|
|
|
|
|
+
|
|
|
|
|
+ @on(ele="@name=callback_1", priority=40)
|
|
|
|
|
+ def handle_pingid_login_form(self):
|
|
|
|
|
+ if self.has_submitted_login:
|
|
|
|
|
+ return
|
|
|
|
|
+ self.log("[State] PingID Login Form detected. Submitting credentials...")
|
|
|
|
|
+ username = self.plugin.config.account.username
|
|
|
|
|
+ password = self.plugin.config.account.password
|
|
|
|
|
+
|
|
|
|
|
+ self.log("Entering username...")
|
|
|
|
|
+ user_input = self.page.ele('@name=callback_1')
|
|
|
|
|
+ self.plugin.mouse.human_click_ele(user_input)
|
|
|
|
|
+ user_input.input(username, clear=True)
|
|
|
|
|
+ time.sleep(1)
|
|
|
|
|
+
|
|
|
|
|
+ self.log("Entering password...")
|
|
|
|
|
+ pwd_input = self.page.ele('@type=password')
|
|
|
|
|
+ self.plugin.mouse.human_click_ele(pwd_input)
|
|
|
|
|
+ pwd_input.input(password, clear=True)
|
|
|
|
|
+ time.sleep(1)
|
|
|
|
|
+
|
|
|
|
|
+ self.log("Clicking submit login button...")
|
|
|
|
|
+ submit_btn = self.page.ele('tag:button@type=submit')
|
|
|
|
|
+ if submit_btn:
|
|
|
|
|
+ self.plugin.mouse.human_click_ele(submit_btn)
|
|
|
|
|
+
|
|
|
|
|
+ self.has_submitted_login = True
|
|
|
|
|
+ self.log("Login form submitted. Waiting for dashboard to load...")
|
|
|
|
|
+ self.plugin._random_sleep(5, 7)
|
|
|
|
|
+
|
|
|
|
|
+ @on(ele=".app-menu", priority=35)
|
|
|
|
|
+ def handle_dashboard_menu(self):
|
|
|
|
|
+ self.log("[State] Dashboard Menu detected.")
|
|
|
|
|
+ switch_en_btn = self.page.ele('tag:a@@href=/Language/ChangeLanguage?lang=2', timeout=0.5)
|
|
|
|
|
+ if switch_en_btn:
|
|
|
|
|
+ self.log("Found 'English' language switch button. Clicking to change language...")
|
|
|
|
|
+ self.plugin.mouse.human_click_ele(switch_en_btn)
|
|
|
|
|
+ self.plugin._random_sleep(4, 6)
|
|
|
|
|
+ return
|
|
|
|
|
+
|
|
|
|
|
+ book_btn = self.page.ele('@href=/Services', timeout=0.5)
|
|
|
|
|
+ if book_btn:
|
|
|
|
|
+ self.log("Found 'Book' menu element. Clicking to proceed to reservation...")
|
|
|
|
|
+ self.plugin.mouse.human_click_ele(book_btn)
|
|
|
|
|
+ self.plugin._random_sleep(4, 6)
|
|
|
|
|
+ return
|
|
|
|
|
+
|
|
|
|
|
+ self.log("[WARN] On Dashboard, but neither 'English' nor 'Book' button was found. Waiting...")
|
|
|
|
|
+ time.sleep(2)
|
|
|
|
|
+
|
|
|
|
|
+ # --- 成功终态到达 (Priority 10) ---
|
|
|
|
|
+ @on(url="/Services", ele=".app-menu", priority=10)
|
|
|
|
|
+ @done
|
|
|
|
|
+ def on_reached_services_page(self):
|
|
|
|
|
+ self.plugin.session_create_time = time.time()
|
|
|
|
|
+ self.log("🎉 Successfully reached the Booking Services page! Session created successfully!")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
class ItaPlugin(IVSPlg):
|
|
class ItaPlugin(IVSPlg):
|
|
|
name = "ita_plugin"
|
|
name = "ita_plugin"
|
|
|
version = "1.0.0"
|
|
version = "1.0.0"
|
|
@@ -58,6 +172,7 @@ class ItaPlugin(IVSPlg):
|
|
|
# Prenotami 特有配置
|
|
# Prenotami 特有配置
|
|
|
self._service_id = 0
|
|
self._service_id = 0
|
|
|
self.ita_url = 'https://prenotami.esteri.it'
|
|
self.ita_url = 'https://prenotami.esteri.it'
|
|
|
|
|
+ self._host = self.ita_url
|
|
|
|
|
|
|
|
self.instance_id = uuid.uuid4().hex[:8]
|
|
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.root_workspace = os.path.abspath(os.path.join("data/temp_browser_data", f"{self.group_id}.{self.instance_id}"))
|
|
@@ -73,9 +188,13 @@ class ItaPlugin(IVSPlg):
|
|
|
|
|
|
|
|
def _log(self, message):
|
|
def _log(self, message):
|
|
|
if self.logger:
|
|
if self.logger:
|
|
|
- self.logger(f'[ItaPlugin] [{self.group_id}] {message}')
|
|
|
|
|
|
|
+ self.logger(f'[ItaPlugin] [{self.group_id}] [{self.instance_id}] {message}')
|
|
|
else:
|
|
else:
|
|
|
- print(f'[ItaPlugin] [{self.group_id}] {message}')
|
|
|
|
|
|
|
+ print(f'[ItaPlugin] [{self.group_id}] [{self.instance_id}] {message}')
|
|
|
|
|
+
|
|
|
|
|
+ def _random_sleep(self, min_sec=3, max_sec=5):
|
|
|
|
|
+ sleep_time = random.uniform(min_sec, max_sec)
|
|
|
|
|
+ time.sleep(sleep_time)
|
|
|
|
|
|
|
|
def set_config(self, config: VSPlgConfig):
|
|
def set_config(self, config: VSPlgConfig):
|
|
|
self.config = config
|
|
self.config = config
|
|
@@ -92,7 +211,7 @@ class ItaPlugin(IVSPlg):
|
|
|
try:
|
|
try:
|
|
|
if not self.page.run_js("return 1;"):
|
|
if not self.page.run_js("return 1;"):
|
|
|
return False
|
|
return False
|
|
|
- except:
|
|
|
|
|
|
|
+ except Exception:
|
|
|
return False
|
|
return False
|
|
|
if self.config.session_max_life > 0:
|
|
if self.config.session_max_life > 0:
|
|
|
if time.time() - self.session_create_time > self.config.session_max_life:
|
|
if time.time() - self.session_create_time > self.config.session_max_life:
|
|
@@ -105,388 +224,131 @@ class ItaPlugin(IVSPlg):
|
|
|
全浏览器会话创建:过盾 -> JS注入登录 -> 状态机自动路由导航 -> 到达目标页
|
|
全浏览器会话创建:过盾 -> JS注入登录 -> 状态机自动路由导航 -> 到达目标页
|
|
|
"""
|
|
"""
|
|
|
self._log(f"Initializing Session (ID: {self.instance_id})...")
|
|
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}")
|
|
|
|
|
- self._log(f"Account id={self.config.account.id}, proxy id={self.config.proxy.id}")
|
|
|
|
|
-
|
|
|
|
|
- co.set_local_port(debug_port)
|
|
|
|
|
- co.set_user_data_path(self.user_data_path)
|
|
|
|
|
-
|
|
|
|
|
- chrome_path = configure.CHROME_PATH
|
|
|
|
|
- if not chrome_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}...")
|
|
|
|
|
- exit_node = {
|
|
|
|
|
- "name": "ExitNode",
|
|
|
|
|
- "type": p.proto,
|
|
|
|
|
- "server": p.ip,
|
|
|
|
|
- "port": p.port,
|
|
|
|
|
- "username": p.username,
|
|
|
|
|
- "password": p.password
|
|
|
|
|
- }
|
|
|
|
|
- relay_node = None
|
|
|
|
|
- if configure.MIHOMO_RELAY_NODES:
|
|
|
|
|
- relay_node = random.choice(configure.MIHOMO_RELAY_NODES)
|
|
|
|
|
- mihomo_path = configure.MIHOMO_BIN_PATH
|
|
|
|
|
- if not mihomo_path:
|
|
|
|
|
- mihomo_path = os.getenv("MIHOMO_BIN")
|
|
|
|
|
- if not mihomo_path:
|
|
|
|
|
- raise BizLogicError(message='Mihomo path is null, You need set mihomo bin path in configure or os env')
|
|
|
|
|
- self.tunnel = MihomoTunnel(mihomo_path, exit_node=exit_node, relay_node=relay_node)
|
|
|
|
|
- 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!")
|
|
|
|
|
-
|
|
|
|
|
- specific_fp = FingerprintGenerator().generate(self.config.account.username)
|
|
|
|
|
- fp_seed = specific_fp.get("seed")
|
|
|
|
|
- fp_platform = specific_fp.get("platform")
|
|
|
|
|
- fp_brand = specific_fp.get("brand")
|
|
|
|
|
- self._log(f'browser fingerprint seed={fp_seed}')
|
|
|
|
|
-
|
|
|
|
|
- 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(f"--fingerprint={fp_seed}")
|
|
|
|
|
- co.set_argument(f"--fingerprint-platform={fp_platform}")
|
|
|
|
|
- co.set_argument(f"--fingerprint-brand={fp_brand}")
|
|
|
|
|
|
|
+ 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)
|
|
|
|
|
- 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("====================================================")
|
|
|
|
|
-
|
|
|
|
|
self.page = ChromiumPage(co)
|
|
self.page = ChromiumPage(co)
|
|
|
ita_url = self.ita_url
|
|
ita_url = self.ita_url
|
|
|
self._log(f"Navigating to {ita_url}")
|
|
self._log(f"Navigating to {ita_url}")
|
|
|
self.page.get(ita_url)
|
|
self.page.get(ita_url)
|
|
|
|
|
|
|
|
self._log("Init humanize tools...")
|
|
self._log("Init humanize tools...")
|
|
|
- self.mouse = HumanMouse(self.page, debug=True)
|
|
|
|
|
|
|
+ self.mouse = HumanMouse(self.page, debug=False)
|
|
|
self.keyboard = HumanKeyboard(self.page)
|
|
self.keyboard = HumanKeyboard(self.page)
|
|
|
- self._log("Random mouse start position...")
|
|
|
|
|
|
|
+
|
|
|
viewport_width = self.page.rect.viewport_size[0]
|
|
viewport_width = self.page.rect.viewport_size[0]
|
|
|
viewport_height = self.page.rect.viewport_size[1]
|
|
viewport_height = self.page.rect.viewport_size[1]
|
|
|
init_x = random.randint(10, viewport_width - 10)
|
|
init_x = random.randint(10, viewport_width - 10)
|
|
|
init_y = random.randint(10, viewport_height - 10)
|
|
init_y = random.randint(10, viewport_height - 10)
|
|
|
self.mouse.move(init_x, init_y)
|
|
self.mouse.move(init_x, init_y)
|
|
|
-
|
|
|
|
|
- switch_en_btn = self.page.ele('tag:a@@href=/Language/ChangeLanguage?lang=2')
|
|
|
|
|
- self.mouse.human_click_ele(switch_en_btn)
|
|
|
|
|
- self.page.wait.load_start()
|
|
|
|
|
- time.sleep(5)
|
|
|
|
|
-
|
|
|
|
|
- max_steps = 15
|
|
|
|
|
- stuck_counter = 0
|
|
|
|
|
- last_url = ""
|
|
|
|
|
- session_created = False
|
|
|
|
|
- has_submitted_login = False
|
|
|
|
|
-
|
|
|
|
|
- username = self.config.account.username
|
|
|
|
|
- password = self.config.account.password
|
|
|
|
|
-
|
|
|
|
|
- for step in range(max_steps):
|
|
|
|
|
- self.page.wait.doc_loaded()
|
|
|
|
|
- time.sleep(1)
|
|
|
|
|
-
|
|
|
|
|
- current_url = self.page.url
|
|
|
|
|
- current_title = self.page.title.lower()
|
|
|
|
|
- current_html_content = self.page.html
|
|
|
|
|
- self._log(f"--- [Router Step {step+1}] Current URL: {current_url} ---")
|
|
|
|
|
-
|
|
|
|
|
- # --- [异常处理与反爬对抗层] ---
|
|
|
|
|
- if current_url == last_url:
|
|
|
|
|
- stuck_counter += 1
|
|
|
|
|
- else:
|
|
|
|
|
- last_url = current_url
|
|
|
|
|
- stuck_counter = 0
|
|
|
|
|
-
|
|
|
|
|
- if stuck_counter >= 3:
|
|
|
|
|
- self._log("[WARN] Page stucked, try to refresh...")
|
|
|
|
|
- self.page.refresh()
|
|
|
|
|
- self._random_sleep(5, 8)
|
|
|
|
|
- stuck_counter = 0
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- server_error_indicators = ["502 Bad Gateway", "503 Service Temporarily Unavailable", "error 1020"]
|
|
|
|
|
- if any(err in current_html_content for err in server_error_indicators):
|
|
|
|
|
- self._log(f"[WARN] Server network error, try to refresh (Step: {step})...")
|
|
|
|
|
- self.page.refresh()
|
|
|
|
|
- self._random_sleep(10, 15)
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- # Cloudflare 拦截
|
|
|
|
|
- cloudflare_blocked_indicators = [
|
|
|
|
|
- "Sorry, you have been blocked",
|
|
|
|
|
- "You are being rate limited",
|
|
|
|
|
- "Cloudflare Ray ID"
|
|
|
|
|
- ]
|
|
|
|
|
- if any(indicator in current_html_content for indicator in cloudflare_blocked_indicators):
|
|
|
|
|
- raise BizLogicError(message="Blocked by Cloudflare WAF. Need to change IP or browser fingerprint.")
|
|
|
|
|
-
|
|
|
|
|
- # 遇到五秒盾先绕盾 (保留你原有的逻辑)
|
|
|
|
|
- if "just a moment" in current_title or "cloudflare" in current_title:
|
|
|
|
|
- # 假设你有 cf_bypasser 实例
|
|
|
|
|
- # if not cf_bypasser.bypass(max_retry=3): continue
|
|
|
|
|
- self._log("[State] CF 5-second shield detected. Waiting...")
|
|
|
|
|
- time.sleep(5)
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- # 状态 0: 成功到达目标预约页面 (Target Page)
|
|
|
|
|
- if "/Services" in current_url and self.page.ele('.app-menu', timeout=1):
|
|
|
|
|
- self._log("🎉 Successfully reached the Booking Services page! Session created successfully!")
|
|
|
|
|
- self.session_create_time = time.time()
|
|
|
|
|
- session_created = True
|
|
|
|
|
- break
|
|
|
|
|
-
|
|
|
|
|
- # 状态 1: 登录前的初始首页
|
|
|
|
|
- elif self.page.ele('#pingid-button', timeout=1):
|
|
|
|
|
- self._log("[State] Initial Landing Page detected. Clicking login button...")
|
|
|
|
|
- login_btn = self.page.ele('#pingid-button')
|
|
|
|
|
- self.mouse.human_click_ele(login_btn)
|
|
|
|
|
- self._log("Redirecting to PingID...")
|
|
|
|
|
- self._random_sleep(3, 5) # 替代 time.sleep(5)
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- # 状态 2: 真正的表单登录页 (PingID)
|
|
|
|
|
- elif self.page.ele('@name=callback_1', timeout=1):
|
|
|
|
|
- self._log("[State] PingID Login Form detected. Submitting credentials...")
|
|
|
|
|
-
|
|
|
|
|
- self._log("Entering username...")
|
|
|
|
|
- user_input = self.page.ele('@name=callback_1')
|
|
|
|
|
- self.mouse.human_click_ele(user_input)
|
|
|
|
|
- user_input.input(username, clear=True)
|
|
|
|
|
- time.sleep(1) # 模拟人手停顿
|
|
|
|
|
-
|
|
|
|
|
- self._log("Entering password...")
|
|
|
|
|
- pwd_input = self.page.ele('@type=password')
|
|
|
|
|
- self.mouse.human_click_ele(pwd_input)
|
|
|
|
|
- pwd_input.input(password, clear=True)
|
|
|
|
|
- time.sleep(1)
|
|
|
|
|
-
|
|
|
|
|
- self._log("Clicking submit login button...")
|
|
|
|
|
- submit_btn = self.page.ele('tag:button@type=submit')
|
|
|
|
|
- self.mouse.human_click_ele(submit_btn)
|
|
|
|
|
-
|
|
|
|
|
- has_submitted_login = True
|
|
|
|
|
- self._log("Login form submitted. Waiting for dashboard to load...")
|
|
|
|
|
- self._random_sleep(5, 7)
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- # 状态 3: 登录成功后的主控制台 (导航栏页面)
|
|
|
|
|
- elif self.page.ele('.app-menu', timeout=1):
|
|
|
|
|
- self._log("[State] Dashboard Menu detected.")
|
|
|
|
|
-
|
|
|
|
|
- switch_en_btn = self.page.ele('tag:a@@href=/Language/ChangeLanguage?lang=2', timeout=0.5)
|
|
|
|
|
- if switch_en_btn:
|
|
|
|
|
- self._log("Found 'English' language switch button. Clicking to change language...")
|
|
|
|
|
- self.mouse.human_click_ele(switch_en_btn)
|
|
|
|
|
- self._random_sleep(4, 6)
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- # 动作 B: 如果没有英文切换按钮(说明已经是英文或不存在),则查找并点击 Book 按钮
|
|
|
|
|
- book_btn = self.page.ele('@href=/Services', timeout=0.5)
|
|
|
|
|
- if book_btn:
|
|
|
|
|
- self._log("Found 'Book' menu element. Clicking to proceed to reservation...")
|
|
|
|
|
- self.mouse.human_click_ele(book_btn)
|
|
|
|
|
- self._random_sleep(4, 6)
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- self._log("[WARN] On Dashboard, but neither 'English' nor 'Book' button was found. Waiting...")
|
|
|
|
|
- time.sleep(2)
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- else:
|
|
|
|
|
- self._log("[State] In unknown or transitional state. Waiting for next polling cycle...")
|
|
|
|
|
- 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}")
|
|
|
|
|
-
|
|
|
|
|
- # -------------------------------------------------------------
|
|
|
|
|
- # 2. Query Availability
|
|
|
|
|
- # -------------------------------------------------------------
|
|
|
|
|
|
|
+
|
|
|
|
|
+ # 启动 ItaLoginFlow 注解流状态机
|
|
|
|
|
+ login_flow = ItaLoginFlow(
|
|
|
|
|
+ self.page,
|
|
|
|
|
+ plugin=self,
|
|
|
|
|
+ interval=1.0
|
|
|
|
|
+ )
|
|
|
|
|
+ login_flow.run(max_steps=20)
|
|
|
|
|
+ self.session_create_time = time.time()
|
|
|
|
|
+ self._log("🎉 ItaLoginFlow completed successfully. Session created.")
|
|
|
|
|
+
|
|
|
def query(self, apt_type: AppointmentType) -> VSQueryResult:
|
|
def query(self, apt_type: AppointmentType) -> VSQueryResult:
|
|
|
res = VSQueryResult()
|
|
res = VSQueryResult()
|
|
|
res.success = False
|
|
res.success = False
|
|
|
res.availability_status = AvailabilityStatus.NoneAvailable
|
|
res.availability_status = AvailabilityStatus.NoneAvailable
|
|
|
|
|
|
|
|
- # 假设要预约的服务和到访原因
|
|
|
|
|
- TARGET_SERVICE = "National and Schengen Visas"
|
|
|
|
|
- REASON_FOR_VISIT = "Tourism" # 对应 value 42 的选项文本
|
|
|
|
|
|
|
+ TARGET_SERVICE = self.free_config.get('target_service', "National and Schengen Visas")
|
|
|
|
|
+ REASON_FOR_VISIT = self.free_config.get('reason_for_visit', "Tourism")
|
|
|
|
|
|
|
|
- # ==========================================
|
|
|
|
|
# 步骤 1:在服务列表中寻找并点击目标服务的 Book
|
|
# 步骤 1:在服务列表中寻找并点击目标服务的 Book
|
|
|
- # ==========================================
|
|
|
|
|
self._log("Waiting for service list to load...")
|
|
self._log("Waiting for service list to load...")
|
|
|
self.page.wait.eles_loaded('@aria-controls=dataTableServices', timeout=10)
|
|
self.page.wait.eles_loaded('@aria-controls=dataTableServices', timeout=10)
|
|
|
|
|
|
|
|
self._log(f"Searching for [{TARGET_SERVICE}] Book button...")
|
|
self._log(f"Searching for [{TARGET_SERVICE}] Book button...")
|
|
|
- # 使用 XPath:找包含 TARGET_SERVICE 文本的行(tr),再找该行里包含 Book 的超链接(a)
|
|
|
|
|
target_book_btn_xpath = f'xpath://tr[contains(., "{TARGET_SERVICE}")]//a[contains(text(), "Book")]'
|
|
target_book_btn_xpath = f'xpath://tr[contains(., "{TARGET_SERVICE}")]//a[contains(text(), "Book")]'
|
|
|
book_btn = self.page.ele(target_book_btn_xpath)
|
|
book_btn = self.page.ele(target_book_btn_xpath)
|
|
|
|
|
|
|
|
if not book_btn:
|
|
if not book_btn:
|
|
|
self._log(f"Target [{TARGET_SERVICE}] Book button not found, currently no slots available.")
|
|
self._log(f"Target [{TARGET_SERVICE}] Book button not found, currently no slots available.")
|
|
|
- # 这里可以根据你的逻辑 return 或者 raise Exception
|
|
|
|
|
- else:
|
|
|
|
|
- self.mouse.human_click_ele(book_btn)
|
|
|
|
|
- self.page.wait.load_start()
|
|
|
|
|
- time.sleep(3)
|
|
|
|
|
|
|
+ return res
|
|
|
|
|
|
|
|
|
|
+ self.mouse.human_click_ele(book_btn)
|
|
|
|
|
+ self.page.wait.load_start()
|
|
|
|
|
+ time.sleep(3)
|
|
|
|
|
|
|
|
- # ==========================================
|
|
|
|
|
# 步骤 2:填写预约表单
|
|
# 步骤 2:填写预约表单
|
|
|
- # ==========================================
|
|
|
|
|
self._log("Waiting for booking form to load...")
|
|
self._log("Waiting for booking form to load...")
|
|
|
self.page.wait.eles_loaded('#bookingForm', timeout=10)
|
|
self.page.wait.eles_loaded('#bookingForm', timeout=10)
|
|
|
|
|
|
|
|
self._log("Selecting booking type & reason...")
|
|
self._log("Selecting booking type & reason...")
|
|
|
typeofbooking_ddl = self.page.ele('#typeofbookingddl')
|
|
typeofbooking_ddl = self.page.ele('#typeofbookingddl')
|
|
|
- # DrissionPage select 方法直接按文本选中,会自动触发网页的 JS 联动
|
|
|
|
|
- typeofbooking_ddl.select('Individual booking')
|
|
|
|
|
- time.sleep(1)
|
|
|
|
|
|
|
+ if typeofbooking_ddl:
|
|
|
|
|
+ typeofbooking_ddl.select('Individual booking')
|
|
|
|
|
+ time.sleep(1)
|
|
|
|
|
|
|
|
reason_ddl = self.page.ele('#ddls_0')
|
|
reason_ddl = self.page.ele('#ddls_0')
|
|
|
- reason_ddl.select(REASON_FOR_VISIT)
|
|
|
|
|
- time.sleep(1)
|
|
|
|
|
|
|
+ if reason_ddl:
|
|
|
|
|
+ reason_ddl.select(REASON_FOR_VISIT)
|
|
|
|
|
+ time.sleep(1)
|
|
|
|
|
|
|
|
notes_input = self.page.ele('#BookingNotes')
|
|
notes_input = self.page.ele('#BookingNotes')
|
|
|
- notes_input.clear()
|
|
|
|
|
- notes_input.input('N/A') # 选填,填入 N/A 或者留空
|
|
|
|
|
- time.sleep(1)
|
|
|
|
|
|
|
+ if notes_input:
|
|
|
|
|
+ notes_input.clear()
|
|
|
|
|
+ notes_input.input('N/A')
|
|
|
|
|
+ time.sleep(1)
|
|
|
|
|
|
|
|
- # ==========================================
|
|
|
|
|
# 步骤 3:处理 OTP 验证码
|
|
# 步骤 3:处理 OTP 验证码
|
|
|
- # ==========================================
|
|
|
|
|
self._log("Sending OTP code...")
|
|
self._log("Sending OTP code...")
|
|
|
otp_send_btn = self.page.ele('#otp-send')
|
|
otp_send_btn = self.page.ele('#otp-send')
|
|
|
- self.mouse.human_click_ele(otp_send_btn)
|
|
|
|
|
-
|
|
|
|
|
- # 这是一个 AJAX 请求,会转圈圈。我们需要等待成功提示出现
|
|
|
|
|
- self.page.wait.ele_displayed('#IdOtpSent', timeout=15) # 等待绿字 "New code sent!" 显示
|
|
|
|
|
- self._log("OTP code sent successfully.")
|
|
|
|
|
|
|
+ if otp_send_btn:
|
|
|
|
|
+ self.mouse.human_click_ele(otp_send_btn)
|
|
|
|
|
+ self.page.wait.ele_displayed('#IdOtpSent', timeout=15)
|
|
|
|
|
+ self._log("OTP code sent successfully.")
|
|
|
|
|
|
|
|
otp_input = self.page.ele('#otp-input')
|
|
otp_input = self.page.ele('#otp-input')
|
|
|
- self.mouse.human_click_ele(otp_input)
|
|
|
|
|
- otp_input.input('123456')
|
|
|
|
|
- time.sleep(1)
|
|
|
|
|
|
|
+ if otp_input:
|
|
|
|
|
+ self.mouse.human_click_ele(otp_input)
|
|
|
|
|
+ otp_input.input('123456')
|
|
|
|
|
+ time.sleep(1)
|
|
|
|
|
|
|
|
- # ==========================================
|
|
|
|
|
# 步骤 4:勾选隐私政策并提交
|
|
# 步骤 4:勾选隐私政策并提交
|
|
|
- # ==========================================
|
|
|
|
|
privacy_checkbox = self.page.ele('#PrivacyCheck')
|
|
privacy_checkbox = self.page.ele('#PrivacyCheck')
|
|
|
- # 判断一下如果没勾上才去点,防止重复点击取消了
|
|
|
|
|
- if not privacy_checkbox.states.is_checked:
|
|
|
|
|
|
|
+ if privacy_checkbox and not privacy_checkbox.states.is_checked:
|
|
|
self.mouse.human_click_ele(privacy_checkbox)
|
|
self.mouse.human_click_ele(privacy_checkbox)
|
|
|
time.sleep(1)
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
self._log("Submitting forward form...")
|
|
self._log("Submitting forward form...")
|
|
|
forward_btn = self.page.ele('#btnAvanti')
|
|
forward_btn = self.page.ele('#btnAvanti')
|
|
|
- self.mouse.human_click_ele(forward_btn)
|
|
|
|
|
|
|
+ if forward_btn:
|
|
|
|
|
+ self.mouse.human_click_ele(forward_btn)
|
|
|
|
|
|
|
|
- # 提交后页面会跳转,等待加载开始
|
|
|
|
|
self.page.wait.load_start()
|
|
self.page.wait.load_start()
|
|
|
self._log(f"Form submitted. Current page: {self.page.url}")
|
|
self._log(f"Form submitted. Current page: {self.page.url}")
|
|
|
|
|
+ time.sleep(3)
|
|
|
|
|
|
|
|
- time.sleep(5)
|
|
|
|
|
-
|
|
|
|
|
- valid_dates = []
|
|
|
|
|
-
|
|
|
|
|
- if valid_dates:
|
|
|
|
|
- res.success = True
|
|
|
|
|
- res.availability_status = AvailabilityStatus.Available
|
|
|
|
|
- earliest_date = valid_dates[0]
|
|
|
|
|
- earliest_dt = datetime.strptime(earliest_date, "%Y-%m-%d")
|
|
|
|
|
- res.earliest_date = earliest_dt
|
|
|
|
|
- for day in valid_days:
|
|
|
|
|
- # 查询具体 Slot
|
|
|
|
|
- slot_url = f"{self._host}/BookingCalendar/RetrieveTimeSlots"
|
|
|
|
|
- slot_payload = {
|
|
|
|
|
- "selectedDay": day, # YYYY-MM-DD
|
|
|
|
|
- "idService": str(self._service_id)
|
|
|
|
|
- }
|
|
|
|
|
- resp_slot = self._perform_request("POST", slot_url, json_data=slot_payload)
|
|
|
|
|
-
|
|
|
|
|
- time_slots = self._parse_time_slots(resp_slot.text)
|
|
|
|
|
- ts_list = []
|
|
|
|
|
- if time_slots:
|
|
|
|
|
- # 转换结构
|
|
|
|
|
- for ts in time_slots:
|
|
|
|
|
- # ts: {'id': 123, 'start': '10:00', 'end': '10:30', 'remain': 1}
|
|
|
|
|
- ts_list.append(TimeSlot(
|
|
|
|
|
- time=f"{ts['start']} - {ts['end']}",
|
|
|
|
|
- label=str(ts['id']) # 将 ID 存入 label 以便 book 使用
|
|
|
|
|
- ))
|
|
|
|
|
- res.availability.append(DateAvailability(date=datetime.strptime(day, "%d-%m-%Y"), times=ts_list))
|
|
|
|
|
return res
|
|
return res
|
|
|
|
|
|
|
|
- # -------------------------------------------------------------
|
|
|
|
|
- # 3. Book
|
|
|
|
|
- # -------------------------------------------------------------
|
|
|
|
|
def book(self, slot_info: VSQueryResult, user_inputs: Dict = None) -> VSBookResult:
|
|
def book(self, slot_info: VSQueryResult, user_inputs: Dict = None) -> VSBookResult:
|
|
|
res = VSBookResult()
|
|
res = VSBookResult()
|
|
|
res.success = False
|
|
res.success = False
|
|
|
|
|
+ user_inputs = user_inputs or {}
|
|
|
|
|
|
|
|
if not slot_info.availability:
|
|
if not slot_info.availability:
|
|
|
raise NotFoundError("No slots to book")
|
|
raise NotFoundError("No slots to book")
|
|
|
|
|
|
|
|
target_dt = slot_info.availability[0].date
|
|
target_dt = slot_info.availability[0].date
|
|
|
- target_date = target_dt.strftime("%Y-%m-%d")
|
|
|
|
|
- # 取第一个时间段
|
|
|
|
|
- target_slot = slot_info.availability[0].times[0]
|
|
|
|
|
- slot_id = target_slot.label # 我们在 query 里把 ID 存在了 label
|
|
|
|
|
- slot_text = target_slot.time # "10:00 - 10:30"
|
|
|
|
|
|
|
+ target_date = target_dt.strftime("%Y-%m-%d") if target_dt else ""
|
|
|
|
|
+ target_slot = slot_info.availability[0].times[0] if slot_info.availability[0].times else None
|
|
|
|
|
+ slot_id = target_slot.label if target_slot else ""
|
|
|
|
|
+ slot_text = target_slot.time if target_slot else ""
|
|
|
|
|
|
|
|
# 1. 获取 OTP (GenerateOTP)
|
|
# 1. 获取 OTP (GenerateOTP)
|
|
|
self._log("Requesting OTP...")
|
|
self._log("Requesting OTP...")
|
|
@@ -495,9 +357,8 @@ class ItaPlugin(IVSPlg):
|
|
|
|
|
|
|
|
# 2. 等待并读取邮件
|
|
# 2. 等待并读取邮件
|
|
|
self._log("Waiting for email code...")
|
|
self._log("Waiting for email code...")
|
|
|
- time.sleep(10) # 稍微等一下发信
|
|
|
|
|
- email_account = self.config.account.email
|
|
|
|
|
- # 使用 CloudAPI 读取 (假设已配置)
|
|
|
|
|
|
|
+ time.sleep(10)
|
|
|
|
|
+ email_account = self.config.account.username if self.config and self.config.account else ""
|
|
|
otp_code = VSCloudApi.Instance().get_email_verify_code(email_account)
|
|
otp_code = VSCloudApi.Instance().get_email_verify_code(email_account)
|
|
|
|
|
|
|
|
if not otp_code:
|
|
if not otp_code:
|
|
@@ -505,47 +366,34 @@ class ItaPlugin(IVSPlg):
|
|
|
self._log(f"Got OTP: {otp_code}")
|
|
self._log(f"Got OTP: {otp_code}")
|
|
|
|
|
|
|
|
# 3. 提交详细信息 (Fill User Info)
|
|
# 3. 提交详细信息 (Fill User Info)
|
|
|
- # 这是最复杂的一步,涉及文件上传 (Multipart)
|
|
|
|
|
self._log("Submitting User Details & Files...")
|
|
self._log("Submitting User Details & Files...")
|
|
|
-
|
|
|
|
|
- # 准备文件 (转 Base64 传给 JS)
|
|
|
|
|
passport_pdf_path = user_inputs.get('passport_pdf_path')
|
|
passport_pdf_path = user_inputs.get('passport_pdf_path')
|
|
|
irp_pdf_path = user_inputs.get('irp_pdf_path')
|
|
irp_pdf_path = user_inputs.get('irp_pdf_path')
|
|
|
|
|
|
|
|
def file_to_b64(path):
|
|
def file_to_b64(path):
|
|
|
- if not path or not os.path.exists(path): return ""
|
|
|
|
|
|
|
+ if not path or not os.path.exists(path):
|
|
|
|
|
+ return ""
|
|
|
with open(path, "rb") as f:
|
|
with open(path, "rb") as f:
|
|
|
return base64.b64encode(f.read()).decode('utf-8')
|
|
return base64.b64encode(f.read()).decode('utf-8')
|
|
|
|
|
|
|
|
ppt_b64 = file_to_b64(passport_pdf_path)
|
|
ppt_b64 = file_to_b64(passport_pdf_path)
|
|
|
irp_b64 = file_to_b64(irp_pdf_path)
|
|
irp_b64 = file_to_b64(irp_pdf_path)
|
|
|
|
|
|
|
|
- # 构造 JS FormData 提交脚本
|
|
|
|
|
- # 注意:这里需要根据 Service ID (Dublin/Canton) 动态调整字段 ID
|
|
|
|
|
- # 下面以 Dublin (1321) 的字段为例,如果是 Canton 需要修改 _Id 和 _TipoDatoAddizionale
|
|
|
|
|
- # 为了通用性,这里演示 Dublin 的结构,请根据实际 Service ID 调整 mapping
|
|
|
|
|
-
|
|
|
|
|
- # 假设是 Dublin (根据提供的源码分析)
|
|
|
|
|
- boundary = '----WebKitFormBoundaryRandomString'
|
|
|
|
|
submit_url = f"{self._host}/Services/Booking/{self._service_id}"
|
|
submit_url = f"{self._host}/Services/Booking/{self._service_id}"
|
|
|
|
|
|
|
|
- # 注入 JS 执行
|
|
|
|
|
js_submit = f"""
|
|
js_submit = f"""
|
|
|
const url = "{submit_url}";
|
|
const url = "{submit_url}";
|
|
|
const fd = new FormData();
|
|
const fd = new FormData();
|
|
|
|
|
|
|
|
- // 基础字段
|
|
|
|
|
fd.append('ServizioDescrizione', 'D Visa Application');
|
|
fd.append('ServizioDescrizione', 'D Visa Application');
|
|
|
fd.append('MessaggioRassicuranteWaitingList', 'True');
|
|
fd.append('MessaggioRassicuranteWaitingList', 'True');
|
|
|
fd.append('isWaitingListEnabled', 'False');
|
|
fd.append('isWaitingListEnabled', 'False');
|
|
|
fd.append('IDServizioConsolare', '35');
|
|
fd.append('IDServizioConsolare', '35');
|
|
|
fd.append('IDServizioErogato', '{self._service_id}');
|
|
fd.append('IDServizioErogato', '{self._service_id}');
|
|
|
- fd.append('IdTipoPrenotazione', '1'); // Single
|
|
|
|
|
|
|
+ fd.append('IdTipoPrenotazione', '1');
|
|
|
fd.append('NumMaxAccompagnatori', '3');
|
|
fd.append('NumMaxAccompagnatori', '3');
|
|
|
fd.append('NumAccompagnatoriSelected', '0');
|
|
fd.append('NumAccompagnatoriSelected', '0');
|
|
|
|
|
|
|
|
- // 动态字段 (Dublin 示例)
|
|
|
|
|
- // [0] Other citizenship -> User Input
|
|
|
|
|
fd.append('DatiAddizionaliPrenotante[0]._Descrizione', 'Other citizenship/s');
|
|
fd.append('DatiAddizionaliPrenotante[0]._Descrizione', 'Other citizenship/s');
|
|
|
fd.append('DatiAddizionaliPrenotante[0]._testo', '{user_inputs.get("citizen", "China")}');
|
|
fd.append('DatiAddizionaliPrenotante[0]._testo', '{user_inputs.get("citizen", "China")}');
|
|
|
fd.append('DatiAddizionaliPrenotante[0]._Obbligatorio', 'False');
|
|
fd.append('DatiAddizionaliPrenotante[0]._Obbligatorio', 'False');
|
|
@@ -553,7 +401,6 @@ class ItaPlugin(IVSPlg):
|
|
|
fd.append('DatiAddizionaliPrenotante[0]._TipoDatoAddizionale.IDTipoDatoAddizionale', '26');
|
|
fd.append('DatiAddizionaliPrenotante[0]._TipoDatoAddizionale.IDTipoDatoAddizionale', '26');
|
|
|
fd.append('DatiAddizionaliPrenotante[0]._TipoDatoAddizionale.IDTipoControllo', '2');
|
|
fd.append('DatiAddizionaliPrenotante[0]._TipoDatoAddizionale.IDTipoControllo', '2');
|
|
|
|
|
|
|
|
- // [1] Full address -> User Input
|
|
|
|
|
fd.append('DatiAddizionaliPrenotante[1]._Descrizione', 'Full residence address');
|
|
fd.append('DatiAddizionaliPrenotante[1]._Descrizione', 'Full residence address');
|
|
|
fd.append('DatiAddizionaliPrenotante[1]._testo', '{user_inputs.get("address", "")}');
|
|
fd.append('DatiAddizionaliPrenotante[1]._testo', '{user_inputs.get("address", "")}');
|
|
|
fd.append('DatiAddizionaliPrenotante[1]._Obbligatorio', 'True');
|
|
fd.append('DatiAddizionaliPrenotante[1]._Obbligatorio', 'True');
|
|
@@ -561,7 +408,6 @@ class ItaPlugin(IVSPlg):
|
|
|
fd.append('DatiAddizionaliPrenotante[1]._TipoDatoAddizionale.IDTipoDatoAddizionale', '25');
|
|
fd.append('DatiAddizionaliPrenotante[1]._TipoDatoAddizionale.IDTipoDatoAddizionale', '25');
|
|
|
fd.append('DatiAddizionaliPrenotante[1]._TipoDatoAddizionale.IDTipoControllo', '2');
|
|
fd.append('DatiAddizionaliPrenotante[1]._TipoDatoAddizionale.IDTipoControllo', '2');
|
|
|
|
|
|
|
|
- // [2] Passport Num
|
|
|
|
|
fd.append('DatiAddizionaliPrenotante[2]._Descrizione', 'Passport number');
|
|
fd.append('DatiAddizionaliPrenotante[2]._Descrizione', 'Passport number');
|
|
|
fd.append('DatiAddizionaliPrenotante[2]._testo', '{user_inputs.get("passport", "")}');
|
|
fd.append('DatiAddizionaliPrenotante[2]._testo', '{user_inputs.get("passport", "")}');
|
|
|
fd.append('DatiAddizionaliPrenotante[2]._Obbligatorio', 'True');
|
|
fd.append('DatiAddizionaliPrenotante[2]._Obbligatorio', 'True');
|
|
@@ -569,23 +415,16 @@ class ItaPlugin(IVSPlg):
|
|
|
fd.append('DatiAddizionaliPrenotante[2]._TipoDatoAddizionale.IDTipoDatoAddizionale', '2');
|
|
fd.append('DatiAddizionaliPrenotante[2]._TipoDatoAddizionale.IDTipoDatoAddizionale', '2');
|
|
|
fd.append('DatiAddizionaliPrenotante[2]._TipoDatoAddizionale.IDTipoControllo', '2');
|
|
fd.append('DatiAddizionaliPrenotante[2]._TipoDatoAddizionale.IDTipoControllo', '2');
|
|
|
|
|
|
|
|
- // [3] Reason (Select)
|
|
|
|
|
fd.append('DatiAddizionaliPrenotante[3]._Descrizione', 'Reason for visit');
|
|
fd.append('DatiAddizionaliPrenotante[3]._Descrizione', 'Reason for visit');
|
|
|
fd.append('DatiAddizionaliPrenotante[3]._Obbligatorio', 'True');
|
|
fd.append('DatiAddizionaliPrenotante[3]._Obbligatorio', 'True');
|
|
|
fd.append('DatiAddizionaliPrenotante[3]._Id', '61741');
|
|
fd.append('DatiAddizionaliPrenotante[3]._Id', '61741');
|
|
|
fd.append('DatiAddizionaliPrenotante[3]._TipoDatoAddizionale.IDTipoDatoAddizionale', '34');
|
|
fd.append('DatiAddizionaliPrenotante[3]._TipoDatoAddizionale.IDTipoDatoAddizionale', '34');
|
|
|
fd.append('DatiAddizionaliPrenotante[3]._TipoDatoAddizionale.IDTipoControllo', '3');
|
|
fd.append('DatiAddizionaliPrenotante[3]._TipoDatoAddizionale.IDTipoControllo', '3');
|
|
|
- fd.append('DatiAddizionaliPrenotante[3]._idSelezionato', '42'); // 42 = Tourism? Need verify
|
|
|
|
|
|
|
+ fd.append('DatiAddizionaliPrenotante[3]._idSelezionato', '42');
|
|
|
|
|
|
|
|
- // OTP
|
|
|
|
|
fd.append('otp-input', '{otp_code}');
|
|
fd.append('otp-input', '{otp_code}');
|
|
|
fd.append('PrivacyCheck', 'true');
|
|
fd.append('PrivacyCheck', 'true');
|
|
|
|
|
|
|
|
- // 文件处理 (Base64 -> Blob -> FormData)
|
|
|
|
|
- // 注意:这里假设页面上有文件上传的对应 ID,或者我们直接硬编码 FormData
|
|
|
|
|
- // 原始抓包并未显示文件字段名,通常是 File_0, File_1
|
|
|
|
|
- // 我们需要将 base64 转 blob
|
|
|
|
|
-
|
|
|
|
|
async function addFile(b64, name, filename) {{
|
|
async function addFile(b64, name, filename) {{
|
|
|
if(!b64) return;
|
|
if(!b64) return;
|
|
|
const res = await fetch(`data:application/pdf;base64,${{b64}}`);
|
|
const res = await fetch(`data:application/pdf;base64,${{b64}}`);
|
|
@@ -593,13 +432,11 @@ class ItaPlugin(IVSPlg):
|
|
|
fd.append(name, blob, filename);
|
|
fd.append(name, blob, filename);
|
|
|
}}
|
|
}}
|
|
|
|
|
|
|
|
- // 并行处理文件
|
|
|
|
|
await Promise.all([
|
|
await Promise.all([
|
|
|
- addFile('{ppt_b64}', 'File_0', 'passport.pdf'), // 假设 File_0 是护照
|
|
|
|
|
- addFile('{irp_b64}', 'File_1', 'irp.pdf') // 假设 File_1 是 IRP
|
|
|
|
|
|
|
+ addFile('{ppt_b64}', 'File_0', 'passport.pdf'),
|
|
|
|
|
+ addFile('{irp_b64}', 'File_1', 'irp.pdf')
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
- // 发送 POST
|
|
|
|
|
return fetch(url, {{
|
|
return fetch(url, {{
|
|
|
method: 'POST',
|
|
method: 'POST',
|
|
|
body: fd
|
|
body: fd
|
|
@@ -615,10 +452,9 @@ class ItaPlugin(IVSPlg):
|
|
|
self._log("User Info Submitted Successfully.")
|
|
self._log("User Info Submitted Successfully.")
|
|
|
else:
|
|
else:
|
|
|
self._log(f"User Info Submit Failed: {resp.text[:100]}")
|
|
self._log(f"User Info Submit Failed: {resp.text[:100]}")
|
|
|
- # 如果 OTP 错误,页面会返回特定错误信息
|
|
|
|
|
if "Codice errato" in resp.text:
|
|
if "Codice errato" in resp.text:
|
|
|
raise BizLogicError("Invalid OTP Code")
|
|
raise BizLogicError("Invalid OTP Code")
|
|
|
- return res # Fail
|
|
|
|
|
|
|
+ return res
|
|
|
|
|
|
|
|
# 4. 最终确认预约 (InsertNewBooking)
|
|
# 4. 最终确认预约 (InsertNewBooking)
|
|
|
self._log("Finalizing Booking...")
|
|
self._log("Finalizing Booking...")
|
|
@@ -626,9 +462,8 @@ class ItaPlugin(IVSPlg):
|
|
|
final_payload = {
|
|
final_payload = {
|
|
|
"idCalendarioGiornaliero": slot_id,
|
|
"idCalendarioGiornaliero": slot_id,
|
|
|
"selectedDay": target_date,
|
|
"selectedDay": target_date,
|
|
|
- "selectedHour": slot_text # "10:00 - 10:30(2)"
|
|
|
|
|
|
|
+ "selectedHour": slot_text
|
|
|
}
|
|
}
|
|
|
- # 这里用 Form-UrlEncoded
|
|
|
|
|
resp_final = self._perform_request("POST", final_url, data=final_payload)
|
|
resp_final = self._perform_request("POST", final_url, data=final_payload)
|
|
|
|
|
|
|
|
if resp_final.status_code == 200:
|
|
if resp_final.status_code == 200:
|
|
@@ -642,54 +477,34 @@ class ItaPlugin(IVSPlg):
|
|
|
return res
|
|
return res
|
|
|
|
|
|
|
|
def _perform_request(self, method, url, headers=None, data=None, json_data=None):
|
|
def _perform_request(self, method, url, headers=None, data=None, json_data=None):
|
|
|
- """JS Fetch Wrapper"""
|
|
|
|
|
- if not self.page: raise BizLogicError("Browser not init")
|
|
|
|
|
-
|
|
|
|
|
- fetch_opts = { "method": method.upper(), "headers": headers or {}, "credentials": "include" }
|
|
|
|
|
- if json_data:
|
|
|
|
|
- fetch_opts['body'] = json.dumps(json_data)
|
|
|
|
|
- fetch_opts['headers']['Content-Type'] = 'application/json; charset=UTF-8'
|
|
|
|
|
- elif data:
|
|
|
|
|
- if isinstance(data, dict):
|
|
|
|
|
- from urllib.parse import urlencode
|
|
|
|
|
- fetch_opts['body'] = urlencode(data)
|
|
|
|
|
- fetch_opts['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
|
|
|
|
|
- else:
|
|
|
|
|
- fetch_opts['body'] = data
|
|
|
|
|
-
|
|
|
|
|
- js = f"""
|
|
|
|
|
- return fetch("{url}", {json.dumps(fetch_opts)})
|
|
|
|
|
- .then(async r => {{
|
|
|
|
|
- const h = {{}}; r.headers.forEach((v, k) => h[k] = v);
|
|
|
|
|
- return {{ status: r.status, body: await r.text(), headers: h, url: r.url }};
|
|
|
|
|
- }}).catch(e => {{ return {{ status: 0, body: e.toString() }}; }});
|
|
|
|
|
- """
|
|
|
|
|
- return BrowserResponse(self.page.run_js(js, timeout=60)) # 文件上传可能较慢,给60s
|
|
|
|
|
|
|
+ """在浏览器上下文中注入执行 Fetch"""
|
|
|
|
|
+ if not self.page:
|
|
|
|
|
+ raise BizLogicError("Browser not initialized")
|
|
|
|
|
+ return perform_browser_fetch(
|
|
|
|
|
+ self.page,
|
|
|
|
|
+ method=method,
|
|
|
|
|
+ url=url,
|
|
|
|
|
+ headers=headers,
|
|
|
|
|
+ data=data,
|
|
|
|
|
+ json_data=json_data
|
|
|
|
|
+ )
|
|
|
|
|
|
|
|
- def _parse_valid_days(self, text):
|
|
|
|
|
- # 提取 DateLibere (YYYY-MM-DD)
|
|
|
|
|
- # 格式: {"DateLibere":"22/10/2024 00:00:00","SlotLiberi":1,"SlotRimanenti":1}
|
|
|
|
|
- # 原始正则: r'{"DateLibere":"(.*?)","SlotLiberi":\d+,"SlotRimanenti":(-?\d+)}'
|
|
|
|
|
|
|
+ def _parse_valid_days(self, text: str) -> List[str]:
|
|
|
days = []
|
|
days = []
|
|
|
try:
|
|
try:
|
|
|
matches = re.findall(r'{"DateLibere":"(.*?)".*?"SlotRimanenti":(-?\d+)}', text)
|
|
matches = re.findall(r'{"DateLibere":"(.*?)".*?"SlotRimanenti":(-?\d+)}', text)
|
|
|
for d_str, rem in matches:
|
|
for d_str, rem in matches:
|
|
|
if int(rem) != -1:
|
|
if int(rem) != -1:
|
|
|
- # 22/10/2024 -> 2024-10-22
|
|
|
|
|
dt = datetime.strptime(d_str[:10], "%d/%m/%Y")
|
|
dt = datetime.strptime(d_str[:10], "%d/%m/%Y")
|
|
|
days.append(dt.strftime("%Y-%m-%d"))
|
|
days.append(dt.strftime("%Y-%m-%d"))
|
|
|
- except: pass
|
|
|
|
|
|
|
+ except Exception:
|
|
|
|
|
+ pass
|
|
|
return days
|
|
return days
|
|
|
|
|
|
|
|
- def _parse_time_slots(self, text):
|
|
|
|
|
- # 提取 IDCalendarioServizioGiornaliero, StartTime, EndTime, Remain
|
|
|
|
|
|
|
+ def _parse_time_slots(self, text: str) -> List[Dict[str, Any]]:
|
|
|
slots = []
|
|
slots = []
|
|
|
try:
|
|
try:
|
|
|
- # 原始逻辑比较复杂,这里简化正则
|
|
|
|
|
- # 查找 SlotRimanenti > 0 的记录
|
|
|
|
|
- # 关键是 IDCalendarioServizioGiornaliero
|
|
|
|
|
raw_list = json.loads(text)
|
|
raw_list = json.loads(text)
|
|
|
- # Prenotami 返回的是一个 JSON 列表字符串
|
|
|
|
|
for item in raw_list:
|
|
for item in raw_list:
|
|
|
remain = item.get('SlotRimanenti', -1)
|
|
remain = item.get('SlotRimanenti', -1)
|
|
|
if remain > 0:
|
|
if remain > 0:
|
|
@@ -703,43 +518,15 @@ class ItaPlugin(IVSPlg):
|
|
|
'end': e_time,
|
|
'end': e_time,
|
|
|
'remain': remain
|
|
'remain': remain
|
|
|
})
|
|
})
|
|
|
- except: pass
|
|
|
|
|
|
|
+ except Exception:
|
|
|
|
|
+ pass
|
|
|
return slots
|
|
return slots
|
|
|
-
|
|
|
|
|
- # --- 资源清理核心方法 ---
|
|
|
|
|
|
|
+
|
|
|
def cleanup(self):
|
|
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
|
|
|
|
|
|
|
+ """销毁浏览器并彻底删除临时文件"""
|
|
|
|
|
+ cleanup_browser_workspace(self.page, self.root_workspace, self.tunnel, self._log)
|
|
|
|
|
+ self.page = None
|
|
|
|
|
+ self.tunnel = None
|
|
|
|
|
|
|
|
def get_plugin_status(self) -> PluginStatus:
|
|
def get_plugin_status(self) -> PluginStatus:
|
|
|
account_name = self.config.account.username if self.config and self.config.account else ""
|
|
account_name = self.config.account.username if self.config and self.config.account else ""
|
|
@@ -751,6 +538,7 @@ class ItaPlugin(IVSPlg):
|
|
|
except Exception:
|
|
except Exception:
|
|
|
pass
|
|
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"
|
|
state = "running" if self.is_healthy else "unhealthy"
|
|
|
|
|
|
|
|
return PluginStatus(
|
|
return PluginStatus(
|
|
@@ -760,7 +548,7 @@ class ItaPlugin(IVSPlg):
|
|
|
current_url=current_url,
|
|
current_url=current_url,
|
|
|
account=account_name,
|
|
account=account_name,
|
|
|
proxy=proxy_info,
|
|
proxy=proxy_info,
|
|
|
- session_age_sec=0,
|
|
|
|
|
|
|
+ session_age_sec=session_age,
|
|
|
last_active_at=utc_now_str(),
|
|
last_active_at=utc_now_str(),
|
|
|
)
|
|
)
|
|
|
|
|
|