September 17, 2014

How to find out when Airfares are Cheapest

Yahoo-owned travel search destination, Hotwire lets you leverage their tripstarter api to optimise price based on a date range and two airports. I wrote a small PoC in python to list the lowest prices between two cities, as seen by hotwire, below:

#!python
import argparse
import logging
import requests
import time

API_KEY = "" # get your own hotwire key
BASE_URL = "http://api.hotwire.com/v1/tripstarter/air?"


def lowest_fare_for_dates(from_airport, to_airport, low_end, high_end):
    options = dict()
    options['startdate'] = low_end
    options['enddate'] = high_end
    options['origin'] = from_airport
    options['dest'] = to_airport
    options['sort'] = 'price'
    options['sortorder'] = 'asc'  # or desc, defaults to asc
    options['apikey'] = API_KEY
    options['format'] = 'json'
    response = requests.get(BASE_URL, params=options)
    logging.debug(response.json())
    return response.json()

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Command Line demo for hotwire flights API -- http://developer.hotwire.com/docs/read/TripStarter_APIs')
    parser.add_argument('--From', action='store', help='Starting point', type=unicode, default='SFO')
    parser.add_argument('--to', action='store', help='Ending point', type=unicode, default='LHR')
    parser.add_argument('--start', action='store', help='Earlier Date (mm/dd/yyyy format)', type=unicode)
    parser.add_argument('--end', action='store', help='Later Date (mm/dd/yyyy format)', type=unicode)
    parser.add_argument('-v', '--verbose', help='Print Debugging messages', action='store_true')

    args = parser.parse_args()

    if args.verbose:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.FATAL)

    price_info = lowest_fare_for_dates(args.From, args.to, args.start, args.end)
    if len(price_info['Result']) == 0:
        print('No results found')
    else:
        print("\n".join(['{} {} in week starting on {}'.format(r['CurrencyCode'], r['AveragePrice'], r['WeekStartDate']) for r in price_info['Result']]))

No comments:

Post a Comment