Created
August 26, 2023 09:45
-
-
Save kaushalvivek/e6284a8d9ccb57b4b7944607820e5837 to your computer and use it in GitHub Desktop.
A short, dirty lambda function, to poll for US Visa appointment updates and email you, when faster slot opens up
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
import ssl | |
import smtplib | |
from datetime import datetime, timedelta | |
import requests | |
from bs4 import BeautifulSoup | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
URL = "https://travel.state.gov/content/travel/en/us-visas/visa-information-resources/global-visa-wait-times.html" | |
CURRENT_APPOINTMENT = datetime(2024, 1, 1) # enter your current appointment date here | |
INDIAN_CITIES = ["Chennai", "Hyderabad", "Kolkata", "Mumbai", "New Delhi"] | |
def lambda_handler(event, context): | |
today = datetime.today() | |
page = requests.get(URL) | |
soup = BeautifulSoup(page.content, "html.parser") | |
faster_options = [] | |
for city in INDIAN_CITIES: | |
city_div = soup.find(string=re.compile(f"^{city}.*")) | |
row = city_div.parent.parent | |
cells = row.find_all("td") | |
target_cell = cells[-1] | |
wait_time = target_cell.text.strip() | |
wait_time_in_days = int(wait_time.split(" ")[0]) | |
if today + timedelta(days=wait_time_in_days) < CURRENT_APPOINTMENT: | |
faster_options.append( | |
{ | |
"city": city, | |
"wait_time": wait_time_in_days, | |
"appointment_date": today + timedelta(days=wait_time_in_days) | |
} | |
) | |
if len(faster_options) > 0: | |
alert(faster_options) | |
print(f"Found {len(faster_options)} faster options than {CURRENT_APPOINTMENT}") | |
def alert(faster_options): | |
print(faster_options) | |
sender_email = os.getenv("SENDER_EMAIL") | |
password = os.getenv("SENDER_PASSWORD") | |
receiver_email = os.getenv("RECEIVER_EMAIL") | |
message = MIMEMultipart("alternative") | |
message["Subject"] = "US Visa Appointment Alert" | |
message["From"] = sender_email | |
message["To"] = receiver_email | |
html = """\ | |
<html> | |
<body> | |
<p>Hi,<br> | |
<br> | |
There are faster options available for US visa appointments in India. Here are the details:<br> | |
<br> | |
<table> | |
<tr> | |
<th>City</th> | |
<th>Wait Time</th> | |
<th>Appointment Date</th> | |
</tr> | |
{rows} | |
</table> | |
<br> | |
<br> | |
Thanks,<br> | |
<br> | |
Your friendly neighborhood bot | |
</p> | |
</body> | |
</html> | |
""" | |
rows = "" | |
for option in faster_options: | |
rows += f"<tr><td>{option['city']}</td><td>{option['wait_time']}</td><td>{option['appointment_date']}</td></tr>" | |
html = html.format(rows=rows) | |
part2 = MIMEText(html, "html") | |
message.attach(part2) | |
context = ssl.create_default_context() | |
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: | |
server.login(sender_email, password) | |
server.sendmail( | |
sender_email, receiver_email, message.as_string() | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment