|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Inspect booking-option seller diversity with swoop.get_booking_results(). |
| 3 | +
|
| 4 | +Google Flights sells a single itinerary through more than one channel: the |
| 5 | +operating airline ("airline-direct") and, for many fares, a list of online |
| 6 | +travel agencies (OTAs) such as Expedia, FlightHub, or CheapOair. |
| 7 | +``get_booking_results()`` returns one ``BookingOption`` per channel, each with |
| 8 | +its own ``seller_code`` / ``seller_name`` and ``booking_url``. |
| 9 | +
|
| 10 | +Whether OTAs show up is decided by Google, not swoop: |
| 11 | +
|
| 12 | + * Premium cabins and most major US carriers (AA/DL/UA/B6 ...) are usually |
| 13 | + airline-direct only — you will see one seller, the airline, across |
| 14 | + several fare brands. That is expected, not a bug. |
| 15 | + * International economy on foreign carriers (PR, OZ, CI, BR, LO ...) often |
| 16 | + returns a wall of OTAs alongside the airline. That is where seller |
| 17 | + diversity shows up. |
| 18 | +
|
| 19 | +So if every option you get back shares one ``seller_code``, try an |
| 20 | +international economy itinerary on a foreign carrier before concluding the |
| 21 | +flight has no OTAs. |
| 22 | +
|
| 23 | +Usage: |
| 24 | + # Default: SFO->MNL economy ~90 days out (an OTA-rich route) |
| 25 | + python examples/booking_options.py |
| 26 | +
|
| 27 | + # Any route / cabin; --index selects which itinerary (0 = top result) |
| 28 | + python examples/booking_options.py JFK DEL 2026-09-15 --cabin economy |
| 29 | + python examples/booking_options.py JFK LAX 2026-06-15 --cabin first --index 2 |
| 30 | +""" |
| 31 | +from __future__ import annotations |
| 32 | + |
| 33 | +import argparse |
| 34 | +import sys |
| 35 | +from datetime import date, timedelta |
| 36 | + |
| 37 | +import swoop |
| 38 | + |
| 39 | + |
| 40 | +def _default_date() -> str: |
| 41 | + return (date.today() + timedelta(days=90)).isoformat() |
| 42 | + |
| 43 | + |
| 44 | +def main() -> int: |
| 45 | + parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) |
| 46 | + parser.add_argument("origin", nargs="?", default="SFO", help="origin IATA code (default SFO)") |
| 47 | + parser.add_argument("destination", nargs="?", default="MNL", help="destination IATA code (default MNL)") |
| 48 | + parser.add_argument("date", nargs="?", default=None, help="YYYY-MM-DD (default ~90 days out)") |
| 49 | + parser.add_argument( |
| 50 | + "--cabin", |
| 51 | + default="economy", |
| 52 | + choices=["economy", "premium-economy", "business", "first"], |
| 53 | + help="cabin class (default economy — where OTAs are most common)", |
| 54 | + ) |
| 55 | + parser.add_argument("--index", type=int, default=0, help="which itinerary to price (0 = top result)") |
| 56 | + args = parser.parse_args() |
| 57 | + |
| 58 | + origin = args.origin.upper() |
| 59 | + destination = args.destination.upper() |
| 60 | + when = args.date or _default_date() |
| 61 | + |
| 62 | + try: |
| 63 | + result = swoop.search(origin, destination, when, cabin=args.cabin) |
| 64 | + except swoop.SwoopError as exc: |
| 65 | + print(f"search failed: {exc}", file=sys.stderr) |
| 66 | + return 1 |
| 67 | + |
| 68 | + if not result.results: |
| 69 | + print(f"No itineraries for {origin}->{destination} on {when}.", file=sys.stderr) |
| 70 | + return 1 |
| 71 | + |
| 72 | + if args.index >= len(result.results): |
| 73 | + print(f"--index {args.index} out of range ({len(result.results)} results).", file=sys.stderr) |
| 74 | + return 1 |
| 75 | + |
| 76 | + trip = result.results[args.index] |
| 77 | + itinerary = trip.legs[0].itinerary |
| 78 | + |
| 79 | + try: |
| 80 | + options = swoop.get_booking_results(itinerary, cabin=args.cabin) |
| 81 | + except swoop.SwoopError as exc: |
| 82 | + print(f"booking lookup failed: {exc}", file=sys.stderr) |
| 83 | + return 1 |
| 84 | + |
| 85 | + if not options: |
| 86 | + print("No booking options returned for this itinerary.", file=sys.stderr) |
| 87 | + return 1 |
| 88 | + |
| 89 | + direct = [opt for opt in options if opt.is_airline_direct] |
| 90 | + otas = [opt for opt in options if not opt.is_airline_direct] |
| 91 | + |
| 92 | + print(f"\n{origin} -> {destination} {when} ({args.cabin})") |
| 93 | + print(f"Itinerary #{args.index}: {itinerary.airline_code} from ${trip.price}\n") |
| 94 | + |
| 95 | + def _row(opt: swoop.BookingOption) -> str: |
| 96 | + seller = opt.seller_name or opt.seller_code or "airline-direct" |
| 97 | + brand = opt.brand_label or opt.fare_family or "—" |
| 98 | + return f" ${opt.price:<6} {seller:<22} {brand}" |
| 99 | + |
| 100 | + print(f"Airline-direct ({len(direct)}):") |
| 101 | + for opt in direct or [None]: |
| 102 | + print(" (none)" if opt is None else _row(opt)) |
| 103 | + |
| 104 | + print(f"\nOnline travel agencies ({len(otas)}):") |
| 105 | + for opt in sorted(otas, key=lambda o: o.price) or [None]: |
| 106 | + print(" (none)" if opt is None else _row(opt)) |
| 107 | + |
| 108 | + distinct = sorted({opt.seller_code for opt in options if opt.seller_code}) |
| 109 | + print(f"\n{len(options)} options across {len(distinct)} distinct sellers: {', '.join(distinct)}") |
| 110 | + if len(distinct) <= 1: |
| 111 | + print( |
| 112 | + "Only one seller here — this itinerary is airline-direct. Try an " |
| 113 | + "international economy route on a foreign carrier (e.g. SFO MNL) " |
| 114 | + "to see the OTA list.", |
| 115 | + ) |
| 116 | + |
| 117 | + cheapest = min(options, key=lambda o: o.price) |
| 118 | + if cheapest.booking_url: |
| 119 | + seller = cheapest.seller_name or cheapest.seller_code or "the airline" |
| 120 | + print(f"\nCheapest (${cheapest.price}) is via {seller}:\n {cheapest.booking_url}") |
| 121 | + |
| 122 | + return 0 |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + raise SystemExit(main()) |
0 commit comments