-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifier.py
77 lines (64 loc) · 2.29 KB
/
notifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
from math import log
import requests
import time
import sys
from datetime import datetime, timedelta
import yagmail
import logging
# API URL
APPOINTMENTS_URL = "https://ttp.cbp.dhs.gov/schedulerapi/slots?orderBy=soonest&limit=1&locationId={}&minimum=1"
EMAIL_TEMPLATE = """
<p>Good news! New Global Entry appointment(s) available on the following date:</p>
%s
"""
# List of Global Entry locations
# Insert your location IDs here
LOCATION_IDS = {
'Newark': 5444,
'Nashville': 10260
}
# How often to run this check in seconds
TIME_WAIT = 60
# Number of days into the future to look for appointments
DAYS_OUT = 60
# Dates
now = datetime.now()
future_date = now + timedelta(days=DAYS_OUT)
def notify_send_email(date):
recipient = "Insert recipient email address here"
try:
yagmail.SMTP(recipient, oauth2_file="~/credentials.json").send(dfrecipient, 'Global Entry Appointment Available!', EMAIL_TEMPLATE % date)
except Exception:
logging.exception('Failed to send succcess e-mail.')
log(e)
def check_appointments(city, id):
url = APPOINTMENTS_URL.format(id)
appointments = requests.get(url).json()
return appointments
def appointment_in_timeframe(now, future_date, appointment_date):
if now <= appt_datetime <= future_date:
return True
else:
return False
try:
while True:
for city, id in LOCATION_IDS.items():
try:
appointments = check_appointments(city, id)
except Exception as e:
print("Could not retrieve appointments from API.")
appointments = []
if appointments:
appt_datetime = datetime.strptime(appointments[0]['startTimestamp'], '%Y-%m-%dT%H:%M')
if appointment_in_timeframe(now, future_date, appt_datetime):
message = "{}: Found an appointment at {}!".format(city, appointments[0]['startTimestamp'])
notify_send_email(message)
else:
print("{}: No appointments during the next {} days.".format(city, DAYS_OUT))
else:
print("{}: No appointments during the next {} days.".format(city, DAYS_OUT))
time.sleep(1)
time.sleep(TIME_WAIT)
except KeyboardInterrupt:
sys.exit(0)