Files
Mark-LIV/actions/flight_finder.py
T

359 lines
12 KiB
Python
Raw Normal View History

2026-03-02 04:30:10 +03:00
import json
import re
import subprocess
2026-04-19 15:22:49 +03:00
import sys
2026-03-02 04:30:10 +03:00
from datetime import datetime, timedelta
from pathlib import Path
2026-04-19 15:22:49 +03:00
from config import is_windows, is_mac, is_linux
2026-03-02 04:30:10 +03:00
2026-04-19 15:22:49 +03:00
def _get_base_dir() -> Path:
2026-03-02 04:30:10 +03:00
if getattr(sys, "frozen", False):
return Path(sys.executable).parent
return Path(__file__).resolve().parent.parent
2026-04-19 15:22:49 +03:00
BASE_DIR = _get_base_dir()
2026-03-02 04:30:10 +03:00
API_CONFIG_PATH = BASE_DIR / "config" / "api_keys.json"
def _get_api_key() -> str:
with open(API_CONFIG_PATH, "r", encoding="utf-8") as f:
return json.load(f)["gemini_api_key"]
2026-04-19 15:22:49 +03:00
_MONTH_MAP: dict[str, int] = {
"january": 1, "february": 2, "march": 3, "april": 4,
"may": 5, "june": 6, "july": 7, "august": 8,
"september": 9, "october": 10, "november": 11, "december": 12,
"ocak": 1, "şubat": 2, "mart": 3, "nisan": 4,
"mayıs": 5, "haziran": 6, "temmuz": 7, "ağustos": 8,
"eylül": 9, "ekim": 10, "kasım": 11, "aralık": 12,
}
_RELATIVE_MAP_KEYS = {
"today", "bugün",
"tomorrow", "yarın",
}
2026-03-02 04:30:10 +03:00
def _parse_date(raw: str) -> str:
2026-04-19 15:22:49 +03:00
raw = raw.strip()
lower = raw.lower()
today = datetime.now()
2026-03-02 04:30:10 +03:00
if re.match(r"\d{4}-\d{2}-\d{2}", raw):
return raw
for fmt in ("%d/%m/%Y", "%m/%d/%Y", "%d.%m.%Y", "%d-%m-%Y"):
try:
return datetime.strptime(raw, fmt).strftime("%Y-%m-%d")
except ValueError:
pass
2026-04-19 15:22:49 +03:00
relative = {
"today": today, "bugün": today,
"tomorrow": today + timedelta(days=1),
"yarın": today + timedelta(days=1),
2026-03-02 04:30:10 +03:00
}
2026-04-19 15:22:49 +03:00
for key, val in relative.items():
2026-03-02 04:30:10 +03:00
if key in lower:
return val.strftime("%Y-%m-%d")
try:
import google.generativeai as genai
genai.configure(api_key=_get_api_key())
model = genai.GenerativeModel("gemini-2.5-flash-lite")
response = model.generate_content(
2026-04-19 15:22:49 +03:00
f"Today is {today.strftime('%Y-%m-%d')}. "
f"Convert this date expression to YYYY-MM-DD: '{raw}'. "
2026-03-02 04:30:10 +03:00
f"Return ONLY the date string, nothing else."
)
result = response.text.strip()
if re.match(r"\d{4}-\d{2}-\d{2}", result):
return result
2026-04-19 15:22:49 +03:00
except Exception as e:
print(f"[FlightFinder] ⚠️ Gemini date parse failed: {e}")
for month_name, month_num in _MONTH_MAP.items():
2026-03-02 04:30:10 +03:00
if month_name in lower:
day_match = re.search(r"\d{1,2}", raw)
if day_match:
day = int(day_match.group())
year = today.year if month_num >= today.month else today.year + 1
return f"{year}-{month_num:02d}-{day:02d}"
2026-04-19 15:22:49 +03:00
# Last resort: today
print(f"[FlightFinder] ⚠️ Could not parse date '{raw}' — using today.")
2026-03-02 04:30:10 +03:00
return today.strftime("%Y-%m-%d")
2026-04-19 15:22:49 +03:00
_CABIN_CODE: dict[str, str] = {
"economy": "1",
"premium": "2",
"business": "3",
"first": "4",
}
2026-03-02 04:30:10 +03:00
def _build_google_flights_url(
origin: str,
destination: str,
date: str,
return_date: str | None = None,
passengers: int = 1,
cabin: str = "economy",
) -> str:
2026-04-19 15:22:49 +03:00
cabin_code = _CABIN_CODE.get(cabin.lower(), "1")
base = "https://www.google.com/travel/flights"
2026-03-02 04:30:10 +03:00
2026-04-19 15:22:49 +03:00
# Google Flights accepts these query params for pre-filling
2026-03-02 04:30:10 +03:00
if return_date:
2026-04-19 15:22:49 +03:00
trip = f"Flights+from+{origin}+to+{destination}+on+{date}+returning+{return_date}"
2026-03-02 04:30:10 +03:00
else:
2026-04-19 15:22:49 +03:00
trip = f"Flights+from+{origin}+to+{destination}+on+{date}"
return (
f"{base}"
f"?q={trip}"
f"&tfs=CBwQAhoeEgoyMDI1LTAzLTE1agcIARIDSVNUcgcIARIDTEhS"
f"&curr=USD"
f"&cabin={cabin_code}"
f"&adults={passengers}"
)
2026-03-02 04:30:10 +03:00
def _search_flights_browser(
origin: str,
destination: str,
date: str,
return_date: str | None,
passengers: int,
cabin: str,
) -> tuple[str, str]:
import time
2026-04-19 15:22:49 +03:00
from actions.browser_control import browser_control
2026-03-02 04:30:10 +03:00
url = _build_google_flights_url(
origin, destination, date, return_date, passengers, cabin
)
print(f"[FlightFinder] 🌐 Opening: {url}")
browser_control({"action": "go_to", "url": url})
2026-04-19 15:22:49 +03:00
time.sleep(5)
2026-03-02 04:30:10 +03:00
2026-04-19 15:22:49 +03:00
raw = browser_control({"action": "get_text"})
return (raw or ""), url
2026-03-02 04:30:10 +03:00
def _parse_flights_with_gemini(
raw_text: str,
origin: str,
destination: str,
date: str,
) -> list[dict]:
import google.generativeai as genai
genai.configure(api_key=_get_api_key())
model = genai.GenerativeModel(
model_name="gemini-2.5-flash",
system_instruction=(
"You are a flight data extraction expert. "
"Extract flight information from raw webpage text. "
2026-04-19 15:22:49 +03:00
"Return ONLY valid JSON — no markdown, no explanation."
),
2026-03-02 04:30:10 +03:00
)
prompt = (
f"Extract flight options from {origin} to {destination} on {date} "
2026-04-19 15:22:49 +03:00
f"from this Google Flights page text:\n\n{raw_text[:12000]}\n\n"
2026-03-02 04:30:10 +03:00
f"Return a JSON array of up to 5 flights:\n"
2026-04-19 15:22:49 +03:00
f'[{{"airline":"...","departure":"HH:MM","arrival":"HH:MM",'
f'"duration":"Xh Ym","stops":0,"price":"...","currency":"USD"}}]\n'
2026-03-02 04:30:10 +03:00
f"If no flights found, return: []"
)
try:
response = model.generate_content(prompt)
2026-04-19 15:22:49 +03:00
text = re.sub(r"```(?:json)?", "", response.text).strip().rstrip("`").strip()
2026-03-02 04:30:10 +03:00
flights = json.loads(text)
return flights if isinstance(flights, list) else []
except Exception as e:
2026-04-19 15:22:49 +03:00
print(f"[FlightFinder] ⚠️ Gemini parse failed: {e}")
2026-03-02 04:30:10 +03:00
return []
def _format_spoken(
flights: list[dict],
origin: str,
destination: str,
date: str,
) -> str:
if not flights:
return (
f"I couldn't find any flights from {origin} to {destination} "
f"on {date}, sir. The page may not have loaded correctly."
)
2026-04-19 15:22:49 +03:00
lines = [f"Here are the top flights from {origin} to {destination} on {date}, sir."]
2026-03-02 04:30:10 +03:00
for i, f in enumerate(flights[:5], 1):
airline = f.get("airline", "Unknown airline")
departure = f.get("departure", "--:--")
arrival = f.get("arrival", "--:--")
duration = f.get("duration", "")
stops = f.get("stops", 0)
price = f.get("price", "")
currency = f.get("currency", "")
stop_str = "non-stop" if stops == 0 else f"{stops} stop{'s' if stops > 1 else ''}"
price_str = f"{price} {currency}".strip() if price else "price unavailable"
dur_str = f", {duration}" if duration else ""
lines.append(
2026-04-19 15:22:49 +03:00
f"Option {i}: {airline}, departing {departure}, "
f"arriving {arrival}{dur_str}, {stop_str}, {price_str}."
2026-03-02 04:30:10 +03:00
)
2026-04-19 15:22:49 +03:00
# Cheapest — strip non-digits for comparison
priced = [f for f in flights if f.get("price")]
if priced:
cheapest = min(
priced,
key=lambda x: int(re.sub(r"[^\d]", "", str(x["price"])) or "999999"),
)
2026-03-02 04:30:10 +03:00
lines.append(
f"The cheapest option is {cheapest.get('airline')} "
f"at {cheapest.get('price')} {cheapest.get('currency', '')}."
)
return " ".join(lines)
2026-04-19 15:22:49 +03:00
def _format_text_report(
2026-03-02 04:30:10 +03:00
flights: list[dict],
origin: str,
destination: str,
date: str,
return_date: str | None,
page_url: str,
) -> str:
lines = [
"JARVIS — Flight Search Results",
"─" * 50,
f"Route : {origin} → {destination}",
f"Date : {date}",
]
if return_date:
lines.append(f"Return : {return_date}")
lines += [
2026-04-19 15:22:49 +03:00
f"Searched : {datetime.now().strftime('%Y-%m-%d %H:%M')}",
2026-03-02 04:30:10 +03:00
f"Source : {page_url}",
"─" * 50,
"",
]
if not flights:
lines.append("No flights found.")
else:
for i, f in enumerate(flights, 1):
stops = f.get("stops", 0)
stop_str = "Non-stop" if stops == 0 else f"{stops} stop(s)"
lines += [
f"Flight {i}:",
2026-04-19 15:22:49 +03:00
f" Airline : {f.get('airline', 'N/A')}",
2026-03-02 04:30:10 +03:00
f" Departure : {f.get('departure', 'N/A')}",
2026-04-19 15:22:49 +03:00
f" Arrival : {f.get('arrival', 'N/A')}",
f" Duration : {f.get('duration', 'N/A')}",
2026-03-02 04:30:10 +03:00
f" Stops : {stop_str}",
f" Price : {f.get('price', 'N/A')} {f.get('currency', '')}",
"",
]
return "\n".join(lines)
2026-04-19 15:22:49 +03:00
def _save_to_desktop(content: str, origin: str, destination: str) -> str:
2026-03-02 04:30:10 +03:00
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"flights_{origin}_{destination}_{ts}.txt".replace(" ", "_")
desktop = Path.home() / "Desktop"
desktop.mkdir(parents=True, exist_ok=True)
filepath = desktop / filename
filepath.write_text(content, encoding="utf-8")
print(f"[FlightFinder] 💾 Saved: {filepath}")
2026-04-19 15:22:49 +03:00
try:
if is_windows():
subprocess.Popen(["notepad.exe", str(filepath)])
elif is_mac():
subprocess.Popen(["open", "-t", str(filepath)])
else:
subprocess.Popen(["xdg-open", str(filepath)])
except Exception as e:
print(f"[FlightFinder] ⚠️ Could not open text editor: {e}")
2026-03-02 04:30:10 +03:00
return str(filepath)
2026-04-19 15:22:49 +03:00
def flight_finder(parameters: dict, player=None, speak=None) -> str:
2026-03-02 04:30:10 +03:00
params = parameters or {}
origin = params.get("origin", "").strip()
destination = params.get("destination", "").strip()
date_raw = params.get("date", "").strip()
2026-04-19 15:22:49 +03:00
return_raw = (params.get("return_date") or "").strip()
passengers = max(1, int(params.get("passengers", 1)))
cabin = params.get("cabin", "economy").strip().lower()
save = bool(params.get("save", False))
2026-03-02 04:30:10 +03:00
if not origin or not destination:
return "Please provide both origin and destination, sir."
if not date_raw:
return "Please provide a departure date, sir."
2026-04-19 15:22:49 +03:00
# Normalise cabin value
if cabin not in _CABIN_CODE:
cabin = "economy"
2026-03-02 04:30:10 +03:00
date = _parse_date(date_raw)
return_date = _parse_date(return_raw) if return_raw else None
if player:
player.write_log(f"[FlightFinder] {origin} → {destination} on {date}")
if speak:
speak(f"Searching flights from {origin} to {destination} on {date}, sir.")
2026-04-19 15:22:49 +03:00
print(
f"[FlightFinder] ▶️ {origin} → {destination} | {date}"
f"{' → ' + return_date if return_date else ''}"
f" | {cabin} | {passengers} pax"
)
2026-03-02 04:30:10 +03:00
try:
raw_text, page_url = _search_flights_browser(
origin, destination, date, return_date, passengers, cabin
)
if not raw_text:
return "Could not retrieve flight data, sir. The page may not have loaded."
if speak:
2026-04-19 15:22:49 +03:00
speak("Analysing the results now, sir.")
2026-03-02 04:30:10 +03:00
flights = _parse_flights_with_gemini(raw_text, origin, destination, date)
2026-04-19 15:22:49 +03:00
spoken = _format_spoken(flights, origin, destination, date)
2026-03-02 04:30:10 +03:00
if speak:
speak(spoken)
result = spoken
if save and flights:
2026-04-19 15:22:49 +03:00
report = _format_text_report(flights, origin, destination, date, return_date, page_url)
saved_path = _save_to_desktop(report, origin, destination)
result += f" Results saved to Desktop: {saved_path}"
2026-03-02 04:30:10 +03:00
return result
except Exception as e:
2026-04-19 15:22:49 +03:00
print(f"[FlightFinder] ❌ {e}")
2026-03-02 04:30:10 +03:00
return f"Flight search failed, sir: {e}"