def generate_flight_timings_weekday(self): for data in self.res: for itinerary_no, itinerary in enumerate(data["itineraries"]): stops_str = self.stops_format(itinerary["segments"]) departure = itinerary["segments"][0]["departure"] departure_place = get_city_name(departure["iataCode"]) departure_date, departure_time = self.datetime_format( departure["at"]) departure_terminal = "(Terminal: {})".format( departure.get("terminal")).replace("(Terminal: None)", "") arrival = itinerary["segments"][-1]["arrival"] arrival_place = get_city_name(arrival["iataCode"]) arrival_date, arrival_time = self.datetime_format( arrival["at"]) arrival_terminal = "(Terminal: {})".format( arrival.get("terminal")).replace("(Terminal: None)", "") airline_iata_code = itinerary["segments"][0]["carrierCode"] airline_name = amadeus.reference_data.airlines.get( airlineCodes=airline_iata_code ).data[0]["businessName"].title() flight_duration = self.duration_format(itinerary["duration"]) if itinerary_no % 2 == 1: trip_leg = "Outbound journey: " else: trip_leg = "Inbound journey: " output = self.flight_time_format( trip_leg, airline_name, departure_place, departure_terminal, departure_date, departure_time, arrival_place, arrival_terminal, arrival_date, arrival_time, flight_duration, stops_str) if datetime.strptime(departure_time, "%I:%M %p").weekday() < 5: yield output
def generate_flight_timings_relative(self): timings = [] for data in self.res: for itinerary_no, itinerary in enumerate(data["itineraries"]): stops_str = self.stops_format(itinerary["segments"], segment_fare_details) departure = itinerary["segments"][0]["departure"] departure_place = get_city_name(departure["iataCode"]) departure_date, departure_time = self.datetime_format( departure["at"]) departure_terminal = "(Terminal: {})".format( departure.get("terminal")).replace("(Terminal: None)", "") arrival = itinerary["segments"][-1]["arrival"] arrival_place = get_city_name(arrival["iataCode"]) arrival_date, arrival_time = self.datetime_format( arrival["at"]) arrival_terminal = "(Terminal: {})".format( arrival.get("terminal")).replace("(Terminal: None)", "") airline_iata_code = itinerary["segments"][0]["carrierCode"] airline_name = amadeus.reference_data.airlines.get( airlineCodes=airline_iata_code ).data[0]["businessName"].title() flight_duration = self.duration_format(itinerary["duration"]) if itinerary_no % 2 == 1: trip_leg = "Outbound journey: " else: trip_leg = "Inbound journey: " output = self.flight_time_format( trip_leg, airline_name, departure_place, departure_terminal, departure_date, departure_time, arrival_place, arrival_terminal, arrival_date, arrival_time, flight_duration, stops_str) timings.append( (output, datetime.strptime(departure_time, "%I:%M %p"), datetime.strptime(arrival_time, "%I:%M %p"))) if self.flight_mod == "early": output = min(timings, key=lambda x: x[1]) yield output[0] elif self.flight_mod == "last": output = max(timings, key=lambda x: x[1]) yield output[0] elif self.flight_mod == "earliest arriving": output = min(timings, key=lambda x: x[2]) yield output[0]
def generate_flight_quotes(self): for data in self.res: itinerary_out = "" for itinerary_no, itinerary in enumerate(data["itineraries"]): segment_fare_details = list( filter(lambda x: x['travelerType'] == 'ADULT', data['travelerPricings']))[0] baggage_allowance = segment_fare_details[ 'fareDetailsBySegment'][-1]['includedCheckedBags'] if baggage_allowance.get("weight"): baggage_allowance_str = str( baggage_allowance["weight"] ) + baggage_allowance["weightUnit"] else: baggage_allowance_str = "25kg" stops_str = self.stops_format(itinerary["segments"]) departure = itinerary["segments"][0]["departure"] departure_place = get_city_name(departure["iataCode"]) departure_date, departure_time = self.datetime_format( departure["at"]) departure_terminal = "(Terminal: {})".format( departure.get("terminal")).replace("(Terminal: None)", "") arrival = itinerary["segments"][-1]["arrival"] arrival_place = get_city_name(arrival["iataCode"]) arrival_date, arrival_time = self.datetime_format( arrival["at"]) arrival_terminal = "(Terminal: {})".format( arrival.get("terminal")).replace("(Terminal: None)", "") airline_iata_code = itinerary["segments"][0]["carrierCode"] # airline_name = get_airline_name(airline_iata_code) airline_name = amadeus.reference_data.airlines.get( airlineCodes=airline_iata_code ).data[0]["businessName"].title() flight_duration = self.duration_format(itinerary["duration"]) price = data["price"] total_cost = currency_code_mapping.get( price["currency"])[1] + price["grandTotal"] if itinerary_no % 2 == 0: trip_leg = "Outbound journey: " else: trip_leg = "Inbound journey: " output = "{} {} Departure from {}{} on {} at {} ---> Arrival in {}{} on {} at {}. {}. {}. Cost: {}. Baggage Allowance : {}.\n".format( trip_leg, airline_name, departure_place, departure_terminal, departure_date, departure_time, arrival_place, arrival_terminal, arrival_date, arrival_time, flight_duration, stops_str, total_cost, baggage_allowance_str) itinerary_out += output yield itinerary_out
def stops_format(self, segments): stops = [] no_of_segments = len(segments) if no_of_segments > 2: stop_no = 1 for segment in segments[:-1]: segment_id = segment["id"] [date, time] = self.datetime_format(segment["arrival"]["at"]) stops.append("{}.{} at {} on {}".format( stop_no, get_city_name(segment["arrival"]["iataCode"]), time, date)) stop_no += 1 stops_str = "{} stops at: ".format(no_of_segments - 1) stops_str += " and ".join([",".join(stops[:-1]), stops[-1]]) elif no_of_segments == 2: segment = segments[0] [date, time] = self.datetime_format(segment["arrival"]["at"]) stops.append("{} at {} on {}".format( get_city_name(segment["arrival"]["iataCode"]), time, date.replace(",", ""))) stops_str = "1 stop at: {}".format(stops[0]) else: stops_str = "" return stops_str
from amadeus_endpoint import AmadeusFlight from iata_mapping import get_city_name, get_airline_name import re try: a = AmadeusFlight("Pune", "Delhi", "2020-07-01") except Exception as e: dispatcher.utter_message(str(e)) else: res = a.generate_flight_prices() quote_id = 0 for data in res: for itinerary in data["itineraries"]: departure = itinerary["segments"][0]["departure"] departure_place = get_city_name(departure["iataCode"]) departure_datetime = departure["at"].split("T") departure_date = departure_datetime[0] departure_time = departure_datetime[1] try: departure_terminal = departure["terminal"] except KeyError: departure_terminal = "" arrival = itinerary["segments"][-1]["arrival"] arrival_place = get_city_name(arrival["iataCode"]) arrival_datetime = arrival["at"].split("T") arrival_date = arrival_datetime[0] arrival_time = arrival_datetime[1] try: arrival_terminal = arrival["terminal"] except KeyError as e:
def get_email_params(self, booking_params, pnr): email_params = { k: booking_params[k] for k in [ "pnr", "base_fare", "supplier_fee", "ticketing_fee", "form_of_payment_fee", "tax", "email_id", "mobile_no", "country_code", "no_of_checkbags" ] } email_params[ "airline_name"] = "<strong>" + amadeus.reference_data.airlines.get( airlineCodes=booking_params["airline_iata"] ).data[0]["businessName"].title() + "</strong>" email_params["airline_iata"] = "<strong>" + booking_params[ "airline_iata"] + "</strong>" email_params["baggage_allowance"] = str( booking_params["baggage_allowance_weight"] ) + booking_params["baggage_allowance_unit"].lower() email_params["user_name"] = booking_params[ "title"] + ". " + booking_params["user_name"] email_params["total"] = booking_params["base_fare"] + booking_params[ "supplier_fee"] + booking_params["ticketing_fee"] + booking_params[ "form_of_payment_fee"] + booking_params["tax"] email_params["outbound"] = { "source_place": get_city_name(self.outbound_params["source_iata"]), "source_airport": get_airport_name(self.outbound_params["source_iata"]), "destination_place": get_city_name(self.outbound_params["destination_iata"]), "destination_airport": get_airport_name(self.outbound_params["destination_iata"]), "depart_date": datetime.strptime(self.outbound_params["depart_date"], "%Y-%m-%d").strftime("%a, %d %b %Y"), "depart_time": self.outbound_params["depart_time"][:5], "arrive_time": self.outbound_params["arrive_time"][:5], "no_of_stops": self.outbound_params["no_of_stops"], "flight_no": self.outbound_params["flight_number"], "flight_duration": self.outbound_params["flight_duration"] } if self.round_trip: email_params["inbound"] = { "source_place": get_city_name(self.inbound_params["source_iata"]), "destination_place": get_city_name(self.inbound_params["destination_iata"]), "depart_date": datetime.strptime(self.inbound_params["depart_date"], "%Y-%m-%d").strftime("%a, %d %b %Y"), "depart_time": self.inbound_params["depart_time"][:5], "arrive_time": self.inbound_params["arrive_time"][:5], "no_of_stops": self.inbound_params["no_of_stops"], "flight_no": self.inbound_params["flight_number"], "flight_duration": self.inbound_params["flight_duration"] } # print(email_params) return email_params
def get_booking_details(self): with self.engine.connect() as connection: query = select([self.bookings]).where( self.bookings.c.pnr.like(self.pnr + '__%')) results = connection.execute(query).fetchall() if len(results) == 0 or len(self.pnr) < 7: out = "Sorry no bookings with booking reference {} available".format( self.pnr) title = user_name = email_id = country_code = mobile_no = None elif len(results) == 1: out = "" [ pnr, title, user_name, booking_date, source_iata, source_terminal, destination_iata, destination_terminal, depart_date, depart_time, arrive_date, arrive_time, stops, flight_duration, no_of_stops, flight_number, adult_paxno, child_paxno, infant_paxno, travel_class, currency, airline_iata, aircraft_type, baggage_allowance_weight, baggage_allowance_unit, email_id, country_code, mobile_no, no_of_checkbags, fare_basis_code, base_fare, supplier, ticketing_fee, form_of_payment_fee, tax, grand_total, travel_bound ] = results[0] if stops: stops = json.loads(stops) stops_str = self.stops_format(stops["segments"]) else: stops_str = "" source_terminal = self.terminal_format(source_terminal) destination_terminal = self.terminal_format( destination_terminal) outbound_params = [ "Outbound", get_airline_name(airline_iata), get_city_name(source_iata), source_terminal, self.date_format(depart_date), self.time_format(depart_time), get_city_name(destination_iata), destination_terminal, self.date_format(arrive_date), self.time_format(arrive_time), " Total duration: " + flight_duration, stops_str ] passenger_details = self.passengers_details_format( adult_paxno, child_paxno, infant_paxno) out += "Welcome back {} {}. Your booking reference is {}. You booked your flight on {}. Total fare paid: {}. Fare basis code: {}. Baggage allowance: {}. Your email address is {} and mobile number is {}. No of passengers travelling: {}.\n\n".format( title, user_name.strip(), pnr[:-2], get_airline_name(airline_iata), currency_code_mapping.get(currency)[1] + str(grand_total), fare_basis_code, str(baggage_allowance_weight) + baggage_allowance_unit, email_id, "+" + str(country_code) + str(mobile_no), passenger_details) out += "{} {} Departure from {}{} on {} at {} ---> Arrival in {}{} on {} at {}. {}. {}\n".format( "Outbound: ", *outbound_params) else: out = "" [ pnr, title, user_name, booking_date, source_iata, source_terminal, destination_iata, destination_terminal, depart_date, depart_time, arrive_date, arrive_time, stops, flight_duration, no_of_stops, flight_number, adult_paxno, child_paxno, infant_paxno, travel_class, currency, airline_iata, aircraft_type, baggage_allowance_weight, baggage_allowance_unit, email_id, country_code, mobile_no, no_of_checkbags, fare_basis_code, base_fare, supplier, ticketing_fee, form_of_payment_fee, tax, grand_total, travel_bound ] = results[0] stops = json.loads(stops) stops_str = self.stops_format(stops["segments"]) source_terminal = self.terminal_format(source_terminal) destination_terminal = self.terminal_format( destination_terminal) inbound_params = [ flight_number, get_city_name(source_iata), source_terminal, self.date_format(depart_date), self.time_format(depart_time), get_city_name(destination_iata), destination_terminal, self.date_format(arrive_date), self.time_format(arrive_time), " Total duration: " + flight_duration, stops_str ] [ pnr, title, user_name, booking_date, source_iata, source_terminal, destination_iata, destination_terminal, depart_date, depart_time, arrive_date, arrive_time, stops, flight_duration, no_of_stops, flight_number, adult_paxno, child_paxno, infant_paxno, travel_class, currency, airline_iata, aircraft_type, baggage_allowance_weight, baggage_allowance_unit, email_id, country_code, mobile_no, no_of_checkbags, fare_basis_code, base_fare, supplier, ticketing_fee, form_of_payment_fee, tax, grand_total, travel_bound ] = results[1] if stops: stops = json.loads(stops) stops_str = self.stops_format(stops["segments"]) else: stops_str = "" source_terminal = self.terminal_format(source_terminal) destination_terminal = self.terminal_format( destination_terminal) outbound_params = [ flight_number, get_city_name(source_iata), source_terminal, self.date_format(depart_date), self.time_format(depart_time), get_city_name(destination_iata), destination_terminal, self.date_format(arrive_date), self.time_format(arrive_time), " Total duration: " + flight_duration, stops_str ] passenger_details = self.passengers_details_format( adult_paxno, child_paxno, infant_paxno) out += "Welcome back {} {}. Your booking reference is {}. You booked your flight on {}. Total fare paid: {}. Fare basis code: {}. Baggage allowance: {}. Your email address is {} and mobile number is {}. No of passengers travelling: {}.\n\n".format( title, user_name.strip(), pnr[:-2], get_airline_name(airline_iata), currency_code_mapping.get(currency)[1] + str(grand_total), fare_basis_code, str(baggage_allowance_weight) + baggage_allowance_unit, email_id, "+" + str(country_code) + str(mobile_no), passenger_details) out += "{} Flight {}. Departure from {}{} on {} at {} ---> Arrival in {}{} on {} at {}. {}. {}\n".format( "Outbound: ", *outbound_params) out += "{} Flight {}. Departure from {}{} on {} at {} ---> Arrival in {}{} on {} at {}. {}. {}".format( "Inbound: ", *inbound_params) return out, title, user_name, email_id, country_code, mobile_no