Last active
February 12, 2023 00:32
-
-
Save hmms/464801cdc5c325a885a8a38246fb6520 to your computer and use it in GitHub Desktop.
A small python script that enables automated extraction of a table from visaslots.info and sends it over email and Telegram
This file contains 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 requests | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
from email.mime.base import MIMEBase | |
from email import encoders | |
import time | |
import requests | |
import pandas as pd | |
from bs4 import BeautifulSoup | |
import smtplib | |
TOKEN = "YOUR-TOKEN" | |
chat_id = "CHAT-ID" | |
email_app_password = "APP-PASS" | |
senders_email = 'abc@abc.co' | |
receivers_email = 'def@abc.co' | |
email_signature = 'Thank you' | |
visa_slots_website = 'https://visaslots.info' | |
def parse_and_send_over_telegram(url): | |
page = requests.get(url) | |
df = pd.read_html(page.content, attrs={'class':'sortable', 'role':'grid'})[0] | |
b1_data = df[df['Visa'].str.contains("B1")] | |
if (df['Earliest'].isnull().values.any()): | |
message = "no New slots found"; | |
print(message) | |
else: | |
message = b1_data.to_string() | |
print(message) | |
send_message_using_telegram(message=message) | |
print(b1_data) | |
def send_message_using_telegram(message,TOKEN=TOKEN,chat_id=chat_id): | |
url = f"https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={chat_id}&text={message}" | |
value = requests.get(url).json(); | |
print(value) # this sends the message | |
def parse_html_to_email(url, sender, sender_password ,receiver, signature): | |
page = requests.get(url) | |
soup = BeautifulSoup(page.content, 'html.parser') | |
tables = soup.find_all('table') | |
body = "" | |
for table in tables: | |
for tr in table.find_all('tr'): | |
row = [] | |
for td in tr.find_all('td'): | |
row.append(td.text) | |
body += " ".join(row) + "\n" | |
#attach the data to an email | |
fromaddr = sender | |
toaddr = receiver | |
msg = MIMEMultipart() | |
msg['From'] = fromaddr | |
msg['To'] = toaddr | |
msg['Subject'] = "B1 Visa Slots info" | |
body += "\n"+signature | |
msg.attach(MIMEText(body, 'plain')) | |
s = smtplib.SMTP('smtp.gmail.com', 587) | |
s.starttls() | |
s.login(fromaddr, sender_password) | |
text = msg.as_string() | |
s.sendmail(fromaddr, toaddr, text) | |
s.quit() | |
i = 0 | |
while True: | |
i = i + 1 | |
if (i % 20 == 0): | |
parse_and_send_over_telegram(visa_slots_website) | |
if (i % 7200 == 0): | |
parse_html_to_email(visa_slots_website, senders_email, email_app_password ,receivers_email, email_signature) | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment