debug_ita_query.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. #!/usr/bin/env python3
  2. """Run one isolated, route-configured Italy-plugin availability probe.
  3. This debug entrypoint logs in, calls ``ItaPlugin.query()`` exactly once for the
  4. configured route, and then cleans up. It never calls ``book()``
  5. and it does not retry the query.
  6. """
  7. from __future__ import annotations
  8. import argparse
  9. import json
  10. import os
  11. import re
  12. import sys
  13. from dataclasses import dataclass
  14. from pathlib import Path
  15. from typing import Mapping
  16. REPO_ROOT = Path(__file__).resolve().parents[1]
  17. if str(REPO_ROOT) not in sys.path:
  18. sys.path.insert(0, str(REPO_ROOT))
  19. from plugins.ita_plugin import BOOKING_FORM_DISCOVERY_SCRIPT, ItaPlugin
  20. from tools.debug_ita_session import (
  21. DebugConfigurationError,
  22. _env_text,
  23. _get_credentials,
  24. _text_for_log,
  25. _url_for_log,
  26. build_plugin_config,
  27. resolve_chrome_path,
  28. )
  29. from utils.local_account import (
  30. LocalAccountConfigurationError,
  31. load_local_account,
  32. )
  33. from utils.ita_local_profile import (
  34. ITA_QUERY_ACCOUNT_FILE,
  35. ItaLocalProfileError,
  36. prepare_ita_query_profile,
  37. )
  38. from vs_types import AppointmentType, AvailabilityStatus, BizLogicError, VSPlgConfig
  39. DEFAULT_ITA_SERVICE_ID = 1321
  40. DEFAULT_ITA_ROUTING_KEY = "slot.dub.it.tourist"
  41. DEFAULT_ITA_CITY = "Dublin"
  42. DEFAULT_ITA_VISA_TYPE = "Tourist"
  43. DEBUG_ROUTE_CAPABILITY = "services_probe_only"
  44. DEBUG_ROUTE_ENV_NAMES = (
  45. "ITA_ROUTING_KEY",
  46. "ITA_SERVICE_ID",
  47. "ITA_CITY",
  48. "ITA_VISA_TYPE",
  49. )
  50. LOCAL_QUERY_ACCOUNT_FILE = ITA_QUERY_ACCOUNT_FILE
  51. _EMAIL_PATTERN = re.compile(r"\b[^\s@]+@[^\s@]+\.[^\s@]+\b")
  52. _LONG_TOKEN_PATTERN = re.compile(r"\b[A-Za-z0-9_-]{24,}\b")
  53. _LONG_NUMBER_PATTERN = re.compile(r"\b\d{6,}\b")
  54. _BOOKING_FORM_DISCOVERY_SCRIPT = BOOKING_FORM_DISCOVERY_SCRIPT
  55. @dataclass(frozen=True, slots=True)
  56. class ItaDebugRoute:
  57. routing_key: str
  58. service_id: int
  59. city: str
  60. visa_type: str
  61. @property
  62. def booking_path(self) -> str:
  63. return f"/Services/Booking/{self.service_id}"
  64. def appointment_type(self) -> AppointmentType:
  65. return AppointmentType(
  66. routing_key=self.routing_key,
  67. country="Italy",
  68. city=self.city,
  69. visa_type=self.visa_type,
  70. )
  71. def apt_config(self) -> dict:
  72. return {
  73. "service_id": self.service_id,
  74. "capability": DEBUG_ROUTE_CAPABILITY,
  75. }
  76. def resolve_debug_route(env: Mapping[str, str]) -> ItaDebugRoute:
  77. """Resolve one explicit debug route, retaining Dublin only as a default."""
  78. route_values = {
  79. name: _env_text(env, name)
  80. for name in DEBUG_ROUTE_ENV_NAMES
  81. }
  82. configured_names = {
  83. name for name, value in route_values.items() if value
  84. }
  85. if configured_names and configured_names != set(DEBUG_ROUTE_ENV_NAMES):
  86. raise DebugConfigurationError(
  87. "Italy route override must set ITA_ROUTING_KEY, ITA_SERVICE_ID, "
  88. "ITA_CITY, and ITA_VISA_TYPE together"
  89. )
  90. routing_key = (
  91. route_values["ITA_ROUTING_KEY"] or DEFAULT_ITA_ROUTING_KEY
  92. )
  93. raw_service_id = route_values["ITA_SERVICE_ID"]
  94. try:
  95. service_id = (
  96. int(raw_service_id)
  97. if raw_service_id
  98. else DEFAULT_ITA_SERVICE_ID
  99. )
  100. except ValueError as exc:
  101. raise DebugConfigurationError(
  102. "ITA_SERVICE_ID must be a positive integer"
  103. ) from exc
  104. if service_id <= 0:
  105. raise DebugConfigurationError(
  106. "ITA_SERVICE_ID must be a positive integer"
  107. )
  108. city = route_values["ITA_CITY"] or DEFAULT_ITA_CITY
  109. visa_type = route_values["ITA_VISA_TYPE"] or DEFAULT_ITA_VISA_TYPE
  110. return ItaDebugRoute(
  111. routing_key=routing_key,
  112. service_id=service_id,
  113. city=city,
  114. visa_type=visa_type,
  115. )
  116. def _get_query_credentials(
  117. env: Mapping[str, str],
  118. *,
  119. non_interactive: bool,
  120. ) -> tuple[str, str]:
  121. """Prefer the ignored local Query account, then preserve the old fallback."""
  122. if LOCAL_QUERY_ACCOUNT_FILE.exists():
  123. try:
  124. account = load_local_account(LOCAL_QUERY_ACCOUNT_FILE)
  125. except LocalAccountConfigurationError as exc:
  126. raise DebugConfigurationError(
  127. f"Local Italy Query account config is invalid: {exc}"
  128. ) from exc
  129. return account.username, account.password
  130. return _get_credentials(env, non_interactive=non_interactive)
  131. def _is_weak_entry_signal(result: object) -> bool:
  132. return bool(
  133. getattr(result, "success", False)
  134. and getattr(result, "availability_status", None)
  135. == AvailabilityStatus.NoneAvailable
  136. and getattr(result, "earliest_date", None) is None
  137. and not getattr(result, "availability", None)
  138. )
  139. def _is_nonempty_query_result(result: object) -> bool:
  140. return bool(
  141. getattr(result, "success", False)
  142. or getattr(result, "availability_status", None)
  143. != AvailabilityStatus.NoneAvailable
  144. or getattr(result, "earliest_date", None) is not None
  145. or bool(getattr(result, "availability", None))
  146. )
  147. def build_query_config(
  148. env: Mapping[str, str],
  149. *,
  150. username: str,
  151. password: str,
  152. persistent_profile_path: Path | None = None,
  153. route: ItaDebugRoute | None = None,
  154. ) -> VSPlgConfig:
  155. """Build a safe CS2 config for exactly one resolved route."""
  156. resolved_route = route or resolve_debug_route(env)
  157. config = build_plugin_config(
  158. env,
  159. username=username,
  160. password=password,
  161. )
  162. config.free_config = {
  163. "query_mode": "services_probe",
  164. "final_submit_enabled": False,
  165. "apt_configs": {
  166. resolved_route.routing_key: resolved_route.apt_config(),
  167. },
  168. }
  169. if persistent_profile_path is not None:
  170. config.free_config = {
  171. **config.free_config,
  172. "persistent_browser_profile_path": str(persistent_profile_path),
  173. }
  174. return config
  175. def _prepare_query_profile(username: str) -> Path:
  176. try:
  177. return prepare_ita_query_profile(username)
  178. except ItaLocalProfileError as exc:
  179. raise DebugConfigurationError(
  180. "Local Italy Query browser profile is invalid"
  181. ) from exc
  182. def _appointment_type(
  183. route: ItaDebugRoute | None = None,
  184. ) -> AppointmentType:
  185. return (route or resolve_debug_route({})).appointment_type()
  186. def _popup_text_for_log(value: object, *, limit: int = 300) -> str:
  187. """Keep a short diagnostic notice while redacting likely identifiers."""
  188. sanitized = re.sub(r"\s+", " ", _text_for_log(value)).strip()
  189. sanitized = _EMAIL_PATTERN.sub("<redacted-email>", sanitized)
  190. sanitized = _LONG_TOKEN_PATTERN.sub("<redacted-token>", sanitized)
  191. sanitized = _LONG_NUMBER_PATTERN.sub("<redacted-number>", sanitized)
  192. if len(sanitized) > limit:
  193. return f"{sanitized[:limit]}...<truncated>"
  194. return sanitized
  195. def _metadata_text(value: object, *, limit: int = 120) -> str | None:
  196. if value is None:
  197. return None
  198. text = re.sub(r"\s+", " ", _text_for_log(value)).strip()
  199. text = _EMAIL_PATTERN.sub("<redacted-email>", text)
  200. text = _LONG_NUMBER_PATTERN.sub("<redacted-number>", text)
  201. if len(text) > limit:
  202. return f"{text[:limit]}...<truncated>"
  203. return text or None
  204. def _sanitize_booking_form_structure(raw: object) -> dict | None:
  205. """Allowlist structural metadata and discard every field value."""
  206. if not isinstance(raw, dict):
  207. return None
  208. raw_form = raw.get("form") if isinstance(raw.get("form"), dict) else {}
  209. raw_fields = raw.get("fields") if isinstance(raw.get("fields"), list) else []
  210. raw_buttons = (
  211. raw.get("buttons") if isinstance(raw.get("buttons"), list) else []
  212. )
  213. fields = []
  214. for item in raw_fields[:100]:
  215. if not isinstance(item, dict):
  216. continue
  217. fields.append(
  218. {
  219. "tag": _metadata_text(item.get("tag")),
  220. "type": _metadata_text(item.get("type")),
  221. "id": _metadata_text(item.get("id")),
  222. "name": _metadata_text(item.get("name")),
  223. "required": bool(item.get("required")),
  224. }
  225. )
  226. buttons = []
  227. for item in raw_buttons[:50]:
  228. if not isinstance(item, dict):
  229. continue
  230. buttons.append(
  231. {
  232. "tag": _metadata_text(item.get("tag")),
  233. "type": _metadata_text(item.get("type")),
  234. "id": _metadata_text(item.get("id")),
  235. "name": _metadata_text(item.get("name")),
  236. "text": _metadata_text(item.get("text")),
  237. }
  238. )
  239. return {
  240. "path": _metadata_text(raw.get("path")),
  241. "form": {
  242. "method": _metadata_text(raw_form.get("method")),
  243. "action_path": _metadata_text(raw_form.get("action_path")),
  244. },
  245. "field_count": int(raw.get("field_count") or len(raw_fields)),
  246. "fields_truncated": bool(
  247. raw.get("fields_truncated") or len(raw_fields) > 100
  248. ),
  249. "fields": fields,
  250. "button_count": int(raw.get("button_count") or len(raw_buttons)),
  251. "buttons_truncated": bool(
  252. raw.get("buttons_truncated") or len(raw_buttons) > 50
  253. ),
  254. "buttons": buttons,
  255. }
  256. def _print_booking_form_structure(
  257. plugin: ItaPlugin,
  258. *,
  259. raw_structure: object = None,
  260. route: ItaDebugRoute | None = None,
  261. ) -> None:
  262. """Read structure only; never read field values or submit the form."""
  263. page = getattr(plugin, "page", None)
  264. if page is None:
  265. print("ITA_BOOKING_FORM_STRUCTURE=not-available")
  266. return
  267. try:
  268. if raw_structure is None:
  269. inspector = getattr(plugin, "_inspect_booking_form_structure", None)
  270. raw_structure = (
  271. inspector((route or resolve_debug_route({})).booking_path)
  272. if callable(inspector)
  273. else page.run_js(_BOOKING_FORM_DISCOVERY_SCRIPT)
  274. )
  275. structure = _sanitize_booking_form_structure(
  276. raw_structure
  277. )
  278. if structure is None:
  279. print("ITA_BOOKING_FORM_STRUCTURE=not-available")
  280. return
  281. print(
  282. "ITA_BOOKING_FORM_STRUCTURE="
  283. + json.dumps(structure, ensure_ascii=False, sort_keys=True)
  284. )
  285. except Exception as exc:
  286. print(
  287. "ITA_BOOKING_FORM_STRUCTURE=unavailable "
  288. f"type={type(exc).__name__} message={_text_for_log(exc)}"
  289. )
  290. def _print_failure_context(plugin: ItaPlugin) -> None:
  291. """Print sanitized text diagnostics; never save Query screenshots."""
  292. page = getattr(plugin, "page", None)
  293. if page is None:
  294. print("diagnostic_page=not-created")
  295. return
  296. try:
  297. print(f"diagnostic_url={_url_for_log(page.url)}")
  298. except Exception as exc:
  299. print(
  300. "diagnostic_url=unavailable "
  301. f"({type(exc).__name__}: {_text_for_log(exc)})"
  302. )
  303. try:
  304. title = _popup_text_for_log(page.title, limit=120)
  305. print(
  306. f"diagnostic_title={title}"
  307. if title
  308. else "diagnostic_title=not-present"
  309. )
  310. except Exception as exc:
  311. print(
  312. "diagnostic_title=unavailable "
  313. f"({type(exc).__name__}: {_text_for_log(exc)})"
  314. )
  315. try:
  316. popup = page.ele(".jconfirm-content", timeout=0.5)
  317. popup_text = _popup_text_for_log(popup.text if popup else "")
  318. print(
  319. f"diagnostic_popup_text={popup_text}"
  320. if popup_text
  321. else "diagnostic_popup_text=not-present"
  322. )
  323. except Exception as exc:
  324. print(
  325. "diagnostic_popup_text=unavailable "
  326. f"({type(exc).__name__}: {_text_for_log(exc)})"
  327. )
  328. def run_query_once(env: Mapping[str, str], *, non_interactive: bool, production_workflow: bool = False) -> int:
  329. if production_workflow and non_interactive:
  330. raise DebugConfigurationError("Production Calendar preparation currently requires manual OTP (ITA-OTP-01)")
  331. route = resolve_debug_route(env)
  332. chrome_path = resolve_chrome_path(env)
  333. os.environ["CHROME_BIN"] = str(chrome_path)
  334. username, password = _get_query_credentials(
  335. env,
  336. non_interactive=non_interactive,
  337. )
  338. profile_path = _prepare_query_profile(username)
  339. config_builder = build_query_config
  340. if production_workflow:
  341. # Reuse route/env validation; production modules never import this tool.
  342. from tools.debug_ita_calendar_gate import build_calendar_gate_config
  343. config_builder = build_calendar_gate_config
  344. config = config_builder(
  345. env,
  346. username=username,
  347. password=password,
  348. persistent_profile_path=profile_path,
  349. route=route,
  350. )
  351. if production_workflow:
  352. config.free_config.update(query_mode="calendar", otp_mode="manual", final_submit_enabled=False)
  353. plugin = ItaPlugin(group_id="ita-query-local-debug")
  354. plugin.set_log(lambda message: print(message, flush=True))
  355. plugin.set_config(config)
  356. exit_code = 0
  357. cleanup_error: Exception | None = None
  358. try:
  359. plugin.create_session()
  360. print(
  361. "ITA_QUERY_PROBE_START "
  362. f"service_id={route.service_id} "
  363. f"routing_key={route.routing_key} query_attempt=1"
  364. )
  365. result = plugin.query(route.appointment_type())
  366. if production_workflow and result.availability_status == AvailabilityStatus.Available and result.earliest_date:
  367. print("ITA_QUERY_WORKFLOW_RESULT=" + json.dumps({
  368. "success": result.success,
  369. "availabilityStatus": result.availability_status.value,
  370. "earliestDate": result.earliest_date.date().isoformat(),
  371. "finalSubmissionEnabled": False,
  372. }, sort_keys=True))
  373. elif _is_weak_entry_signal(result):
  374. _print_booking_form_structure(plugin, route=route)
  375. print(
  376. "ITA_QUERY_WEAK_SIGNAL "
  377. f"service_id={route.service_id} "
  378. f"routing_key={route.routing_key} "
  379. f"status={result.availability_status.value} "
  380. "evidence=booking-form"
  381. )
  382. exit_code = 4
  383. elif _is_nonempty_query_result(result):
  384. print(
  385. "ITA_QUERY_SAFE_STOP "
  386. f"service_id={route.service_id} "
  387. f"routing_key={route.routing_key} "
  388. "reason=non-empty-availability-result"
  389. )
  390. exit_code = 4
  391. else:
  392. print(
  393. "ITA_QUERY_NONE_AVAILABLE "
  394. f"service_id={route.service_id} "
  395. f"routing_key={route.routing_key} "
  396. f"status={result.availability_status.value}"
  397. )
  398. except BizLogicError as exc:
  399. print(
  400. f"ITA_QUERY_FAILED type={type(exc).__name__} "
  401. f"message={_text_for_log(exc)}"
  402. )
  403. _print_failure_context(plugin)
  404. exit_code = 1
  405. except Exception as exc:
  406. print(
  407. f"ITA_QUERY_FAILED type={type(exc).__name__} "
  408. f"message={_text_for_log(exc)}"
  409. )
  410. _print_failure_context(plugin)
  411. exit_code = 1
  412. finally:
  413. try:
  414. plugin.cleanup()
  415. print("ITA_QUERY_CLEANUP_OK")
  416. except Exception as exc:
  417. cleanup_error = exc
  418. print(
  419. f"ITA_QUERY_CLEANUP_FAILED type={type(exc).__name__} "
  420. f"message={_text_for_log(exc)}",
  421. file=sys.stderr,
  422. )
  423. return 3 if cleanup_error is not None else exit_code
  424. def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
  425. parser = argparse.ArgumentParser(
  426. description=(
  427. "One-shot route-configured Italy Query debugger "
  428. "(one query, no retry, never books)."
  429. )
  430. )
  431. parser.add_argument(
  432. "--non-interactive",
  433. action="store_true",
  434. help="Fail instead of prompting when credentials are missing.",
  435. )
  436. parser.add_argument(
  437. "--production-workflow", action="store_true",
  438. help="Use the production Calendar Query entry, including one manual OTP preparation; never book.",
  439. )
  440. return parser.parse_args(argv)
  441. def main(argv: list[str] | None = None) -> int:
  442. args = parse_args(argv)
  443. try:
  444. if args.production_workflow:
  445. return run_query_once(os.environ, non_interactive=args.non_interactive, production_workflow=True)
  446. return run_query_once(os.environ, non_interactive=args.non_interactive)
  447. except DebugConfigurationError as exc:
  448. print(f"ITA_QUERY_CONFIG_ERROR: {exc}", file=sys.stderr)
  449. return 2
  450. if __name__ == "__main__":
  451. raise SystemExit(main())