Table of Contents generated with DocToc
A library for querying airfares via the Google QPX API. Instead of returning JSON, the results are parsed into Python objects containing a subset of properties returned in the raw response.
Let's find a one-way nonstop flight from Melbourne (MEL
) to Shanghai (PVG
) for a single adult, departing on the 9th of July 2017:
from flypy import *
# Instantiate a new Query object.
query = Query()
# Add the origin airport to the query.
query.add_origin("MEL")
# Add the destination airport to the query.
query.add_dest("PVG")
# Add the intended departure date.
query.add_dept_date("2017-07-09")
# Add a single adult passenger.
query.add_pax(1)
# Restrict the maximum stops to 0 (nonstop).
query.add_max_stops(0)
# Send the query to QPX, and store the QueryResponse object.
response = query.send()
query.send()
returns a QueryResponse
object which stores the raw response and the created objects. We can use this to display some details of the trip:
trips = response.get_trips()
for trip in trips:
print(trip)
for journey in trip.journeys:
print(journey)
for leg in journey.legs:
print(leg)
# [Trip] JOURNEYS: 1. COST: AUD689.25.
# [Journey] MEL to PVG.
# [Leg] MU738 from MEL to PVG. DUR: 645. AC: 332. DEPT: 2017-07-09 11:00:00+10:00. ARR: 2017-07-09 19:45:00+08:00.
# [Trip] JOURNEYS: 1. COST: AUD795.14.
# [Journey] MEL to PVG.
# [Leg] QF341 from MEL to PVG. DUR: 645. AC: 332. DEPT: 2017-07-09 11:00:00+10:00. ARR: 2017-07-09 19:45:00+08:00.
- Clone the repo.
- Extract and
cd
into the cloned repo. - Run
pip install -r requirements.txt
in your shell to install any required libraries. - Place the
flypy
folder inside your project. - Input your API key in the
api_key.py
file in theflypy
folder. Follow the directions here to obtain one if you don't have one already. - Import
flypy
inside your code -from flypy import *
Query
objects are used to specify the details of the airfares we want. A successful query returns a QueryResponse
object which includes an array of trip options found by QPX. A trip option is a Trip
object representing an airfare which meets all of the query criteria.
Trip
s have Journey
s; a single Journey
for one-way airfares, and two for return airfares. Each Journey
comprises of Leg
s and Layover
s. Leg
s are always present and represent a flight. Layover
s are present when multiple flights are required within a Journey
and a connection time occurs at an airport.
A blank query can be created by instantiating an object of the Query
class. query = Query
sets up a query which will return an unlimited number of results. An integer can be passed into the constructor to limit the number of results, e.g. query = Query(1)
will return a single result.
Search parameters can be added to the query using the query.add*
methods.
A basic Query
has four mandatory search parameters:
origin
: the IATA code of the originating airport- e.g.
query.add_origin("MEL")
- e.g.
dest
: the IATA code of the destination airport- e.g.
query.add_dest("PVG")
- e.g.
dept_date
: the intended departure date from the originating airport- e.g.
query.add_dept_date("2017-07-09")
- e.g.
pax
: the number of passengers of each type travelling- e.g.
query.add_pax(1) # adds an adult passenger
- e.g.
The query will not run unless all mandatory parameters are supplied.
The following optional parameters can be supplied to further customise the search:
query.add_return_date(...)
query.add_airline(...) # restricts results to a particular airline
query.add_max_stops(...) # maximum allowed layovers
More documentation for these methods are available inline in the Query class.
Once the desired parameters are added, it's time to send the query! response = query.send()
will fire off the query and return a QueryResponse
object, which we'll describe in the next section.
A QueryResponse
object contains the raw JSON response from QPX, and the various trip objects generated by the library. We'll mostly be concerned with the trip objects.
response.get_trips()
returns an array of Trip
objects for each result returned by the query.
A Trip
has a total cost - the total airfare in the currency of the originating country, and an array of Journey
objects, which represent each portion of the trip. A one-way fare includes a single Journey
, a return airfare includes two.
get_cost()
- returns the cost of the trip in the originating country's currencyget_journeys()
- returns an array of Journey objects
Trip
s can be printed to show a high level overview.
A Journey
has Leg
s, and Layover
s. The latter is only present when connections occur at airports.
get_legs()
- returns an array of Leg objectsget_layovers()
- returns an array of Layover objects
Journey
s can be printed to show a high level overview.
A Leg
has the following methods:
get_origin()
- returns a dictionary of the originating airport's detailsget_dest()
- returns a dictionary of the destination airport's detailsget_dept_time()
- returns the departure time and date with timezoneget_arr_time()
- returns the arrival time and date with timezonget_flight()
- returns the flight number and carrier nameget_aircraft()
- returns the aircraft code and nameget_duration()
- returns the duration of the flight in minutes
Leg
s can be printed to show a high level overview.
A Layover
has the following methods:
get_layover_airport()
- returns a dictionary of the layover airport's detailsget_layover_dur()
- returns the duration of the layover in minutesget_layover_start()
- returns the starting time of the layover (e.g. when the flight arrives)get_layover_end()
- returns the ending time of the layover (e.g. when the departing flight leaves)
Layover
s can be printed to show a high level overview.
- Airlines JSON data sourced from https://github.com/BesrourMS/Airlines
- Airports data sourced from http://ourairports.com/data/ and converted to JSON format.
The project is licensed under the MIT license.