def get_prices(trips, db_filename, config): """ DB structure: { /* Date of search */ "2013-01-01": { /* str(Trip) */ "SFO:BOS:2013-02-01": [ FlightInfo(...), FlightInfo(...), /* ... */ ] }, "2013-01-02": { /* ... */ } } """ wait_time = int(config['wait']) if 'wait' in config else 10 DATE_FMT = "%Y-%m-%d" with closing(shelve.open(db_filename)) as db: db_flights = {} for trip in trips: flights = [] date_str = trip.date.strftime(DATE_FMT) sw_json = run_casper(config, ['casper/southwest_prices.coffee', trip.origin, trip.dest, date_str]) flights += parse.southwest(sw_json.decode()) bing_json = run_casper(config, ['casper/bing_prices.coffee', trip.origin, trip.dest, date_str]) flights += parse.bing(bing_json.decode()) if len(flights) == 0: print("WARNING: no flights returned {}!".format(trip)) else: db_flights[str(trip)] = flights sleep(wait_time) db[date.today().strftime(DATE_FMT)] = db_flights
def get_prices(trips, db_filename, config): """ DB structure: { /* Date of search */ "2013-01-01": { /* str(Trip) */ "SFO:BOS:2013-02-01": [ FlightInfo(...), FlightInfo(...), /* ... */ ] }, "2013-01-02": { /* ... */ } } """ wait_time = int(config['wait']) if 'wait' in config else 10 DATE_FMT = "%Y-%m-%d" with closing(shelve.open(db_filename)) as db: db_flights = {} for trip in trips: flights = [] date_str = trip.date.strftime(DATE_FMT) sw_json = run_casper(config, [ 'casper/southwest_prices.coffee', trip.origin, trip.dest, date_str ]) flights += parse.southwest(sw_json.decode()) bing_json = run_casper(config, [ 'casper/bing_prices.coffee', trip.origin, trip.dest, date_str ]) flights += parse.bing(bing_json.decode()) if len(flights) == 0: print("WARNING: no flights returned {}!".format(trip)) else: db_flights[str(trip)] = flights sleep(wait_time) db[date.today().strftime(DATE_FMT)] = db_flights
from __future__ import print_function, unicode_literals, division import sys import json from datetime import date, timedelta from flightprice import next_day, run_casper with open(sys.argv[1], "r") as f: config = json.load(f) for o in config["origins"]: for d in config["destinations"]: for day_spec in config["days"]: day = day_spec[0] length = day_spec[1] out_dt = next_day(date.today(), day) in_dt = out_dt + timedelta(length) print("Trip: {} -> {} ({}); {} -> {} ({})".format( o, d, out_dt.strftime('%a'), d, o, in_dt.strftime('%a') )) if 'casperjs_cmd' in config: # Try running casper cmd output = run_casper(config, []) if not output.startswith(b'CasperJS'): print("WARNING: can't run casperjs_cmd")
from __future__ import print_function, unicode_literals, division import sys import json from datetime import date, timedelta from flightprice import next_day, run_casper with open(sys.argv[1], "r") as f: config = json.load(f) for o in config["origins"]: for d in config["destinations"]: for day_spec in config["days"]: day = day_spec[0] length = day_spec[1] out_dt = next_day(date.today(), day) in_dt = out_dt + timedelta(length) print("Trip: {} -> {} ({}); {} -> {} ({})".format( o, d, out_dt.strftime('%a'), d, o, in_dt.strftime('%a'))) if 'casperjs_cmd' in config: # Try running casper cmd output = run_casper(config, []) if not output.startswith(b'CasperJS'): print("WARNING: can't run casperjs_cmd")