| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- #!/usr/bin/env python3
- """Run one isolated, route-configured Italy-plugin availability probe.
- This debug entrypoint logs in, calls ``ItaPlugin.query()`` exactly once for the
- configured route, and then cleans up. It never calls ``book()``
- and it does not retry the query.
- """
- from __future__ import annotations
- import argparse
- import json
- import os
- import re
- import sys
- from dataclasses import dataclass
- from pathlib import Path
- from typing import Mapping
- REPO_ROOT = Path(__file__).resolve().parents[1]
- if str(REPO_ROOT) not in sys.path:
- sys.path.insert(0, str(REPO_ROOT))
- from plugins.ita_plugin import BOOKING_FORM_DISCOVERY_SCRIPT, ItaPlugin
- from tools.debug_ita_session import (
- DebugConfigurationError,
- _env_text,
- _get_credentials,
- _text_for_log,
- _url_for_log,
- build_plugin_config,
- resolve_chrome_path,
- )
- from utils.local_account import (
- LocalAccountConfigurationError,
- load_local_account,
- )
- from utils.ita_local_profile import (
- ITA_QUERY_ACCOUNT_FILE,
- ItaLocalProfileError,
- prepare_ita_query_profile,
- )
- from vs_types import AppointmentType, AvailabilityStatus, BizLogicError, VSPlgConfig
- DEFAULT_ITA_SERVICE_ID = 1321
- DEFAULT_ITA_ROUTING_KEY = "slot.dub.it.tourist"
- DEFAULT_ITA_CITY = "Dublin"
- DEFAULT_ITA_VISA_TYPE = "Tourist"
- DEBUG_ROUTE_CAPABILITY = "services_probe_only"
- DEBUG_ROUTE_ENV_NAMES = (
- "ITA_ROUTING_KEY",
- "ITA_SERVICE_ID",
- "ITA_CITY",
- "ITA_VISA_TYPE",
- )
- LOCAL_QUERY_ACCOUNT_FILE = ITA_QUERY_ACCOUNT_FILE
- _EMAIL_PATTERN = re.compile(r"\b[^\s@]+@[^\s@]+\.[^\s@]+\b")
- _LONG_TOKEN_PATTERN = re.compile(r"\b[A-Za-z0-9_-]{24,}\b")
- _LONG_NUMBER_PATTERN = re.compile(r"\b\d{6,}\b")
- _BOOKING_FORM_DISCOVERY_SCRIPT = BOOKING_FORM_DISCOVERY_SCRIPT
- @dataclass(frozen=True, slots=True)
- class ItaDebugRoute:
- routing_key: str
- service_id: int
- city: str
- visa_type: str
- @property
- def booking_path(self) -> str:
- return f"/Services/Booking/{self.service_id}"
- def appointment_type(self) -> AppointmentType:
- return AppointmentType(
- routing_key=self.routing_key,
- country="Italy",
- city=self.city,
- visa_type=self.visa_type,
- )
- def apt_config(self) -> dict:
- return {
- "service_id": self.service_id,
- "capability": DEBUG_ROUTE_CAPABILITY,
- }
- def resolve_debug_route(env: Mapping[str, str]) -> ItaDebugRoute:
- """Resolve one explicit debug route, retaining Dublin only as a default."""
- route_values = {
- name: _env_text(env, name)
- for name in DEBUG_ROUTE_ENV_NAMES
- }
- configured_names = {
- name for name, value in route_values.items() if value
- }
- if configured_names and configured_names != set(DEBUG_ROUTE_ENV_NAMES):
- raise DebugConfigurationError(
- "Italy route override must set ITA_ROUTING_KEY, ITA_SERVICE_ID, "
- "ITA_CITY, and ITA_VISA_TYPE together"
- )
- routing_key = (
- route_values["ITA_ROUTING_KEY"] or DEFAULT_ITA_ROUTING_KEY
- )
- raw_service_id = route_values["ITA_SERVICE_ID"]
- try:
- service_id = (
- int(raw_service_id)
- if raw_service_id
- else DEFAULT_ITA_SERVICE_ID
- )
- except ValueError as exc:
- raise DebugConfigurationError(
- "ITA_SERVICE_ID must be a positive integer"
- ) from exc
- if service_id <= 0:
- raise DebugConfigurationError(
- "ITA_SERVICE_ID must be a positive integer"
- )
- city = route_values["ITA_CITY"] or DEFAULT_ITA_CITY
- visa_type = route_values["ITA_VISA_TYPE"] or DEFAULT_ITA_VISA_TYPE
- return ItaDebugRoute(
- routing_key=routing_key,
- service_id=service_id,
- city=city,
- visa_type=visa_type,
- )
- def _get_query_credentials(
- env: Mapping[str, str],
- *,
- non_interactive: bool,
- ) -> tuple[str, str]:
- """Prefer the ignored local Query account, then preserve the old fallback."""
- if LOCAL_QUERY_ACCOUNT_FILE.exists():
- try:
- account = load_local_account(LOCAL_QUERY_ACCOUNT_FILE)
- except LocalAccountConfigurationError as exc:
- raise DebugConfigurationError(
- f"Local Italy Query account config is invalid: {exc}"
- ) from exc
- return account.username, account.password
- return _get_credentials(env, non_interactive=non_interactive)
- def _is_weak_entry_signal(result: object) -> bool:
- return bool(
- getattr(result, "success", False)
- and getattr(result, "availability_status", None)
- == AvailabilityStatus.NoneAvailable
- and getattr(result, "earliest_date", None) is None
- and not getattr(result, "availability", None)
- )
- def _is_nonempty_query_result(result: object) -> bool:
- return bool(
- getattr(result, "success", False)
- or getattr(result, "availability_status", None)
- != AvailabilityStatus.NoneAvailable
- or getattr(result, "earliest_date", None) is not None
- or bool(getattr(result, "availability", None))
- )
- def build_query_config(
- env: Mapping[str, str],
- *,
- username: str,
- password: str,
- persistent_profile_path: Path | None = None,
- route: ItaDebugRoute | None = None,
- ) -> VSPlgConfig:
- """Build a safe CS2 config for exactly one resolved route."""
- resolved_route = route or resolve_debug_route(env)
- config = build_plugin_config(
- env,
- username=username,
- password=password,
- )
- config.free_config = {
- "query_mode": "services_probe",
- "final_submit_enabled": False,
- "apt_configs": {
- resolved_route.routing_key: resolved_route.apt_config(),
- },
- }
- if persistent_profile_path is not None:
- config.free_config = {
- **config.free_config,
- "persistent_browser_profile_path": str(persistent_profile_path),
- }
- return config
- def _prepare_query_profile(username: str) -> Path:
- try:
- return prepare_ita_query_profile(username)
- except ItaLocalProfileError as exc:
- raise DebugConfigurationError(
- "Local Italy Query browser profile is invalid"
- ) from exc
- def _appointment_type(
- route: ItaDebugRoute | None = None,
- ) -> AppointmentType:
- return (route or resolve_debug_route({})).appointment_type()
- def _popup_text_for_log(value: object, *, limit: int = 300) -> str:
- """Keep a short diagnostic notice while redacting likely identifiers."""
- sanitized = re.sub(r"\s+", " ", _text_for_log(value)).strip()
- sanitized = _EMAIL_PATTERN.sub("<redacted-email>", sanitized)
- sanitized = _LONG_TOKEN_PATTERN.sub("<redacted-token>", sanitized)
- sanitized = _LONG_NUMBER_PATTERN.sub("<redacted-number>", sanitized)
- if len(sanitized) > limit:
- return f"{sanitized[:limit]}...<truncated>"
- return sanitized
- def _metadata_text(value: object, *, limit: int = 120) -> str | None:
- if value is None:
- return None
- text = re.sub(r"\s+", " ", _text_for_log(value)).strip()
- text = _EMAIL_PATTERN.sub("<redacted-email>", text)
- text = _LONG_NUMBER_PATTERN.sub("<redacted-number>", text)
- if len(text) > limit:
- return f"{text[:limit]}...<truncated>"
- return text or None
- def _sanitize_booking_form_structure(raw: object) -> dict | None:
- """Allowlist structural metadata and discard every field value."""
- if not isinstance(raw, dict):
- return None
- raw_form = raw.get("form") if isinstance(raw.get("form"), dict) else {}
- raw_fields = raw.get("fields") if isinstance(raw.get("fields"), list) else []
- raw_buttons = (
- raw.get("buttons") if isinstance(raw.get("buttons"), list) else []
- )
- fields = []
- for item in raw_fields[:100]:
- if not isinstance(item, dict):
- continue
- fields.append(
- {
- "tag": _metadata_text(item.get("tag")),
- "type": _metadata_text(item.get("type")),
- "id": _metadata_text(item.get("id")),
- "name": _metadata_text(item.get("name")),
- "required": bool(item.get("required")),
- }
- )
- buttons = []
- for item in raw_buttons[:50]:
- if not isinstance(item, dict):
- continue
- buttons.append(
- {
- "tag": _metadata_text(item.get("tag")),
- "type": _metadata_text(item.get("type")),
- "id": _metadata_text(item.get("id")),
- "name": _metadata_text(item.get("name")),
- "text": _metadata_text(item.get("text")),
- }
- )
- return {
- "path": _metadata_text(raw.get("path")),
- "form": {
- "method": _metadata_text(raw_form.get("method")),
- "action_path": _metadata_text(raw_form.get("action_path")),
- },
- "field_count": int(raw.get("field_count") or len(raw_fields)),
- "fields_truncated": bool(
- raw.get("fields_truncated") or len(raw_fields) > 100
- ),
- "fields": fields,
- "button_count": int(raw.get("button_count") or len(raw_buttons)),
- "buttons_truncated": bool(
- raw.get("buttons_truncated") or len(raw_buttons) > 50
- ),
- "buttons": buttons,
- }
- def _print_booking_form_structure(
- plugin: ItaPlugin,
- *,
- raw_structure: object = None,
- route: ItaDebugRoute | None = None,
- ) -> None:
- """Read structure only; never read field values or submit the form."""
- page = getattr(plugin, "page", None)
- if page is None:
- print("ITA_BOOKING_FORM_STRUCTURE=not-available")
- return
- try:
- if raw_structure is None:
- inspector = getattr(plugin, "_inspect_booking_form_structure", None)
- raw_structure = (
- inspector((route or resolve_debug_route({})).booking_path)
- if callable(inspector)
- else page.run_js(_BOOKING_FORM_DISCOVERY_SCRIPT)
- )
- structure = _sanitize_booking_form_structure(
- raw_structure
- )
- if structure is None:
- print("ITA_BOOKING_FORM_STRUCTURE=not-available")
- return
- print(
- "ITA_BOOKING_FORM_STRUCTURE="
- + json.dumps(structure, ensure_ascii=False, sort_keys=True)
- )
- except Exception as exc:
- print(
- "ITA_BOOKING_FORM_STRUCTURE=unavailable "
- f"type={type(exc).__name__} message={_text_for_log(exc)}"
- )
- def _print_failure_context(plugin: ItaPlugin) -> None:
- """Print sanitized text diagnostics; never save Query screenshots."""
- page = getattr(plugin, "page", None)
- if page is None:
- print("diagnostic_page=not-created")
- return
- try:
- print(f"diagnostic_url={_url_for_log(page.url)}")
- except Exception as exc:
- print(
- "diagnostic_url=unavailable "
- f"({type(exc).__name__}: {_text_for_log(exc)})"
- )
- try:
- title = _popup_text_for_log(page.title, limit=120)
- print(
- f"diagnostic_title={title}"
- if title
- else "diagnostic_title=not-present"
- )
- except Exception as exc:
- print(
- "diagnostic_title=unavailable "
- f"({type(exc).__name__}: {_text_for_log(exc)})"
- )
- try:
- popup = page.ele(".jconfirm-content", timeout=0.5)
- popup_text = _popup_text_for_log(popup.text if popup else "")
- print(
- f"diagnostic_popup_text={popup_text}"
- if popup_text
- else "diagnostic_popup_text=not-present"
- )
- except Exception as exc:
- print(
- "diagnostic_popup_text=unavailable "
- f"({type(exc).__name__}: {_text_for_log(exc)})"
- )
- def run_query_once(env: Mapping[str, str], *, non_interactive: bool, production_workflow: bool = False) -> int:
- if production_workflow and non_interactive:
- raise DebugConfigurationError("Production Calendar preparation currently requires manual OTP (ITA-OTP-01)")
- route = resolve_debug_route(env)
- chrome_path = resolve_chrome_path(env)
- os.environ["CHROME_BIN"] = str(chrome_path)
- username, password = _get_query_credentials(
- env,
- non_interactive=non_interactive,
- )
- profile_path = _prepare_query_profile(username)
- config_builder = build_query_config
- if production_workflow:
- # Reuse route/env validation; production modules never import this tool.
- from tools.debug_ita_calendar_gate import build_calendar_gate_config
- config_builder = build_calendar_gate_config
- config = config_builder(
- env,
- username=username,
- password=password,
- persistent_profile_path=profile_path,
- route=route,
- )
- if production_workflow:
- config.free_config.update(query_mode="calendar", otp_mode="manual", final_submit_enabled=False)
- plugin = ItaPlugin(group_id="ita-query-local-debug")
- plugin.set_log(lambda message: print(message, flush=True))
- plugin.set_config(config)
- exit_code = 0
- cleanup_error: Exception | None = None
- try:
- plugin.create_session()
- print(
- "ITA_QUERY_PROBE_START "
- f"service_id={route.service_id} "
- f"routing_key={route.routing_key} query_attempt=1"
- )
- result = plugin.query(route.appointment_type())
- if production_workflow and result.availability_status == AvailabilityStatus.Available and result.earliest_date:
- print("ITA_QUERY_WORKFLOW_RESULT=" + json.dumps({
- "success": result.success,
- "availabilityStatus": result.availability_status.value,
- "earliestDate": result.earliest_date.date().isoformat(),
- "finalSubmissionEnabled": False,
- }, sort_keys=True))
- elif _is_weak_entry_signal(result):
- _print_booking_form_structure(plugin, route=route)
- print(
- "ITA_QUERY_WEAK_SIGNAL "
- f"service_id={route.service_id} "
- f"routing_key={route.routing_key} "
- f"status={result.availability_status.value} "
- "evidence=booking-form"
- )
- exit_code = 4
- elif _is_nonempty_query_result(result):
- print(
- "ITA_QUERY_SAFE_STOP "
- f"service_id={route.service_id} "
- f"routing_key={route.routing_key} "
- "reason=non-empty-availability-result"
- )
- exit_code = 4
- else:
- print(
- "ITA_QUERY_NONE_AVAILABLE "
- f"service_id={route.service_id} "
- f"routing_key={route.routing_key} "
- f"status={result.availability_status.value}"
- )
- except BizLogicError as exc:
- print(
- f"ITA_QUERY_FAILED type={type(exc).__name__} "
- f"message={_text_for_log(exc)}"
- )
- _print_failure_context(plugin)
- exit_code = 1
- except Exception as exc:
- print(
- f"ITA_QUERY_FAILED type={type(exc).__name__} "
- f"message={_text_for_log(exc)}"
- )
- _print_failure_context(plugin)
- exit_code = 1
- finally:
- try:
- plugin.cleanup()
- print("ITA_QUERY_CLEANUP_OK")
- except Exception as exc:
- cleanup_error = exc
- print(
- f"ITA_QUERY_CLEANUP_FAILED type={type(exc).__name__} "
- f"message={_text_for_log(exc)}",
- file=sys.stderr,
- )
- return 3 if cleanup_error is not None else exit_code
- def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
- parser = argparse.ArgumentParser(
- description=(
- "One-shot route-configured Italy Query debugger "
- "(one query, no retry, never books)."
- )
- )
- parser.add_argument(
- "--non-interactive",
- action="store_true",
- help="Fail instead of prompting when credentials are missing.",
- )
- parser.add_argument(
- "--production-workflow", action="store_true",
- help="Use the production Calendar Query entry, including one manual OTP preparation; never book.",
- )
- return parser.parse_args(argv)
- def main(argv: list[str] | None = None) -> int:
- args = parse_args(argv)
- try:
- if args.production_workflow:
- return run_query_once(os.environ, non_interactive=args.non_interactive, production_workflow=True)
- return run_query_once(os.environ, non_interactive=args.non_interactive)
- except DebugConfigurationError as exc:
- print(f"ITA_QUERY_CONFIG_ERROR: {exc}", file=sys.stderr)
- return 2
- if __name__ == "__main__":
- raise SystemExit(main())
|