"""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 base64 import b64encode 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 plugin import IPlugin from core_types import ( AvailabilityStatus, BizLogicError, NotFoundError, DateAvailability, PermissionDeniedError, PluginStatus, RateLimiteddError, SessionExpiredOrInvalidError, BookResult, QueryResult, Proxy, utc_now_str, ) from toolkit.cloud_api import CloudApi from utils.cloudflare_bypass_for_scraping import CloudflareBypasser from utils.flow_engine import BaseFlow, done, guard, on from utils.keyboard import HumanKeyboard, Key from utils.mouse import HumanMouse, MOUSE_PROFILES from utils.plugin_utils import ( cleanup_browser_workspace, filter_dates_in_range, init_chromium_options, perform_browser_fetch, save_screenshot, ) from utils.scroll import HumanScroll 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" PRENOTAMI_FORWARDED_OTP_SENDER = "noreply-prenotami at 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 additional_fields: Mapping[str, AdditionalFieldConfig] = field(default_factory=dict) @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, ) ADDITIONAL_FIELD_KINDS = ("text", "date", "select", "file") def _parse_additional_fields(raw, path): """An optional per-desk map of {site description: how to fill it}. Absent means "this desk has no extra form fields", which keeps every desk that predates this option byte-for-byte identical. """ if raw is None: return {} fields, parsed = _mapping(raw, path), {} for descrizione, spec in fields.items(): name = _text(descrizione, path) spec = _mapping(spec, f"{path}.{name}") kind = _text(spec.get("kind"), f"{path}.{name}.kind") if kind not in ADDITIONAL_FIELD_KINDS: raise BizLogicError(f"Italy {path}.{name}.kind is unsupported") placeholder = spec.get("placeholder") parsed[name] = AdditionalFieldConfig( _text(spec.get("selector"), f"{path}.{name}.selector"), kind, _text(spec.get("value_from"), f"{path}.{name}.value_from"), None if placeholder is None else _text(placeholder, f"{path}.{name}.placeholder"), ) return parsed 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 ( 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"), sender=mail_raw.get("sender", PRENOTAMI_OTP_SENDER), ) 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, _parse_additional_fields(form.get("additional_fields"), f"{key}.form.additional_fields"), ) 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 === 'service') return {path: location.pathname, service}; if (mode === 'context' || mode === 'anchor') { const dates = all('.events a[data-date]').map(item => item.dataset.date).filter(Boolean); const fallbackDay = dates.length > 0 ? dates[0] : ''; const monthSwitch = document.querySelector('#datetimepicker th.picker-switch, .bootstrap-datetimepicker-widget th.picker-switch, section.calendario .picker-switch'); const monthText = monthSwitch ? (monthSwitch.textContent || '').trim() : ''; return {path: location.pathname, service, day: selectedDay, fallbackDay, monthText}; } 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_service(page): if same_origin_path(page.url) != CALENDAR_PATH: raise BizLogicError("Italy Calendar page is not current") raw = page.run_js(CALENDAR_DOM_SCRIPT, "service", "", "", "", "", 0, False) if not isinstance(raw, dict) or raw.get("path") != CALENDAR_PATH: raise BizLogicError("Italy Calendar service is not ready") raw_service = raw.get("service") if not raw_service: raise BizLogicError("Italy Calendar service is not ready") return _runtime_id(raw_service) 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_service(page) except BizLogicError as exc: if exc.message != "Italy Calendar service is not ready": raise 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") _CALENDAR_MONTH_NAMES = { "gennaio": 1, "febbraio": 2, "marzo": 3, "aprile": 4, "maggio": 5, "giugno": 6, "luglio": 7, "agosto": 8, "settembre": 9, "ottobre": 10, "novembre": 11, "dicembre": 12, "january": 1, "february": 2, "march": 3, "april": 4, "may": 5, "june": 6, "july": 7, "august": 8, "september": 9, "october": 10, "november": 11, "december": 12, } _MONTH_WORD_RE = re.compile( r"\b(" + "|".join(_CALENDAR_MONTH_NAMES.keys()) + r")\b", re.IGNORECASE, ) _YEAR_RE = re.compile(r"\b(20\d{2})\b") def _parse_month_text_anchor(text: str): if not text: return None month_match = _MONTH_WORD_RE.search(text) if not month_match: return None month_num = _CALENDAR_MONTH_NAMES[month_match.group(1).lower()] year_match = _YEAR_RE.search(text) if year_match: return date(int(year_match.group(1)), month_num, 1) return None def _extract_calendar_anchor_from_raw(raw: dict) -> date: day_str = str(raw.get("day") or "").strip() if day_str: try: return date.fromisoformat(day_str) except ValueError: raise BizLogicError("Italy Calendar date format changed") from None month_text = str(raw.get("monthText") or "").strip() if month_text: parsed_anchor = _parse_month_text_anchor(month_text) if parsed_anchor: return parsed_anchor fallback_day_str = str(raw.get("fallbackDay") or "").strip() if fallback_day_str: try: return date.fromisoformat(fallback_day_str).replace(day=1) except ValueError: raise BizLogicError("Italy Calendar date format changed") from None raise BizLogicError("Italy Calendar anchor is not ready") def read_calendar_anchor(page) -> date: if same_origin_path(page.url) != CALENDAR_PATH: raise BizLogicError("Italy Calendar page is not current") raw = page.run_js(CALENDAR_DOM_SCRIPT, "anchor", "", "", "", "", 0, False) if not isinstance(raw, dict) or raw.get("path") != CALENDAR_PATH: raise BizLogicError("Italy Calendar anchor is not ready") return _extract_calendar_anchor_from_raw(raw) def wait_for_calendar_anchor(plugin, page, timeout=5) -> date: deadline = time.monotonic() + timeout while time.monotonic() < deadline: if plugin.page is not page: raise SessionExpiredOrInvalidError("Italy applicant Session changed") try: return read_calendar_anchor(page) except BizLogicError as exc: if exc.message != "Italy Calendar anchor is not ready": raise time.sleep(0.1) continue raise BizLogicError("Italy Calendar anchor did not initialize") 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") service = _runtime_id(raw.get("service")) anchor = _extract_calendar_anchor_from_raw(raw) return service, anchor 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, current = read_calendar_context(page) anchor = plugin._prepared_calendar.initial_month or current 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(plugin, service, target, timeout): page = plugin.page 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") plugin.mouse.human_click_ele(control) 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) selected_month = month_start.year, month_start.month _move_to_month(plugin, service, selected_month, 30) inventory = fetch_calendar_month(page, service, month_start) days = available_calendar_days(inventory, start_day=start_day, end_day=end_day) plugin._log(f"Italy Book Calendar month={month_start:%Y-%m} candidates={len(days)}") 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) -> BookResult: 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 = BookResult(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 BOOKING_CONTRACT_FIELDS = ( "pathMatch", "form", "bookingControl", "bookingOption", "purposeControl", "purposeOption", "privacy", "otpInput", "otpSend", "avanti", ) def wait_for_booking_contract(page, *, script, timeout=10): deadline, missing = time.monotonic() + timeout, list(BOOKING_CONTRACT_FIELDS) while time.monotonic() < deadline: raw = page.run_js(script) if not isinstance(raw, dict) or any(type(raw.get(key)) is not bool for key in BOOKING_CONTRACT_FIELDS): raise BizLogicError("Italy booking contract probe returned invalid data") missing = [key for key in BOOKING_CONTRACT_FIELDS if not raw[key]] if not missing: return raw time.sleep(0.25) raise BizLogicError(f"Italy booking form structure changed: {','.join(missing)}") def guarded_preflight_script(route: ItaRouteConfig, booking_path: str, *, readonly=False) -> 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), "readonly": readonly} 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); const recognized = Boolean(option && item.labels.map(norm).includes(norm(option.textContent))); if (!cfg.readonly && recognized && control.value !== item.value) {{ control.value = item.value; control.dispatchEvent(new Event('change', {{bubbles: true}})); }} return {{control: Boolean(control), option: recognized, selected: Boolean(recognized && control.value === item.value)}}; }} const booking = choose(cfg.booking), purpose = choose(cfg.purpose); const privacy = document.querySelector('#PrivacyCheck'); if (!cfg.readonly && 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"]'); const pathMatch = location.pathname === cfg.path; return {{ pathMatch, form: Boolean(form), bookingControl: booking.control, bookingOption: booking.option, purposeControl: purpose.control, purposeOption: purpose.option, privacy: Boolean(privacy), otpInput: Boolean(otp), otpSend: Boolean(send), avanti: Boolean(avanti), formReady: Boolean(pathMatch && form && booking.selected && purpose.selected && 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() states = getattr(page, "states", None) if states and getattr(states, "has_alert", None) is True: try: page.handle_alert(accept=True) except Exception: pass 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 SET_ADDITIONAL_SELECT_SCRIPT = """ const control = document.getElementById(arguments[0]); if (!control || control.disabled) return false; control.value = arguments[1]; control.dispatchEvent(new Event('change', {bubbles: true})); return control.value === arguments[1]; """ _DESCRIZIONE_NAME = re.compile(r"^(DatiAddizionaliPrenotante|DocumentiPrenotante)\[(\d+)\]\._Descrizione$") # The booking page states: "E' possibile inserire file in formato PDF di massimo un 1MB". PDF_MAGIC = b"%PDF-" PDF_MAX_BYTES = 1024 * 1024 def _check_upload_bytes(descrizione, path): with open(path, "rb") as handle: payload = handle.read() if not payload.startswith(PDF_MAGIC): raise BizLogicError(f"Italy booking file {descrizione!r} is not a PDF") if len(payload) > PDF_MAX_BYTES: raise BizLogicError(f"Italy booking file {descrizione!r} exceeds 1MB") def _format_booking_dates(page, route, inputs): values = dict(inputs or {}) for descrizione, spec in route.additional_fields.items(): if spec.kind != "date" or not values.get(spec.value_from): continue element = _element_or_fail(page, f"#{spec.selector}") input_type = (element.attr("type") or "").strip().casefold() placeholder = (element.attr("placeholder") or "").strip().casefold() if input_type == "date" or (input_type == "text" and placeholder == "yyyy-mm-dd"): output_format = "%Y-%m-%d" elif input_type == "text" and placeholder == "dd/mm/yyyy": output_format = "%d/%m/%Y" else: raise BizLogicError(f"Italy booking date field {descrizione!r} has an unknown format") raw = str(values[spec.value_from]).strip() try: if re.fullmatch(r"\d{4}-\d{2}-\d{2}", raw): parsed = date.fromisoformat(raw) elif re.fullmatch(r"\d{2}/\d{2}/\d{4}", raw): parsed = datetime.strptime(raw, "%d/%m/%Y").date() else: raise ValueError except ValueError: raise BizLogicError(f"Italy booking date field {descrizione!r} is invalid") from None values[spec.value_from] = parsed.strftime(output_format) return values def fill_booking_form(plugin, route: ItaRouteConfig, inputs) -> None: """Write explicit values only; prepare_calendar selects the source by role.""" if not route.additional_fields: return inputs = inputs or {} page = plugin.page mouse = getattr(plugin, "mouse", None) keyboard = getattr(plugin, "keyboard", None) scroll = getattr(plugin, "scroll", None) # 仅在非 Mock 的真实 CDP 浏览器中按需初始化拟人化组件 if hasattr(page, "run_cdp") and type(page).__name__ != "Mock": if mouse is None: try: profile_name = random.choice(list(MOUSE_PROFILES.keys())) if MOUSE_PROFILES else "average_joe" mouse = plugin.mouse = HumanMouse(page, timing=MOUSE_PROFILES.get(profile_name), debug=True) except Exception: pass if keyboard is None: try: keyboard = plugin.keyboard = HumanKeyboard(page) except Exception: pass if scroll is None: try: scroll = plugin.scroll = HumanScroll(page) except Exception: pass is_human = bool(mouse and keyboard and hasattr(page, "run_cdp") and type(page).__name__ != "Mock") for descrizione, spec in route.additional_fields.items(): element = _element_or_fail(page, f"#{spec.selector}") value = inputs.get(spec.value_from) if not value: raise BizLogicError( f"Italy booking field {descrizione!r} has no value ({spec.value_from})") value = str(value) if spec.kind == "file": if value.startswith("http://") or value.startswith("https://"): # Chrome uploads from its own filesystem, so a remote document has to # be fetched into this instance's workspace first. import requests response = requests.get(value, timeout=30) if response.status_code != 200: raise BizLogicError( f"Italy booking file {descrizione!r} could not be downloaded " f"({response.status_code})") value = os.path.join(plugin.root_workspace, f"upload_{spec.selector}.pdf") with open(value, "wb") as handle: handle.write(response.content) elif not os.path.isfile(value): raise NotFoundError(f"Italy booking file {descrizione!r} is missing: {value}") # The page states PDF only, max 1MB; check here so a bad document fails # before an OTP is spent on a form that cannot be submitted. _check_upload_bytes(descrizione, value) if is_human: if scroll: scroll.scroll_to_element(element, humanize=True) time.sleep(random.uniform(0.3, 0.6)) try: mid_x, mid_y = element.rect.viewport_midpoint if mid_x and mid_y: mouse.move(mid_x, mid_y, humanize=True) except Exception: pass time.sleep(random.uniform(0.6, 1.1)) # 模拟文件选择框耗时 element.input(value) if is_human: time.sleep(random.uniform(0.4, 0.8)) time.sleep(random.uniform(0.6, 1.2)) # 字段切换停顿 continue # The site truncates silently at maxlength=100, so a longer value would be # written but submitted short. if len(value) > 100: raise BizLogicError( f"Italy booking field {descrizione!r} exceeds the site's 100 character limit") if spec.kind == "date" and (element.attr("type") or "").strip().casefold() == "date": # Native date values are ISO; localized key typing and Enter are unsafe here. written = element.run_js(""" if (this.disabled || this.readOnly) return false; this.value = arguments[0]; this.dispatchEvent(new Event("input", {bubbles: true})); this.dispatchEvent(new Event("change", {bubbles: true})); return this.value === arguments[0] && this.validity.valid; """, value) elif is_human: if scroll: scroll.scroll_to_element(element, humanize=True) time.sleep(random.uniform(0.3, 0.6)) if spec.kind == "select": try: mouse.human_click_ele(element) except Exception: element.click() time.sleep(random.uniform(0.5, 1.0)) # 人类视线浏览下拉项 try: opt = element.ele(f'@value={value}', timeout=0.5) if opt: mouse.human_click_ele(opt) else: element.select.by_value(value) except Exception: element.select.by_value(value) written = page.run_js(SET_ADDITIONAL_SELECT_SCRIPT, spec.selector, value) time.sleep(random.uniform(0.4, 0.8)) elif spec.kind == "date": # Flatpickr 日历输入(点击打开日历 -> 观察 -> 拟人化击键输入 -> 回车确认 -> 状态同步) try: mouse.human_click_ele(element) except Exception: element.click() time.sleep(random.uniform(0.5, 0.9)) # 观察日历弹层 try: keyboard.hotkey(Key.CONTROL, Key.A) time.sleep(random.uniform(0.1, 0.2)) keyboard.press(Key.BACKSPACE) time.sleep(random.uniform(0.1, 0.2)) except Exception: pass keyboard.type_text(value, humanize=True) time.sleep(random.uniform(0.3, 0.6)) keyboard.press(Key.ENTER) time.sleep(random.uniform(0.3, 0.5)) page.run_js(''' const sel = arguments[0]; const val = arguments[1]; const el = document.getElementById(sel); if (el) { el.value = val; if (el._flatpickr) { try { el._flatpickr.setDate(val, true); } catch(e){} } el.dispatchEvent(new Event('change', {bubbles: true})); el.dispatchEvent(new Event('input', {bubbles: true})); } ''', spec.selector, value) time.sleep(random.uniform(0.4, 0.8)) written = element.run_js("return this.value === arguments[0];", value) if written is not True: element.input(value, clear=True) written = element.run_js("return this.value === arguments[0];", value) else: # 文本输入框 try: mouse.human_click_ele(element) except Exception: element.click() time.sleep(random.uniform(0.3, 0.6)) # 准备打字停顿 try: keyboard.hotkey(Key.CONTROL, Key.A) time.sleep(random.uniform(0.1, 0.2)) keyboard.press(Key.BACKSPACE) time.sleep(random.uniform(0.1, 0.2)) except Exception: pass keyboard.type_text(value, humanize=True) time.sleep(random.uniform(0.4, 0.8)) # 视觉核验停顿 written = element.run_js("return this.value === arguments[0];", value) if written is not True: element.input(value, clear=True) written = element.run_js("return this.value === arguments[0];", value) # 字段切换之间的认知停顿 (0.6s ~ 1.2s) time.sleep(random.uniform(0.6, 1.2)) else: if spec.kind == "select": written = page.run_js(SET_ADDITIONAL_SELECT_SCRIPT, spec.selector, value) else: element.click() element.input(value, clear=True) written = element.run_js("return this.value === arguments[0];", value) if written is not True: raise BizLogicError(f"Italy booking field {descrizione!r} could not be written") for descrizione in _required_unconfigured(page, route): raise BizLogicError(f"Italy required booking field is not configured: {descrizione}") def _required_unconfigured(page, route): """Site-declared required fields that this route config does not fill. Additional fields declare themselves through `[N]._Obbligatorio`; the document uploads carry no such marker, so their presence in the form is what makes them required. `purpose_index` is exempt because the purpose control is configured separately, not as an additional field. """ purpose_index = None if route.purpose is not None: match = re.fullmatch(r"ddls_(\d+)", route.purpose.control_id) if match: purpose_index = match.group(1) missing = [] for element in page.eles('css:input[type="hidden"]'): match = _DESCRIZIONE_NAME.fullmatch(element.attr("name") or "") if not match: continue group, index = match.group(1), match.group(2) descrizione = (element.attr("value") or "").strip() if not descrizione: continue if group == "DatiAddizionaliPrenotante": if index == purpose_index: continue obbligatorio = page.ele( f'css:input[name="DatiAddizionaliPrenotante[{index}]._Obbligatorio"]', timeout=1) if obbligatorio is None or (obbligatorio.attr("value") or "").strip() != "True": continue if descrizione not in route.additional_fields: missing.append(descrizione) return missing # ============================================================================= # TEMPORARY diagnostic capture - DELETE THIS WHOLE BLOCK (and its call sites) # once the Ireland desk is understood. It exists only because a slot window is # rare and arrives unattended, so the one visit has to leave a full record. # # !! The capture directory holds SECRETS (session cookies, OTP codes, the # !! applicant's real values). `data/ita_capture/` is gitignored - keep it that # !! way and delete the files once the findings are written down. # ============================================================================= def _capture(plugin): """The diagnostic recorder, or a no-op stand-in when capture is off.""" return getattr(plugin, "_capture", None) or _NULL_CAPTURE ITA_CAPTURE_DIR = "data/ita_capture" ITA_CAPTURE_HTML_BYTES = 2_000_000 ITA_CAPTURE_BODY_BYTES = 500_000 ITA_CAPTURE_PACKETS = 2_000 ITA_CAPTURE_STAGES = ("services", "after_prenota", "booking_form", "before_otp", "after_otp", "calendar") class _NullCapture: """Swallows capture calls when the recorder is absent or disabled.""" enabled = False def start(self): pass def drain(self): pass def snapshot(self, stage, note="", *, force=False): pass def error(self, stage, exc): pass def write(self): pass _NULL_CAPTURE = _NullCapture() class ItaCapture: """Records the one-off visit: page HTML, every request/response, and errors.""" def __init__(self, plugin): self.plugin = plugin config = plugin.free_config.get("capture") config = config if isinstance(config, Mapping) else {} self.enabled = config.get("enabled") is True stages = config.get("html_stages") self.stages = tuple(stages) if isinstance(stages, (list, tuple)) else ITA_CAPTURE_STAGES self.packets = [] self.notes = [] self.listening = False def _run_dir(self): return os.path.abspath(os.path.join(ITA_CAPTURE_DIR, self.plugin.instance_id)) def start(self): if not self.enabled or self.listening or not self.plugin.page: return try: self.plugin.page.listen.start(targets=True) self.listening = True self.plugin._log("Italy capture: network recording started") except Exception as exc: # diagnostics must never break the booking path self.notes.append(f"listen.start failed: {type(exc).__name__}: {exc}") def drain(self): if not self.listening: return try: while len(self.packets) < ITA_CAPTURE_PACKETS: packet = self.plugin.page.listen.wait(count=1, timeout=0.05, raise_err=False) # Listener.wait returns False (not None) once the timeout passes with an # empty queue, and only ever yields one packet because fit_count is on. # Anything without a url is not a packet, so stop draining. if not getattr(packet, "url", None): break self.packets.append(self._describe(packet)) except Exception as exc: self.notes.append(f"drain failed: {type(exc).__name__}: {exc}") def _describe(self, packet): record = {"url": packet.url, "method": packet.method, "resource_type": packet.resourceType, "frame_id": packet.frameId} try: request = packet.request if request is not None: record["request_headers"] = dict(request.headers or {}) # CDP can omit cookie extra-info even when request/body are present. record["request_cookies"] = ( request.cookies if request.extra_info.associatedCookies is not None else None) record["request_params"] = request.params post = request.postData record["post_data"] = ( post[:ITA_CAPTURE_BODY_BYTES] if isinstance(post, str) else post) except Exception as exc: record["request_error"] = f"{type(exc).__name__}: {exc}" try: response = packet.response if response is not None: record["status"] = response.status record["mime_type"] = response.mimeType record["response_headers"] = dict(response.headers or {}) body = response.body if isinstance(body, bytes): record["body"] = b64encode(body[:ITA_CAPTURE_BODY_BYTES]).decode("ascii") record["body_encoding"] = "base64" record["body_bytes"] = len(body) else: record["body"] = ( body[:ITA_CAPTURE_BODY_BYTES] if isinstance(body, str) else body) except Exception as exc: record["response_error"] = f"{type(exc).__name__}: {exc}" return record def snapshot(self, stage, note="", *, force=False): """Record the page as a person would see it, plus the values the site carries.""" if not self.enabled or not self.plugin.page or (not force and stage not in self.stages): return try: run = self._run_dir() os.makedirs(run, exist_ok=True) # Keep every PRENOTA result so a later query cannot overwrite its evidence. name = (f"{stage}_{datetime.now(timezone.utc):%Y%m%dT%H%M%S_%fZ}" if stage == "after_prenota" else stage) html = self.plugin.page.html or "" with open(os.path.join(run, f"{name}.html"), "w", encoding="utf-8") as handle: handle.write(html[:ITA_CAPTURE_HTML_BYTES]) state = self._state(stage, note) screenshot = save_screenshot( self.plugin.page, self.plugin.instance_id, f"capture_{name}", save_dir=run, full_page=True) state["screenshot"] = os.path.basename(screenshot) if screenshot else None with open(os.path.join(run, f"{name}.state.json"), "w", encoding="utf-8") as handle: json.dump(state, handle, ensure_ascii=False, indent=1) except Exception as exc: self.notes.append(f"snapshot {stage} failed: {type(exc).__name__}: {exc}") def _state(self, stage, note): page = self.plugin.page return {"stage": stage, "note": note, "at": datetime.now(timezone.utc).isoformat(), "url": page.url, "hidden_fields": self._hidden_fields(page), "selects": self._selects(page)} @staticmethod def _hidden_fields(page): fields = {} for element in page.eles('css:input[type="hidden"]'): name = element.attr("name") if name: fields[name] = element.attr("value") return fields @staticmethod def _selects(page): selects = {} for element in page.eles("css:select"): element_id = element.attr("id") or element.attr("name") if element_id: selects[element_id] = element.attr("value") return selects def error(self, stage, exc): if not self.enabled: return self.drain() self.snapshot(stage, note=f"{type(exc).__name__}: {exc}", force=True) def write(self): """Flush everything. Never raises - this runs while an error is propagating.""" if not self.enabled: return try: self.drain() try: if self.listening: self.plugin.page.listen.stop() self.listening = False except Exception as exc: self.notes.append(f"listen.stop failed: {type(exc).__name__}: {exc}") run = self._run_dir() os.makedirs(run, exist_ok=True) payload = {"instance_id": self.plugin.instance_id, "account": self.plugin.config.account.username if self.plugin.config else None, "free_config": self.plugin.free_config, "packets": self.packets, "notes": self.notes} with open(os.path.join(run, "capture.json"), "w", encoding="utf-8") as handle: json.dump(payload, handle, ensure_ascii=False, indent=1) self.plugin._log(f"Italy capture: {len(self.packets)} records written to {run}") except Exception as exc: self.notes.append(f"write failed: {type(exc).__name__}: {exc}") try: self.plugin._log(f"Italy capture write failed: {type(exc).__name__}: {exc}") except Exception: pass def _enter_otp(plugin, otp): field = _element_or_fail(plugin.page, "#otp-input") if getattr(plugin, "mouse", None): plugin.mouse.human_click_ele(field) else: field.click() if getattr(plugin, "keyboard", None): time.sleep(random.uniform(0.3, 0.6)) try: plugin.keyboard.hotkey(Key.CONTROL, Key.A) time.sleep(0.1) plugin.keyboard.press(Key.BACKSPACE) time.sleep(0.1) except Exception: pass plugin.keyboard.type_text(otp, humanize=True) time.sleep(random.uniform(0.4, 0.8)) else: field.input(otp, clear=True) if field.run_js("return this.value === arguments[0];", otp) is not True: 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 CloudApi.Instance()).fetch_mail_content( email=mail_config.mailbox_for(recipient), sender=mail_config.sender.strip(), 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, inputs=None): 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) if getattr(plugin, "_capture", None): _capture(plugin).snapshot("before_otp", note=f"role={role}, form ready") # Query uses only the dedicated query profile. Book never falls back to it. form_inputs = ({spec.value_from: spec.placeholder for spec in route.additional_fields.values()} if role == "query" else inputs) fill_booking_form(plugin, route, _format_booking_dates(page, route, form_inputs)) if getattr(plugin, "_capture", None): _capture(plugin).snapshot("form_filled", note=f"extra fields written, role={role}") 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) wait_for_calendar_anchor(plugin, page) plugin._record_calendar_prepared_route(route, role=role, additional_inputs=form_inputs) if getattr(plugin, "_capture", None): _capture(plugin).snapshot("calendar", note=f"reached the calendar, role={role}") _capture(plugin).write() 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 # The applicant inputs the extra form fields were last written from. This is what # stops a calendar prepared for querying (placeholder values) from being reused to # place a real booking. additional_inputs: Mapping[str, Any] | None = None initial_month: date | None = None 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) if getattr(self.plugin, "keyboard", None): time.sleep(random.uniform(0.2, 0.5)) try: self.plugin.keyboard.hotkey(Key.CONTROL, Key.A) time.sleep(0.1) self.plugin.keyboard.press(Key.BACKSPACE) time.sleep(0.1) except Exception: pass self.plugin.keyboard.type_text(value, humanize=True) time.sleep(random.uniform(0.4, 0.8)) else: 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(IPlugin): 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.mouse = self.keyboard = self.scroll = 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 self._inspection_hold_reason = None self._capture = None # TEMPORARY diagnostic capture, see ItaCapture 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) inspection = self.free_config.get("inspection_mode", 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") if type(inspection) is not bool: raise BizLogicError("Italy inspection_mode must be a boolean") local_proxy_port = self.free_config.get("local_proxy_port") if local_proxy_port is not None and ( type(local_proxy_port) is not int or not 1 <= local_proxy_port <= 65535): raise BizLogicError("Italy local_proxy_port must be an integer from 1 to 65535") self._prepared_calendar = None def _browser_proxy(self): local_proxy_port = self.free_config.get("local_proxy_port") if local_proxy_port is not None: # Standalone still fetches a pool proxy; this only changes the browser route. return Proxy(proto="http", ip="127.0.0.1", port=local_proxy_port) return self.config.proxy 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, additional_inputs=None): self._prepared_calendar = None values = {spec.value_from: str((additional_inputs or {}).get(spec.value_from, "")) for spec in route.additional_fields.values()} service, anchor = read_calendar_context(self.page) self._prepared_calendar = _PreparedCalendar( self.config.account.username.strip().casefold(), route.routing_key, service, self.page.tab_id, role, values, anchor.replace(day=1)) def _discard_stale_booking_form(self, route, inputs): """A query form or changed applicant values require a fresh booking form.""" state = self._prepared_calendar if not route.additional_fields or state is None: return if (state.role != "book" or state.additional_inputs is None or any( not inputs.get(spec.value_from) or state.additional_inputs.get(spec.value_from) != str(inputs[spec.value_from]) for spec in route.additional_fields.values())): self._prepared_calendar = None 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 _freeze_for_inspection(self, exc): if self.free_config.get("inspection_mode") is not True or not self.page: return reason = getattr(exc, "message", None) or str(exc) self._inspection_hold_reason = f"{type(exc).__name__}: {reason}" self.keep_browser_on_error = True self._log(f"Italy inspection hold activated: {self._inspection_hold_reason}") def _raise_if_inspection_held(self): reason = getattr(self, "_inspection_hold_reason", None) if reason: raise BizLogicError(f"Italy inspection hold: {reason}") 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") proxy = self._browser_proxy() local_proxy_port = self.free_config.get("local_proxy_port") if local_proxy_port is not None: self._log(f"Using local HTTP proxy at 127.0.0.1:{local_proxy_port}") options, self.tunnel = init_chromium_options( user_data_path=self.user_data_path, username_seed=self.config.account.username, proxy=proxy, headless=False, window_size="1920,1080", logger=self._log, ) self.page = ChromiumPage(options) try: page_set = getattr(self.page, "set", None) if page_set and hasattr(page_set, "auto_handle_alert"): self.page.set.auto_handle_alert(accept=True) if hasattr(self.page, "run_cdp"): self.page.run_cdp( "Page.addScriptToEvaluateOnNewDocument", source=""" window.alert = function(msg){ console.log('[Alert suppressed]:', msg); }; try { Object.defineProperty(navigator, 'webdriver', {get: () => undefined}); } catch(e){} """ ) except Exception as exc: self._log(f"Italy CDP alert & stealth suppression non-fatal: {exc}") self.page.get(self.ita_url) # TEMPORARY diagnostic capture (see ItaCapture); no-op unless configured. self._capture = ItaCapture(self) self.scroll = HumanScroll(self.page) profile_name = random.choice(list(MOUSE_PROFILES.keys())) if MOUSE_PROFILES else "average_joe" self._log(f"[HumanMouse] profile selected: {profile_name}") # Drawing a debug canvas can lose its JS context during navigation and abort login. self.mouse = HumanMouse(self.page, timing=MOUSE_PROFILES.get(profile_name), debug=False) self.keyboard = HumanKeyboard(self.page) width, height = self.page.rect.viewport_size self.mouse.move(random.randint(10, width - 10), random.randint(10, height - 10), humanize=True) 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: error = NotFoundError("Italy login element is missing") self._freeze_for_inspection(error) raise error from exc except (SessionExpiredOrInvalidError, RateLimiteddError, NotFoundError, BizLogicError) as exc: self._freeze_for_inspection(exc) raise except Exception as exc: error = BizLogicError(f"Italy session creation failed: {type(exc).__name__}") self._freeze_for_inspection(error) raise error from exc def query(self, apt_type): self._raise_if_inspection_held() 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 QueryResult(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 QueryResult( 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 QueryResult( 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: error = NotFoundError("Italy Query element is missing") self._freeze_for_inspection(error) raise error from exc except (SessionExpiredOrInvalidError, RateLimiteddError, NotFoundError, BizLogicError) as exc: self._freeze_for_inspection(exc) if getattr(self, "_capture", None): self._capture.error("query_failed", exc) self._capture.write() raise except Exception as exc: error = BizLogicError(f"Italy Query failed: {type(exc).__name__}") self._freeze_for_inspection(error) if getattr(self, "_capture", None): self._capture.error("query_failed", exc) self._capture.write() raise error 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() capture = _capture(self) capture.start() 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}") click_started_at = datetime.now(timezone.utc).isoformat() try: 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("#WlNotAvailable", timeout=1) notice = self.page.ele(".jconfirm-content", timeout=1) marker_text = (marker.attr("value") if marker else "") or "" notice_text = (notice.text if notice else "") or "" text = (marker_text or notice_text).strip().casefold() no_availability = "posti disponibili" in text outcome = "no availability" if no_availability else "unknown service response" else: outcome = "booking page" if path == booking_path else "unexpected destination" except Exception as exc: # Diagnostics preserve the original failure for the framework to handle. capture.snapshot("after_prenota", force=True, note=f"click_started_at={click_started_at}; click/navigation failed: " f"{type(exc).__name__}: {exc}") capture.write() raise capture.snapshot("after_prenota", note=f"click_started_at={click_started_at}; PRENOTA click returned; {outcome}") if path == "/Services": if no_availability: capture.write() return False # The service explicitly reports no availability. raise BizLogicError( "Italy service returned an unknown response", extra={"safe_diagnostic": { "path": path, "availability_marker_present": bool(marker), "notice_present": bool(notice), }}, ) if path == booking_path and getattr(self, "_capture", None): self._capture.snapshot("booking_form", note="entered booking page before contract validation") if path != booking_path or not self.page.ele("#bookingForm", timeout=2): raise BizLogicError("Italy booking form is unavailable") wait_for_booking_contract( self.page, script=guarded_preflight_script(route, booking_path, readonly=True)) return True def book(self, slot_info, user_inputs=None): self._raise_if_inspection_held() try: if not self.page or not isinstance(slot_info, QueryResult): 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: state = self._require_calendar_context( route, role=self._prepared_calendar.role, username=username) self._discard_stale_booking_form(route, inputs) if self._prepared_calendar is not None: self._prepared_calendar = replace(state, role="book") if self._prepared_calendar is None: if not self._enter_service(route): return BookResult(success=False, label=BOOKING_RECHECK_NONE_AVAILABLE_LABEL) prepare_calendar(self, route, role="book", inputs=inputs) 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 BookResult(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) self._freeze_for_inspection( BizLogicError("Italy selected slot is waiting for final inspection")) return BookResult(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: error = NotFoundError("Italy Book element is missing") self._freeze_for_inspection(error) raise error from exc except (SessionExpiredOrInvalidError, RateLimiteddError, NotFoundError, BizLogicError) as exc: self._freeze_for_inspection(exc) if getattr(self, "_capture", None): self._capture.error("book_failed", exc) self._capture.write() raise except Exception as exc: error = BizLogicError(f"Italy Book failed: {type(exc).__name__}") self._freeze_for_inspection(error) if getattr(self, "_capture", None): self._capture.error("book_failed", exc) self._capture.write() raise error 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._browser_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