Skip to content

Instantly share code, notes, and snippets.

@thisiswei
Created September 12, 2015 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thisiswei/50c26230e8923167d9b9 to your computer and use it in GitHub Desktop.
Save thisiswei/50c26230e8923167d9b9 to your computer and use it in GitHub Desktop.
get flights
'''
pip install pprint
pip install utensil
pip install requests
script for grabbing deals from here: http://content.flyfrontier.com/ways-to-save/online-deals
i.e., get deals flying out of Denver and flying back to LGA or PHL:
python /tmp/tmp6.py --console --o=DEN --d=LGA,PHL
'''
import re
import requests
from collections import defaultdict
def _get_json():
return requests.get("http://content.flyfrontier.com/api/marketingapi/getmarketingsaledata?toCityName=All%20Cities&toCitySaleName=All%20Sales&r=1442024258274",
headers={
"Accept": "application/json, text/javascript, */*; q=0.01",
"Accept-Encoding": "gzip, deflate, sdch",
"Accept-Language": "en-US,en;q=0.8,zh-CN;q=0.6",
"Connection": "keep-alive",
"Content-Type": "application/json; charset=utf-8",
"DNT": "1",
"Referer": "http://content.flyfrontier.com/ways-to-save/online-deals",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
},
cookies={
"optimizelyBuckets": "%7B%222669850174%22%3A%222661030082%22%2C%223184220259%22%3A%223176170500%22%2C%223219100379%22%3A%223203460419%22%2C%223240230820%22%3A%223240230821%22%2C%223443590597%22%3A%223443590598%22%7D",
"optimizelyEndUserId": "oeu1442024257721r0.6081004193983972",
"optimizelyPendingLogEvents": "%5B%5D",
"optimizelySegments": "%7B%222425250345%22%3A%22direct%22%2C%222444691053%22%3A%22gc%22%2C%222448750186%22%3A%22false%22%2C%222630260345%22%3A%22none%22%2C%223350590731%22%3A%22true%22%7D",
"webhost": "3990802186.20480.0000",
}).json()
def construct_maps():
"""
:return: dict(origin: list(destination))
"""
json_ = _get_json()
maps = defaultdict(set)
for j in json_:
for s in j['MarketingSales']:
maps[_normalize(s['FromCity'])].add(_normalize(s['ToCity']))
return maps
def _normalize(string):
# 'San Jose Cabo (SJD)' -> 'SJD'
return re.search('.+?\((\w+)\)', string).group(1)
def find(origin, destinations):
frontier = [[origin]]
seen = set()
to_return = []
maps = construct_maps()
while frontier:
last = frontier.pop(0)
dests = maps.get(last[-1])
for dest in dests:
if dest in destinations:
to_return.append(last + [dest])
continue
if dest not in seen:
seen.add(dest)
frontier.append(last + [dest])
continue
return to_return
def main():
define('o', type=str, help='origin')
define('d', type=str, help='destinations', multiple=True)
define('print', type=bool, multiple=False)
parse_command_line()
basicConfig(options=options)
rs = find(options.o, options.d)
pprint(rs)
if __name__ == '__main__':
from utensils.options import define
from utensils.options import options
from utensils.options import parse_command_line
from utensils.loggingutils import basicConfig
from pprint import pprint
exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment