"""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, replace from datetime import date, datetime, time as datetime_time, timezone from enum import Enum from threading import current_thread, main_thread from typing import Any, Mapping, Sequence from urllib.parse import urljoin, urlparse from DrissionPage import ChromiumPage from DrissionPage.errors import ElementNotFoundError, PageDisconnectedError from vs_plg import IVSPlg from vs_types import ( AvailabilityStatus, 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.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 TURNSTILE_PASSIVE_SECONDS = 5 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 BizLogicError("Italy mail.authorization_email must be a valid email address") 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 BizLogicError(f"Italy {path} must be a positive integer") if maximum is not None and value > maximum: raise BizLogicError(f"Italy {path} must not exceed {maximum}") return value def _mapping(value, path): if not isinstance(value, Mapping): raise BizLogicError(f"Italy {path} must be an object") return value def _text(value, path): if not isinstance(value, str) or not value.strip(): raise BizLogicError(f"Italy {path} must be a non-empty string") 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 BizLogicError(f"Italy {path}.{prefix}_labels must be a non-empty list") labels = tuple(_text(value, path) for value in labels) if len(set(labels)) != len(labels): raise BizLogicError(f"Italy {path}.{prefix}_labels contains duplicates") 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 NotFoundError(f"Italy apt_configs is missing route {key}") raw = _mapping(configs[key], key) capability_name = _text(raw.get("capability"), f"{key}.capability") try: capability = ItaCapability(capability_name) except ValueError as exc: raise BizLogicError(f"Italy {key}.capability is unsupported") from exc 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 or (mail_raw.get("subject_keywords", "OTP Code"), mail_raw.get("body_keywords", "OTP Code:")) != ("OTP Code", "OTP Code:")): raise BizLogicError(f"Italy {key}.mail filters must match the Prenot@Mi OTP template") mail = OtpMailConfig( authorization_email=mail_raw.get("authorization_email"), expiry_seconds=mail_raw.get("expiry_seconds", 300), authorization_source=mail_raw.get("authorization_source", "configured"), ) 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 _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 BizLogicError(f"Italy {path} date must use DD/MM/YYYY 00:00:00") try: return datetime.strptime(value, "%d/%m/%Y %H:%M:%S").date() except ValueError as exc: raise BizLogicError(f"Italy {path} contains an invalid date") from exc def _integer(item, key, path): if key not in item: raise BizLogicError(f"Italy response is missing {path}.{key}") if type(item[key]) is not int: raise BizLogicError(f"Italy {path}.{key} must be an integer") 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 BizLogicError(f"Italy {path} time must have zero seconds and milliseconds") try: return datetime_time(parts[0], parts[1]) except ValueError as exc: raise BizLogicError(f"Italy {path} contains an invalid time") from exc def _parse_calendar_items(payload, time_slots=False): if not isinstance(payload, str) or not payload: raise BizLogicError("Italy Calendar payload must be a non-empty string") if len(payload) > MAX_PROTOCOL_PAYLOAD_CHARS: raise BizLogicError("Italy Calendar payload exceeds the size limit") try: inner = json.loads(payload) except json.JSONDecodeError as exc: raise BizLogicError("Italy Calendar outer JSON is invalid") from exc if not isinstance(inner, str): raise BizLogicError("Italy Calendar payload must contain a JSON-encoded array string") try: items = json.loads(inner) except json.JSONDecodeError as exc: raise BizLogicError("Italy Calendar inner JSON is invalid") from exc if not isinstance(items, list): raise BizLogicError("Italy Calendar decoded payload must be an array") if len(items) > MAX_PROTOCOL_ITEMS: raise BizLogicError("Italy Calendar payload exceeds the item limit") if not all(isinstance(item, Mapping) for item in items): raise BizLogicError("Italy Calendar items must be objects") records, identities, daily_ids = [], set(), set() for index, item in enumerate(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 BizLogicError(f"Italy {path} contains conflicting inventory counts") 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 BizLogicError(f"Italy {path} slot start must precede its end") daily_id = _integer(item, "IDCalendarioServizioGiornaliero", path) if daily_id <= 0: raise BizLogicError(f"Italy {path} daily calendar id must be positive") if daily_id in daily_ids: raise BizLogicError(f"Italy {path} daily calendar id is duplicated") 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 BizLogicError(f"Italy {path} contains a duplicate date or time slot") 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): return _parse_calendar_items(payload) def parse_time_slots(payload: str): return _parse_calendar_items(payload, time_slots=True) def available_calendar_days(items, *, start_day=None, end_day=None): if start_day and end_day and start_day > end_day: raise BizLogicError("Italy inventory date range is inverted") 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 items if item.is_available and item.day.isoformat() in allowed) def parse_otp_body(body: str): if not isinstance(body, str) or not body.strip(): raise BizLogicError("Italy OTP mail body must be a non-empty string") if len(body) > MAX_PROTOCOL_PAYLOAD_CHARS: raise BizLogicError("Italy OTP mail body exceeds the size limit") values = set(_OTP_PATTERN.findall(html.unescape(re.sub(r"<[^>]*>", " ", body)))) if not values: raise NotFoundError("Italy OTP code was not found in the email") if len(values) != 1: raise BizLogicError("Italy OTP email contains conflicting codes") 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 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") try: return _runtime_id(raw.get("service")), date.fromisoformat(str(raw.get("day"))) except ValueError: raise BizLogicError("Italy Calendar date format changed") from None def wait_for_calendar_service(plugin, page, expected_service, timeout=5): expected_service = _runtime_id(expected_service) deadline = time.monotonic() + timeout while time.monotonic() < deadline: if plugin.page is not page: raise SessionExpiredOrInvalidError("Italy applicant Session changed") try: service = read_calendar_context(page)[0] except BizLogicError: time.sleep(0.1) continue if service != 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 (PageDisconnectedError, ElementNotFoundError, SessionExpiredOrInvalidError, RateLimiteddError, NotFoundError, BizLogicError): 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 BizLogicError("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 ({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(plugin, route): page = plugin.page service, anchor = read_calendar_context(page) for index, month in enumerate(month_starts(anchor, route.lookahead_months)): if index: time.sleep(DEFAULT_MONTH_CLICK_INTERVAL_SECONDS) plugin._require_calendar_context(route, role="query") days = fetch_calendar_month(page, service, month) plugin._require_calendar_context(route, role="query") available = available_calendar_days(days) plugin._log(f"Italy Calendar probe index={index} available={len(available)}") yield available if available: return def _snapshot(page, mode, service, day, candidate=None, click=False): raw = page.run_js( CALENDAR_DOM_SCRIPT, mode, service, day.isoformat(), candidate.daily_calendar_id if candidate else "", candidate.time_label if candidate else "", candidate.slots_free if candidate else 0, 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 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) 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) 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(plugin, route, start_day, end_day, *, session_is_current): page = plugin.page service, anchor = read_calendar_context(page) for index, month_start in enumerate(month_starts(anchor, route.lookahead_months)): if session_is_current() is not True: raise SessionExpiredOrInvalidError("Italy applicant Session changed") if index: time.sleep(DEFAULT_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 plugin._log(f"Italy Book Calendar month={month_start:%Y-%m} candidates={len(days)}") if days: _move_to_month(page, service, selected_month, 30) for item in days: slots = fetch_calendar_slots(page, service, item.day) candidate = next((slot for slot in slots if slot.is_available), None) if candidate is None: continue deadline = time.monotonic() + 5 if session_is_current() is not True: raise SessionExpiredOrInvalidError("Italy applicant Session changed") _snapshot(page, "date", service, item.day, click=True) while time.monotonic() < deadline: if session_is_current() is not True: raise SessionExpiredOrInvalidError("Italy applicant Session changed") date_state = _snapshot(page, "date", service, item.day) slot_state = _snapshot(page, "slot", service, item.day, candidate) if date_state.get("selected") and slot_state.get("candidateReady"): if not slot_state.get("selected"): _snapshot(page, "slot", service, item.day, candidate, True) return verify_precommit_selection(page, runtime_service=service, candidate=candidate) time.sleep(0.1) raise BizLogicError("Italy Calendar slot did not finish loading") if selected_month >= (end_day.year, end_day.month): break return None FINAL_PATH = "/BookingCalendar/InsertNewBooking" FINAL_ACCEPTED_LABEL = "italy-final-accepted" def _submit_final_once(plugin, route: ItaRouteConfig, slot: SlotCandidate) -> VSBookResult: result = None 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(): raise BizLogicError("Italy final booking was rejected by the website") 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 result is None: # The next Booker signal must prepare again and obtain fresh IDs. plugin._prepared_calendar = None 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), turnstilePresent: Boolean(token), turnstileReady: Boolean(token && token.value), otpValuePresent: Boolean(otp && otp.value.trim()), otpSendEnabled: Boolean(send && !send.disabled), avantiEnabled: Boolean(avanti && !avanti.disabled), }}; """ def wait_for_booking_preflight(page, *, script, after_otp=False, timeout=120, check_current=None, log=None): deadline = time.monotonic() + timeout click_after, click_attempted, pending_logged, raw = None, False, False, None while time.monotonic() < deadline: if check_current: check_current() raw = page.run_js(script) if not isinstance(raw, dict) or any(type(raw.get(key)) is not bool for key in ( "formReady", "turnstilePresent", "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") form_ready = raw["formReady"] and (after_otp or raw["otpSendEnabled"]) turnstile_present = raw["turnstilePresent"] if turnstile_present and click_after is None: click_after = time.monotonic() + TURNSTILE_PASSIVE_SECONDS elif not turnstile_present: click_after = None if form_ready and raw["turnstileReady"] and raw["avantiEnabled"]: return raw if form_ready and not raw["turnstileReady"] and log and not pending_logged: log("Italy booking verification state: " f"stage={'after_otp' if after_otp else 'before_otp'} " f"turnstile_present={turnstile_present} " f"otp_send_enabled={raw['otpSendEnabled']} " f"avanti_enabled={raw['avantiEnabled']}") pending_logged = True if (form_ready and turnstile_present and not raw["turnstileReady"] and not click_attempted and time.monotonic() >= click_after): click_attempted = True try: clicked = CloudflareBypasser( page, log=log is not None, logger=log).click_verification_button(is_dfs=False) except (PageDisconnectedError, ElementNotFoundError, SessionExpiredOrInvalidError, RateLimiteddError, NotFoundError, BizLogicError): raise except Exception as exc: raise BizLogicError("Italy booking verification click failed") from exc if log: log(f"Italy booking verification fallback click result={clicked}.") 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_widget_pending" if not raw["turnstilePresent"] 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 _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 mail_config is None: raise NotFoundError("Italy OTP mail configuration is missing") if not isinstance(mail_config, OtpMailConfig): raise BizLogicError("Italy OTP mail configuration is invalid") if not isinstance(recipient, str) or not _EMAIL_PATTERN.fullmatch(recipient.strip()): raise BizLogicError("Italy OTP recipient is invalid") mail_config.mailbox_for(recipient) def fetch_otp_from_cloud(mail_config, *, recipient, sent_at, api=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, ) except PermissionDeniedError as exc: raise BizLogicError("Italy OTP mailbox not permitted") from exc except (SessionExpiredOrInvalidError, RateLimiteddError, NotFoundError): raise 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 exc if status and int(status.group(1)) in {408, 502, 503, 504}: raise BizLogicError("Italy OTP mailbox provider timed out") from exc raise BizLogicError("Italy OTP mailbox read failed") from exc except Exception as exc: raise BizLogicError("Italy OTP mailbox read failed") from exc return parse_otp_body(body) 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 BizLogicError("Italy Calendar requires automatic OTP or an attached terminal") if not cloud and current_thread() is not main_thread(): raise BizLogicError("Italy manual OTP requires single-instance debugging on the main thread; " "use otp_mode=cloud for workers") 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(): if (plugin.page is not page or plugin.config.account.username.strip().casefold() != username or same_origin_path(page.url) != booking_path): raise SessionExpiredOrInvalidError("Italy preparation page or account changed") def click(selector): current() plugin.mouse.human_click_ele(_element_or_fail(page, selector)) script = guarded_preflight_script(route, booking_path) wait_for_booking_preflight(page, script=script, check_current=current, log=plugin._log) sent_at = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S") click("#otp-send") plugin._log(f"Italy {role} OTP sent.") if cloud: otp = fetch_otp_from_cloud(route.mail, recipient=username, sent_at=sent_at) else: otp = input("Italy booking OTP: ").strip() if not re.fullmatch(r"[0-9]{4,12}", otp): raise BizLogicError("Italy OTP format rejected") current() _enter_otp(plugin, otp) wait_for_booking_preflight( page, script=script, after_otp=True, check_current=current, log=plugin._log) 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(plugin, page, 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 BizLogicError("Italy authentication requires Radware browser validation") @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): if "You are being rate limited" in self.page.html: raise RateLimiteddError("Italy authentication rate limited") raise BizLogicError("Italy authentication was blocked") @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): raise BizLogicError("Italy authentication server is temporarily unavailable") @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", CALENDAR_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"}): 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 as exc: raise BizLogicError("Italy appointment type requires a routing_key") 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 _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 (AttributeError, TypeError): return False def _record_calendar_prepared_route(self, route, *, role): self._prepared_calendar = None self._prepared_calendar = _PreparedCalendar( self.config.account.username.strip().casefold(), route.routing_key, read_calendar_context(self.page)[0], self.page.tab_id, role) def _require_calendar_context(self, route, *, role, username=None): if not self._calendar_context_matches(route, role=role, username=username): raise SessionExpiredOrInvalidError("Italy Calendar Session changed") if read_calendar_context(self.page)[0] != self._prepared_calendar.runtime_service: raise SessionExpiredOrInvalidError("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): try: 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 = HumanMouse(self.page, debug=True) width, height = self.page.rect.viewport_size self.mouse.move(random.randint(10, width - 10), random.randint(10, height - 10)) flow = ItaLoginFlow(self) flow.run(max_steps=40, timeout=120) except PageDisconnectedError as exc: raise SessionExpiredOrInvalidError("Italy browser disconnected during session creation") from exc except ElementNotFoundError as exc: raise NotFoundError("Italy login element is missing") from exc except (SessionExpiredOrInvalidError, RateLimiteddError, NotFoundError, BizLogicError): raise except Exception as exc: raise BizLogicError(f"Italy session creation failed: {type(exc).__name__}") from exc def query(self, apt_type): try: if self.free_config.get("query_mode", CALENDAR_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 self._prepared_calendar is None or same_origin_path(self.page.url) != CALENDAR_PATH: if not self._enter_service(route): return VSQueryResult(success=False, apt_type=apt_type, availability_status=AvailabilityStatus.NoneAvailable) self._prepared_calendar = None prepare_calendar(self, route, role="query") if self._prepared_calendar is not None and self._prepared_calendar.role == "book": self._prepared_calendar = replace( self._require_calendar_context(route, role="book"), role="query") self._require_calendar_context(route, role="query") probes = scan_until_first_available_month(self, route) available = next((days for days in probes if days), ()) 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=[])]) except PageDisconnectedError as exc: raise SessionExpiredOrInvalidError("Italy browser disconnected during Query") from exc except ElementNotFoundError as exc: raise NotFoundError("Italy Query element is missing") from exc except (SessionExpiredOrInvalidError, RateLimiteddError, NotFoundError, BizLogicError): raise except Exception as exc: raise BizLogicError(f"Italy Query failed: {type(exc).__name__}") from exc def _enter_service(self, route) -> bool: 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() 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 False # The service explicitly reports no availability. 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 True def book(self, slot_info, user_inputs=None): try: 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 not None and self._prepared_calendar.role == "query": self._prepared_calendar = replace( self._require_calendar_context(route, role="query", username=username), role="book") if self._prepared_calendar is None: if not self._enter_service(route): 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, route, start, end, session_is_current=lambda: self._calendar_context_matches( route, role="book", username=username), ) 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) except PageDisconnectedError as exc: raise SessionExpiredOrInvalidError("Italy browser disconnected during Book") from exc except ElementNotFoundError as exc: raise NotFoundError("Italy Book element is missing") from exc except (SessionExpiredOrInvalidError, RateLimiteddError, NotFoundError, BizLogicError): raise except Exception as exc: raise BizLogicError(f"Italy Book failed: {type(exc).__name__}") from exc def cleanup(self): if getattr(self, "keep_browser_on_error", False): self._log("Italy debug browser retained after test failure.") return 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