├── .gitignore ├── README.md ├── data.json ├── google_flight ├── __init__.py └── google_flight_api.py └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | local_settings.py 2 | *.swp 3 | *.pyc 4 | *.log 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | google_flight_api 4 | ====== 5 | 6 | A simple wrapper for the google flight api 7 | 8 | Installation 9 | ===== 10 | 11 | pip install google_flight_api 12 | 13 | Usage 14 | ===== 15 | 16 | First, get yourself a `QPX Express Airfare API `_. 17 | 18 | Test your json request https://qpx-express-demo.itasoftware.com/ 19 | 20 | 21 | 22 | from google_flight import google_flight_api 23 | import datetime 24 | 25 | start_date = datetime.datetime.now() 26 | end_date = datetime.datetime.now() 27 | start_date += datetime.timedelta(days=2) 28 | end_date += datetime.timedelta(days=7) 29 | 30 | g = google_flight_api.GoogleFlight("MY_API_KEY") 31 | data = { 32 | "request": { 33 | "slice": [ 34 | { 35 | "origin": "PVD", 36 | "destination": "MCO", 37 | "date": start_date.strftime("%Y-%m-%d") #"2017-08-04" 38 | }, 39 | { 40 | "origin": "MCO", 41 | "destination": "PVD", 42 | "date": end_date.strftime("%Y-%m-%d") #"2017-08-24" 43 | } 44 | ], 45 | "passengers": { 46 | "adultCount": 1, 47 | "infantInLapCount": 0, 48 | "infantInSeatCount": 0, 49 | "childCount": 0, 50 | "seniorCount": 0 51 | }, 52 | "solutions": 20, 53 | "refundable": 'false' 54 | } 55 | } 56 | g.get(data) 57 | 58 | 59 | 60 | Examples 61 | ======== 62 | g.KEY 63 | g.URL 64 | g.request 65 | g.params 66 | g.count 67 | g.data 68 | g.trips 69 | 70 | Return the first tripOption 71 | 72 | g.lowest() 73 | 74 | Print out all the search result 75 | 76 | g.print_result() 77 | 78 | Print out json to readable 79 | 80 | g.readable(json_data) 81 | 82 | 83 | Check you api key 84 | ======== 85 | https://console.cloud.google.com -> Go to APIs overview -> Credentials -------------------------------------------------------------------------------- /google_flight/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxu/google_flight_api/e5b169ffc3dd1c0afb826a2a938ae07802f6d779/google_flight/__init__.py -------------------------------------------------------------------------------- /google_flight/google_flight_api.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import json 3 | 4 | 5 | LOCALFILE = False 6 | 7 | try: 8 | from local_settings import * 9 | except ImportError, e: 10 | pass 11 | 12 | 13 | __version__ = "0.1" 14 | 15 | URL = 'https://www.googleapis.com/qpxExpress/v1/trips/search' 16 | headers = {'Content-Type': 'application/json'} 17 | 18 | class Struct: 19 | def __init__(self, **entries): 20 | self.__dict__.update(entries) 21 | 22 | class GoogleFlight(object): 23 | 24 | def __init__(self, key): 25 | self.URL = 'https://www.googleapis.com/qpxExpress/v1/trips/search?key=%s'%key 26 | self.KEY = key 27 | self.DEBUG = False 28 | self.request = None 29 | self.params = {} 30 | self.count = 0 31 | self.data = {} 32 | self.trips = None 33 | 34 | def get(self, params): 35 | self.params = params 36 | 37 | if LOCALFILE: 38 | with open('data.json') as data_file: 39 | data = json.load(data_file) 40 | 41 | else: 42 | r = requests.post(self.URL, data=json.dumps(params), headers=headers) 43 | self.request = r 44 | data = json.loads(r.text) 45 | 46 | self.data = data 47 | 48 | if self.DEBUG: 49 | self.write(self.data) 50 | 51 | try: 52 | self.count = len(self.data["trips"]["tripOption"]) 53 | except: 54 | raise Exception(self.data) 55 | self.trips = self.data["trips"]["tripOption"] 56 | 57 | def write(self, data): 58 | f = open('debug.json', 'w') 59 | f.write(str(data)) 60 | f.close() 61 | 62 | def readable(self,data): 63 | print json.dumps(data, indent=4, sort_keys=True) 64 | 65 | 66 | def print_trip(self,trip): 67 | Slice = 0 68 | for s in trip["slice"]: 69 | Slice = Slice + 1 70 | print " Slice %s" % Slice 71 | for flight in s["segment"]: 72 | flight_number = flight["flight"]["number"] 73 | flight_carrier = flight["flight"]["carrier"] 74 | flight_origin = flight['leg'][0]['origin'] 75 | flight_departureTime = flight['leg'][0]['departureTime'] 76 | flight_destination = flight['leg'][0]['destination'] 77 | flight_arrivalTime = flight['leg'][0]['arrivalTime'] 78 | flight_mileage = flight['leg'][0]['mileage'] 79 | print " %s%s %s %s %s %s %s mileage"%(flight_carrier, flight_number, flight_origin, flight_departureTime, flight_destination, flight_arrivalTime, flight_mileage) 80 | 81 | 82 | def print_result(self): 83 | if self.trips != None: 84 | Solution = 0 85 | for trip in self.trips: 86 | Solution = Solution + 1 87 | print "\nSolution# %s Sale Price: %s"% (Solution, trip["saleTotal"]) 88 | self.print_trip(trip) 89 | else: 90 | print "No data yet" 91 | 92 | def lowest(self): 93 | if self.trips != None: 94 | lowest = self.data["trips"]["tripOption"][0] 95 | for trip in self.data["trips"]["tripOption"]: 96 | if trip['saleTotal'] < lowest['saleTotal']: 97 | lowest = trip 98 | return Struct(**lowest) 99 | else: 100 | print "No data yet" 101 | 102 | 103 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from distutils.core import setup 4 | 5 | setup(name='google_flight_api', 6 | version='1.0', 7 | description='A simple wrapper for the google flight api', 8 | author='Yang Xu', 9 | author_email='yangxu432@gmail.com', 10 | url='https://github.com/yangxu/google_flight_api', 11 | license="MIT", 12 | packages=['google_flight'], 13 | ) --------------------------------------------------------------------------------