"""Prenot@Mi Query/Book adapter; Italy-specific production code stays here.""" from __future__ import annotations import html import json import os import random import re import sys import time import uuid from dataclasses import asdict, dataclass, field from datetime import date, datetime, time as datetime_time, timezone from enum import Enum from threading import Lock from typing import Any, Mapping, Sequence from urllib.parse import urljoin, urlparse from DrissionPage import ChromiumPage import vs_plg as _ita_host from vs_plg import IVSPlg from vs_types import ( AvailabilityStatus, BizException, BizLogicError, NotFoundError, DateAvailability, PermissionDeniedError, PluginStatus, RateLimiteddError, SessionExpiredOrInvalidError, VSBookResult, VSQueryResult, utc_now_str, ) from toolkit.vs_cloud_api import VSCloudApi from utils.cloudflare_bypass_for_scraping import CloudflareBypasser from utils.flow_engine import BaseFlow, done, guard, on from utils.keyboard import HumanKeyboard from utils.mouse import HumanMouse from utils.plugin_utils import ( cleanup_browser_workspace, filter_dates_in_range, init_chromium_options, perform_browser_fetch, ) MAX_PROTOCOL_ITEMS = 512 MAX_PROTOCOL_PAYLOAD_CHARS = 1_000_000 MAX_LOOKAHEAD_MONTHS = 120 PRENOTAMI_OTP_SENDER = "noreply-prenotami@esteri.it" _EMAIL_PATTERN = re.compile(r"^[^@\s]+@[^@\s]+\.[^@\s]+$") _OTP_PATTERN = re.compile(r"(? str: value = account_email if self.authorization_source == "account" else self.authorization_email if not isinstance(value, str) or not _EMAIL_PATTERN.fullmatch(value.strip()): raise ItaProtocolError("otp_authorization_email_invalid", path="mail.authorization_email") return value.strip() @dataclass(frozen=True, slots=True) class ItaRouteConfig: routing_key: str service_id: int capability: ItaCapability lookahead_months: int = 0 booking_type: FormControlConfig | None = None purpose: FormControlConfig | None = None mail: OtpMailConfig | None = None @dataclass(frozen=True, slots=True) class CalendarDay: day: date slots_free: int slots_remaining: int @property def is_available(self): return self.slots_free > 0 @dataclass(frozen=True, slots=True) class SlotCandidate(CalendarDay): start_time: datetime_time end_time: datetime_time daily_calendar_id: str = field(repr=False) @property def time_label(self): return f"{self.start_time:%H:%M} - {self.end_time:%H:%M}" def _positive_int(value, path, maximum=None): if type(value) is not int or value <= 0: raise ItaProtocolError("positive_integer_required", path=path) if maximum is not None and value > maximum: raise ItaProtocolError("lookahead_too_large", path=path) return value def _mapping(value, path): if not isinstance(value, Mapping): raise ItaProtocolError("mapping_required", path=path) return value def _text(value, path): if not isinstance(value, str) or not value.strip(): raise ItaProtocolError("string_required", path=path) return value.strip() def _control(form, prefix, path): labels = form.get(f"{prefix}_labels") if isinstance(labels, (str, bytes)) or not isinstance(labels, Sequence) or not labels: raise ItaProtocolError("labels_required", path=path) labels = tuple(_text(value, path) for value in labels) if len(set(labels)) != len(labels): raise ItaProtocolError("labels_duplicate", path=path) return FormControlConfig( _text(form.get(f"{prefix}_control_id"), path), _text(form.get(f"{prefix}_value"), path), labels, ) def resolve_route_config(configs: Mapping[str, Any], routing_key: str) -> ItaRouteConfig: configs, key = _mapping(configs, "apt_configs"), _text(routing_key, "routing_key") if key not in configs: raise ItaProtocolError("route_missing", path="apt_configs") raw = _mapping(configs[key], key) capability_name = _text(raw.get("capability"), f"{key}.capability") try: capability = ItaCapability(capability_name) except ValueError: raise ItaProtocolError("capability_unsupported", path=f"{key}.capability") from None service = _positive_int(raw.get("service_id"), f"{key}.service_id") if capability is ItaCapability.SERVICES_PROBE_ONLY: return ItaRouteConfig(key, service, capability) form = _mapping(raw.get("form"), f"{key}.form") scan = _mapping(raw.get("calendar_scan"), f"{key}.calendar_scan") mail_raw = raw.get("mail") mail = None if mail_raw is not None: mail_raw = _mapping(mail_raw, f"{key}.mail") if str(mail_raw.get("sender", PRENOTAMI_OTP_SENDER)).strip().casefold() != PRENOTAMI_OTP_SENDER: raise ItaProtocolError("otp_sender_not_allowlisted", path=f"{key}.mail.sender") source = mail_raw.get("authorization_source", "configured") mail = OtpMailConfig( mail_raw.get("authorization_email"), mail_raw.get("subject_keywords", "OTP Code"), mail_raw.get("body_keywords", "OTP Code:"), mail_raw.get("expiry_seconds", 300), source, ) return ItaRouteConfig( key, service, capability, _positive_int(scan.get("lookahead_months"), f"{key}.calendar_scan", MAX_LOOKAHEAD_MONTHS), _control(form, "booking_type", f"{key}.form"), _control(form, "purpose", f"{key}.form"), mail, ) def decode_double_json_array(payload: str, *, max_items: int = MAX_PROTOCOL_ITEMS): if not isinstance(payload, str) or not payload: raise ItaProtocolError("payload_type") if len(payload) > MAX_PROTOCOL_PAYLOAD_CHARS: raise ItaProtocolError("payload_too_large") if type(max_items) is not int or max_items <= 0: raise ItaProtocolError("max_items_invalid") try: inner = json.loads(payload) except json.JSONDecodeError as exc: raise ItaProtocolError("outer_json_invalid") from exc if not isinstance(inner, str): raise ItaProtocolError("payload_layer_count") try: records = json.loads(inner) except json.JSONDecodeError as exc: raise ItaProtocolError("inner_json_invalid") from exc if not isinstance(records, list): raise ItaProtocolError("payload_layer_count") if len(records) > max_items: raise ItaProtocolError("item_limit_exceeded") if not all(isinstance(item, Mapping) for item in records): raise ItaProtocolError("item_type") return tuple(records) def _site_date(value, path): if not isinstance(value, str) or not re.fullmatch(r"\d{2}/\d{2}/\d{4} 00:00:00", value): raise ItaProtocolError("date_format", path=path) try: return datetime.strptime(value, "%d/%m/%Y %H:%M:%S").date() except ValueError as exc: raise ItaProtocolError("date_invalid", path=path) from exc def _integer(item, key, path): if key not in item: raise ItaProtocolError("field_missing", path=f"{path}.{key}") if type(item[key]) is not int: raise ItaProtocolError("integer_required", path=f"{path}.{key}") return item[key] def _clock(value, path): value = _mapping(value, path) parts = [_integer(value, key, path) for key in ( "Hours", "Minutes", "Seconds", "Milliseconds")] if parts[2] or parts[3]: raise ItaProtocolError("time_precision", path=path) try: return datetime_time(parts[0], parts[1]) except ValueError as exc: raise ItaProtocolError("time_invalid", path=path) from exc def _parse_calendar_items(payload, time_slots=False, max_items=MAX_PROTOCOL_ITEMS): records, identities, daily_ids = [], set(), set() for index, item in enumerate(decode_double_json_array(payload, max_items=max_items)): path = f"{'time_slots' if time_slots else 'availability'}[{index}]" free, remaining = _integer(item, "SlotLiberi", path), _integer(item, "SlotRimanenti", path) if not ((free > 0 and remaining >= 0) or (free == 0 and remaining < 0)): raise ItaProtocolError("inventory_conflict", path=path) day = _site_date(item.get("Data" if time_slots else "DateLibere"), path) if time_slots: start = _clock(item.get("OrarioInizioFascia"), f"{path}.OrarioInizioFascia") end = _clock(item.get("OrarioFineFascia"), f"{path}.OrarioFineFascia") if start >= end: raise ItaProtocolError("time_range_invalid", path=path) daily_id = _integer(item, "IDCalendarioServizioGiornaliero", path) if daily_id <= 0: raise ItaProtocolError("positive_integer_required", path=path) if daily_id in daily_ids: raise ItaProtocolError("duplicate_daily_id", path=path) daily_ids.add(daily_id) record = SlotCandidate(day, free, remaining, start, end, str(daily_id)) identity = day, start, end else: record, identity = CalendarDay(day, free, remaining), day if identity in identities: raise ItaProtocolError("duplicate_time_range" if time_slots else "duplicate_calendar_day") identities.add(identity) records.append(record) return tuple(sorted(records, key=lambda item: (item.day, getattr(item, "start_time", datetime_time.min)))) def parse_calendar_availability(payload: str, *, max_items: int = MAX_PROTOCOL_ITEMS): return _parse_calendar_items(payload, max_items=max_items) def parse_time_slots(payload: str, *, max_items: int = MAX_PROTOCOL_ITEMS): return _parse_calendar_items(payload, time_slots=True, max_items=max_items) def _available_inventory(items, *, start_day=None, end_day=None): if start_day and end_day and start_day > end_day: raise ItaProtocolError("date_range_invalid") allowed = set(filter_dates_in_range( [item.day.isoformat() for item in items], (start_day or date.min).isoformat(), (end_day or date.max).isoformat(), shuffle=False, )) return tuple(item for item in sorted( (item for item in items if item.is_available and item.day.isoformat() in allowed), key=lambda item: (item.day, getattr(item, "start_time", datetime_time.min)), )) available_calendar_days = available_slot_candidates = _available_inventory def parse_otp_body(body: str, *, max_chars: int = MAX_PROTOCOL_PAYLOAD_CHARS): if not isinstance(body, str) or not body.strip(): raise ItaProtocolError("otp_body_invalid") if len(body) > max_chars: raise ItaProtocolError("otp_body_too_large") values = set(_OTP_PATTERN.findall(html.unescape(re.sub(r"<[^>]*>", " ", body)))) if len(values) != 1: raise ItaProtocolError("otp_not_found" if not values else "otp_ambiguous") return values.pop() def month_starts(anchor_day: date, lookahead_months: int): months = _positive_int(lookahead_months, "lookahead_months", MAX_LOOKAHEAD_MONTHS) return tuple(date(anchor_day.year + (anchor_day.month - 1 + i) // 12, (anchor_day.month - 1 + i) % 12 + 1, 1) for i in range(months)) CALENDAR_PATH = "/BookingCalendar" AVAILABILITY_PATH = "/BookingCalendar/RetrieveCalendarAvailability" TIME_SLOTS_PATH = "/BookingCalendar/RetrieveTimeSlots" DEFAULT_MONTH_CLICK_INTERVAL_SECONDS = 1.5 def same_origin_path(raw_url): try: parsed = urlparse(str(raw_url or "")) port = parsed.port except ValueError: raise BizLogicError("Italy request left the expected origin") from None if (parsed.scheme != "https" or parsed.hostname != "prenotami.esteri.it" or port not in (None, 443) or parsed.username is not None): raise BizLogicError("Italy request left the expected origin") return parsed.path CALENDAR_DOM_SCRIPT = r""" const [mode, serviceId, day, dailyId, timeLabel, capacity, shouldClick] = arguments; const all = (selector, root = document) => Array.from(root.querySelectorAll(selector)); const services = all('#idServizio'), timeline = all('.events a.selected[data-date]'); const service = services.length === 1 ? services[0].value : ''; const selectedDay = timeline.length === 1 ? timeline[0].dataset.date : ''; if (mode === 'context') return {path: location.pathname, service, day: selectedDay}; const contextReady = location.pathname === '/BookingCalendar' && service === serviceId; const links = all('.events a[data-date]').filter(item => item.dataset.date === day); const link = links.length === 1 ? links[0] : null; if (mode === 'date') { const candidateReady = Boolean(contextReady && link && !link.classList.contains('notAvailableDay') && !link.classList.contains('disabled')); let selected = Boolean(candidateReady && link.classList.contains('selected')), clicked = false; if (shouldClick && candidateReady && !selected) { link.click(); clicked = true; } selected = Boolean(candidateReady && link.classList.contains('selected')); return {contextReady, candidateCount: links.length, candidateReady, selected, clicked}; } const content = all('.events-content li.selected').find(item => item.dataset.date === day); const matches = content ? all('.fascia', content).filter(slot => { const input = slot.querySelector('#idHiddenFasciaServizio'); return input && input.value === dailyId; }) : []; const slot = matches.length === 1 ? matches[0] : null, spans = slot ? slot.querySelectorAll('span') : []; const clean = value => String(value || '').replace(/\s+/g, ' ').trim(); const candidateReady = Boolean(contextReady && slot && !slot.classList.contains('notAvailableFascia') && spans.length === 2 && clean(spans[0].textContent) === timeLabel && clean(spans[1].textContent) === `(${capacity})`); let selected = Boolean(candidateReady && slot.classList.contains('act')), clicked = false; if (shouldClick && candidateReady && !selected) { slot.click(); clicked = true; } selected = Boolean(candidateReady && slot.classList.contains('act')); return {contextReady, candidateCount: matches.length, candidateReady, selected, clicked}; """ def _runtime_id(value): if value is None or isinstance(value, bool): raise BizLogicError("Italy Calendar service id is invalid") value = str(value).strip() if not re.fullmatch(r"[1-9]\d{0,127}", value): raise BizLogicError("Italy Calendar service id is invalid") return value def _request_day(value): try: return date.fromisoformat(str(value)) except ValueError: raise BizLogicError("Italy Calendar date format changed") from None def read_calendar_context(page): if same_origin_path(page.url) != CALENDAR_PATH: raise BizLogicError("Italy Calendar page is not current") raw = page.run_js(CALENDAR_DOM_SCRIPT, "context", "", "", "", "", 0, False) if not isinstance(raw, dict) or raw.get("path") != CALENDAR_PATH: raise BizLogicError("Italy Calendar context is not ready") return _runtime_id(raw.get("service")), _request_day(raw.get("day")) def wait_for_calendar_service(page, *, session_is_current, expected_service=None, timeout=5): deadline = time.monotonic() + timeout while time.monotonic() < deadline: if session_is_current() is not True: raise SessionExpiredOrInvalidError("Italy applicant Session changed") try: service = read_calendar_context(page)[0] except BizLogicError: time.sleep(0.1) continue if expected_service and service != _runtime_id(expected_service): raise BizLogicError("Italy Calendar service changed") return service raise BizLogicError("Italy Calendar service did not initialize") def _calendar_post(page, path, payload, parser, *, form=False): try: response = perform_browser_fetch( page, "POST", path, **{"data" if form else "json_data": payload}, timeout=30) except BizException: raise except Exception as exc: raise BizLogicError(f"Italy Calendar request failed: {type(exc).__name__}") from exc status, host = response.status_code, urlparse(str(response.url or "")).hostname if host == "iam.esteri.it" or status == 401: raise SessionExpiredOrInvalidError("Italy Calendar authentication expired") if status == 403: raise PermissionDeniedError("Italy Calendar access denied") if status == 429: raise RateLimiteddError("Italy Calendar rate limited") if status == 404: raise NotFoundError("Italy Calendar endpoint is unavailable") if status in {0, 408, 502, 503, 504}: raise BizLogicError("Italy Calendar temporarily unavailable") if status != 200 or same_origin_path(response.url) != path: raise BizLogicError("Italy Calendar returned an unexpected response") try: return parser(response.text) except (ValueError, TypeError) as exc: raise BizLogicError( f"Italy Calendar response rejected ({getattr(exc, 'code', type(exc).__name__)})") from exc def fetch_calendar_month(page, service, selected_day): selected = selected_day.strftime("%d/%m/%Y") days = _calendar_post( page, AVAILABILITY_PATH, {"_Servizio": _runtime_id(service), "selectedDay": selected}, parse_calendar_availability, ) if any(item.day.replace(day=1) != selected_day.replace(day=1) for item in days): raise BizLogicError("Italy Calendar returned dates for another month") return days def fetch_calendar_slots(page, service, selected_day): slots = _calendar_post( page, TIME_SLOTS_PATH, {"selectedDay": selected_day.isoformat(), "idService": _runtime_id(service)}, parse_time_slots, ) if any(slot.day != selected_day for slot in slots): raise BizLogicError("Italy Calendar returned slots for another day") return slots def scan_until_first_available_month( page, *, lookahead_months, log=None, check_current=None, month_click_interval_seconds=DEFAULT_MONTH_CLICK_INTERVAL_SECONDS, ): service, anchor = read_calendar_context(page) scans = [] for index, month in enumerate(month_starts(anchor, lookahead_months)): if index and month_click_interval_seconds: time.sleep(month_click_interval_seconds) if check_current: check_current() days = fetch_calendar_month(page, service, month) if check_current: check_current() scans.append(days) available = available_calendar_days(days) if log: log(f"Italy Calendar probe index={index} available={len(available)}") if available: break return tuple(scans) def click_next_month(page, *, timeout=10): control = page.ele("css:#datetimepicker th.dtpicker-next", timeout=timeout) if not control: raise NotFoundError("Italy Calendar next-month control is unavailable") control.click(by_js=True) def _snapshot(page, mode, service, day, candidate=None, click=False): candidate = candidate or SlotCandidate(day, 0, -1, datetime_time.min, datetime_time.min, "") raw = page.run_js( CALENDAR_DOM_SCRIPT, mode, service, day.isoformat(), candidate.daily_calendar_id, candidate.time_label, candidate.slots_free, click, ) if (not isinstance(raw, dict) or type(raw.get("candidateCount")) is not int or any(type(raw.get(key)) is not bool for key in ( "contextReady", "candidateReady", "selected", "clicked")) or not raw["contextReady"] or raw["candidateCount"] not in (0, 1) or (raw["candidateCount"] == 0 and (mode != "slot" or click or raw["candidateReady"] or raw["selected"] or raw["clicked"]))): raise BizLogicError("Italy Calendar selection context changed") return raw def verify_precommit_selection(page, *, runtime_service, candidate): raw = _snapshot( page, "slot", runtime_service, candidate.day, candidate, False) if (not candidate.is_available or raw["candidateCount"] != 1 or not raw["candidateReady"] or not raw["selected"]): raise BizLogicError("Italy Calendar selected slot changed") return candidate def select_slot_before_final( page, *, target_day, days, slots, runtime_service, dom_timeout=5, session_is_current=None, ): inventory = [item for item in days if item.day == target_day and item.is_available] candidates = available_slot_candidates(slots, start_day=target_day, end_day=target_day) if not inventory or not candidates: return None candidate, deadline = candidates[0], time.monotonic() + dom_timeout if session_is_current and session_is_current() is not True: raise SessionExpiredOrInvalidError("Italy applicant Session changed") _snapshot(page, "date", runtime_service, target_day, click=True) while time.monotonic() < deadline: if session_is_current and session_is_current() is not True: raise SessionExpiredOrInvalidError("Italy applicant Session changed") date_state = _snapshot(page, "date", runtime_service, target_day) slot_state = _snapshot(page, "slot", runtime_service, target_day, candidate) if date_state.get("selected") and slot_state.get("candidateReady"): if not slot_state.get("selected"): _snapshot(page, "slot", runtime_service, target_day, candidate, True) return verify_precommit_selection( page, runtime_service=runtime_service, candidate=candidate) time.sleep(0.1) raise BizLogicError("Italy Calendar slot did not finish loading") def book_date_preferences(user_inputs): values = [] for key, default in (("expected_start_date", "2000-01-01"), ("expected_end_date", "2100-01-01")): try: values.append(date.fromisoformat(user_inputs.get(key, default))) except (AttributeError, TypeError, ValueError): raise BizLogicError(f"Italy {key} must use YYYY-MM-DD") from None if values[0] > values[1]: raise BizLogicError("Italy date preference is inverted") return tuple(values) def _move_to_month(page, service, target, timeout): current = read_calendar_context(page)[1] while (current.year, current.month) < target: expected = (current.year + 1, 1) if current.month == 12 else (current.year, current.month + 1) click_next_month(page, timeout=timeout) deadline = time.monotonic() + timeout while time.monotonic() < deadline: actual_service, current = read_calendar_context(page) if actual_service != service: raise BizLogicError("Italy Calendar service changed") if (current.year, current.month) == expected: break time.sleep(0.1) else: raise BizLogicError("Italy Calendar month did not load") if (current.year, current.month) != target: raise BizLogicError("Italy Calendar cannot navigate backwards") def recheck_calendar_before_final( page, *, lookahead_months, start_day, end_day, session_is_current, month_click_interval_seconds=DEFAULT_MONTH_CLICK_INTERVAL_SECONDS, timeout=30, dom_timeout=5, log=None, ): service, anchor = read_calendar_context(page) for index, month_start in enumerate(month_starts(anchor, lookahead_months)): if session_is_current() is not True: raise SessionExpiredOrInvalidError("Italy applicant Session changed") if index and month_click_interval_seconds: time.sleep(month_click_interval_seconds) inventory = fetch_calendar_month(page, service, month_start) days = available_calendar_days(inventory, start_day=start_day, end_day=end_day) selected_month = month_start.year, month_start.month if log: log(f"Italy Book Calendar month={month_start:%Y-%m} candidates={len(days)}") if days: _move_to_month(page, service, selected_month, timeout) for item in days: slots = fetch_calendar_slots(page, service, item.day) candidate = select_slot_before_final( page, target_day=item.day, days=inventory, slots=slots, runtime_service=service, dom_timeout=dom_timeout, session_is_current=session_is_current, ) if candidate: return candidate if selected_month >= (end_day.year, end_day.month): break return None FINAL_PATH = "/BookingCalendar/InsertNewBooking" FINAL_FAILED_LABEL = "italy-final-failed" FINAL_ACCEPTED_LABEL = "italy-final-accepted" def _submit_final_once(plugin, route: ItaRouteConfig, slot: SlotCandidate) -> VSBookResult: result = VSBookResult(success=False, label=FINAL_FAILED_LABEL) try: page = plugin.page username = plugin.config.account.username.strip().casefold() prepared = plugin._require_calendar_context(route, role="book", username=username) if (not username or not isinstance(slot, SlotCandidate) or page.tab_id != prepared.tab_id or same_origin_path(page.url) != CALENDAR_PATH): raise BizLogicError("Italy final context is not current") verify_precommit_selection(page, runtime_service=prepared.runtime_service, candidate=slot) data = _calendar_post(page, FINAL_PATH, { "idCalendarioGiornaliero": slot.daily_calendar_id, "selectedDay": slot.day.isoformat(), "selectedHour": f"{slot.time_label}\n({slot.slots_free})", }, json.loads, form=True) if not isinstance(data, dict) or not isinstance(data.get("errorMessage"), str): raise BizLogicError("Italy final response is missing its result") if data["errorMessage"].strip(): return result destination = data.get("url") if not isinstance(destination, str) or not destination.strip(): raise BizLogicError("Italy final response is missing its receipt URL") path = same_origin_path(urljoin("https://prenotami.esteri.it", destination.strip())) if not re.fullmatch(r"/Reservation/?|/ReservationDetail/Index/[^/]+/?", path): raise BizLogicError("Italy final response did not lead to a receipt") result = VSBookResult(success=True, account=username, label=FINAL_ACCEPTED_LABEL, book_date=slot.day.isoformat(), book_time=slot.time_label) return result finally: if not result.success: # The next Booker signal must prepare again and obtain fresh IDs. plugin._reset_calendar_state() _SENT_AT_FORMAT = "%Y-%m-%d %H:%M:%S" _OTP_INPUT_LOCK = vars(_ita_host).setdefault("_coordinate_ita_otp_input_lock", Lock()) def guarded_preflight_script(route: ItaRouteConfig, booking_path: str) -> str: if not route.booking_type or not route.purpose: raise BizLogicError("Italy route is missing form controls") config = {"path": booking_path, "booking": asdict(route.booking_type), "purpose": asdict(route.purpose)} return f""" const cfg = {json.dumps(config, ensure_ascii=False)}; const form = document.querySelector('#bookingForm'); const norm = value => String(value || '').replace(/\\s+/g, ' ').trim().toLowerCase(); function choose(item) {{ const control = document.getElementById(item.control_id); const option = control && Array.from(control.options).find(value => value.value === item.value); if (!option || !item.labels.map(norm).includes(norm(option.textContent))) return false; if (control.value !== item.value) {{ control.value = item.value; control.dispatchEvent(new Event('change', {{bubbles: true}})); }} return control.value === item.value; }} const booking = choose(cfg.booking), purpose = choose(cfg.purpose); const privacy = document.querySelector('#PrivacyCheck'); if (privacy && !privacy.checked) privacy.click(); const otp = document.querySelector('#otp-input'), send = document.querySelector('#otp-send'); const avanti = document.querySelector('#btnAvanti'); const token = document.querySelector('input[name="cf-turnstile-response"]'); return {{ formReady: Boolean(location.pathname === cfg.path && form && booking && purpose && privacy && privacy.checked), turnstileReady: Boolean(token && token.value), otpValuePresent: Boolean(otp && otp.value.trim()), otpSendEnabled: Boolean(send && !send.disabled), avantiEnabled: Boolean(avanti && !avanti.disabled), }}; """ def validate_booking_preflight(raw, *, after_otp=False, allow_pending=False): if type(after_otp) is not bool or type(allow_pending) is not bool: raise BizLogicError("Italy booking preflight policy is invalid") if not isinstance(raw, dict) or any(type(raw.get(key)) is not bool for key in ( "formReady", "turnstileReady", "otpValuePresent", "otpSendEnabled", "avantiEnabled")): raise BizLogicError("Italy booking form structure changed") if raw["otpValuePresent"] is not after_otp: raise BizLogicError("Italy booking OTP stage changed") ready = (raw["formReady"] and (after_otp or raw["otpSendEnabled"]) and raw["turnstileReady"] and raw["avantiEnabled"]) if not allow_pending and not ready: raise BizLogicError("Italy booking verification is not ready") def wait_for_booking_preflight(page, *, script, after_otp=False, timeout=120, check_current=None): deadline, bypasser, attempts, raw = time.monotonic() + timeout, None, 0, None while time.monotonic() < deadline: if check_current: check_current() raw = page.run_js(script) validate_booking_preflight(raw, after_otp=after_otp, allow_pending=True) form_ready = raw["formReady"] and (after_otp or raw["otpSendEnabled"]) if form_ready and raw["turnstileReady"] and raw["avantiEnabled"]: return raw if form_ready and not raw["turnstileReady"] and attempts < 3: bypasser = bypasser or CloudflareBypasser(page, log=False) try: bypasser.click_verification_button(is_dfs=False) except Exception: pass attempts += 1 time.sleep(1) reason = ("form_pending" if not raw or not raw["formReady"] else "otp_send_disabled" if not after_otp and not raw["otpSendEnabled"] else "turnstile_token_pending" if not raw["turnstileReady"] else "avanti_disabled") raise BizLogicError(f"Italy booking verification timed out: {reason}") def _element_or_fail(page, selector): element = page.ele(selector, timeout=5) if not element: raise NotFoundError(f"Italy booking element is missing: {selector}") return element def _read_manual_otp(): value = input("Italy booking OTP: ").strip() if not re.fullmatch(r"[0-9]{4,12}", value): raise BizLogicError("Italy OTP format rejected") return value def _enter_otp(plugin, otp): field = _element_or_fail(plugin.page, "#otp-input") plugin.mouse.human_click_ele(field) field.input(otp, clear=True) if field.run_js("return this.value === arguments[0];", otp) is not True: raise BizLogicError("Italy OTP write failed") def validate_cloud_otp_environment(mail_config, recipient): if not isinstance(mail_config, OtpMailConfig): raise BizLogicError("Italy OTP mail configuration is missing") if not isinstance(recipient, str) or not _EMAIL_PATTERN.fullmatch(recipient.strip()): raise BizLogicError("Italy OTP recipient is invalid") mail_config.mailbox_for(recipient) return recipient.strip() def fetch_otp_from_cloud(mail_config, *, recipient, sent_at, api=None): validate_cloud_otp_environment(mail_config, recipient) try: datetime.strptime(sent_at, _SENT_AT_FORMAT) except (TypeError, ValueError): raise BizLogicError("Italy OTP sent time is invalid") from None try: body = (api or VSCloudApi.Instance()).fetch_mail_content( email=mail_config.mailbox_for(recipient), sender=PRENOTAMI_OTP_SENDER, recipient=recipient, subject_keywords="OTP Code", body_keywords="OTP Code:", sent_date=sent_at, expiry=mail_config.expiry_seconds, only_text=True, ) return parse_otp_body(body) except ItaProtocolError as exc: raise BizLogicError(f"Italy OTP body invalid ({exc.code})") from None except PermissionDeniedError: raise PermissionDeniedError("Italy OTP mailbox not permitted") from None except SessionExpiredOrInvalidError: raise SessionExpiredOrInvalidError("Italy OTP mailbox session expired") from None except RateLimiteddError: raise RateLimiteddError("Italy OTP mailbox rate limited") from None except NotFoundError: raise NotFoundError("Italy OTP email not found or mailbox not authorized") from None except BizLogicError as exc: status = re.search(r"\bHTTP Error (\d{3})\b", str(getattr(exc, "message", ""))) if status and int(status.group(1)) == 404: raise NotFoundError("Italy OTP email not found or mailbox not authorized") from None if status and int(status.group(1)) in {408, 502, 503, 504}: raise BizLogicError("Italy OTP mailbox provider timed out") from None raise BizLogicError("Italy OTP mailbox read failed") from None except Exception: raise BizLogicError("Italy OTP mailbox read failed") from None def _wait_for_calendar(page, timeout=30): deadline = time.monotonic() + timeout while time.monotonic() < deadline: if urlparse(str(page.url or "")).path.rstrip("/") == CALENDAR_PATH: page.wait.doc_loaded() return time.sleep(0.25) raise BizLogicError("Italy AVANTI did not reach Calendar") def prepare_calendar(plugin, route, *, role): if role not in {"query", "book"} or route.capability is not ItaCapability.SINGLE_SCHENGEN_TOURISM: raise BizLogicError("Italy Calendar preparation requires a tourism route") cloud = plugin.free_config.get("otp_mode") == "cloud" if not cloud and (plugin.free_config.get("otp_mode") != "manual" or not sys.stdin.isatty()): raise PermissionDeniedError("Italy Calendar requires automatic OTP or an attached terminal") username, page, booking_path = ( plugin.config.account.username.strip().casefold(), plugin.page, plugin._booking_path(route)) if cloud: validate_cloud_otp_environment(route.mail, username) def current(path): if plugin.page is not page or same_origin_path(page.url) != path: raise SessionExpiredOrInvalidError("Italy preparation page changed") def click(selector): current(booking_path) plugin.mouse.human_click_ele(_element_or_fail(page, selector)) with _OTP_INPUT_LOCK: script = guarded_preflight_script(route, booking_path) wait_for_booking_preflight(page, script=script, check_current=lambda: current(booking_path)) sent_at = datetime.now(timezone.utc).strftime(_SENT_AT_FORMAT) click("#otp-send") plugin._log(f"Italy {role} OTP sent.") otp = (fetch_otp_from_cloud(route.mail, recipient=username, sent_at=sent_at) if cloud else _read_manual_otp()) _enter_otp(plugin, otp) wait_for_booking_preflight( page, script=script, after_otp=True, check_current=lambda: current(booking_path)) page.set.auto_handle_alert(accept=True) try: click("#btnAvanti") _wait_for_calendar(page) finally: page.set.auto_handle_alert(on_off=False) wait_for_calendar_service( page, session_is_current=lambda: plugin.page is page, expected_service=route.service_id, ) plugin._record_calendar_prepared_route(route, role=role) BOOKING_RECHECK_NONE_AVAILABLE_LABEL = "no-availability-after-user-recheck" BOOKING_CALENDAR_PREFINAL_STOP_LABEL = "calendar-ready-before-final" RADWARE_VALIDATION_HOSTS = frozenset({"validate.perfdrive.com"}) SERVICE_LINK_WAIT_SECONDS = 15 SERVICES_PROBE_QUERY_MODE = "services_probe" CALENDAR_QUERY_MODE = "calendar" @dataclass(frozen=True, repr=False, eq=False) class _PreparedCalendar: username: str routing_key: str runtime_service: str tab_id: str role: str class ItaLoginFlow(BaseFlow): """Italy page rules on the host flow engine; never resubmit credentials.""" def __init__(self, plugin): self.plugin = plugin self.entered = self.submitted = False super().__init__(plugin.page, interval=3, logger=plugin._log, max_stuck_cycles=None) def at(self, host, path): url = urlparse(self.page.url) return url.hostname == host and (url.path.rstrip("/") or "/") == path @guard(predicate=lambda f: urlparse(f.page.url).hostname in RADWARE_VALIDATION_HOSTS, priority=1) def radware(self): raise PermissionDeniedError( "Italy authentication requires Radware browser validation; automatic retry is disabled") @guard(predicate=lambda f: urlparse(f.page.url).scheme != "https" or urlparse(f.page.url).netloc.lower() not in { "prenotami.esteri.it", "iam.esteri.it", "prenotami.esteri.it:443", "iam.esteri.it:443"}, priority=2) def foreign_origin(self): raise BizLogicError("Italy authentication left the expected website") @guard(predicate=lambda f: any(value in f.page.html for value in ( "Sorry, you have been blocked", "You are being rate limited", "error 1020")), priority=3) def blocked(self): raise PermissionDeniedError("Italy authentication was blocked; automatic retry is disabled") @guard(predicate=lambda f: any(value in f.page.html for value in ( "502 Bad Gateway", "503 Service Temporarily Unavailable")), priority=4) def server_error(self): if self.entered or self.submitted: raise BizLogicError("Italy authentication failed after login started; no resubmission") self.page.refresh() self.plugin._random_sleep(10, 15) @guard(title="just a moment", priority=5) @guard(title="cloudflare", priority=5) def verification_pending(self): self.log("Waiting for the website's human verification") time.sleep(5) @on(ele=".app-menu", predicate=lambda f: f.at("prenotami.esteri.it", "/UserArea") and bool(f.page.ele("@href=/Services", timeout=0.2)), priority=20) @done def authenticated(self): self.plugin._log("Successfully reached authenticated /UserArea.") self.plugin.session_create_time = time.time() self.plugin.is_healthy = True @on(ele="#pingid-button", predicate=lambda f: f.at("prenotami.esteri.it", "/") and not f.entered, priority=30) def enter(self): self.entered = True self.plugin.mouse.human_click_ele(self.page.ele("#pingid-button")) self.plugin._log("Redirecting to PingID...") self.plugin._random_sleep(3, 5) @on(ele="@name=callback_1", predicate=lambda f: f.at("iam.esteri.it", "/signin") and not f.submitted, priority=40) def credentials(self): username, password, submit = [self.page.ele(selector, timeout=0.2) for selector in ("@name=callback_1", "@name=callback_2", "tag:button@type=submit")] if not all((username, password, submit)): return # IAM can render its controls in separate updates. for label, element, value in (("username", username, self.plugin.config.account.username), ("password", password, self.plugin.config.account.password)): self.plugin._log(f"Entering {label}...") self.plugin.mouse.human_click_ele(element) element.input(value, clear=True) time.sleep(1) self.submitted = True self.plugin.mouse.human_click_ele(submit) self.plugin._log("Login form submitted. Waiting for dashboard to load...") self.plugin._random_sleep(5, 7) class ItaPlugin(IVSPlg): name = "ita_plugin" version = "1.0.3" description = "意大利 Prenot@Mi 查询与预约插件" def __init__(self, group_id): self.group_id = group_id self.config = None self.free_config = {} self.is_healthy = True self.logger = self.page = self.tunnel = None self.ita_url = "https://prenotami.esteri.it" self.instance_id = uuid.uuid4().hex[:8] self.root_workspace = os.path.abspath( os.path.join("data/temp_browser_data", f"{group_id}.{self.instance_id}")) self.user_data_path = os.path.join(self.root_workspace, "user_data") os.makedirs(self.root_workspace, exist_ok=True) self.session_create_time = 0 self._prepared_calendar = None def set_log(self, logger): self.logger = logger def _log(self, message): (self.logger or print)(f"[ItaPlugin] [{self.group_id}] {message}") def _random_sleep(self, minimum=30, maximum=60): seconds = random.uniform(minimum, maximum) self._log(f"Anti-Rate-Limit: Sleeping for {seconds:.2f} seconds...") time.sleep(seconds) def set_config(self, config): self.config, self.free_config = config, config.free_config or {} query_mode = self.free_config.get("query_mode", SERVICES_PROBE_QUERY_MODE) otp_mode = self.free_config.get("otp_mode") final = self.free_config.get("final_submit_enabled", False) if query_mode not in {SERVICES_PROBE_QUERY_MODE, CALENDAR_QUERY_MODE}: raise BizLogicError("Italy query_mode is invalid") if otp_mode not in {None, "manual", "cloud"}: raise BizLogicError("Italy otp_mode is invalid") if final is not False and not ( final is True and otp_mode in {"manual", "cloud"} and query_mode == SERVICES_PROBE_QUERY_MODE): raise BizLogicError("Italy final submission configuration is invalid") self._prepared_calendar = None def _resolve_route(self, apt_type): try: return resolve_route_config( self.free_config.get("apt_configs", {}), apt_type.routing_key) except (AttributeError, ItaProtocolError) as exc: detail = getattr(exc, "code", type(exc).__name__) raise BizLogicError(f"Italy route configuration rejected: {detail}") from exc @staticmethod def _booking_path(route): return f"/Services/Booking/{route.service_id}" def _authenticated_path(self): host = urlparse(str(self.page.url or "")).hostname if host == "iam.esteri.it" or self.page.ele("#pingid-button", timeout=0.2): raise SessionExpiredOrInvalidError("Italy Session expired") return same_origin_path(self.page.url) def _reset_calendar_state(self): self._prepared_calendar = None def _calendar_context_matches(self, route, *, role, username=None): state = self._prepared_calendar try: account = self.config.account.username.strip().casefold() return ( isinstance(state, _PreparedCalendar) and self.is_healthy and state.tab_id == self.page.tab_id and state.username == account and state.routing_key == route.routing_key and state.role == role and (username is None or username == account) ) except Exception: return False def _record_calendar_prepared_route(self, route, *, role): try: self._prepared_calendar = _PreparedCalendar( self.config.account.username.strip().casefold(), route.routing_key, read_calendar_context(self.page)[0], self.page.tab_id, role) except Exception: self._prepared_calendar = None raise BizLogicError("Italy Calendar identity is unavailable") from None def _require_calendar_context(self, route, *, role, username=None): if not self._calendar_context_matches(route, role=role, username=username): raise BizLogicError("Italy Calendar Session changed") if read_calendar_context(self.page)[0] != self._prepared_calendar.runtime_service: raise BizLogicError("Italy Calendar service changed") return self._prepared_calendar def _invalidate_query_session(self): self.is_healthy = False self._prepared_calendar = None def keep_alive(self): pass def health_check(self): if not self.is_healthy or not self.page: return False try: alive = self.page.run_js("return 1;") == 1 except Exception: alive = False expired = self.config.session_max_life > 0 and ( time.time() - self.session_create_time > self.config.session_max_life) if not alive or expired: self._invalidate_query_session() return False return True def create_session(self): self._log(f"Initializing Session (ID: {self.instance_id})...") self._invalidate_query_session() account = getattr(self.config, "account", None) username, password = getattr(account, "username", None), getattr(account, "password", None) if (not isinstance(username, str) or not username.strip() or not isinstance(password, str) or not password): raise BizLogicError("Italy website username and password are required") options, self.tunnel = init_chromium_options( user_data_path=self.user_data_path, username_seed=self.config.account.username, proxy=self.config.proxy, headless=False, window_size="1920,1080", logger=self._log, ) self.page = ChromiumPage(options) self.page.get(self.ita_url) self.mouse, self.keyboard = HumanMouse(self.page, debug=True), HumanKeyboard(self.page) width, height = self.page.rect.viewport_size self.mouse.move(random.randint(10, width - 10), random.randint(10, height - 10)) flow = ItaLoginFlow(self) try: flow.run(max_steps=40, timeout=120) except BizException: raise except Exception as exc: raise BizLogicError(f"Italy login failed: {type(exc).__name__}; url={self.page.url}") from None def query(self, apt_type): if self.free_config.get("query_mode") != CALENDAR_QUERY_MODE: raise BizLogicError("Italy Query requires query_mode=calendar") if not self.health_check(): raise SessionExpiredOrInvalidError("Italy Query Session is unavailable") route = self._resolve_route(apt_type) if same_origin_path(self.page.url) != CALENDAR_PATH: entry = self._enter_service(apt_type, route=route) if not entry.success: return entry self._reset_calendar_state() prepare_calendar(self, route, role="query") self._require_calendar_context(route, role="query") probes = scan_until_first_available_month( self.page, lookahead_months=route.lookahead_months, log=self._log, check_current=lambda: self._require_calendar_context(route, role="query")) available = available_calendar_days(probes[-1]) if not available: return VSQueryResult( success=False, apt_type=apt_type, availability_status=AvailabilityStatus.NoneAvailable) earliest = datetime.combine(available[0].day, datetime.min.time()) self._log(f"Italy Calendar earliest_date={earliest.date().isoformat()}") return VSQueryResult( success=True, apt_type=apt_type, availability_status=AvailabilityStatus.Available, earliest_date=earliest, availability=[DateAvailability(date=earliest, times=[])]) def _enter_service(self, apt_type, *, route=None): route, path = route or self._resolve_route(apt_type), None booking_path = self._booking_path(route) self.page.get(f"{self.ita_url}/Services") self.page.wait.doc_loaded() if self._authenticated_path() != "/Services": raise BizLogicError("Italy service list is unavailable") link = self.page.ele(f"tag:a@@href={booking_path}", timeout=SERVICE_LINK_WAIT_SECONDS) if not link: raise NotFoundError(f"Italy service link is missing: {booking_path}") self.mouse.human_click_ele(link) self.page.wait.load_start() self.page.wait.doc_loaded() path = self._authenticated_path() empty = VSQueryResult( success=False, apt_type=apt_type, availability_status=AvailabilityStatus.NoneAvailable) if path == "/Services": marker = self.page.ele("#WNotAvailable", timeout=1) notice = self.page.ele(".jconfirm-content", timeout=1) text = ((marker.attr("value") if marker else "") or (notice.text if notice else "") or "").strip().casefold() if text and ("posti disponibili" in text or marker): return empty raise BizLogicError("Italy service returned an unknown response") if path != booking_path or not self.page.ele("#bookingForm", timeout=2): raise BizLogicError("Italy booking form is unavailable") return VSQueryResult( success=True, apt_type=apt_type, availability_status=AvailabilityStatus.NoneAvailable) def book(self, slot_info, user_inputs=None): if not self.page or not isinstance(slot_info, VSQueryResult): raise BizLogicError("Italy Book requires a Query signal") route = self._resolve_route(slot_info.apt_type) inputs = user_inputs if isinstance(user_inputs, Mapping) else {} username = str(inputs.get("username", "")).strip().casefold() if not username or username != self.config.account.username.strip().casefold(): raise BizLogicError("Italy Book requires the applicant account") if self._prepared_calendar is None: entry = self._enter_service(slot_info.apt_type, route=route) if not entry.success: return VSBookResult(success=False, label=BOOKING_RECHECK_NONE_AVAILABLE_LABEL) prepare_calendar(self, route, role="book") start, end = book_date_preferences(inputs) slot = recheck_calendar_before_final( self.page, lookahead_months=route.lookahead_months, start_day=start, end_day=end, session_is_current=lambda: self._calendar_context_matches( route, role="book", username=username), log=self._log, ) if not slot: return VSBookResult(success=False, label=BOOKING_RECHECK_NONE_AVAILABLE_LABEL) self._log(f"Italy Book selected {slot.day} {slot.time_label}") if self.free_config.get("final_submit_enabled") is True: return _submit_final_once(self, route, slot) return VSBookResult(success=False, label=BOOKING_CALENDAR_PREFINAL_STOP_LABEL) def cleanup(self): self._invalidate_query_session() cleanup_browser_workspace( self.page, self.root_workspace, self.tunnel, self._log) self.page = self.tunnel = None def get_plugin_status(self): proxy = self.config.proxy if self.config else None current_url = "" if self.page: try: current_url = str(self.page.url) except Exception: pass # Status reporting must still work after browser disconnection. return PluginStatus( instance_id=self.instance_id, is_healthy=self.is_healthy, state="running" if self.is_healthy else "unhealthy", current_url=current_url, account=self.config.account.username if self.config else "", proxy=f"{proxy.ip}:{proxy.port}" if proxy and proxy.ip else "", session_age_sec=max(0, int(time.time() - self.session_create_time)) if self.session_create_time > 0 else 0, last_active_at=utc_now_str(), ) def __del__(self): try: self.cleanup() except Exception: pass