def search_generic(term, flow="", sort=""): """Call the generic autocomplete API. term: search term flow: either 'hotels' or 'flights' sort: indicates the order of the objects returned (e.g. 'airports,cities'). """ response = get(BASE + str(term) + "?flow=" + flow + "&sort=" + sort) if 'autocomplete' in response: return response['autocomplete']
def get_roundtrip_flight(adults="1", children="0", infants="0", cabintype="ECONOMY", stops="MORE_THAN_ONE", **kwargs): """Return info on roundtrip flights Required arguments: orig: 3-letter code for airport or city of origin to: 3-letter code for airport or city of destination departure: yyyy-mm-dd date of departure returning: yyyy-mm-dd date of return Optional arguments: adults: quantity of adults travelling children: quantity of children travelling infants: quantity of infants travelling cabintype: cabin type preference (ECONOMY, BUSINESS, FIRSTCLASS, PREMIUM_ECONOMY, PREMIUM_BUSINESS, PREMIUM_FIRSTCLASS) stopsadvancedparameter: how many stops (NONE, ONE, MORE_THAN_ONE) Return value is subject to change. Right now it's a dict cointaining: result['flights']: raw info on flights result['matrix']: condensed summary to build the matrix as seen in despegar.com's main search page. """ required_args = ['orig', 'to', 'departure', 'returning'] for arg in required_args: if arg not in kwargs: raise TypeError("Missed a required argument. Required arguments " "are: " + ", ".join(required_args)) retry = 0 content = "" while retry < 10: try: content = get(BASE + "roundTrip" + "/" + kwargs['orig'] + "/" + kwargs['to'] + "/" + kwargs['departure'] + "/" + kwargs['returning'] + "/" + adults + "/" + children + "/" + infants + "?cabintype=" + cabintype) break except HTTPError: retry += 1 time.sleep(1) result = {} if retry == 10: print("Exceeded retry count for " + kwargs['departure']) if 'flights' in content: result['flights'] = content['flights'] if 'meta' in content: if 'summaryMatrix' in content['meta']: result['matrix'] = content['meta']['summaryMatrix'] return result
def get_mult_flights(adults="1", children="0", infants="0", cabintype="ECONOMY", stops="MORE_THAN_ONE", **kwargs): """Return info on multiple flights Required arguments: orig: comma-separated list of 3-letter code for airport or city of origin to: comma-separated list of 3-letter code for airport or city of destination departure: comma-separated list of yyyy-mm-dd dates of departure The rest of the information required/returned is the same as get_roundtrip_flight. Required arguments for this function must have commas in them, so that despegar.com can match, e.g.: - flight from orig[0] to to[0] on departure[0] - flight from orig[1] to to[0] on departure[1] """ required_args = ['orig', 'to', 'departure'] for arg in required_args: if arg not in kwargs: raise TypeError("Missed a required argument. Required arguments " "are: " + ", ".join(required_args)) if kwargs[arg].find(',') == -1: raise TypeError("Arguments in this function must have commas!") content = "" retry = 0 while retry < 10: try: content = get(BASE + "multipleDestinations" + "/" + kwargs['orig'] + "/" + kwargs['to'] + "/" + kwargs['departure'] + "/" + adults + "/" + children + "/" + infants + "?cabintype=" + cabintype) break except HTTPError: retry += 1 time.sleep(1) result = {} if retry == 10: print("Exceeded retry count for " + kwargs['departure']) if 'flights' in content: result['flights'] = content['flights'] for item in result['flights']: # Copy each flight's 'id' to its root so we can work it as usual item['id'] = item['itineraryInfo']['id'] if 'meta' in content: if 'summaryMatrix' in content['meta']: result['matrix'] = content['meta']['summaryMatrix'] return result
def search_airlines(term): """Only search for airlines""" response = get(BASE + "airlines/" + str(term)) if 'data' in response: return response['data']
def search_airports(term): """Only search for airports""" response = get(BASE + "airports/" + str(term)) if 'autocomplete' in response: return response['autocomplete']
def search_cities(term): """Only search for cities""" response = get(BASE + "cities/" + str(term)) if 'autocomplete' in response: return response['autocomplete']
def get_city(code): validate_params([code]) response = get(BASE + str(code)) if 'cities' in response: return response['cities'][0]
def get_airport(code): """Get an airport based on a 3-letter code""" validate_params([code]) response = get(BASE + str(code)) if 'airports' in response: return response['airports'][0]