mirror of
https://github.com/pyro2927/SouthwestCheckin.git
synced 2025-12-06 01:13:19 +00:00
add functionality for email and text notifications
This commit is contained in:
68
checkin.py
68
checkin.py
@@ -2,13 +2,15 @@
|
|||||||
"""Southwest Checkin.
|
"""Southwest Checkin.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
checkin.py CONFIRMATION_NUMBER FIRST_NAME LAST_NAME [-v | --verbose]
|
checkin.py CONFIRMATION_NUMBER FIRST_NAME LAST_NAME [--email=<email_addr> | --mobile=<phone_num>] [-v | --verbose]
|
||||||
checkin.py (-h | --help)
|
checkin.py (-h | --help)
|
||||||
checkin.py --version
|
checkin.py --version
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-h --help Show this screen.
|
-h --help Show this screen.
|
||||||
-v --verbose Show debugging information.
|
-v --verbose Show debugging information.
|
||||||
|
--email=<email_addr> Email address where notification will be sent to.
|
||||||
|
--mobile=<phone_num> Phone number where text notification will be sent to.
|
||||||
--version Show version.
|
--version Show version.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@@ -39,21 +41,25 @@ headers = {'Host': 'mobile.southwest.com', 'Content-Type': 'application/json', '
|
|||||||
# Basically, there sometimes appears a "hiccup" in Southwest where things
|
# Basically, there sometimes appears a "hiccup" in Southwest where things
|
||||||
# aren't exactly available 24-hours before, so we try a few times
|
# aren't exactly available 24-hours before, so we try a few times
|
||||||
def safe_request(url, body=None):
|
def safe_request(url, body=None):
|
||||||
attempts = 0
|
try:
|
||||||
while True:
|
attempts = 0
|
||||||
if body is not None:
|
while True:
|
||||||
r = requests.post(url, headers=headers, json=body)
|
if body is not None:
|
||||||
else:
|
r = requests.post(url, headers=headers, json=body)
|
||||||
r = requests.get(url, headers=headers)
|
else:
|
||||||
data = r.json()
|
r = requests.get(url, headers=headers)
|
||||||
if 'httpStatusCode' in data and data['httpStatusCode'] in ['NOT_FOUND', 'BAD_REQUEST', 'FORBIDDEN']:
|
data = r.json()
|
||||||
attempts += 1
|
if 'httpStatusCode' in data and data['httpStatusCode'] in ['NOT_FOUND', 'BAD_REQUEST', 'FORBIDDEN']:
|
||||||
print(data['message'])
|
attempts += 1
|
||||||
if attempts > MAX_ATTEMPTS:
|
print(data['message'])
|
||||||
sys.exit("Unable to get data, killing self")
|
if attempts > MAX_ATTEMPTS:
|
||||||
time.sleep(CHECKIN_INTERVAL_SECONDS)
|
sys.exit("Unable to get data, killing self")
|
||||||
continue
|
time.sleep(CHECKIN_INTERVAL_SECONDS)
|
||||||
return data
|
continue
|
||||||
|
return data
|
||||||
|
except ValueError:
|
||||||
|
# Ignore responses with no json data in body
|
||||||
|
pass
|
||||||
|
|
||||||
def lookup_existing_reservation(number, first, last):
|
def lookup_existing_reservation(number, first, last):
|
||||||
# Find our existing record
|
# Find our existing record
|
||||||
@@ -73,7 +79,22 @@ def checkin(number, first, last):
|
|||||||
print("Attempting check-in...")
|
print("Attempting check-in...")
|
||||||
return safe_request(url, info_needed['body'])['checkInConfirmationPage']
|
return safe_request(url, info_needed['body'])['checkInConfirmationPage']
|
||||||
|
|
||||||
def schedule_checkin(flight_time, number, first, last):
|
def send_notification(checkindata, emailaddr=None, mobilenum=None):
|
||||||
|
info_needed = checkindata['_links']['boardingPasses']
|
||||||
|
url = "{}mobile-air-operations{}".format(BASE_URL, info_needed['href'])
|
||||||
|
mbpdata = safe_request(url, info_needed['body'])
|
||||||
|
info_needed = mbpdata['checkInViewBoardingPassPage']['_links']
|
||||||
|
url = "{}mobile-air-operations{}".format(BASE_URL, info_needed['href'])
|
||||||
|
if emailaddr:
|
||||||
|
info_needed['body']['mediaType'] = 'EMAIL'
|
||||||
|
info_needed['body']['emailAddress'] = emailaddr
|
||||||
|
if mobilenum:
|
||||||
|
info_needed['body']['mediaType'] = 'SMS'
|
||||||
|
info_needed['body']['phoneNumber'] = mobilenum
|
||||||
|
print("Attempting to send boarding pass...")
|
||||||
|
safe_request(url, info_needed['body'])
|
||||||
|
|
||||||
|
def schedule_checkin(flight_time, number, first, last, email, mobile):
|
||||||
checkin_time = flight_time - timedelta(days=1)
|
checkin_time = flight_time - timedelta(days=1)
|
||||||
current_time = datetime.now(pytz.utc).astimezone(get_localzone())
|
current_time = datetime.now(pytz.utc).astimezone(get_localzone())
|
||||||
# check to see if we need to sleep until 24 hours before flight
|
# check to see if we need to sleep until 24 hours before flight
|
||||||
@@ -89,8 +110,13 @@ def schedule_checkin(flight_time, number, first, last):
|
|||||||
for flight in data['flights']:
|
for flight in data['flights']:
|
||||||
for doc in flight['passengers']:
|
for doc in flight['passengers']:
|
||||||
print("{} got {}{}!".format(doc['name'], doc['boardingGroup'], doc['boardingPosition']))
|
print("{} got {}{}!".format(doc['name'], doc['boardingGroup'], doc['boardingPosition']))
|
||||||
|
if email:
|
||||||
|
send_notification(data, emailaddr=email)
|
||||||
|
elif mobile:
|
||||||
|
send_notification(data, mobilenum=mobile)
|
||||||
|
|
||||||
def auto_checkin(reservation_number, first_name, last_name):
|
|
||||||
|
def auto_checkin(reservation_number, first_name, last_name, email, mobile):
|
||||||
body = lookup_existing_reservation(reservation_number, first_name, last_name)
|
body = lookup_existing_reservation(reservation_number, first_name, last_name)
|
||||||
|
|
||||||
# Get our local current time
|
# Get our local current time
|
||||||
@@ -114,7 +140,7 @@ def auto_checkin(reservation_number, first_name, last_name):
|
|||||||
if date > now:
|
if date > now:
|
||||||
# found a flight for checkin!
|
# found a flight for checkin!
|
||||||
print("Flight information found, departing {} at {}".format(airport, date.strftime('%b %d %I:%M%p')))
|
print("Flight information found, departing {} at {}".format(airport, date.strftime('%b %d %I:%M%p')))
|
||||||
schedule_checkin(date, reservation_number, first_name, last_name)
|
schedule_checkin(date, reservation_number, first_name, last_name, email, mobile)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
arguments = docopt(__doc__, version='Southwest Checkin 0.2')
|
arguments = docopt(__doc__, version='Southwest Checkin 0.2')
|
||||||
@@ -123,5 +149,7 @@ if __name__ == '__main__':
|
|||||||
reservation_number = arguments['CONFIRMATION_NUMBER']
|
reservation_number = arguments['CONFIRMATION_NUMBER']
|
||||||
first_name = arguments['FIRST_NAME']
|
first_name = arguments['FIRST_NAME']
|
||||||
last_name = arguments['LAST_NAME']
|
last_name = arguments['LAST_NAME']
|
||||||
|
email = arguments['--email']
|
||||||
|
mobile = arguments['--mobile']
|
||||||
|
|
||||||
auto_checkin(reservation_number, first_name, last_name)
|
auto_checkin(reservation_number, first_name, last_name, email, mobile)
|
||||||
|
|||||||
Reference in New Issue
Block a user