ita_plugin.py 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996
  1. """Prenot@Mi Query/Book adapter; Italy-specific production code stays here."""
  2. from __future__ import annotations
  3. import html
  4. import json
  5. import os
  6. import random
  7. import re
  8. import sys
  9. import time
  10. import uuid
  11. from base64 import b64encode
  12. from dataclasses import asdict, dataclass, field, replace
  13. from datetime import date, datetime, time as datetime_time, timezone
  14. from enum import Enum
  15. from threading import current_thread, main_thread
  16. from typing import Any, Mapping, Sequence
  17. from urllib.parse import urljoin, urlparse
  18. from DrissionPage import ChromiumPage
  19. from DrissionPage.errors import ElementNotFoundError, PageDisconnectedError
  20. from plugin import IPlugin
  21. from core_types import (
  22. AvailabilityStatus, BizLogicError, NotFoundError,
  23. DateAvailability, PermissionDeniedError, PluginStatus, RateLimiteddError,
  24. SessionExpiredOrInvalidError, BookResult, QueryResult, Proxy, utc_now_str,
  25. )
  26. from toolkit.cloud_api import CloudApi
  27. from utils.cloudflare_bypass_for_scraping import CloudflareBypasser
  28. from utils.flow_engine import BaseFlow, done, guard, on
  29. from utils.keyboard import HumanKeyboard, Key
  30. from utils.mouse import HumanMouse, MOUSE_PROFILES
  31. from utils.plugin_utils import (
  32. cleanup_browser_workspace, filter_dates_in_range, init_chromium_options,
  33. perform_browser_fetch, save_screenshot,
  34. )
  35. from utils.scroll import HumanScroll
  36. MAX_PROTOCOL_ITEMS = 512
  37. MAX_PROTOCOL_PAYLOAD_CHARS = 1_000_000
  38. MAX_LOOKAHEAD_MONTHS = 120
  39. TURNSTILE_PASSIVE_SECONDS = 5
  40. PRENOTAMI_OTP_SENDER = "noreply-prenotami@esteri.it"
  41. PRENOTAMI_FORWARDED_OTP_SENDER = "noreply-prenotami at esteri.it"
  42. _EMAIL_PATTERN = re.compile(r"^[^@\s]+@[^@\s]+\.[^@\s]+$")
  43. _OTP_PATTERN = re.compile(r"(?<!\w)OTP\s+Code\s*:\s*([0-9]{6})(?!\w)", re.I)
  44. class ItaCapability(str, Enum):
  45. SERVICES_PROBE_ONLY = "services_probe_only"
  46. SINGLE_SCHENGEN_TOURISM = "single_schengen_tourism"
  47. @dataclass(frozen=True, slots=True)
  48. class FormControlConfig:
  49. control_id: str
  50. value: str
  51. labels: tuple[str, ...]
  52. @dataclass(frozen=True, slots=True)
  53. class AdditionalFieldConfig:
  54. selector: str
  55. kind: str
  56. value_from: str
  57. placeholder: str | None = None
  58. @dataclass(frozen=True, slots=True)
  59. class OtpMailConfig:
  60. authorization_email: str | None = field(default=None, repr=False)
  61. expiry_seconds: int = 300
  62. authorization_source: str = "configured"
  63. sender: str = PRENOTAMI_OTP_SENDER
  64. def __post_init__(self):
  65. if not isinstance(self.sender, str) or self.sender.strip().casefold() not in {
  66. PRENOTAMI_OTP_SENDER, PRENOTAMI_FORWARDED_OTP_SENDER,
  67. }:
  68. raise BizLogicError("Italy mail.sender must match a Prenot@Mi OTP sender")
  69. if self.authorization_source not in {"account", "configured"}:
  70. raise BizLogicError("Italy mail.authorization_source must be account or configured")
  71. if self.authorization_source == "account" and self.authorization_email is not None:
  72. raise BizLogicError("Italy account mailbox source cannot specify authorization_email")
  73. if self.authorization_source == "configured":
  74. self.mailbox_for("")
  75. if type(self.expiry_seconds) is not int or not 1 <= self.expiry_seconds <= 300:
  76. raise BizLogicError("Italy mail.expiry_seconds must be an integer from 1 to 300")
  77. def mailbox_for(self, account_email: str) -> str:
  78. value = account_email if self.authorization_source == "account" else self.authorization_email
  79. if not isinstance(value, str) or not _EMAIL_PATTERN.fullmatch(value.strip()):
  80. raise BizLogicError("Italy mail.authorization_email must be a valid email address")
  81. return value.strip()
  82. @dataclass(frozen=True, slots=True)
  83. class ItaRouteConfig:
  84. routing_key: str
  85. service_id: int
  86. capability: ItaCapability
  87. lookahead_months: int = 0
  88. booking_type: FormControlConfig | None = None
  89. purpose: FormControlConfig | None = None
  90. mail: OtpMailConfig | None = None
  91. additional_fields: Mapping[str, AdditionalFieldConfig] = field(default_factory=dict)
  92. @dataclass(frozen=True, slots=True)
  93. class CalendarDay:
  94. day: date
  95. slots_free: int
  96. slots_remaining: int
  97. @property
  98. def is_available(self):
  99. return self.slots_free > 0
  100. @dataclass(frozen=True, slots=True)
  101. class SlotCandidate(CalendarDay):
  102. start_time: datetime_time
  103. end_time: datetime_time
  104. daily_calendar_id: str = field(repr=False)
  105. @property
  106. def time_label(self):
  107. return f"{self.start_time:%H:%M} - {self.end_time:%H:%M}"
  108. def _positive_int(value, path, maximum=None):
  109. if type(value) is not int or value <= 0:
  110. raise BizLogicError(f"Italy {path} must be a positive integer")
  111. if maximum is not None and value > maximum:
  112. raise BizLogicError(f"Italy {path} must not exceed {maximum}")
  113. return value
  114. def _mapping(value, path):
  115. if not isinstance(value, Mapping):
  116. raise BizLogicError(f"Italy {path} must be an object")
  117. return value
  118. def _text(value, path):
  119. if not isinstance(value, str) or not value.strip():
  120. raise BizLogicError(f"Italy {path} must be a non-empty string")
  121. return value.strip()
  122. def _control(form, prefix, path):
  123. labels = form.get(f"{prefix}_labels")
  124. if isinstance(labels, (str, bytes)) or not isinstance(labels, Sequence) or not labels:
  125. raise BizLogicError(f"Italy {path}.{prefix}_labels must be a non-empty list")
  126. labels = tuple(_text(value, path) for value in labels)
  127. if len(set(labels)) != len(labels):
  128. raise BizLogicError(f"Italy {path}.{prefix}_labels contains duplicates")
  129. return FormControlConfig(
  130. _text(form.get(f"{prefix}_control_id"), path),
  131. _text(form.get(f"{prefix}_value"), path),
  132. labels,
  133. )
  134. ADDITIONAL_FIELD_KINDS = ("text", "date", "select", "file")
  135. def _parse_additional_fields(raw, path):
  136. """An optional per-desk map of {site description: how to fill it}.
  137. Absent means "this desk has no extra form fields", which keeps every desk that
  138. predates this option byte-for-byte identical.
  139. """
  140. if raw is None:
  141. return {}
  142. fields, parsed = _mapping(raw, path), {}
  143. for descrizione, spec in fields.items():
  144. name = _text(descrizione, path)
  145. spec = _mapping(spec, f"{path}.{name}")
  146. kind = _text(spec.get("kind"), f"{path}.{name}.kind")
  147. if kind not in ADDITIONAL_FIELD_KINDS:
  148. raise BizLogicError(f"Italy {path}.{name}.kind is unsupported")
  149. placeholder = spec.get("placeholder")
  150. parsed[name] = AdditionalFieldConfig(
  151. _text(spec.get("selector"), f"{path}.{name}.selector"),
  152. kind,
  153. _text(spec.get("value_from"), f"{path}.{name}.value_from"),
  154. None if placeholder is None else _text(placeholder, f"{path}.{name}.placeholder"),
  155. )
  156. return parsed
  157. def resolve_route_config(configs: Mapping[str, Any], routing_key: str) -> ItaRouteConfig:
  158. configs, key = _mapping(configs, "apt_configs"), _text(routing_key, "routing_key")
  159. if key not in configs:
  160. raise NotFoundError(f"Italy apt_configs is missing route {key}")
  161. raw = _mapping(configs[key], key)
  162. capability_name = _text(raw.get("capability"), f"{key}.capability")
  163. try:
  164. capability = ItaCapability(capability_name)
  165. except ValueError as exc:
  166. raise BizLogicError(f"Italy {key}.capability is unsupported") from exc
  167. service = _positive_int(raw.get("service_id"), f"{key}.service_id")
  168. if capability is ItaCapability.SERVICES_PROBE_ONLY:
  169. return ItaRouteConfig(key, service, capability)
  170. form = _mapping(raw.get("form"), f"{key}.form")
  171. scan = _mapping(raw.get("calendar_scan"), f"{key}.calendar_scan")
  172. mail_raw = raw.get("mail")
  173. mail = None
  174. if mail_raw is not None:
  175. mail_raw = _mapping(mail_raw, f"{key}.mail")
  176. if (
  177. mail_raw.get("subject_keywords", "OTP Code"),
  178. mail_raw.get("body_keywords", "OTP Code:"),
  179. ) != ("OTP Code", "OTP Code:"):
  180. raise BizLogicError(f"Italy {key}.mail filters must match the Prenot@Mi OTP template")
  181. mail = OtpMailConfig(
  182. authorization_email=mail_raw.get("authorization_email"),
  183. expiry_seconds=mail_raw.get("expiry_seconds", 300),
  184. authorization_source=mail_raw.get("authorization_source", "configured"),
  185. sender=mail_raw.get("sender", PRENOTAMI_OTP_SENDER),
  186. )
  187. return ItaRouteConfig(
  188. key, service, capability,
  189. _positive_int(scan.get("lookahead_months"), f"{key}.calendar_scan", MAX_LOOKAHEAD_MONTHS),
  190. _control(form, "booking_type", f"{key}.form"),
  191. _control(form, "purpose", f"{key}.form"),
  192. mail,
  193. _parse_additional_fields(form.get("additional_fields"), f"{key}.form.additional_fields"),
  194. )
  195. def _site_date(value, path):
  196. if not isinstance(value, str) or not re.fullmatch(r"\d{2}/\d{2}/\d{4} 00:00:00", value):
  197. raise BizLogicError(f"Italy {path} date must use DD/MM/YYYY 00:00:00")
  198. try:
  199. return datetime.strptime(value, "%d/%m/%Y %H:%M:%S").date()
  200. except ValueError as exc:
  201. raise BizLogicError(f"Italy {path} contains an invalid date") from exc
  202. def _integer(item, key, path):
  203. if key not in item:
  204. raise BizLogicError(f"Italy response is missing {path}.{key}")
  205. if type(item[key]) is not int:
  206. raise BizLogicError(f"Italy {path}.{key} must be an integer")
  207. return item[key]
  208. def _clock(value, path):
  209. value = _mapping(value, path)
  210. parts = [_integer(value, key, path) for key in (
  211. "Hours", "Minutes", "Seconds", "Milliseconds")]
  212. if parts[2] or parts[3]:
  213. raise BizLogicError(f"Italy {path} time must have zero seconds and milliseconds")
  214. try:
  215. return datetime_time(parts[0], parts[1])
  216. except ValueError as exc:
  217. raise BizLogicError(f"Italy {path} contains an invalid time") from exc
  218. def _parse_calendar_items(payload, time_slots=False):
  219. if not isinstance(payload, str) or not payload:
  220. raise BizLogicError("Italy Calendar payload must be a non-empty string")
  221. if len(payload) > MAX_PROTOCOL_PAYLOAD_CHARS:
  222. raise BizLogicError("Italy Calendar payload exceeds the size limit")
  223. try:
  224. inner = json.loads(payload)
  225. except json.JSONDecodeError as exc:
  226. raise BizLogicError("Italy Calendar outer JSON is invalid") from exc
  227. if not isinstance(inner, str):
  228. raise BizLogicError("Italy Calendar payload must contain a JSON-encoded array string")
  229. try:
  230. items = json.loads(inner)
  231. except json.JSONDecodeError as exc:
  232. raise BizLogicError("Italy Calendar inner JSON is invalid") from exc
  233. if not isinstance(items, list):
  234. raise BizLogicError("Italy Calendar decoded payload must be an array")
  235. if len(items) > MAX_PROTOCOL_ITEMS:
  236. raise BizLogicError("Italy Calendar payload exceeds the item limit")
  237. if not all(isinstance(item, Mapping) for item in items):
  238. raise BizLogicError("Italy Calendar items must be objects")
  239. records, identities, daily_ids = [], set(), set()
  240. for index, item in enumerate(items):
  241. path = f"{'time_slots' if time_slots else 'availability'}[{index}]"
  242. free, remaining = _integer(item, "SlotLiberi", path), _integer(item, "SlotRimanenti", path)
  243. if not ((free > 0 and remaining >= 0) or (free == 0 and remaining < 0)):
  244. raise BizLogicError(f"Italy {path} contains conflicting inventory counts")
  245. day = _site_date(item.get("Data" if time_slots else "DateLibere"), path)
  246. if time_slots:
  247. start = _clock(item.get("OrarioInizioFascia"), f"{path}.OrarioInizioFascia")
  248. end = _clock(item.get("OrarioFineFascia"), f"{path}.OrarioFineFascia")
  249. if start >= end:
  250. raise BizLogicError(f"Italy {path} slot start must precede its end")
  251. daily_id = _integer(item, "IDCalendarioServizioGiornaliero", path)
  252. if daily_id <= 0:
  253. raise BizLogicError(f"Italy {path} daily calendar id must be positive")
  254. if daily_id in daily_ids:
  255. raise BizLogicError(f"Italy {path} daily calendar id is duplicated")
  256. daily_ids.add(daily_id)
  257. record = SlotCandidate(day, free, remaining, start, end, str(daily_id))
  258. identity = day, start, end
  259. else:
  260. record, identity = CalendarDay(day, free, remaining), day
  261. if identity in identities:
  262. raise BizLogicError(f"Italy {path} contains a duplicate date or time slot")
  263. identities.add(identity)
  264. records.append(record)
  265. return tuple(sorted(records, key=lambda item: (item.day, getattr(item, "start_time", datetime_time.min))))
  266. def parse_calendar_availability(payload: str):
  267. return _parse_calendar_items(payload)
  268. def parse_time_slots(payload: str):
  269. return _parse_calendar_items(payload, time_slots=True)
  270. def available_calendar_days(items, *, start_day=None, end_day=None):
  271. if start_day and end_day and start_day > end_day:
  272. raise BizLogicError("Italy inventory date range is inverted")
  273. allowed = set(filter_dates_in_range(
  274. [item.day.isoformat() for item in items],
  275. (start_day or date.min).isoformat(), (end_day or date.max).isoformat(),
  276. shuffle=False,
  277. ))
  278. return tuple(item for item in items if item.is_available and item.day.isoformat() in allowed)
  279. def parse_otp_body(body: str):
  280. if not isinstance(body, str) or not body.strip():
  281. raise BizLogicError("Italy OTP mail body must be a non-empty string")
  282. if len(body) > MAX_PROTOCOL_PAYLOAD_CHARS:
  283. raise BizLogicError("Italy OTP mail body exceeds the size limit")
  284. values = set(_OTP_PATTERN.findall(html.unescape(re.sub(r"<[^>]*>", " ", body))))
  285. if not values:
  286. raise NotFoundError("Italy OTP code was not found in the email")
  287. if len(values) != 1:
  288. raise BizLogicError("Italy OTP email contains conflicting codes")
  289. return values.pop()
  290. def month_starts(anchor_day: date, lookahead_months: int):
  291. months = _positive_int(lookahead_months, "lookahead_months", MAX_LOOKAHEAD_MONTHS)
  292. return tuple(date(anchor_day.year + (anchor_day.month - 1 + i) // 12,
  293. (anchor_day.month - 1 + i) % 12 + 1, 1) for i in range(months))
  294. CALENDAR_PATH = "/BookingCalendar"
  295. AVAILABILITY_PATH = "/BookingCalendar/RetrieveCalendarAvailability"
  296. TIME_SLOTS_PATH = "/BookingCalendar/RetrieveTimeSlots"
  297. DEFAULT_MONTH_CLICK_INTERVAL_SECONDS = 1.5
  298. def same_origin_path(raw_url):
  299. try:
  300. parsed = urlparse(str(raw_url or ""))
  301. port = parsed.port
  302. except ValueError:
  303. raise BizLogicError("Italy request left the expected origin") from None
  304. if (parsed.scheme != "https" or parsed.hostname != "prenotami.esteri.it"
  305. or port not in (None, 443) or parsed.username is not None):
  306. raise BizLogicError("Italy request left the expected origin")
  307. return parsed.path
  308. CALENDAR_DOM_SCRIPT = r"""
  309. const [mode, serviceId, day, dailyId, timeLabel, capacity, shouldClick] = arguments;
  310. const all = (selector, root = document) => Array.from(root.querySelectorAll(selector));
  311. const services = all('#idServizio'), timeline = all('.events a.selected[data-date]');
  312. const service = services.length === 1 ? services[0].value : '';
  313. const selectedDay = timeline.length === 1 ? timeline[0].dataset.date : '';
  314. if (mode === 'service') return {path: location.pathname, service};
  315. if (mode === 'context' || mode === 'anchor') {
  316. const dates = all('.events a[data-date]').map(item => item.dataset.date).filter(Boolean);
  317. const fallbackDay = dates.length > 0 ? dates[0] : '';
  318. const monthSwitch = document.querySelector('#datetimepicker th.picker-switch, .bootstrap-datetimepicker-widget th.picker-switch, section.calendario .picker-switch');
  319. const monthText = monthSwitch ? (monthSwitch.textContent || '').trim() : '';
  320. return {path: location.pathname, service, day: selectedDay, fallbackDay, monthText};
  321. }
  322. const contextReady = location.pathname === '/BookingCalendar' && service === serviceId;
  323. const links = all('.events a[data-date]').filter(item => item.dataset.date === day);
  324. const link = links.length === 1 ? links[0] : null;
  325. if (mode === 'date') {
  326. const candidateReady = Boolean(contextReady && link &&
  327. !link.classList.contains('notAvailableDay') && !link.classList.contains('disabled'));
  328. let selected = Boolean(candidateReady && link.classList.contains('selected')), clicked = false;
  329. if (shouldClick && candidateReady && !selected) { link.click(); clicked = true; }
  330. selected = Boolean(candidateReady && link.classList.contains('selected'));
  331. return {contextReady, candidateCount: links.length, candidateReady, selected, clicked};
  332. }
  333. const content = all('.events-content li.selected').find(item => item.dataset.date === day);
  334. const matches = content ? all('.fascia', content).filter(slot => {
  335. const input = slot.querySelector('#idHiddenFasciaServizio');
  336. return input && input.value === dailyId;
  337. }) : [];
  338. const slot = matches.length === 1 ? matches[0] : null, spans = slot ? slot.querySelectorAll('span') : [];
  339. const clean = value => String(value || '').replace(/\s+/g, ' ').trim();
  340. const candidateReady = Boolean(contextReady && slot && !slot.classList.contains('notAvailableFascia') &&
  341. spans.length === 2 && clean(spans[0].textContent) === timeLabel &&
  342. clean(spans[1].textContent) === `(${capacity})`);
  343. let selected = Boolean(candidateReady && slot.classList.contains('act')), clicked = false;
  344. if (shouldClick && candidateReady && !selected) { slot.click(); clicked = true; }
  345. selected = Boolean(candidateReady && slot.classList.contains('act'));
  346. return {contextReady, candidateCount: matches.length, candidateReady, selected, clicked};
  347. """
  348. def _runtime_id(value):
  349. if value is None or isinstance(value, bool):
  350. raise BizLogicError("Italy Calendar service id is invalid")
  351. value = str(value).strip()
  352. if not re.fullmatch(r"[1-9]\d{0,127}", value):
  353. raise BizLogicError("Italy Calendar service id is invalid")
  354. return value
  355. def read_calendar_service(page):
  356. if same_origin_path(page.url) != CALENDAR_PATH:
  357. raise BizLogicError("Italy Calendar page is not current")
  358. raw = page.run_js(CALENDAR_DOM_SCRIPT, "service", "", "", "", "", 0, False)
  359. if not isinstance(raw, dict) or raw.get("path") != CALENDAR_PATH:
  360. raise BizLogicError("Italy Calendar service is not ready")
  361. raw_service = raw.get("service")
  362. if not raw_service:
  363. raise BizLogicError("Italy Calendar service is not ready")
  364. return _runtime_id(raw_service)
  365. def wait_for_calendar_service(plugin, page, expected_service, timeout=5):
  366. expected_service = _runtime_id(expected_service)
  367. deadline = time.monotonic() + timeout
  368. while time.monotonic() < deadline:
  369. if plugin.page is not page:
  370. raise SessionExpiredOrInvalidError("Italy applicant Session changed")
  371. try:
  372. service = read_calendar_service(page)
  373. except BizLogicError as exc:
  374. if exc.message != "Italy Calendar service is not ready":
  375. raise
  376. time.sleep(0.1)
  377. continue
  378. if service != expected_service:
  379. raise BizLogicError("Italy Calendar service changed")
  380. return service
  381. raise BizLogicError("Italy Calendar service did not initialize")
  382. _CALENDAR_MONTH_NAMES = {
  383. "gennaio": 1, "febbraio": 2, "marzo": 3, "aprile": 4,
  384. "maggio": 5, "giugno": 6, "luglio": 7, "agosto": 8,
  385. "settembre": 9, "ottobre": 10, "novembre": 11, "dicembre": 12,
  386. "january": 1, "february": 2, "march": 3, "april": 4,
  387. "may": 5, "june": 6, "july": 7, "august": 8,
  388. "september": 9, "october": 10, "november": 11, "december": 12,
  389. }
  390. _MONTH_WORD_RE = re.compile(
  391. r"\b(" + "|".join(_CALENDAR_MONTH_NAMES.keys()) + r")\b",
  392. re.IGNORECASE,
  393. )
  394. _YEAR_RE = re.compile(r"\b(20\d{2})\b")
  395. def _parse_month_text_anchor(text: str):
  396. if not text:
  397. return None
  398. month_match = _MONTH_WORD_RE.search(text)
  399. if not month_match:
  400. return None
  401. month_num = _CALENDAR_MONTH_NAMES[month_match.group(1).lower()]
  402. year_match = _YEAR_RE.search(text)
  403. if year_match:
  404. return date(int(year_match.group(1)), month_num, 1)
  405. return None
  406. def _extract_calendar_anchor_from_raw(raw: dict) -> date:
  407. day_str = str(raw.get("day") or "").strip()
  408. if day_str:
  409. try:
  410. return date.fromisoformat(day_str)
  411. except ValueError:
  412. raise BizLogicError("Italy Calendar date format changed") from None
  413. month_text = str(raw.get("monthText") or "").strip()
  414. if month_text:
  415. parsed_anchor = _parse_month_text_anchor(month_text)
  416. if parsed_anchor:
  417. return parsed_anchor
  418. fallback_day_str = str(raw.get("fallbackDay") or "").strip()
  419. if fallback_day_str:
  420. try:
  421. return date.fromisoformat(fallback_day_str).replace(day=1)
  422. except ValueError:
  423. raise BizLogicError("Italy Calendar date format changed") from None
  424. raise BizLogicError("Italy Calendar anchor is not ready")
  425. def read_calendar_anchor(page) -> date:
  426. if same_origin_path(page.url) != CALENDAR_PATH:
  427. raise BizLogicError("Italy Calendar page is not current")
  428. raw = page.run_js(CALENDAR_DOM_SCRIPT, "anchor", "", "", "", "", 0, False)
  429. if not isinstance(raw, dict) or raw.get("path") != CALENDAR_PATH:
  430. raise BizLogicError("Italy Calendar anchor is not ready")
  431. return _extract_calendar_anchor_from_raw(raw)
  432. def wait_for_calendar_anchor(plugin, page, timeout=5) -> date:
  433. deadline = time.monotonic() + timeout
  434. while time.monotonic() < deadline:
  435. if plugin.page is not page:
  436. raise SessionExpiredOrInvalidError("Italy applicant Session changed")
  437. try:
  438. return read_calendar_anchor(page)
  439. except BizLogicError as exc:
  440. if exc.message != "Italy Calendar anchor is not ready":
  441. raise
  442. time.sleep(0.1)
  443. continue
  444. raise BizLogicError("Italy Calendar anchor did not initialize")
  445. def read_calendar_context(page):
  446. if same_origin_path(page.url) != CALENDAR_PATH:
  447. raise BizLogicError("Italy Calendar page is not current")
  448. raw = page.run_js(CALENDAR_DOM_SCRIPT, "context", "", "", "", "", 0, False)
  449. if not isinstance(raw, dict) or raw.get("path") != CALENDAR_PATH:
  450. raise BizLogicError("Italy Calendar context is not ready")
  451. service = _runtime_id(raw.get("service"))
  452. anchor = _extract_calendar_anchor_from_raw(raw)
  453. return service, anchor
  454. def _calendar_post(page, path, payload, parser, *, form=False):
  455. try:
  456. response = perform_browser_fetch(
  457. page, "POST", path, **{"data" if form else "json_data": payload}, timeout=30)
  458. except (PageDisconnectedError, ElementNotFoundError,
  459. SessionExpiredOrInvalidError, RateLimiteddError, NotFoundError, BizLogicError):
  460. raise
  461. except Exception as exc:
  462. raise BizLogicError(f"Italy Calendar request failed: {type(exc).__name__}") from exc
  463. status, host = response.status_code, urlparse(str(response.url or "")).hostname
  464. if host == "iam.esteri.it" or status == 401:
  465. raise SessionExpiredOrInvalidError("Italy Calendar authentication expired")
  466. if status == 403:
  467. raise BizLogicError("Italy Calendar access denied")
  468. if status == 429:
  469. raise RateLimiteddError("Italy Calendar rate limited")
  470. if status == 404:
  471. raise NotFoundError("Italy Calendar endpoint is unavailable")
  472. if status in {0, 408, 502, 503, 504}:
  473. raise BizLogicError("Italy Calendar temporarily unavailable")
  474. if status != 200 or same_origin_path(response.url) != path:
  475. raise BizLogicError("Italy Calendar returned an unexpected response")
  476. try:
  477. return parser(response.text)
  478. except (ValueError, TypeError) as exc:
  479. raise BizLogicError(f"Italy Calendar response rejected ({type(exc).__name__})") from exc
  480. def fetch_calendar_month(page, service, selected_day):
  481. selected = selected_day.strftime("%d/%m/%Y")
  482. days = _calendar_post(
  483. page, AVAILABILITY_PATH,
  484. {"_Servizio": _runtime_id(service), "selectedDay": selected},
  485. parse_calendar_availability,
  486. )
  487. if any(item.day.replace(day=1) != selected_day.replace(day=1) for item in days):
  488. raise BizLogicError("Italy Calendar returned dates for another month")
  489. return days
  490. def fetch_calendar_slots(page, service, selected_day):
  491. slots = _calendar_post(
  492. page, TIME_SLOTS_PATH,
  493. {"selectedDay": selected_day.isoformat(), "idService": _runtime_id(service)},
  494. parse_time_slots,
  495. )
  496. if any(slot.day != selected_day for slot in slots):
  497. raise BizLogicError("Italy Calendar returned slots for another day")
  498. return slots
  499. def scan_until_first_available_month(plugin, route):
  500. page = plugin.page
  501. service, current = read_calendar_context(page)
  502. anchor = plugin._prepared_calendar.initial_month or current
  503. for index, month in enumerate(month_starts(anchor, route.lookahead_months)):
  504. if index:
  505. time.sleep(DEFAULT_MONTH_CLICK_INTERVAL_SECONDS)
  506. plugin._require_calendar_context(route, role="query")
  507. days = fetch_calendar_month(page, service, month)
  508. plugin._require_calendar_context(route, role="query")
  509. available = available_calendar_days(days)
  510. plugin._log(f"Italy Calendar probe index={index} available={len(available)}")
  511. yield available
  512. if available:
  513. return
  514. def _snapshot(page, mode, service, day, candidate=None, click=False):
  515. raw = page.run_js(
  516. CALENDAR_DOM_SCRIPT, mode, service, day.isoformat(),
  517. candidate.daily_calendar_id if candidate else "", candidate.time_label if candidate else "",
  518. candidate.slots_free if candidate else 0, click,
  519. )
  520. if (not isinstance(raw, dict) or type(raw.get("candidateCount")) is not int
  521. or any(type(raw.get(key)) is not bool for key in (
  522. "contextReady", "candidateReady", "selected", "clicked"))
  523. or not raw["contextReady"] or raw["candidateCount"] not in (0, 1)
  524. or (raw["candidateCount"] == 0 and (mode != "slot" or click
  525. or raw["candidateReady"] or raw["selected"] or raw["clicked"]))):
  526. raise BizLogicError("Italy Calendar selection context changed")
  527. return raw
  528. def verify_precommit_selection(page, *, runtime_service, candidate):
  529. raw = _snapshot(
  530. page, "slot", runtime_service, candidate.day, candidate, False)
  531. if (not candidate.is_available or raw["candidateCount"] != 1
  532. or not raw["candidateReady"] or not raw["selected"]):
  533. raise BizLogicError("Italy Calendar selected slot changed")
  534. return candidate
  535. def book_date_preferences(user_inputs):
  536. values = []
  537. for key, default in (("expected_start_date", "2000-01-01"),
  538. ("expected_end_date", "2100-01-01")):
  539. try:
  540. values.append(date.fromisoformat(user_inputs.get(key, default)))
  541. except (AttributeError, TypeError, ValueError):
  542. raise BizLogicError(f"Italy {key} must use YYYY-MM-DD") from None
  543. if values[0] > values[1]:
  544. raise BizLogicError("Italy date preference is inverted")
  545. return tuple(values)
  546. def _move_to_month(plugin, service, target, timeout):
  547. page = plugin.page
  548. current = read_calendar_context(page)[1]
  549. while (current.year, current.month) < target:
  550. expected = (current.year + 1, 1) if current.month == 12 else (current.year, current.month + 1)
  551. control = page.ele("css:#datetimepicker th.dtpicker-next", timeout=timeout)
  552. if not control:
  553. raise NotFoundError("Italy Calendar next-month control is unavailable")
  554. plugin.mouse.human_click_ele(control)
  555. deadline = time.monotonic() + timeout
  556. while time.monotonic() < deadline:
  557. actual_service, current = read_calendar_context(page)
  558. if actual_service != service:
  559. raise BizLogicError("Italy Calendar service changed")
  560. if (current.year, current.month) == expected:
  561. break
  562. time.sleep(0.1)
  563. else:
  564. raise BizLogicError("Italy Calendar month did not load")
  565. if (current.year, current.month) != target:
  566. raise BizLogicError("Italy Calendar cannot navigate backwards")
  567. def recheck_calendar_before_final(plugin, route, start_day, end_day, *, session_is_current):
  568. page = plugin.page
  569. service, anchor = read_calendar_context(page)
  570. for index, month_start in enumerate(month_starts(anchor, route.lookahead_months)):
  571. if session_is_current() is not True:
  572. raise SessionExpiredOrInvalidError("Italy applicant Session changed")
  573. if index:
  574. time.sleep(DEFAULT_MONTH_CLICK_INTERVAL_SECONDS)
  575. selected_month = month_start.year, month_start.month
  576. _move_to_month(plugin, service, selected_month, 30)
  577. inventory = fetch_calendar_month(page, service, month_start)
  578. days = available_calendar_days(inventory, start_day=start_day, end_day=end_day)
  579. plugin._log(f"Italy Book Calendar month={month_start:%Y-%m} candidates={len(days)}")
  580. for item in days:
  581. slots = fetch_calendar_slots(page, service, item.day)
  582. candidate = next((slot for slot in slots if slot.is_available), None)
  583. if candidate is None:
  584. continue
  585. deadline = time.monotonic() + 5
  586. if session_is_current() is not True:
  587. raise SessionExpiredOrInvalidError("Italy applicant Session changed")
  588. _snapshot(page, "date", service, item.day, click=True)
  589. while time.monotonic() < deadline:
  590. if session_is_current() is not True:
  591. raise SessionExpiredOrInvalidError("Italy applicant Session changed")
  592. date_state = _snapshot(page, "date", service, item.day)
  593. slot_state = _snapshot(page, "slot", service, item.day, candidate)
  594. if date_state.get("selected") and slot_state.get("candidateReady"):
  595. if not slot_state.get("selected"):
  596. _snapshot(page, "slot", service, item.day, candidate, True)
  597. return verify_precommit_selection(page, runtime_service=service, candidate=candidate)
  598. time.sleep(0.1)
  599. raise BizLogicError("Italy Calendar slot did not finish loading")
  600. if selected_month >= (end_day.year, end_day.month):
  601. break
  602. return None
  603. FINAL_PATH = "/BookingCalendar/InsertNewBooking"
  604. FINAL_ACCEPTED_LABEL = "italy-final-accepted"
  605. def _submit_final_once(plugin, route: ItaRouteConfig, slot: SlotCandidate) -> BookResult:
  606. result = None
  607. try:
  608. page = plugin.page
  609. username = plugin.config.account.username.strip().casefold()
  610. prepared = plugin._require_calendar_context(route, role="book", username=username)
  611. if (not username or not isinstance(slot, SlotCandidate)
  612. or page.tab_id != prepared.tab_id or same_origin_path(page.url) != CALENDAR_PATH):
  613. raise BizLogicError("Italy final context is not current")
  614. verify_precommit_selection(page, runtime_service=prepared.runtime_service, candidate=slot)
  615. data = _calendar_post(page, FINAL_PATH, {
  616. "idCalendarioGiornaliero": slot.daily_calendar_id,
  617. "selectedDay": slot.day.isoformat(),
  618. "selectedHour": f"{slot.time_label}\n({slot.slots_free})",
  619. }, json.loads, form=True)
  620. if not isinstance(data, dict) or not isinstance(data.get("errorMessage"), str):
  621. raise BizLogicError("Italy final response is missing its result")
  622. if data["errorMessage"].strip():
  623. raise BizLogicError("Italy final booking was rejected by the website")
  624. destination = data.get("url")
  625. if not isinstance(destination, str) or not destination.strip():
  626. raise BizLogicError("Italy final response is missing its receipt URL")
  627. path = same_origin_path(urljoin("https://prenotami.esteri.it", destination.strip()))
  628. if not re.fullmatch(r"/Reservation/?|/ReservationDetail/Index/[^/]+/?", path):
  629. raise BizLogicError("Italy final response did not lead to a receipt")
  630. result = BookResult(success=True, account=username, label=FINAL_ACCEPTED_LABEL,
  631. book_date=slot.day.isoformat(), book_time=slot.time_label)
  632. return result
  633. finally:
  634. if result is None:
  635. # The next Booker signal must prepare again and obtain fresh IDs.
  636. plugin._prepared_calendar = None
  637. BOOKING_CONTRACT_FIELDS = (
  638. "pathMatch", "form", "bookingControl", "bookingOption", "purposeControl",
  639. "purposeOption", "privacy", "otpInput", "otpSend", "avanti",
  640. )
  641. def wait_for_booking_contract(page, *, script, timeout=10):
  642. deadline, missing = time.monotonic() + timeout, list(BOOKING_CONTRACT_FIELDS)
  643. while time.monotonic() < deadline:
  644. raw = page.run_js(script)
  645. if not isinstance(raw, dict) or any(type(raw.get(key)) is not bool
  646. for key in BOOKING_CONTRACT_FIELDS):
  647. raise BizLogicError("Italy booking contract probe returned invalid data")
  648. missing = [key for key in BOOKING_CONTRACT_FIELDS if not raw[key]]
  649. if not missing:
  650. return raw
  651. time.sleep(0.25)
  652. raise BizLogicError(f"Italy booking form structure changed: {','.join(missing)}")
  653. def guarded_preflight_script(route: ItaRouteConfig, booking_path: str, *, readonly=False) -> str:
  654. if not route.booking_type or not route.purpose:
  655. raise BizLogicError("Italy route is missing form controls")
  656. config = {"path": booking_path, "booking": asdict(route.booking_type),
  657. "purpose": asdict(route.purpose), "readonly": readonly}
  658. return f"""
  659. const cfg = {json.dumps(config, ensure_ascii=False)};
  660. const form = document.querySelector('#bookingForm');
  661. const norm = value => String(value || '').replace(/\\s+/g, ' ').trim().toLowerCase();
  662. function choose(item) {{
  663. const control = document.getElementById(item.control_id);
  664. const option = control && Array.from(control.options).find(value => value.value === item.value);
  665. const recognized = Boolean(option && item.labels.map(norm).includes(norm(option.textContent)));
  666. if (!cfg.readonly && recognized && control.value !== item.value) {{
  667. control.value = item.value;
  668. control.dispatchEvent(new Event('change', {{bubbles: true}}));
  669. }}
  670. return {{control: Boolean(control), option: recognized,
  671. selected: Boolean(recognized && control.value === item.value)}};
  672. }}
  673. const booking = choose(cfg.booking), purpose = choose(cfg.purpose);
  674. const privacy = document.querySelector('#PrivacyCheck');
  675. if (!cfg.readonly && privacy && !privacy.checked) privacy.click();
  676. const otp = document.querySelector('#otp-input'), send = document.querySelector('#otp-send');
  677. const avanti = document.querySelector('#btnAvanti');
  678. const token = document.querySelector('input[name="cf-turnstile-response"]');
  679. const pathMatch = location.pathname === cfg.path;
  680. return {{
  681. pathMatch,
  682. form: Boolean(form),
  683. bookingControl: booking.control,
  684. bookingOption: booking.option,
  685. purposeControl: purpose.control,
  686. purposeOption: purpose.option,
  687. privacy: Boolean(privacy),
  688. otpInput: Boolean(otp),
  689. otpSend: Boolean(send),
  690. avanti: Boolean(avanti),
  691. formReady: Boolean(pathMatch && form && booking.selected && purpose.selected && privacy && privacy.checked),
  692. turnstilePresent: Boolean(token),
  693. turnstileReady: Boolean(token && token.value),
  694. otpValuePresent: Boolean(otp && otp.value.trim()),
  695. otpSendEnabled: Boolean(send && !send.disabled),
  696. avantiEnabled: Boolean(avanti && !avanti.disabled),
  697. }};
  698. """
  699. def wait_for_booking_preflight(page, *, script, after_otp=False, timeout=120,
  700. check_current=None, log=None):
  701. deadline = time.monotonic() + timeout
  702. click_after, click_attempted, pending_logged, raw = None, False, False, None
  703. while time.monotonic() < deadline:
  704. if check_current:
  705. check_current()
  706. states = getattr(page, "states", None)
  707. if states and getattr(states, "has_alert", None) is True:
  708. try:
  709. page.handle_alert(accept=True)
  710. except Exception:
  711. pass
  712. raw = page.run_js(script)
  713. if not isinstance(raw, dict) or any(type(raw.get(key)) is not bool for key in (
  714. "formReady", "turnstilePresent", "turnstileReady", "otpValuePresent",
  715. "otpSendEnabled", "avantiEnabled")):
  716. raise BizLogicError("Italy booking form structure changed")
  717. if raw["otpValuePresent"] is not after_otp:
  718. raise BizLogicError("Italy booking OTP stage changed")
  719. form_ready = raw["formReady"] and (after_otp or raw["otpSendEnabled"])
  720. turnstile_present = raw["turnstilePresent"]
  721. if turnstile_present and click_after is None:
  722. click_after = time.monotonic() + TURNSTILE_PASSIVE_SECONDS
  723. elif not turnstile_present:
  724. click_after = None
  725. if form_ready and raw["turnstileReady"] and raw["avantiEnabled"]:
  726. return raw
  727. if form_ready and not raw["turnstileReady"] and log and not pending_logged:
  728. log("Italy booking verification state: "
  729. f"stage={'after_otp' if after_otp else 'before_otp'} "
  730. f"turnstile_present={turnstile_present} "
  731. f"otp_send_enabled={raw['otpSendEnabled']} "
  732. f"avanti_enabled={raw['avantiEnabled']}")
  733. pending_logged = True
  734. if (form_ready and turnstile_present and not raw["turnstileReady"]
  735. and not click_attempted and time.monotonic() >= click_after):
  736. click_attempted = True
  737. try:
  738. clicked = CloudflareBypasser(
  739. page, log=log is not None, logger=log).click_verification_button(is_dfs=False)
  740. except (PageDisconnectedError, ElementNotFoundError,
  741. SessionExpiredOrInvalidError, RateLimiteddError, NotFoundError, BizLogicError):
  742. raise
  743. except Exception as exc:
  744. raise BizLogicError("Italy booking verification click failed") from exc
  745. if log:
  746. log(f"Italy booking verification fallback click result={clicked}.")
  747. time.sleep(1)
  748. reason = ("form_pending" if not raw or not raw["formReady"] else
  749. "otp_send_disabled" if not after_otp and not raw["otpSendEnabled"] else
  750. "turnstile_widget_pending" if not raw["turnstilePresent"] else
  751. "turnstile_token_pending" if not raw["turnstileReady"] else "avanti_disabled")
  752. raise BizLogicError(f"Italy booking verification timed out: {reason}")
  753. def _element_or_fail(page, selector):
  754. element = page.ele(selector, timeout=5)
  755. if not element:
  756. raise NotFoundError(f"Italy booking element is missing: {selector}")
  757. return element
  758. SET_ADDITIONAL_SELECT_SCRIPT = """
  759. const control = document.getElementById(arguments[0]);
  760. if (!control || control.disabled) return false;
  761. control.value = arguments[1];
  762. control.dispatchEvent(new Event('change', {bubbles: true}));
  763. return control.value === arguments[1];
  764. """
  765. _DESCRIZIONE_NAME = re.compile(r"^(DatiAddizionaliPrenotante|DocumentiPrenotante)\[(\d+)\]\._Descrizione$")
  766. # The booking page states: "E' possibile inserire file in formato PDF di massimo un 1MB".
  767. PDF_MAGIC = b"%PDF-"
  768. PDF_MAX_BYTES = 1024 * 1024
  769. def _check_upload_bytes(descrizione, path):
  770. with open(path, "rb") as handle:
  771. payload = handle.read()
  772. if not payload.startswith(PDF_MAGIC):
  773. raise BizLogicError(f"Italy booking file {descrizione!r} is not a PDF")
  774. if len(payload) > PDF_MAX_BYTES:
  775. raise BizLogicError(f"Italy booking file {descrizione!r} exceeds 1MB")
  776. def _format_booking_dates(page, route, inputs):
  777. values = dict(inputs or {})
  778. for descrizione, spec in route.additional_fields.items():
  779. if spec.kind != "date" or not values.get(spec.value_from):
  780. continue
  781. element = _element_or_fail(page, f"#{spec.selector}")
  782. input_type = (element.attr("type") or "").strip().casefold()
  783. placeholder = (element.attr("placeholder") or "").strip().casefold()
  784. if input_type == "date" or (input_type == "text" and placeholder == "yyyy-mm-dd"):
  785. output_format = "%Y-%m-%d"
  786. elif input_type == "text" and placeholder == "dd/mm/yyyy":
  787. output_format = "%d/%m/%Y"
  788. else:
  789. raise BizLogicError(f"Italy booking date field {descrizione!r} has an unknown format")
  790. raw = str(values[spec.value_from]).strip()
  791. try:
  792. if re.fullmatch(r"\d{4}-\d{2}-\d{2}", raw):
  793. parsed = date.fromisoformat(raw)
  794. elif re.fullmatch(r"\d{2}/\d{2}/\d{4}", raw):
  795. parsed = datetime.strptime(raw, "%d/%m/%Y").date()
  796. else:
  797. raise ValueError
  798. except ValueError:
  799. raise BizLogicError(f"Italy booking date field {descrizione!r} is invalid") from None
  800. values[spec.value_from] = parsed.strftime(output_format)
  801. return values
  802. def fill_booking_form(plugin, route: ItaRouteConfig, inputs) -> None:
  803. """Write explicit values only; prepare_calendar selects the source by role."""
  804. if not route.additional_fields:
  805. return
  806. inputs = inputs or {}
  807. page = plugin.page
  808. mouse = getattr(plugin, "mouse", None)
  809. keyboard = getattr(plugin, "keyboard", None)
  810. scroll = getattr(plugin, "scroll", None)
  811. # 仅在非 Mock 的真实 CDP 浏览器中按需初始化拟人化组件
  812. if hasattr(page, "run_cdp") and type(page).__name__ != "Mock":
  813. if mouse is None:
  814. try:
  815. profile_name = random.choice(list(MOUSE_PROFILES.keys())) if MOUSE_PROFILES else "average_joe"
  816. mouse = plugin.mouse = HumanMouse(page, timing=MOUSE_PROFILES.get(profile_name), debug=True)
  817. except Exception:
  818. pass
  819. if keyboard is None:
  820. try:
  821. keyboard = plugin.keyboard = HumanKeyboard(page)
  822. except Exception:
  823. pass
  824. if scroll is None:
  825. try:
  826. scroll = plugin.scroll = HumanScroll(page)
  827. except Exception:
  828. pass
  829. is_human = bool(mouse and keyboard and hasattr(page, "run_cdp") and type(page).__name__ != "Mock")
  830. for descrizione, spec in route.additional_fields.items():
  831. element = _element_or_fail(page, f"#{spec.selector}")
  832. value = inputs.get(spec.value_from)
  833. if not value:
  834. raise BizLogicError(
  835. f"Italy booking field {descrizione!r} has no value ({spec.value_from})")
  836. value = str(value)
  837. if spec.kind == "file":
  838. if value.startswith("http://") or value.startswith("https://"):
  839. # Chrome uploads from its own filesystem, so a remote document has to
  840. # be fetched into this instance's workspace first.
  841. import requests
  842. response = requests.get(value, timeout=30)
  843. if response.status_code != 200:
  844. raise BizLogicError(
  845. f"Italy booking file {descrizione!r} could not be downloaded "
  846. f"({response.status_code})")
  847. value = os.path.join(plugin.root_workspace, f"upload_{spec.selector}.pdf")
  848. with open(value, "wb") as handle:
  849. handle.write(response.content)
  850. elif not os.path.isfile(value):
  851. raise NotFoundError(f"Italy booking file {descrizione!r} is missing: {value}")
  852. # The page states PDF only, max 1MB; check here so a bad document fails
  853. # before an OTP is spent on a form that cannot be submitted.
  854. _check_upload_bytes(descrizione, value)
  855. if is_human:
  856. if scroll:
  857. scroll.scroll_to_element(element, humanize=True)
  858. time.sleep(random.uniform(0.3, 0.6))
  859. try:
  860. mid_x, mid_y = element.rect.viewport_midpoint
  861. if mid_x and mid_y:
  862. mouse.move(mid_x, mid_y, humanize=True)
  863. except Exception:
  864. pass
  865. time.sleep(random.uniform(0.6, 1.1)) # 模拟文件选择框耗时
  866. element.input(value)
  867. if is_human:
  868. time.sleep(random.uniform(0.4, 0.8))
  869. time.sleep(random.uniform(0.6, 1.2)) # 字段切换停顿
  870. continue
  871. # The site truncates silently at maxlength=100, so a longer value would be
  872. # written but submitted short.
  873. if len(value) > 100:
  874. raise BizLogicError(
  875. f"Italy booking field {descrizione!r} exceeds the site's 100 character limit")
  876. if spec.kind == "date" and (element.attr("type") or "").strip().casefold() == "date":
  877. # Native date values are ISO; localized key typing and Enter are unsafe here.
  878. written = element.run_js("""
  879. if (this.disabled || this.readOnly) return false;
  880. this.value = arguments[0];
  881. this.dispatchEvent(new Event("input", {bubbles: true}));
  882. this.dispatchEvent(new Event("change", {bubbles: true}));
  883. return this.value === arguments[0] && this.validity.valid;
  884. """, value)
  885. elif is_human:
  886. if scroll:
  887. scroll.scroll_to_element(element, humanize=True)
  888. time.sleep(random.uniform(0.3, 0.6))
  889. if spec.kind == "select":
  890. try:
  891. mouse.human_click_ele(element)
  892. except Exception:
  893. element.click()
  894. time.sleep(random.uniform(0.5, 1.0)) # 人类视线浏览下拉项
  895. try:
  896. opt = element.ele(f'@value={value}', timeout=0.5)
  897. if opt:
  898. mouse.human_click_ele(opt)
  899. else:
  900. element.select.by_value(value)
  901. except Exception:
  902. element.select.by_value(value)
  903. written = page.run_js(SET_ADDITIONAL_SELECT_SCRIPT, spec.selector, value)
  904. time.sleep(random.uniform(0.4, 0.8))
  905. elif spec.kind == "date":
  906. # Flatpickr 日历输入(点击打开日历 -> 观察 -> 拟人化击键输入 -> 回车确认 -> 状态同步)
  907. try:
  908. mouse.human_click_ele(element)
  909. except Exception:
  910. element.click()
  911. time.sleep(random.uniform(0.5, 0.9)) # 观察日历弹层
  912. try:
  913. keyboard.hotkey(Key.CONTROL, Key.A)
  914. time.sleep(random.uniform(0.1, 0.2))
  915. keyboard.press(Key.BACKSPACE)
  916. time.sleep(random.uniform(0.1, 0.2))
  917. except Exception:
  918. pass
  919. keyboard.type_text(value, humanize=True)
  920. time.sleep(random.uniform(0.3, 0.6))
  921. keyboard.press(Key.ENTER)
  922. time.sleep(random.uniform(0.3, 0.5))
  923. page.run_js('''
  924. const sel = arguments[0];
  925. const val = arguments[1];
  926. const el = document.getElementById(sel);
  927. if (el) {
  928. el.value = val;
  929. if (el._flatpickr) {
  930. try { el._flatpickr.setDate(val, true); } catch(e){}
  931. }
  932. el.dispatchEvent(new Event('change', {bubbles: true}));
  933. el.dispatchEvent(new Event('input', {bubbles: true}));
  934. }
  935. ''', spec.selector, value)
  936. time.sleep(random.uniform(0.4, 0.8))
  937. written = element.run_js("return this.value === arguments[0];", value)
  938. if written is not True:
  939. element.input(value, clear=True)
  940. written = element.run_js("return this.value === arguments[0];", value)
  941. else:
  942. # 文本输入框
  943. try:
  944. mouse.human_click_ele(element)
  945. except Exception:
  946. element.click()
  947. time.sleep(random.uniform(0.3, 0.6)) # 准备打字停顿
  948. try:
  949. keyboard.hotkey(Key.CONTROL, Key.A)
  950. time.sleep(random.uniform(0.1, 0.2))
  951. keyboard.press(Key.BACKSPACE)
  952. time.sleep(random.uniform(0.1, 0.2))
  953. except Exception:
  954. pass
  955. keyboard.type_text(value, humanize=True)
  956. time.sleep(random.uniform(0.4, 0.8)) # 视觉核验停顿
  957. written = element.run_js("return this.value === arguments[0];", value)
  958. if written is not True:
  959. element.input(value, clear=True)
  960. written = element.run_js("return this.value === arguments[0];", value)
  961. # 字段切换之间的认知停顿 (0.6s ~ 1.2s)
  962. time.sleep(random.uniform(0.6, 1.2))
  963. else:
  964. if spec.kind == "select":
  965. written = page.run_js(SET_ADDITIONAL_SELECT_SCRIPT, spec.selector, value)
  966. else:
  967. element.click()
  968. element.input(value, clear=True)
  969. written = element.run_js("return this.value === arguments[0];", value)
  970. if written is not True:
  971. raise BizLogicError(f"Italy booking field {descrizione!r} could not be written")
  972. for descrizione in _required_unconfigured(page, route):
  973. raise BizLogicError(f"Italy required booking field is not configured: {descrizione}")
  974. def _required_unconfigured(page, route):
  975. """Site-declared required fields that this route config does not fill.
  976. Additional fields declare themselves through `[N]._Obbligatorio`; the document
  977. uploads carry no such marker, so their presence in the form is what makes them
  978. required. `purpose_index` is exempt because the purpose control is configured
  979. separately, not as an additional field.
  980. """
  981. purpose_index = None
  982. if route.purpose is not None:
  983. match = re.fullmatch(r"ddls_(\d+)", route.purpose.control_id)
  984. if match:
  985. purpose_index = match.group(1)
  986. missing = []
  987. for element in page.eles('css:input[type="hidden"]'):
  988. match = _DESCRIZIONE_NAME.fullmatch(element.attr("name") or "")
  989. if not match:
  990. continue
  991. group, index = match.group(1), match.group(2)
  992. descrizione = (element.attr("value") or "").strip()
  993. if not descrizione:
  994. continue
  995. if group == "DatiAddizionaliPrenotante":
  996. if index == purpose_index:
  997. continue
  998. obbligatorio = page.ele(
  999. f'css:input[name="DatiAddizionaliPrenotante[{index}]._Obbligatorio"]', timeout=1)
  1000. if obbligatorio is None or (obbligatorio.attr("value") or "").strip() != "True":
  1001. continue
  1002. if descrizione not in route.additional_fields:
  1003. missing.append(descrizione)
  1004. return missing
  1005. # =============================================================================
  1006. # TEMPORARY diagnostic capture - DELETE THIS WHOLE BLOCK (and its call sites)
  1007. # once the Ireland desk is understood. It exists only because a slot window is
  1008. # rare and arrives unattended, so the one visit has to leave a full record.
  1009. #
  1010. # !! The capture directory holds SECRETS (session cookies, OTP codes, the
  1011. # !! applicant's real values). `data/ita_capture/` is gitignored - keep it that
  1012. # !! way and delete the files once the findings are written down.
  1013. # =============================================================================
  1014. def _capture(plugin):
  1015. """The diagnostic recorder, or a no-op stand-in when capture is off."""
  1016. return getattr(plugin, "_capture", None) or _NULL_CAPTURE
  1017. ITA_CAPTURE_DIR = "data/ita_capture"
  1018. ITA_CAPTURE_HTML_BYTES = 2_000_000
  1019. ITA_CAPTURE_BODY_BYTES = 500_000
  1020. ITA_CAPTURE_PACKETS = 2_000
  1021. ITA_CAPTURE_STAGES = ("services", "after_prenota", "booking_form", "before_otp", "after_otp", "calendar")
  1022. class _NullCapture:
  1023. """Swallows capture calls when the recorder is absent or disabled."""
  1024. enabled = False
  1025. def start(self): pass
  1026. def drain(self): pass
  1027. def snapshot(self, stage, note="", *, force=False): pass
  1028. def error(self, stage, exc): pass
  1029. def write(self): pass
  1030. _NULL_CAPTURE = _NullCapture()
  1031. class ItaCapture:
  1032. """Records the one-off visit: page HTML, every request/response, and errors."""
  1033. def __init__(self, plugin):
  1034. self.plugin = plugin
  1035. config = plugin.free_config.get("capture")
  1036. config = config if isinstance(config, Mapping) else {}
  1037. self.enabled = config.get("enabled") is True
  1038. stages = config.get("html_stages")
  1039. self.stages = tuple(stages) if isinstance(stages, (list, tuple)) else ITA_CAPTURE_STAGES
  1040. self.packets = []
  1041. self.notes = []
  1042. self.listening = False
  1043. def _run_dir(self):
  1044. return os.path.abspath(os.path.join(ITA_CAPTURE_DIR, self.plugin.instance_id))
  1045. def start(self):
  1046. if not self.enabled or self.listening or not self.plugin.page:
  1047. return
  1048. try:
  1049. self.plugin.page.listen.start(targets=True)
  1050. self.listening = True
  1051. self.plugin._log("Italy capture: network recording started")
  1052. except Exception as exc: # diagnostics must never break the booking path
  1053. self.notes.append(f"listen.start failed: {type(exc).__name__}: {exc}")
  1054. def drain(self):
  1055. if not self.listening:
  1056. return
  1057. try:
  1058. while len(self.packets) < ITA_CAPTURE_PACKETS:
  1059. packet = self.plugin.page.listen.wait(count=1, timeout=0.05, raise_err=False)
  1060. # Listener.wait returns False (not None) once the timeout passes with an
  1061. # empty queue, and only ever yields one packet because fit_count is on.
  1062. # Anything without a url is not a packet, so stop draining.
  1063. if not getattr(packet, "url", None):
  1064. break
  1065. self.packets.append(self._describe(packet))
  1066. except Exception as exc:
  1067. self.notes.append(f"drain failed: {type(exc).__name__}: {exc}")
  1068. def _describe(self, packet):
  1069. record = {"url": packet.url, "method": packet.method,
  1070. "resource_type": packet.resourceType, "frame_id": packet.frameId}
  1071. try:
  1072. request = packet.request
  1073. if request is not None:
  1074. record["request_headers"] = dict(request.headers or {})
  1075. # CDP can omit cookie extra-info even when request/body are present.
  1076. record["request_cookies"] = (
  1077. request.cookies if request.extra_info.associatedCookies is not None else None)
  1078. record["request_params"] = request.params
  1079. post = request.postData
  1080. record["post_data"] = (
  1081. post[:ITA_CAPTURE_BODY_BYTES] if isinstance(post, str) else post)
  1082. except Exception as exc:
  1083. record["request_error"] = f"{type(exc).__name__}: {exc}"
  1084. try:
  1085. response = packet.response
  1086. if response is not None:
  1087. record["status"] = response.status
  1088. record["mime_type"] = response.mimeType
  1089. record["response_headers"] = dict(response.headers or {})
  1090. body = response.body
  1091. if isinstance(body, bytes):
  1092. record["body"] = b64encode(body[:ITA_CAPTURE_BODY_BYTES]).decode("ascii")
  1093. record["body_encoding"] = "base64"
  1094. record["body_bytes"] = len(body)
  1095. else:
  1096. record["body"] = (
  1097. body[:ITA_CAPTURE_BODY_BYTES] if isinstance(body, str) else body)
  1098. except Exception as exc:
  1099. record["response_error"] = f"{type(exc).__name__}: {exc}"
  1100. return record
  1101. def snapshot(self, stage, note="", *, force=False):
  1102. """Record the page as a person would see it, plus the values the site carries."""
  1103. if not self.enabled or not self.plugin.page or (not force and stage not in self.stages):
  1104. return
  1105. try:
  1106. run = self._run_dir()
  1107. os.makedirs(run, exist_ok=True)
  1108. # Keep every PRENOTA result so a later query cannot overwrite its evidence.
  1109. name = (f"{stage}_{datetime.now(timezone.utc):%Y%m%dT%H%M%S_%fZ}"
  1110. if stage == "after_prenota" else stage)
  1111. html = self.plugin.page.html or ""
  1112. with open(os.path.join(run, f"{name}.html"), "w", encoding="utf-8") as handle:
  1113. handle.write(html[:ITA_CAPTURE_HTML_BYTES])
  1114. state = self._state(stage, note)
  1115. screenshot = save_screenshot(
  1116. self.plugin.page, self.plugin.instance_id, f"capture_{name}",
  1117. save_dir=run, full_page=True)
  1118. state["screenshot"] = os.path.basename(screenshot) if screenshot else None
  1119. with open(os.path.join(run, f"{name}.state.json"), "w", encoding="utf-8") as handle:
  1120. json.dump(state, handle, ensure_ascii=False, indent=1)
  1121. except Exception as exc:
  1122. self.notes.append(f"snapshot {stage} failed: {type(exc).__name__}: {exc}")
  1123. def _state(self, stage, note):
  1124. page = self.plugin.page
  1125. return {"stage": stage, "note": note,
  1126. "at": datetime.now(timezone.utc).isoformat(),
  1127. "url": page.url,
  1128. "hidden_fields": self._hidden_fields(page),
  1129. "selects": self._selects(page)}
  1130. @staticmethod
  1131. def _hidden_fields(page):
  1132. fields = {}
  1133. for element in page.eles('css:input[type="hidden"]'):
  1134. name = element.attr("name")
  1135. if name:
  1136. fields[name] = element.attr("value")
  1137. return fields
  1138. @staticmethod
  1139. def _selects(page):
  1140. selects = {}
  1141. for element in page.eles("css:select"):
  1142. element_id = element.attr("id") or element.attr("name")
  1143. if element_id:
  1144. selects[element_id] = element.attr("value")
  1145. return selects
  1146. def error(self, stage, exc):
  1147. if not self.enabled:
  1148. return
  1149. self.drain()
  1150. self.snapshot(stage, note=f"{type(exc).__name__}: {exc}", force=True)
  1151. def write(self):
  1152. """Flush everything. Never raises - this runs while an error is propagating."""
  1153. if not self.enabled:
  1154. return
  1155. try:
  1156. self.drain()
  1157. try:
  1158. if self.listening:
  1159. self.plugin.page.listen.stop()
  1160. self.listening = False
  1161. except Exception as exc:
  1162. self.notes.append(f"listen.stop failed: {type(exc).__name__}: {exc}")
  1163. run = self._run_dir()
  1164. os.makedirs(run, exist_ok=True)
  1165. payload = {"instance_id": self.plugin.instance_id,
  1166. "account": self.plugin.config.account.username if self.plugin.config else None,
  1167. "free_config": self.plugin.free_config,
  1168. "packets": self.packets, "notes": self.notes}
  1169. with open(os.path.join(run, "capture.json"), "w", encoding="utf-8") as handle:
  1170. json.dump(payload, handle, ensure_ascii=False, indent=1)
  1171. self.plugin._log(f"Italy capture: {len(self.packets)} records written to {run}")
  1172. except Exception as exc:
  1173. self.notes.append(f"write failed: {type(exc).__name__}: {exc}")
  1174. try:
  1175. self.plugin._log(f"Italy capture write failed: {type(exc).__name__}: {exc}")
  1176. except Exception:
  1177. pass
  1178. def _enter_otp(plugin, otp):
  1179. field = _element_or_fail(plugin.page, "#otp-input")
  1180. if getattr(plugin, "mouse", None):
  1181. plugin.mouse.human_click_ele(field)
  1182. else:
  1183. field.click()
  1184. if getattr(plugin, "keyboard", None):
  1185. time.sleep(random.uniform(0.3, 0.6))
  1186. try:
  1187. plugin.keyboard.hotkey(Key.CONTROL, Key.A)
  1188. time.sleep(0.1)
  1189. plugin.keyboard.press(Key.BACKSPACE)
  1190. time.sleep(0.1)
  1191. except Exception:
  1192. pass
  1193. plugin.keyboard.type_text(otp, humanize=True)
  1194. time.sleep(random.uniform(0.4, 0.8))
  1195. else:
  1196. field.input(otp, clear=True)
  1197. if field.run_js("return this.value === arguments[0];", otp) is not True:
  1198. field.input(otp, clear=True)
  1199. if field.run_js("return this.value === arguments[0];", otp) is not True:
  1200. raise BizLogicError("Italy OTP write failed")
  1201. def validate_cloud_otp_environment(mail_config, recipient):
  1202. if mail_config is None:
  1203. raise NotFoundError("Italy OTP mail configuration is missing")
  1204. if not isinstance(mail_config, OtpMailConfig):
  1205. raise BizLogicError("Italy OTP mail configuration is invalid")
  1206. if not isinstance(recipient, str) or not _EMAIL_PATTERN.fullmatch(recipient.strip()):
  1207. raise BizLogicError("Italy OTP recipient is invalid")
  1208. mail_config.mailbox_for(recipient)
  1209. def fetch_otp_from_cloud(mail_config, *, recipient, sent_at, api=None):
  1210. try:
  1211. body = (api or CloudApi.Instance()).fetch_mail_content(
  1212. email=mail_config.mailbox_for(recipient), sender=mail_config.sender.strip(), recipient=recipient,
  1213. subject_keywords="OTP Code", body_keywords="OTP Code:",
  1214. sent_date=sent_at, expiry=mail_config.expiry_seconds, only_text=True,
  1215. )
  1216. except PermissionDeniedError as exc:
  1217. raise BizLogicError("Italy OTP mailbox not permitted") from exc
  1218. except (SessionExpiredOrInvalidError, RateLimiteddError, NotFoundError):
  1219. raise
  1220. except BizLogicError as exc:
  1221. status = re.search(r"\bHTTP Error (\d{3})\b", str(getattr(exc, "message", "")))
  1222. if status and int(status.group(1)) == 404:
  1223. raise NotFoundError("Italy OTP email not found or mailbox not authorized") from exc
  1224. if status and int(status.group(1)) in {408, 502, 503, 504}:
  1225. raise BizLogicError("Italy OTP mailbox provider timed out") from exc
  1226. raise BizLogicError("Italy OTP mailbox read failed") from exc
  1227. except Exception as exc:
  1228. raise BizLogicError("Italy OTP mailbox read failed") from exc
  1229. return parse_otp_body(body)
  1230. def _wait_for_calendar(page, timeout=30):
  1231. deadline = time.monotonic() + timeout
  1232. while time.monotonic() < deadline:
  1233. if urlparse(str(page.url or "")).path.rstrip("/") == CALENDAR_PATH:
  1234. page.wait.doc_loaded()
  1235. return
  1236. time.sleep(0.25)
  1237. raise BizLogicError("Italy AVANTI did not reach Calendar")
  1238. def prepare_calendar(plugin, route, *, role, inputs=None):
  1239. if role not in {"query", "book"} or route.capability is not ItaCapability.SINGLE_SCHENGEN_TOURISM:
  1240. raise BizLogicError("Italy Calendar preparation requires a tourism route")
  1241. cloud = plugin.free_config.get("otp_mode") == "cloud"
  1242. if not cloud and (plugin.free_config.get("otp_mode") != "manual" or not sys.stdin.isatty()):
  1243. raise BizLogicError("Italy Calendar requires automatic OTP or an attached terminal")
  1244. if not cloud and current_thread() is not main_thread():
  1245. raise BizLogicError("Italy manual OTP requires single-instance debugging on the main thread; "
  1246. "use otp_mode=cloud for workers")
  1247. username, page, booking_path = (
  1248. plugin.config.account.username.strip().casefold(), plugin.page, plugin._booking_path(route))
  1249. if cloud:
  1250. validate_cloud_otp_environment(route.mail, username)
  1251. def current():
  1252. if (plugin.page is not page or plugin.config.account.username.strip().casefold() != username
  1253. or same_origin_path(page.url) != booking_path):
  1254. raise SessionExpiredOrInvalidError("Italy preparation page or account changed")
  1255. def click(selector):
  1256. current()
  1257. plugin.mouse.human_click_ele(_element_or_fail(page, selector))
  1258. script = guarded_preflight_script(route, booking_path)
  1259. wait_for_booking_preflight(page, script=script, check_current=current, log=plugin._log)
  1260. if getattr(plugin, "_capture", None):
  1261. _capture(plugin).snapshot("before_otp", note=f"role={role}, form ready")
  1262. # Query uses only the dedicated query profile. Book never falls back to it.
  1263. form_inputs = ({spec.value_from: spec.placeholder for spec in route.additional_fields.values()}
  1264. if role == "query" else inputs)
  1265. fill_booking_form(plugin, route, _format_booking_dates(page, route, form_inputs))
  1266. if getattr(plugin, "_capture", None):
  1267. _capture(plugin).snapshot("form_filled", note=f"extra fields written, role={role}")
  1268. sent_at = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
  1269. click("#otp-send")
  1270. plugin._log(f"Italy {role} OTP sent.")
  1271. if cloud:
  1272. otp = fetch_otp_from_cloud(route.mail, recipient=username, sent_at=sent_at)
  1273. else:
  1274. otp = input("Italy booking OTP: ").strip()
  1275. if not re.fullmatch(r"[0-9]{4,12}", otp):
  1276. raise BizLogicError("Italy OTP format rejected")
  1277. current()
  1278. _enter_otp(plugin, otp)
  1279. wait_for_booking_preflight(
  1280. page, script=script, after_otp=True, check_current=current, log=plugin._log)
  1281. page.set.auto_handle_alert(accept=True)
  1282. try:
  1283. click("#btnAvanti")
  1284. _wait_for_calendar(page)
  1285. finally:
  1286. page.set.auto_handle_alert(on_off=False)
  1287. wait_for_calendar_service(plugin, page, route.service_id)
  1288. wait_for_calendar_anchor(plugin, page)
  1289. plugin._record_calendar_prepared_route(route, role=role, additional_inputs=form_inputs)
  1290. if getattr(plugin, "_capture", None):
  1291. _capture(plugin).snapshot("calendar", note=f"reached the calendar, role={role}")
  1292. _capture(plugin).write()
  1293. BOOKING_RECHECK_NONE_AVAILABLE_LABEL = "no-availability-after-user-recheck"
  1294. BOOKING_CALENDAR_PREFINAL_STOP_LABEL = "calendar-ready-before-final"
  1295. RADWARE_VALIDATION_HOSTS = frozenset({"validate.perfdrive.com"})
  1296. SERVICE_LINK_WAIT_SECONDS = 15
  1297. SERVICES_PROBE_QUERY_MODE = "services_probe"
  1298. CALENDAR_QUERY_MODE = "calendar"
  1299. @dataclass(frozen=True, repr=False, eq=False)
  1300. class _PreparedCalendar:
  1301. username: str
  1302. routing_key: str
  1303. runtime_service: str
  1304. tab_id: str
  1305. role: str
  1306. # The applicant inputs the extra form fields were last written from. This is what
  1307. # stops a calendar prepared for querying (placeholder values) from being reused to
  1308. # place a real booking.
  1309. additional_inputs: Mapping[str, Any] | None = None
  1310. initial_month: date | None = None
  1311. class ItaLoginFlow(BaseFlow):
  1312. """Italy page rules on the host flow engine; never resubmit credentials."""
  1313. def __init__(self, plugin):
  1314. self.plugin = plugin
  1315. self.entered = self.submitted = False
  1316. super().__init__(plugin.page, interval=3, logger=plugin._log, max_stuck_cycles=None)
  1317. def at(self, host, path):
  1318. url = urlparse(self.page.url)
  1319. return url.hostname == host and (url.path.rstrip("/") or "/") == path
  1320. @guard(predicate=lambda f: urlparse(f.page.url).hostname in RADWARE_VALIDATION_HOSTS, priority=1)
  1321. def radware(self):
  1322. raise BizLogicError("Italy authentication requires Radware browser validation")
  1323. @guard(predicate=lambda f: urlparse(f.page.url).scheme != "https" or
  1324. urlparse(f.page.url).netloc.lower() not in {
  1325. "prenotami.esteri.it", "iam.esteri.it", "prenotami.esteri.it:443", "iam.esteri.it:443"}, priority=2)
  1326. def foreign_origin(self):
  1327. raise BizLogicError("Italy authentication left the expected website")
  1328. @guard(predicate=lambda f: any(value in f.page.html for value in (
  1329. "Sorry, you have been blocked", "You are being rate limited", "error 1020")), priority=3)
  1330. def blocked(self):
  1331. if "You are being rate limited" in self.page.html:
  1332. raise RateLimiteddError("Italy authentication rate limited")
  1333. raise BizLogicError("Italy authentication was blocked")
  1334. @guard(predicate=lambda f: any(value in f.page.html for value in (
  1335. "502 Bad Gateway", "503 Service Temporarily Unavailable")), priority=4)
  1336. def server_error(self):
  1337. raise BizLogicError("Italy authentication server is temporarily unavailable")
  1338. @guard(title="just a moment", priority=5)
  1339. @guard(title="cloudflare", priority=5)
  1340. def verification_pending(self):
  1341. self.log("Waiting for the website's human verification")
  1342. time.sleep(5)
  1343. @on(ele=".app-menu", predicate=lambda f: f.at("prenotami.esteri.it", "/UserArea") and
  1344. bool(f.page.ele("@href=/Services", timeout=0.2)), priority=20)
  1345. @done
  1346. def authenticated(self):
  1347. self.plugin._log("Successfully reached authenticated /UserArea.")
  1348. self.plugin.session_create_time = time.time()
  1349. self.plugin.is_healthy = True
  1350. @on(ele="#pingid-button", predicate=lambda f: f.at("prenotami.esteri.it", "/") and not f.entered, priority=30)
  1351. def enter(self):
  1352. self.entered = True
  1353. self.plugin.mouse.human_click_ele(self.page.ele("#pingid-button"))
  1354. self.plugin._log("Redirecting to PingID...")
  1355. self.plugin._random_sleep(3, 5)
  1356. @on(ele="@name=callback_1", predicate=lambda f: f.at("iam.esteri.it", "/signin") and not f.submitted, priority=40)
  1357. def credentials(self):
  1358. username, password, submit = [self.page.ele(selector, timeout=0.2) for selector in
  1359. ("@name=callback_1", "@name=callback_2", "tag:button@type=submit")]
  1360. if not all((username, password, submit)):
  1361. return # IAM can render its controls in separate updates.
  1362. for label, element, value in (("username", username, self.plugin.config.account.username),
  1363. ("password", password, self.plugin.config.account.password)):
  1364. self.plugin._log(f"Entering {label}...")
  1365. self.plugin.mouse.human_click_ele(element)
  1366. if getattr(self.plugin, "keyboard", None):
  1367. time.sleep(random.uniform(0.2, 0.5))
  1368. try:
  1369. self.plugin.keyboard.hotkey(Key.CONTROL, Key.A)
  1370. time.sleep(0.1)
  1371. self.plugin.keyboard.press(Key.BACKSPACE)
  1372. time.sleep(0.1)
  1373. except Exception:
  1374. pass
  1375. self.plugin.keyboard.type_text(value, humanize=True)
  1376. time.sleep(random.uniform(0.4, 0.8))
  1377. else:
  1378. element.input(value, clear=True)
  1379. time.sleep(1)
  1380. self.submitted = True
  1381. self.plugin.mouse.human_click_ele(submit)
  1382. self.plugin._log("Login form submitted. Waiting for dashboard to load...")
  1383. self.plugin._random_sleep(5, 7)
  1384. class ItaPlugin(IPlugin):
  1385. name = "ita_plugin"
  1386. version = "1.0.3"
  1387. description = "意大利 Prenot@Mi 查询与预约插件"
  1388. def __init__(self, group_id):
  1389. self.group_id = group_id
  1390. self.config = None
  1391. self.free_config = {}
  1392. self.is_healthy = True
  1393. self.logger = self.page = self.tunnel = None
  1394. self.mouse = self.keyboard = self.scroll = None
  1395. self.ita_url = "https://prenotami.esteri.it"
  1396. self.instance_id = uuid.uuid4().hex[:8]
  1397. self.root_workspace = os.path.abspath(
  1398. os.path.join("data/temp_browser_data", f"{group_id}.{self.instance_id}"))
  1399. self.user_data_path = os.path.join(self.root_workspace, "user_data")
  1400. os.makedirs(self.root_workspace, exist_ok=True)
  1401. self.session_create_time = 0
  1402. self._prepared_calendar = None
  1403. self._inspection_hold_reason = None
  1404. self._capture = None # TEMPORARY diagnostic capture, see ItaCapture
  1405. def set_log(self, logger):
  1406. self.logger = logger
  1407. def _log(self, message):
  1408. (self.logger or print)(f"[ItaPlugin] [{self.group_id}] {message}")
  1409. def _random_sleep(self, minimum=30, maximum=60):
  1410. seconds = random.uniform(minimum, maximum)
  1411. self._log(f"Anti-Rate-Limit: Sleeping for {seconds:.2f} seconds...")
  1412. time.sleep(seconds)
  1413. def set_config(self, config):
  1414. self.config, self.free_config = config, config.free_config or {}
  1415. query_mode = self.free_config.get("query_mode", CALENDAR_QUERY_MODE)
  1416. otp_mode = self.free_config.get("otp_mode")
  1417. final = self.free_config.get("final_submit_enabled", False)
  1418. inspection = self.free_config.get("inspection_mode", False)
  1419. if query_mode not in {SERVICES_PROBE_QUERY_MODE, CALENDAR_QUERY_MODE}:
  1420. raise BizLogicError("Italy query_mode is invalid")
  1421. if otp_mode not in {None, "manual", "cloud"}:
  1422. raise BizLogicError("Italy otp_mode is invalid")
  1423. if final is not False and not (
  1424. final is True and otp_mode in {"manual", "cloud"}):
  1425. raise BizLogicError("Italy final submission configuration is invalid")
  1426. if type(inspection) is not bool:
  1427. raise BizLogicError("Italy inspection_mode must be a boolean")
  1428. local_proxy_port = self.free_config.get("local_proxy_port")
  1429. if local_proxy_port is not None and (
  1430. type(local_proxy_port) is not int or not 1 <= local_proxy_port <= 65535):
  1431. raise BizLogicError("Italy local_proxy_port must be an integer from 1 to 65535")
  1432. self._prepared_calendar = None
  1433. def _browser_proxy(self):
  1434. local_proxy_port = self.free_config.get("local_proxy_port")
  1435. if local_proxy_port is not None:
  1436. # Standalone still fetches a pool proxy; this only changes the browser route.
  1437. return Proxy(proto="http", ip="127.0.0.1", port=local_proxy_port)
  1438. return self.config.proxy
  1439. def _resolve_route(self, apt_type):
  1440. try:
  1441. return resolve_route_config(
  1442. self.free_config.get("apt_configs", {}), apt_type.routing_key)
  1443. except AttributeError as exc:
  1444. raise BizLogicError("Italy appointment type requires a routing_key") from exc
  1445. @staticmethod
  1446. def _booking_path(route):
  1447. return f"/Services/Booking/{route.service_id}"
  1448. def _authenticated_path(self):
  1449. host = urlparse(str(self.page.url or "")).hostname
  1450. if host == "iam.esteri.it" or self.page.ele("#pingid-button", timeout=0.2):
  1451. raise SessionExpiredOrInvalidError("Italy Session expired")
  1452. return same_origin_path(self.page.url)
  1453. def _calendar_context_matches(self, route, *, role, username=None):
  1454. state = self._prepared_calendar
  1455. try:
  1456. account = self.config.account.username.strip().casefold()
  1457. return (
  1458. isinstance(state, _PreparedCalendar) and self.is_healthy and
  1459. state.tab_id == self.page.tab_id and state.username == account and
  1460. state.routing_key == route.routing_key and state.role == role and
  1461. (username is None or username == account)
  1462. )
  1463. except (AttributeError, TypeError):
  1464. return False
  1465. def _record_calendar_prepared_route(self, route, *, role, additional_inputs=None):
  1466. self._prepared_calendar = None
  1467. values = {spec.value_from: str((additional_inputs or {}).get(spec.value_from, ""))
  1468. for spec in route.additional_fields.values()}
  1469. service, anchor = read_calendar_context(self.page)
  1470. self._prepared_calendar = _PreparedCalendar(
  1471. self.config.account.username.strip().casefold(), route.routing_key,
  1472. service, self.page.tab_id, role, values, anchor.replace(day=1))
  1473. def _discard_stale_booking_form(self, route, inputs):
  1474. """A query form or changed applicant values require a fresh booking form."""
  1475. state = self._prepared_calendar
  1476. if not route.additional_fields or state is None:
  1477. return
  1478. if (state.role != "book" or state.additional_inputs is None or any(
  1479. not inputs.get(spec.value_from) or
  1480. state.additional_inputs.get(spec.value_from) != str(inputs[spec.value_from])
  1481. for spec in route.additional_fields.values())):
  1482. self._prepared_calendar = None
  1483. def _require_calendar_context(self, route, *, role, username=None):
  1484. if not self._calendar_context_matches(route, role=role, username=username):
  1485. raise SessionExpiredOrInvalidError("Italy Calendar Session changed")
  1486. if read_calendar_context(self.page)[0] != self._prepared_calendar.runtime_service:
  1487. raise SessionExpiredOrInvalidError("Italy Calendar service changed")
  1488. return self._prepared_calendar
  1489. def _invalidate_query_session(self):
  1490. self.is_healthy = False
  1491. self._prepared_calendar = None
  1492. def _freeze_for_inspection(self, exc):
  1493. if self.free_config.get("inspection_mode") is not True or not self.page:
  1494. return
  1495. reason = getattr(exc, "message", None) or str(exc)
  1496. self._inspection_hold_reason = f"{type(exc).__name__}: {reason}"
  1497. self.keep_browser_on_error = True
  1498. self._log(f"Italy inspection hold activated: {self._inspection_hold_reason}")
  1499. def _raise_if_inspection_held(self):
  1500. reason = getattr(self, "_inspection_hold_reason", None)
  1501. if reason:
  1502. raise BizLogicError(f"Italy inspection hold: {reason}")
  1503. def keep_alive(self):
  1504. pass
  1505. def health_check(self):
  1506. if not self.is_healthy or not self.page:
  1507. return False
  1508. try:
  1509. alive = self.page.run_js("return 1;") == 1
  1510. except Exception:
  1511. alive = False
  1512. expired = self.config.session_max_life > 0 and (
  1513. time.time() - self.session_create_time > self.config.session_max_life)
  1514. if not alive or expired:
  1515. self._invalidate_query_session()
  1516. return False
  1517. return True
  1518. def create_session(self):
  1519. try:
  1520. self._log(f"Initializing Session (ID: {self.instance_id})...")
  1521. self._invalidate_query_session()
  1522. account = getattr(self.config, "account", None)
  1523. username, password = getattr(account, "username", None), getattr(account, "password", None)
  1524. if (not isinstance(username, str) or not username.strip()
  1525. or not isinstance(password, str) or not password):
  1526. raise BizLogicError("Italy website username and password are required")
  1527. proxy = self._browser_proxy()
  1528. local_proxy_port = self.free_config.get("local_proxy_port")
  1529. if local_proxy_port is not None:
  1530. self._log(f"Using local HTTP proxy at 127.0.0.1:{local_proxy_port}")
  1531. options, self.tunnel = init_chromium_options(
  1532. user_data_path=self.user_data_path,
  1533. username_seed=self.config.account.username,
  1534. proxy=proxy,
  1535. headless=False,
  1536. window_size="1920,1080",
  1537. logger=self._log,
  1538. )
  1539. self.page = ChromiumPage(options)
  1540. try:
  1541. page_set = getattr(self.page, "set", None)
  1542. if page_set and hasattr(page_set, "auto_handle_alert"):
  1543. self.page.set.auto_handle_alert(accept=True)
  1544. if hasattr(self.page, "run_cdp"):
  1545. self.page.run_cdp(
  1546. "Page.addScriptToEvaluateOnNewDocument",
  1547. source="""
  1548. window.alert = function(msg){ console.log('[Alert suppressed]:', msg); };
  1549. try { Object.defineProperty(navigator, 'webdriver', {get: () => undefined}); } catch(e){}
  1550. """
  1551. )
  1552. except Exception as exc:
  1553. self._log(f"Italy CDP alert & stealth suppression non-fatal: {exc}")
  1554. self.page.get(self.ita_url)
  1555. # TEMPORARY diagnostic capture (see ItaCapture); no-op unless configured.
  1556. self._capture = ItaCapture(self)
  1557. self.scroll = HumanScroll(self.page)
  1558. profile_name = random.choice(list(MOUSE_PROFILES.keys())) if MOUSE_PROFILES else "average_joe"
  1559. self._log(f"[HumanMouse] profile selected: {profile_name}")
  1560. # Drawing a debug canvas can lose its JS context during navigation and abort login.
  1561. self.mouse = HumanMouse(self.page, timing=MOUSE_PROFILES.get(profile_name), debug=False)
  1562. self.keyboard = HumanKeyboard(self.page)
  1563. width, height = self.page.rect.viewport_size
  1564. self.mouse.move(random.randint(10, width - 10), random.randint(10, height - 10), humanize=True)
  1565. flow = ItaLoginFlow(self)
  1566. flow.run(max_steps=40, timeout=120)
  1567. except PageDisconnectedError as exc:
  1568. raise SessionExpiredOrInvalidError("Italy browser disconnected during session creation") from exc
  1569. except ElementNotFoundError as exc:
  1570. error = NotFoundError("Italy login element is missing")
  1571. self._freeze_for_inspection(error)
  1572. raise error from exc
  1573. except (SessionExpiredOrInvalidError, RateLimiteddError, NotFoundError, BizLogicError) as exc:
  1574. self._freeze_for_inspection(exc)
  1575. raise
  1576. except Exception as exc:
  1577. error = BizLogicError(f"Italy session creation failed: {type(exc).__name__}")
  1578. self._freeze_for_inspection(error)
  1579. raise error from exc
  1580. def query(self, apt_type):
  1581. self._raise_if_inspection_held()
  1582. try:
  1583. if self.free_config.get("query_mode", CALENDAR_QUERY_MODE) != CALENDAR_QUERY_MODE:
  1584. raise BizLogicError("Italy Query requires query_mode=calendar")
  1585. if not self.health_check():
  1586. raise SessionExpiredOrInvalidError("Italy Query Session is unavailable")
  1587. route = self._resolve_route(apt_type)
  1588. if self._prepared_calendar is None or same_origin_path(self.page.url) != CALENDAR_PATH:
  1589. if not self._enter_service(route):
  1590. return QueryResult(success=False, apt_type=apt_type,
  1591. availability_status=AvailabilityStatus.NoneAvailable)
  1592. self._prepared_calendar = None
  1593. prepare_calendar(self, route, role="query")
  1594. if self._prepared_calendar is not None and self._prepared_calendar.role == "book":
  1595. self._prepared_calendar = replace(
  1596. self._require_calendar_context(route, role="book"), role="query")
  1597. self._require_calendar_context(route, role="query")
  1598. probes = scan_until_first_available_month(self, route)
  1599. available = next((days for days in probes if days), ())
  1600. if not available:
  1601. return QueryResult(
  1602. success=False, apt_type=apt_type,
  1603. availability_status=AvailabilityStatus.NoneAvailable)
  1604. earliest = datetime.combine(available[0].day, datetime.min.time())
  1605. self._log(f"Italy Calendar earliest_date={earliest.date().isoformat()}")
  1606. return QueryResult(
  1607. success=True, apt_type=apt_type,
  1608. availability_status=AvailabilityStatus.Available,
  1609. earliest_date=earliest,
  1610. availability=[DateAvailability(date=earliest, times=[])])
  1611. except PageDisconnectedError as exc:
  1612. raise SessionExpiredOrInvalidError("Italy browser disconnected during Query") from exc
  1613. except ElementNotFoundError as exc:
  1614. error = NotFoundError("Italy Query element is missing")
  1615. self._freeze_for_inspection(error)
  1616. raise error from exc
  1617. except (SessionExpiredOrInvalidError, RateLimiteddError, NotFoundError, BizLogicError) as exc:
  1618. self._freeze_for_inspection(exc)
  1619. if getattr(self, "_capture", None):
  1620. self._capture.error("query_failed", exc)
  1621. self._capture.write()
  1622. raise
  1623. except Exception as exc:
  1624. error = BizLogicError(f"Italy Query failed: {type(exc).__name__}")
  1625. self._freeze_for_inspection(error)
  1626. if getattr(self, "_capture", None):
  1627. self._capture.error("query_failed", exc)
  1628. self._capture.write()
  1629. raise error from exc
  1630. def _enter_service(self, route) -> bool:
  1631. booking_path = self._booking_path(route)
  1632. self.page.get(f"{self.ita_url}/Services")
  1633. self.page.wait.doc_loaded()
  1634. capture = _capture(self)
  1635. capture.start()
  1636. if self._authenticated_path() != "/Services":
  1637. raise BizLogicError("Italy service list is unavailable")
  1638. link = self.page.ele(f"tag:a@@href={booking_path}", timeout=SERVICE_LINK_WAIT_SECONDS)
  1639. if not link:
  1640. raise NotFoundError(f"Italy service link is missing: {booking_path}")
  1641. click_started_at = datetime.now(timezone.utc).isoformat()
  1642. try:
  1643. self.mouse.human_click_ele(link)
  1644. self.page.wait.load_start()
  1645. self.page.wait.doc_loaded()
  1646. path = self._authenticated_path()
  1647. if path == "/Services":
  1648. marker = self.page.ele("#WlNotAvailable", timeout=1)
  1649. notice = self.page.ele(".jconfirm-content", timeout=1)
  1650. marker_text = (marker.attr("value") if marker else "") or ""
  1651. notice_text = (notice.text if notice else "") or ""
  1652. text = (marker_text or notice_text).strip().casefold()
  1653. no_availability = "posti disponibili" in text
  1654. outcome = "no availability" if no_availability else "unknown service response"
  1655. else:
  1656. outcome = "booking page" if path == booking_path else "unexpected destination"
  1657. except Exception as exc:
  1658. # Diagnostics preserve the original failure for the framework to handle.
  1659. capture.snapshot("after_prenota", force=True,
  1660. note=f"click_started_at={click_started_at}; click/navigation failed: "
  1661. f"{type(exc).__name__}: {exc}")
  1662. capture.write()
  1663. raise
  1664. capture.snapshot("after_prenota",
  1665. note=f"click_started_at={click_started_at}; PRENOTA click returned; {outcome}")
  1666. if path == "/Services":
  1667. if no_availability:
  1668. capture.write()
  1669. return False # The service explicitly reports no availability.
  1670. raise BizLogicError(
  1671. "Italy service returned an unknown response",
  1672. extra={"safe_diagnostic": {
  1673. "path": path,
  1674. "availability_marker_present": bool(marker),
  1675. "notice_present": bool(notice),
  1676. }},
  1677. )
  1678. if path == booking_path and getattr(self, "_capture", None):
  1679. self._capture.snapshot("booking_form", note="entered booking page before contract validation")
  1680. if path != booking_path or not self.page.ele("#bookingForm", timeout=2):
  1681. raise BizLogicError("Italy booking form is unavailable")
  1682. wait_for_booking_contract(
  1683. self.page, script=guarded_preflight_script(route, booking_path, readonly=True))
  1684. return True
  1685. def book(self, slot_info, user_inputs=None):
  1686. self._raise_if_inspection_held()
  1687. try:
  1688. if not self.page or not isinstance(slot_info, QueryResult):
  1689. raise BizLogicError("Italy Book requires a Query signal")
  1690. route = self._resolve_route(slot_info.apt_type)
  1691. inputs = user_inputs if isinstance(user_inputs, Mapping) else {}
  1692. username = str(inputs.get("username", "")).strip().casefold()
  1693. if not username or username != self.config.account.username.strip().casefold():
  1694. raise BizLogicError("Italy Book requires the applicant account")
  1695. if self._prepared_calendar is not None:
  1696. state = self._require_calendar_context(
  1697. route, role=self._prepared_calendar.role, username=username)
  1698. self._discard_stale_booking_form(route, inputs)
  1699. if self._prepared_calendar is not None:
  1700. self._prepared_calendar = replace(state, role="book")
  1701. if self._prepared_calendar is None:
  1702. if not self._enter_service(route):
  1703. return BookResult(success=False, label=BOOKING_RECHECK_NONE_AVAILABLE_LABEL)
  1704. prepare_calendar(self, route, role="book", inputs=inputs)
  1705. start, end = book_date_preferences(inputs)
  1706. slot = recheck_calendar_before_final(
  1707. self, route, start, end,
  1708. session_is_current=lambda: self._calendar_context_matches(
  1709. route, role="book", username=username),
  1710. )
  1711. if not slot:
  1712. return BookResult(success=False, label=BOOKING_RECHECK_NONE_AVAILABLE_LABEL)
  1713. self._log(f"Italy Book selected {slot.day} {slot.time_label}")
  1714. if self.free_config.get("final_submit_enabled") is True:
  1715. return _submit_final_once(self, route, slot)
  1716. self._freeze_for_inspection(
  1717. BizLogicError("Italy selected slot is waiting for final inspection"))
  1718. return BookResult(success=False, label=BOOKING_CALENDAR_PREFINAL_STOP_LABEL)
  1719. except PageDisconnectedError as exc:
  1720. raise SessionExpiredOrInvalidError("Italy browser disconnected during Book") from exc
  1721. except ElementNotFoundError as exc:
  1722. error = NotFoundError("Italy Book element is missing")
  1723. self._freeze_for_inspection(error)
  1724. raise error from exc
  1725. except (SessionExpiredOrInvalidError, RateLimiteddError, NotFoundError, BizLogicError) as exc:
  1726. self._freeze_for_inspection(exc)
  1727. if getattr(self, "_capture", None):
  1728. self._capture.error("book_failed", exc)
  1729. self._capture.write()
  1730. raise
  1731. except Exception as exc:
  1732. error = BizLogicError(f"Italy Book failed: {type(exc).__name__}")
  1733. self._freeze_for_inspection(error)
  1734. if getattr(self, "_capture", None):
  1735. self._capture.error("book_failed", exc)
  1736. self._capture.write()
  1737. raise error from exc
  1738. def cleanup(self):
  1739. if getattr(self, "keep_browser_on_error", False):
  1740. self._log("Italy debug browser retained after test failure.")
  1741. return
  1742. self._invalidate_query_session()
  1743. cleanup_browser_workspace(
  1744. self.page, self.root_workspace, self.tunnel, self._log)
  1745. self.page = self.tunnel = None
  1746. def get_plugin_status(self):
  1747. proxy = self._browser_proxy() if self.config else None
  1748. current_url = ""
  1749. if self.page:
  1750. try:
  1751. current_url = str(self.page.url)
  1752. except Exception:
  1753. pass # Status reporting must still work after browser disconnection.
  1754. return PluginStatus(
  1755. instance_id=self.instance_id,
  1756. is_healthy=self.is_healthy,
  1757. state="running" if self.is_healthy else "unhealthy",
  1758. current_url=current_url,
  1759. account=self.config.account.username if self.config else "",
  1760. proxy=f"{proxy.ip}:{proxy.port}" if proxy and proxy.ip else "",
  1761. session_age_sec=max(0, int(time.time() - self.session_create_time))
  1762. if self.session_create_time > 0 else 0,
  1763. last_active_at=utc_now_str(),
  1764. )
  1765. def __del__(self):
  1766. try:
  1767. self.cleanup()
  1768. except Exception:
  1769. pass