| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559 |
- import time
- import json
- import random
- import re
- import os
- import uuid
- import base64
- from datetime import datetime
- from typing import List, Dict, Optional, Any, Callable
- from DrissionPage import ChromiumPage
- 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.vs_cloud_api import VSCloudApi
- from utils.cloudflare_bypass_for_scraping import CloudflareBypasser
- from utils.mouse import HumanMouse
- 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,
- 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):
- name = "ita_plugin"
- version = "1.0.0"
- description = "意大利Prenot@Mi领事预约插件 (支持拟人化交互与多领区调度)"
- 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.page: Optional[ChromiumPage] = None
-
- # Prenotami 特有配置
- self._service_id = 0
- self.ita_url = 'https://prenotami.esteri.it'
- self._host = self.ita_url
-
- 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]) -> None:
- self.logger = logger
-
- def _log(self, message):
- if self.logger:
- self.logger(f'[ItaPlugin] [{self.group_id}] [{self.instance_id}] {message}')
- else:
- 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):
- self.config = config
- self.free_config = config.free_config or {}
- # Service ID (e.g., 1321 for Ireland, 5059 for Guangzhou)
- self._service_id = self.free_config.get('service_id', 0)
-
- def keep_alive(self):
- pass
- def health_check(self) -> bool:
- if not self.is_healthy or not self.page:
- return False
- try:
- if not self.page.run_js("return 1;"):
- return False
- except Exception:
- return False
- if self.config.session_max_life > 0:
- if time.time() - self.session_create_time > self.config.session_max_life:
- self._log("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)
- ita_url = self.ita_url
- self._log(f"Navigating to {ita_url}")
- self.page.get(ita_url)
-
- self._log("Init humanize tools...")
- self.mouse = HumanMouse(self.page, debug=False)
- 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)
- # 启动 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:
- res = VSQueryResult()
- res.success = False
- res.availability_status = AvailabilityStatus.NoneAvailable
-
- 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
- self._log("Waiting for service list to load...")
- self.page.wait.eles_loaded('@aria-controls=dataTableServices', timeout=10)
- self._log(f"Searching for [{TARGET_SERVICE}] Book button...")
- target_book_btn_xpath = f'xpath://tr[contains(., "{TARGET_SERVICE}")]//a[contains(text(), "Book")]'
- book_btn = self.page.ele(target_book_btn_xpath)
- if not book_btn:
- self._log(f"Target [{TARGET_SERVICE}] Book button not found, currently no slots available.")
- return res
- self.mouse.human_click_ele(book_btn)
- self.page.wait.load_start()
- time.sleep(3)
- # 步骤 2:填写预约表单
- self._log("Waiting for booking form to load...")
- self.page.wait.eles_loaded('#bookingForm', timeout=10)
- self._log("Selecting booking type & reason...")
- typeofbooking_ddl = self.page.ele('#typeofbookingddl')
- if typeofbooking_ddl:
- typeofbooking_ddl.select('Individual booking')
- time.sleep(1)
- reason_ddl = self.page.ele('#ddls_0')
- if reason_ddl:
- reason_ddl.select(REASON_FOR_VISIT)
- time.sleep(1)
- notes_input = self.page.ele('#BookingNotes')
- if notes_input:
- notes_input.clear()
- notes_input.input('N/A')
- time.sleep(1)
- # 步骤 3:处理 OTP 验证码
- self._log("Sending OTP code...")
- otp_send_btn = self.page.ele('#otp-send')
- 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')
- if otp_input:
- self.mouse.human_click_ele(otp_input)
- otp_input.input('123456')
- time.sleep(1)
- # 步骤 4:勾选隐私政策并提交
- privacy_checkbox = self.page.ele('#PrivacyCheck')
- if privacy_checkbox and not privacy_checkbox.states.is_checked:
- self.mouse.human_click_ele(privacy_checkbox)
- time.sleep(1)
- self._log("Submitting forward form...")
- forward_btn = self.page.ele('#btnAvanti')
- if forward_btn:
- self.mouse.human_click_ele(forward_btn)
- self.page.wait.load_start()
- self._log(f"Form submitted. Current page: {self.page.url}")
- time.sleep(3)
- return res
- def book(self, slot_info: VSQueryResult, user_inputs: Dict = None) -> VSBookResult:
- res = VSBookResult()
- res.success = False
- user_inputs = user_inputs or {}
-
- if not slot_info.availability:
- raise NotFoundError("No slots to book")
-
- target_dt = slot_info.availability[0].date
- 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)
- self._log("Requesting OTP...")
- otp_url = f"{self._host}/BookingCalendar/GenerateOTP?ServiceID={self._service_id}"
- self._perform_request("POST", otp_url)
-
- # 2. 等待并读取邮件
- self._log("Waiting for email code...")
- 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)
-
- if not otp_code:
- raise BizLogicError("Failed to retrieve OTP code")
- self._log(f"Got OTP: {otp_code}")
- # 3. 提交详细信息 (Fill User Info)
- self._log("Submitting User Details & Files...")
- passport_pdf_path = user_inputs.get('passport_pdf_path')
- irp_pdf_path = user_inputs.get('irp_pdf_path')
-
- def file_to_b64(path):
- if not path or not os.path.exists(path):
- return ""
- with open(path, "rb") as f:
- return base64.b64encode(f.read()).decode('utf-8')
- ppt_b64 = file_to_b64(passport_pdf_path)
- irp_b64 = file_to_b64(irp_pdf_path)
-
- submit_url = f"{self._host}/Services/Booking/{self._service_id}"
-
- js_submit = f"""
- const url = "{submit_url}";
- const fd = new FormData();
-
- fd.append('ServizioDescrizione', 'D Visa Application');
- fd.append('MessaggioRassicuranteWaitingList', 'True');
- fd.append('isWaitingListEnabled', 'False');
- fd.append('IDServizioConsolare', '35');
- fd.append('IDServizioErogato', '{self._service_id}');
- fd.append('IdTipoPrenotazione', '1');
- fd.append('NumMaxAccompagnatori', '3');
- fd.append('NumAccompagnatoriSelected', '0');
-
- fd.append('DatiAddizionaliPrenotante[0]._Descrizione', 'Other citizenship/s');
- fd.append('DatiAddizionaliPrenotante[0]._testo', '{user_inputs.get("citizen", "China")}');
- fd.append('DatiAddizionaliPrenotante[0]._Obbligatorio', 'False');
- fd.append('DatiAddizionaliPrenotante[0]._Id', '61738');
- fd.append('DatiAddizionaliPrenotante[0]._TipoDatoAddizionale.IDTipoDatoAddizionale', '26');
- fd.append('DatiAddizionaliPrenotante[0]._TipoDatoAddizionale.IDTipoControllo', '2');
-
- fd.append('DatiAddizionaliPrenotante[1]._Descrizione', 'Full residence address');
- fd.append('DatiAddizionaliPrenotante[1]._testo', '{user_inputs.get("address", "")}');
- fd.append('DatiAddizionaliPrenotante[1]._Obbligatorio', 'True');
- fd.append('DatiAddizionaliPrenotante[1]._Id', '61739');
- fd.append('DatiAddizionaliPrenotante[1]._TipoDatoAddizionale.IDTipoDatoAddizionale', '25');
- fd.append('DatiAddizionaliPrenotante[1]._TipoDatoAddizionale.IDTipoControllo', '2');
- fd.append('DatiAddizionaliPrenotante[2]._Descrizione', 'Passport number');
- fd.append('DatiAddizionaliPrenotante[2]._testo', '{user_inputs.get("passport", "")}');
- fd.append('DatiAddizionaliPrenotante[2]._Obbligatorio', 'True');
- fd.append('DatiAddizionaliPrenotante[2]._Id', '61740');
- fd.append('DatiAddizionaliPrenotante[2]._TipoDatoAddizionale.IDTipoDatoAddizionale', '2');
- fd.append('DatiAddizionaliPrenotante[2]._TipoDatoAddizionale.IDTipoControllo', '2');
- fd.append('DatiAddizionaliPrenotante[3]._Descrizione', 'Reason for visit');
- fd.append('DatiAddizionaliPrenotante[3]._Obbligatorio', 'True');
- fd.append('DatiAddizionaliPrenotante[3]._Id', '61741');
- fd.append('DatiAddizionaliPrenotante[3]._TipoDatoAddizionale.IDTipoDatoAddizionale', '34');
- fd.append('DatiAddizionaliPrenotante[3]._TipoDatoAddizionale.IDTipoControllo', '3');
- fd.append('DatiAddizionaliPrenotante[3]._idSelezionato', '42');
- fd.append('otp-input', '{otp_code}');
- fd.append('PrivacyCheck', 'true');
-
- async function addFile(b64, name, filename) {{
- if(!b64) return;
- const res = await fetch(`data:application/pdf;base64,${{b64}}`);
- const blob = await res.blob();
- fd.append(name, blob, filename);
- }}
-
- await Promise.all([
- addFile('{ppt_b64}', 'File_0', 'passport.pdf'),
- addFile('{irp_b64}', 'File_1', 'irp.pdf')
- ]);
- return fetch(url, {{
- method: 'POST',
- body: fd
- }}).then(async r => {{
- return {{ status: r.status, url: r.url, text: await r.text() }};
- }}).catch(e => {{ return {{ status: 0, text: e.toString() }}; }});
- """
-
- result_dict = self.page.run_js(js_submit)
- resp = BrowserResponse(result_dict)
-
- if resp.status_code == 302 or "BookingCalendar" in resp.url:
- self._log("User Info Submitted Successfully.")
- else:
- self._log(f"User Info Submit Failed: {resp.text[:100]}")
- if "Codice errato" in resp.text:
- raise BizLogicError("Invalid OTP Code")
- return res
- # 4. 最终确认预约 (InsertNewBooking)
- self._log("Finalizing Booking...")
- final_url = f"{self._host}/BookingCalendar/InsertNewBooking"
- final_payload = {
- "idCalendarioGiornaliero": slot_id,
- "selectedDay": target_date,
- "selectedHour": slot_text
- }
- resp_final = self._perform_request("POST", final_url, data=final_payload)
-
- if resp_final.status_code == 200:
- self._log("Booking Confirmed!")
- res.success = True
- res.book_date = target_date
- res.book_time = slot_text
- else:
- self._log(f"Final Booking Failed: {resp_final.status_code}")
-
- return res
-
- def _perform_request(self, method, url, headers=None, data=None, json_data=None):
- """在浏览器上下文中注入执行 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: str) -> List[str]:
- days = []
- try:
- matches = re.findall(r'{"DateLibere":"(.*?)".*?"SlotRimanenti":(-?\d+)}', text)
- for d_str, rem in matches:
- if int(rem) != -1:
- dt = datetime.strptime(d_str[:10], "%d/%m/%Y")
- days.append(dt.strftime("%Y-%m-%d"))
- except Exception:
- pass
- return days
- def _parse_time_slots(self, text: str) -> List[Dict[str, Any]]:
- slots = []
- try:
- raw_list = json.loads(text)
- for item in raw_list:
- remain = item.get('SlotRimanenti', -1)
- if remain > 0:
- start = item['OrarioInizioFascia']
- end = item['OrarioFineFascia']
- s_time = f"{start['Hours']:02d}:{start['Minutes']:02d}"
- e_time = f"{end['Hours']:02d}:{end['Minutes']:02d}"
- slots.append({
- 'id': item['IDCalendarioServizioGiornaliero'],
- 'start': s_time,
- 'end': e_time,
- 'remain': remain
- })
- except Exception:
- pass
- return slots
- 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()
|