def auto_checkin(threads, reservation_number, first_name, last_name, prikey, verbose=False): r = Reservation(reservation_number, first_name, last_name, verbose) body = r.lookup_existing_reservation() # Get our local current time now = datetime.utcnow().replace(tzinfo=utc) tomorrow = now + timedelta(days=1) # find all eligible legs for checkin for leg in body['bounds']: # calculate departure for this leg airport = "{}, {}".format(leg['departureAirport']['name'], leg['departureAirport']['state']) takeoff = "{} {}".format(leg['departureDate'], leg['departureTime']) airport_tz = openflights.timezone_for_airport( leg['departureAirport']['code']) date = airport_tz.localize(datetime.strptime(takeoff, '%Y-%m-%d %H:%M')) if date > now: # found a flight for checkin! print(("Flight information found, departing {} at {}".format( airport, date.strftime('%b %d %I:%M%p')))) # Checkin with a thread t = Thread(target=schedule_checkin, args=(date, r, prikey)) t.daemon = True t.start() threads.append(t) # Need to go to the next conf so send back threads to manage return threads
def checkin(confirmation, flight_info_index): print("Checking in confirmation: {}".format(confirmation['confirmation'])) reservation = Reservation(confirmation['confirmation'], confirmation['firstName'], confirmation['lastName'], []) # This will try to checkin multiple times data = None try: data = reservation.checkin() failed = False except Exception as e: print("Failed") failed = True confirmation['flightInfo'][flight_info_index]['failed'] = failed if failed: r.set(confirmation.get('confirmation'), json.dumps(confirmation)) return confirmation confirmation['flightInfo'][flight_info_index]['checkedIn'] = True for flight in data['flights']: for doc in flight['passengers']: confirmation['flightInfo'][flight_info_index]['results'].append( { 'name': doc['name'], 'boardingGroup': doc['boardingGroup'], 'boardingPosition': doc['boardingPosition'] } ) app.logger.info("{} got {}{}!".format(doc['name'], doc['boardingGroup'], doc['boardingPosition'])) r.set(confirmation.get('confirmation'), json.dumps(confirmation)) return confirmation
def lookup_reservation(reservation_number, first_name, last_name, verbose=False): r = Reservation(reservation_number, first_name, last_name, verbose) body = r.lookup_existing_reservation() # Get our local current time now = datetime.utcnow().replace(tzinfo=timezone.utc) tomorrow = now + timedelta(days=1) flight_times = [] # find all eligible legs for checkin for leg in body["bounds"]: # calculate departure for this leg airport = "{}, {}".format(leg["departureAirport"]["name"], leg["departureAirport"]["state"]) takeoff = "{} {}".format(leg["departureDate"], leg["departureTime"]) airport_tz = openflights.timezone_for_airport( leg["departureAirport"]["code"]) date = airport_tz.localize(datetime.strptime(takeoff, "%Y-%m-%d %H:%M")) if date > now: # found a flight for checkin! print("Flight information found, departing {} at {}".format( airport, date.strftime("%b %d %I:%M%p"))) flight_times.append(date) return r, flight_times
def auto_checkin(reservation_number, first_name, last_name, email_address=None, verbose=True): reservation = Reservation(reservation_number, first_name, last_name, verbose) body = reservation.lookup_existing_reservation() # Get our local current time now = datetime.utcnow().replace(tzinfo=utc) tomorrow = now + timedelta(days=1) leg_id_to_threads = dict() # find all eligible legs for checkin for leg in body['bounds']: # calculate departure for this leg airport = "{}, {}".format(leg['departureAirport']['name'], leg['departureAirport']['state']) takeoff = "{} {}".format(leg['departureDate'], leg['departureTime']) departure_airport = leg['departureAirport']['code'] destination_airport = leg['destinationAirport']['code'] airport_tz = openflights.timezone_for_airport( leg['departureAirport']['code']) date = airport_tz.localize(datetime.strptime(takeoff, '%Y-%m-%d %H:%M')) leg_id = '{}.{}.{}.{}.{}.{}'.format(reservation_number, first_name, last_name, departure_airport, destination_airport, takeoff) if date > now: # found a flight for checkin! logging.info("Flight information found, departing {} at {}".format( airport, date.strftime('%b %d %I:%M%p'))) # Checkin with a thread t = Thread(target=schedule_checkin, args=(date, reservation)) t.daemon = True t.start() leg_id_to_threads[leg_id] = t reservation_dict = { 'reservation': reservation, 'leg_id_to_threads': leg_id_to_threads, 'reservation_number': reservation_number, 'first_name': first_name, 'last_name': last_name, 'email_address': email_address } return reservation_dict
def auto_checkin(reservation_number, first_name, last_name, email="", verbose=False): r = Reservation(reservation_number, first_name, last_name, verbose) body = r.lookup_existing_reservation() # Get our local current time now = datetime.utcnow().replace(tzinfo=utc) tomorrow = now + timedelta(days=1) threads = [] # find all eligible legs for checkin for leg in body['bounds']: # calculate departure for this leg airport = "{}, {}".format(leg['departureAirport']['name'], leg['departureAirport']['state']) takeoff = "{} {}".format(leg['departureDate'], leg['departureTime']) airport_tz = openflights.timezone_for_airport( leg['departureAirport']['code']) date = airport_tz.localize(datetime.strptime(takeoff, '%Y-%m-%d %H:%M')) if date > now: # found a flight for checkin! print("Flight information found, departing {} at {}".format( airport, date.strftime('%b %d %I:%M%p'))) # Checkin with a thread t = Thread(target=schedule_checkin, args=(date, r, email)) t.daemon = True t.start() threads.append(t) time.sleep(0.2) print("No more flights associated with this reservation") # cleanup threads while handling Ctrl+C while True: if len(threads) == 0: break for t in threads: t.join(5) if not t.is_alive(): threads.remove(t) break
def set_takeoff(reservation_number, first_name, last_name, verbose=False): r = Reservation(reservation_number, first_name, last_name, verbose) body = r.lookup_existing_reservation() # connect to db to store date db = flights_db.connect() cursor = db.cursor(dictionary=True) i = 0 # find all eligible legs for checkin for leg in body['bounds']: # calculate departure for this leg print("calc departure in leg") airport = "{}, {}".format(leg['departureAirport']['name'], leg['departureAirport']['state']) takeoff = "{} {}".format(leg['departureDate'], leg['departureTime']) for item in leg['flights']: flightnum = item['number'] airport_tz = openflights.timezone_for_airport( leg['departureAirport']['code']) # need to convert this to computer time so we start checkin at the right time cpu_tz = openflights.timezone_for_airport('DEN') temp = airport_tz.localize(datetime.strptime(takeoff, '%Y-%m-%d %H:%M')) date = temp.astimezone(cpu_tz) if i == 0: query = "UPDATE flightinfo SET takeoff=%s, flightnum=%s WHERE conf=%s" cursor.execute(query, (date, flightnum, reservation_number)) db.commit() else: query = "INSERT INTO flightinfo (takeoff, flightnum, conf, first, last) VALUES (%s, %s, %s, %s, %s)" cursor.execute( query, (date, flightnum, reservation_number, first_name, last_name)) db.commit() i += 1 db.close() return ()
def _auto_checkin(reservation_number, first_name, last_name, notify=[]): r = Reservation(reservation_number, first_name, last_name, notify) body = r.lookup_existing_reservation() if body is None: my_logger.warning("Giving up on " + reservation_number) return # Get our local current time now = datetime.utcnow().replace(tzinfo=utc) tomorrow = now + timedelta(days=1) threads = [] # find all eligible legs for checkin for leg in body["bounds"]: # calculate departure for this leg airport = "{}, {}".format(leg["departureAirport"]["name"], leg["departureAirport"]["state"]) takeoff = "{} {}".format(leg["departureDate"], leg["departureTime"]) airport_tz = openflights.timezone_for_airport( leg["departureAirport"]["code"]) date = airport_tz.localize(datetime.strptime(takeoff, "%Y-%m-%d %H:%M")) if date > now: # found a flight for checkin! my_logger.info( "Flight information found, departing {} at {}".format( airport, date.strftime("%b %d %I:%M%p"))) # Checkin with a thread stop_signal = Event() t = Thread(target=schedule_checkin, args=(date, r, stop_signal)) t.daemon = True t.start() threads.append({"thread": t, "signal": stop_signal}) return threads
def submit_confirmation(): data = json.loads(request.data) # Validate errors = [] if not data.get('firstName'): errors.append('provide a first name') if not data.get('lastName'): errors.append('provide a last name') if not data.get('confirmation') or len(data.get('confirmation')) != 6: errors.append('provide a correct confirmation') recaptcha = data.get('recaptcha') if recaptcha is None or len( recaptcha) == 0 or not verify_captcha(recaptcha): return jsonify({"errors": ["recaptcha was incorrect"]}), 400 # Don't want this to go into the database del data['recaptcha'] # Bail if we have errors if len(errors) > 0: return jsonify({"errors": errors}), 400 notifications = [] if data.get('email') is not None: notifications.append({ 'mediaType': 'EMAIL', 'emailAddress': data.get('email') }) if data.get('phone') is not None: notifications.append({ 'mediaType': 'SMS', 'phoneNumber': data.get('phone') }) data['confirmation'] = data['confirmation'].upper() reservation = Reservation(data['confirmation'], data['firstName'], data['lastName'], notifications) try: body = reservation.lookup_existing_reservation() except Exception as e: # Couldn't find the reservation return jsonify({"errors": [str(e)]}), 400 if body is None: app.logger.warn("body response was none from southwest API") return jsonify({"errors": ["could not get reservation information"]}), 400 flight_info_list = [] # find all eligible legs for checkin for leg in body['bounds']: flight_info = {} # calculate departure for this leg airport = "{}, {}".format(leg['departureAirport']['name'], leg['departureAirport']['state']) destination_airport = "{}, {}".format(leg['arrivalAirport']['name'], leg['arrivalAirport']['state']) takeoff = "{}-{}".format(leg['departureDate'], leg['departureTime']) flight_info['takeoff'] = takeoff flight_info['departureAirport'] = airport flight_info['destinationAirport'] = destination_airport flight_info['checkedIn'] = False flight_info['numberOfPeople'] = int( leg['passengerTypeCounts']['adult']) + int( leg['passengerTypeCounts']['senior']) flight_info['boundType'] = leg['boundType'] flight_info['travelTime'] = leg['travelTime'] flight_info['nextDayArrival'] = leg['isNextDayArrival'] flight_info['arrivalTime'] = leg['arrivalTime'] airport_tz = timezone_for_airport(leg['departureAirport']['code']) local_dt = airport_tz.localize( datetime.datetime.strptime(takeoff, '%Y-%m-%d-%H:%M')) utc_dt = local_dt.astimezone(utc) # Crazy converserino here utc_day = datetime.datetime.combine(utc_dt.date(), datetime.time(0, 0, 0), tzinfo=utc) flight_info['utcDepartureTimestamp'] = int( datetime.datetime.timestamp(local_dt)) flight_info['utcDay'] = int(datetime.datetime.timestamp(utc_day)) flight_info['results'] = [] flight_info['failed'] = False flight_info_list.append(flight_info) data['flightInfo'] = flight_info_list for flight_info in flight_info_list: r.sadd(flight_info.get('utcDay'), data.get('confirmation')) r.set(data.get('confirmation').upper(), json.dumps(data)) return jsonify(data), 201