def find_best_travel(self, list_airports=None, days=None, num_countries=None, departure_date=None): flights = Flights('KwG8uSgsdnyDstlNsMeKCPioagUNLYJS') if departure_date == None: departure_date = time.time() days_per_city = int(days / num_countries) distance_matrix = np.zeros((len(list_airports), len(list_airports))) total = len(list_airports) * len(list_airports) counter = 0 for i, airport_departure in enumerate(list_airports): print(airport_departure) for j, airport_arrival in enumerate(list_airports): print(airport_arrival) if airport_arrival == airport_departure: distance_matrix[i, j] = 0 else: prices_list = [] for duration in range(0, days, days_per_city): print(duration) date = datetime.datetime.fromtimestamp( departure_date + duration * 24 * 3600).strftime('%Y-%m-%d') print(date) resp = flights.low_fare_search(origin=airport_departure, destination=airport_arrival, departure_date=date, duration='1') try: price = resp['results'][0]['fare']['total_price'] except: continue print(price) prices_list.append(float(price)) if prices_list == []: distance_matrix[i, j] = 10e6 else: distance_matrix[i, j] = np.mean(prices_list) counter += 1 current_task.update_state( state='PROGRESS', meta={'process_percent': round(float(counter / total), 2)}) return distance_matrix.tolist()
def find_best_travel(list_airports=None, days=None, num_countries=None, departure_date=None): flights = Flights('rTgACcDGGTOrYi9vGotfQOM2wfHAGly8') if departure_date == None: departure_date = time.time() days_per_city = int(days / num_countries) distance_matrix = np.zeros((len(list_airports), len(list_airports))) for i, airport_departure in enumerate(list_airports): print(airport_departure) for j, airport_arrival in enumerate(list_airports): print(airport_arrival) if airport_arrival == airport_departure: distance_matrix[i, j] = 0 else: prices_list = [] for duration in range(0, 40, days_per_city): print(duration) date = datetime.datetime.fromtimestamp( departure_date + duration * 24 * 3600).strftime('%Y-%m-%d') print(date) resp = flights.low_fare_search(origin=airport_departure, destination=airport_arrival, departure_date=date, duration='1') try: price = resp['results'][0]['fare']['total_price'] except: continue print(price) prices_list.append(float(price)) if prices_list == []: distance_matrix[i, j] = 10e6 else: distance_matrix[i, j] = np.mean(prices_list) return distance_matrix
writer = csv.DictWriter(csvprecos, fieldnames=fieldnames) writer.writeheader() with open('viagens.csv', 'r') as csvfile: csvreader = csv.reader(csvfile, delimiter=';', quotechar='|') for row in csvreader: datapesquisa = time.strftime("%d-%m-%Y") origem = row[0] destino = row[1] dataida = row[2] result = flights.low_fare_search( origin=origem, destination=destino, currency='BRL', departure_date=dataida, ) data = result if ('status' in result and result['status']== 400): with open('prices.csv', 'a+') as csvprecos: writer = csv.DictWriter(csvprecos, fieldnames=fieldnames) writer.writerow({'DataPesquisa': datapesquisa, 'Origem': origem, 'Destino': destino, 'DataIda': dataida, "Preco": "NAO ENCONTRADA"}) else: try: preco = data['results'][0]['fare']['total_price']
def get_flight_info(orig, dest, depart_date, ret_date, duration, num_results='3'): duration2 = str(duration) + "--" + str(duration) print(duration2) flights = Flights("S1ukqxEJjvLGcmtKD1wSPEEp9apIcsiC") resp = flights.low_fare_search( origin=orig, #'LGA' destination=dest, departure_date=depart_date, return_date=ret_date, duration=duration2, #'4--7' number_of_results=num_results) #'10' # Loop over all suggested flights if ('results' not in resp): return 0 num_results = len(resp['results']) outbound_len = [None] * num_results outbound_departure_time = [None] * num_results outbound_departure_location = [None] * num_results outbound_arrival_time = [None] * num_results outbound_arrival_location = [None] * num_results inbound_len = [None] * num_results inbound_departure_time = [None] * num_results inbound_departure_location = [None] * num_results inbound_arrival_time = [None] * num_results inbound_arrival_location = [None] * num_results flight_price = [None] * num_results num_seats_available_inbound = [[] for i in range(num_results)] num_seats_available_outbound = [[] for i in range(num_results)] terminal_inbound = [[] for i in range(num_results)] terminal_outbound = [[] for i in range(num_results)] flight_number_inbound = [[] for i in range(num_results)] flight_number_outbound = [[] for i in range(num_results)] marketing_airline_inbound = [[] for i in range(num_results)] marketing_airline_outbound = [[] for i in range(num_results)] print(num_results) for i in range(num_results): # Outbound outbound_len[i] = len( resp['results'][i]['itineraries'][0]['outbound']['flights']) - 1 outbound_departure_time[i] = resp['results'][i]['itineraries'][0][ 'outbound']['flights'][0]['departs_at'] outbound_departure_location[i] = resp['results'][i]['itineraries'][0][ 'outbound']['flights'][0]['origin']['airport'] outbound_arrival_time[i] = resp['results'][i]['itineraries'][0][ 'outbound']['flights'][outbound_len[i]]['arrives_at'] outbound_arrival_location[i] = resp['results'][i]['itineraries'][0][ 'outbound']['flights'][outbound_len[i]]['destination']['airport'] for j in resp['results'][i]['itineraries'][0]['outbound']['flights']: num_seats_available_outbound[i].append( j['booking_info']['seats_remaining']) if 'terminal' in j['destination']: terminal_outbound[i].append(j['destination']['terminal']) else: terminal_outbound[i].append('TBD') flight_number_outbound[i].append(j['flight_number']) marketing_airline_outbound[i].append( get_airline_name(j['marketing_airline'])) # Inbound inbound_len[i] = len( resp['results'][i]['itineraries'][0]['inbound']['flights']) - 1 inbound_departure_time[i] = resp['results'][i]['itineraries'][0][ 'inbound']['flights'][0]['departs_at'] inbound_departure_location[i] = resp['results'][i]['itineraries'][0][ 'inbound']['flights'][0]['origin']['airport'] inbound_arrival_time[i] = resp['results'][i]['itineraries'][0][ 'inbound']['flights'][inbound_len[i]]['arrives_at'] inbound_arrival_location[i] = resp['results'][i]['itineraries'][0][ 'inbound']['flights'][inbound_len[i]]['destination']['airport'] for j in resp['results'][i]['itineraries'][0]['inbound']['flights']: num_seats_available_inbound[i].append( j['booking_info']['seats_remaining']) if 'terminal' in j['destination']: terminal_inbound[i].append(j['destination']['terminal']) else: terminal_inbound[i].append('TBD') flight_number_inbound[i].append(j['flight_number']) marketing_airline_inbound[i].append( get_airline_name(j['marketing_airline'])) flight_price[i] = resp['results'][i]['fare']['total_price'] return outbound_len, inbound_len, outbound_departure_time, outbound_departure_location, outbound_arrival_time, outbound_arrival_location, inbound_departure_time, inbound_departure_location, inbound_arrival_time, inbound_arrival_location, flight_price, num_seats_available_inbound, num_seats_available_outbound, terminal_inbound, terminal_outbound, flight_number_inbound, flight_number_outbound, marketing_airline_inbound, marketing_airline_outbound