Documentation
¶
Overview ¶
Package flights is a strongly-typed Go client for scraping Google Flights. It encodes queries as Base64-encoded Protobuf strings (the ?tfs= param), fetches the page with a browser-impersonating HTTP client, and parses the embedded JavaScript data blob.
Index ¶
- Variables
- func FetchFlightsHTML(q QueryOrString, proxy string, fetchIntegration FetchIntegration) (string, error)
- func GetFlights(q QueryOrString, proxy string, integration Integration) (any, error)
- func Int32P(v int32) *int32
- type Airline
- type Airport
- type Alliance
- type CarbonEmission
- type Currency
- type DataSourceIntegration
- type FetchIntegration
- type FlightQuery
- type Flights
- type Integration
- type JsMetadata
- type Language
- type Passengers
- type Query
- func (q *Query) Currency() string
- func (q *Query) FlightData() []*pb.FlightData
- func (q *Query) Language() string
- func (q *Query) Params() (map[string]string, error)
- func (q *Query) Passengers() []pb.Passenger
- func (q *Query) SeatType() (SeatType, error)
- func (q *Query) ToBytes() ([]byte, error)
- func (q *Query) ToStr() (string, error)
- func (q *Query) TripType() (TripType, error)
- func (q *Query) URL() (string, error)
- type QueryOptions
- type QueryOrString
- type ResultList
- type SeatType
- type SimpleDatetime
- type SingleFlight
- type StringQuery
- type TripType
Constants ¶
This section is empty.
Variables ¶
var ErrFlightsNotFound = errors.New("flights: no flights found")
ErrFlightsNotFound is returned when Google Flights returns no results or an explicit error response for the requested query.
Functions ¶
func FetchFlightsHTML ¶
func FetchFlightsHTML(q QueryOrString, proxy string, fetchIntegration FetchIntegration) (string, error)
FetchFlightsHTML fetches the raw Google Flights HTML page for the given query. When proxy is non-empty it is used as the HTTP proxy. When fetchIntegration is non-nil, the integration's FetchHTML method is called instead of the default TLS-impersonating client.
func GetFlights ¶
func GetFlights(q QueryOrString, proxy string, integration Integration) (any, error)
GetFlights fetches Google Flights results for the given query.
- If integration is nil, the built-in browser-impersonating client is used and the HTML is parsed into a ResultList.
- If integration implements FetchIntegration, its FetchHTML is called and the result is parsed.
- If integration implements DataSourceIntegration, its Fetch method is called directly (bypassing HTML fetch + parse entirely).
Types ¶
type CarbonEmission ¶
type CarbonEmission struct {
// TypicalOnRoute is the typical CO₂ for this route in grams.
TypicalOnRoute int
// Emission is the actual emission for this itinerary in grams.
Emission int
}
CarbonEmission holds CO₂ data associated with a whole itinerary.
type Currency ¶
type Currency = string
Currency is an ISO 4217 currency code accepted by Google Flights. An empty string tells Google to use the requester's locale.
type DataSourceIntegration ¶
type DataSourceIntegration interface {
Integration
Fetch(q QueryOrString) (any, error)
}
DataSourceIntegration completely replaces the fetch + parse pipeline with an external data source (e.g. SearchApi). Its Fetch method returns an arbitrary result type.
type FetchIntegration ¶
type FetchIntegration interface {
Integration
FetchHTML(q QueryOrString) (string, error)
}
FetchIntegration replaces the default HTTP fetch step. Its FetchHTML method returns the raw HTML of the Google Flights page, which is then parsed by the standard parser.
type FlightQuery ¶
type FlightQuery struct {
// Date is the departure date in "YYYY-MM-DD" format. A time.Time is also
// accepted via NewFlightQueryFromTime.
Date string
FromAirport string
ToAirport string
MaxStops *int32
Airlines []string
EarliestDepartureHour *int32
LatestDepartureHour *int32
EarliestArrivalHour *int32
LatestArrivalHour *int32
MaxDurationMinutes *int32
ConnectingAirports []string
MinLayoverMinutes *int32
MaxLayoverMinutes *int32
LessEmissionsOnly bool
}
FlightQuery describes a single flight leg within a search.
All optional filter fields use pointer types so the zero value (nil) means "not set", which is distinct from 0 (a meaningful value for some filters).
func NewFlightQuery ¶
func NewFlightQuery(date, from, to string) FlightQuery
NewFlightQuery constructs a FlightQuery with the given date string.
func NewFlightQueryFromTime ¶
func NewFlightQueryFromTime(date time.Time, from, to string) FlightQuery
NewFlightQueryFromTime constructs a FlightQuery from a time.Time value.
func (FlightQuery) WithMaxStops ¶
func (q FlightQuery) WithMaxStops(m int32) FlightQuery
WithMaxStops returns a copy of q with MaxStops set to m.
type Flights ¶
type Flights struct {
// Type is the label Google attaches to the itinerary (e.g. "nonstop").
Type string
// Price is the fare amount in the requested currency, or nil when
// Google returns an unpriced itinerary.
Price *int
Airlines []string
Flights []SingleFlight
Carbon CarbonEmission
}
Flights is a complete bookable itinerary returned by Google Flights.
type Integration ¶
type Integration interface{}
Integration is a marker type; all plug-in types implement either FetchIntegration or DataSourceIntegration.
type JsMetadata ¶
JsMetadata carries the airline and alliance tables extracted from the page.
type Language ¶
type Language = string
Language is an IETF BCP-47 language tag accepted by Google Flights. An empty string tells Google to use the requester's locale.
type Passengers ¶
Passengers holds a passenger headcount breakdown. Total must not exceed 9, and InfantsOnLap must not exceed Adults.
func NewPassengers ¶
func NewPassengers(adults, children, infantsInSeat, infantsOnLap int) (Passengers, error)
NewPassengers validates and returns a Passengers value.
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
Query is the serialisable search query. Build one with CreateQuery.
func CreateQuery ¶
func CreateQuery(flights []FlightQuery, passengers Passengers, opts QueryOptions) (*Query, error)
CreateQuery builds a Query from a list of flight legs and options. If opts.MaxStops is set, it is applied to every leg that has no per-leg MaxStops already set.
func (*Query) FlightData ¶
func (q *Query) FlightData() []*pb.FlightData
FlightData returns the raw protobuf FlightData slice (used by integrations).
func (*Query) Params ¶
Params returns the URL query parameters as a map suitable for http request building.
func (*Query) Passengers ¶
Passengers returns the raw passenger enum slice (used by integrations).
func (*Query) ToStr ¶
ToStr serialises the query to a Base64-encoded string (the value for Google's `?tfs=` query parameter).
type QueryOptions ¶
type QueryOptions struct {
Seat SeatType
Trip TripType
Language Language
Currency Currency
// MaxStops applies to every leg that doesn't already have MaxStops set.
MaxStops *int32
// MaxPrice is the maximum fare in the selected Currency.
MaxPrice *int32
CarryOnBags int
CheckedBags int
HideSeparateAndSelfTransfer bool
ExcludeBasicEconomy bool
}
QueryOptions holds the global (non-per-leg) settings for a search.
type QueryOrString ¶
type QueryOrString interface {
// contains filtered or unexported methods
}
QueryOrString accepts either a *Query (structured) or a plain string search term, mirroring the Python API's `q: Query | str` parameter.
type ResultList ¶
type ResultList struct {
Items []Flights
Metadata JsMetadata
}
ResultList is the parsed list of itineraries, with page metadata attached.
func GetFlightsDirect ¶
func GetFlightsDirect(q QueryOrString, proxy string, fi FetchIntegration) (ResultList, error)
GetFlightsDirect is a type-safe convenience wrapper that returns a ResultList directly, for callers that use the default fetcher or a FetchIntegration.
func Parse ¶
func Parse(htmlContent string) (ResultList, error)
Parse extracts the embedded Google Flights data from a raw HTML page string and returns a ResultList.
func ParseJS ¶
func ParseJS(js string) (ResultList, error)
ParseJS parses the raw text content of the <script class="ds:1"> element. It is exported so unit tests can call it directly with fixture data, mirroring the Python codebase's test strategy.
type SimpleDatetime ¶
type SimpleDatetime struct {
// Date is [year, month, day].
Date [3]int
// Time is [hour, minute].
Time [2]int
}
SimpleDatetime holds a calendar date and a wall-clock time as separate integer tuples, matching Google's sparse encoding exactly.
type SingleFlight ¶
type SingleFlight struct {
FromAirport Airport
ToAirport Airport
Departure SimpleDatetime
Arrival SimpleDatetime
// Duration is flight time in minutes.
Duration int
PlaneType string
}
SingleFlight is one leg within a multi-segment itinerary.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
run
command
|
|
|
Package integrations provides plug-in integrations for the flights package.
|
Package integrations provides plug-in integrations for the flights package. |
|
searchapi
Package searchapi implements the SearchApi DataSourceIntegration for the flights package, providing richer structured results than the direct scraper.
|
Package searchapi implements the SearchApi DataSourceIntegration for the flights package, providing richer structured results than the direct scraper. |
|
internal
|
|
|
jsonutil
Package jsonutil provides helpers for navigating deeply-nested JSON structures decoded into []any / map[string]any values.
|
Package jsonutil provides helpers for navigating deeply-nested JSON structures decoded into []any / map[string]any values. |