-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotification.py
83 lines (68 loc) · 2.68 KB
/
notification.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
78
79
80
81
82
83
import requests
import time
from bs4 import BeautifulSoup
import re
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import os
def send_notification(email, password, recipient, subject, body):
msg = MIMEMultipart()
msg['From'] = email
msg['To'] = recipient
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.sendmail(email, recipient, text)
server.quit()
def fetch_course_info(url):
response = requests.get(url)
if response.status_code == 200:
print("Successfully fetched course info.")
html_content = response.text
# Use BeautifulSoup to parse the HTML content
soup = BeautifulSoup(html_content, 'html.parser')
# Find the "Section Max Enrollment" data
max_enrollment_text = soup.find('b', string="Section Max Enrollment: ")
# Find the "Section Enrolled: " data
enrollment_text = soup.find('b', string="Section Enrolled: ")
# Get the next sibling (which should be the number)
max_enrollment_number = max_enrollment_text.next_sibling
enrollment_number = enrollment_text.next_sibling
# Extract just the number using regular expression
if max_enrollment_number:
match = re.search(r'\d+', max_enrollment_number)
if match:
max_enrollment_number = int(match.group())
if enrollment_number:
match = re.search(r'\d+', enrollment_number)
if match:
enrollment_number = int(match.group())
print("Section Max Enrollment:", max_enrollment_number)
print("Section Enrolled: ", enrollment_number)
if max_enrollment_number > enrollment_number:
print("The course has empty seats!")
# Define your email, password, recipient, subject and body
email = "SENDER EMAIL HERE"
password = "SENDER PASSWORD HERE"
recipient = "RECIPIENT EMAIL HERE"
subject = "Course Seat Available"
body = "There are empty seats available in the course."
send_notification(email, password, recipient, subject, body)
else:
print("The course is full!")
else:
print("Failed to fetch course info. Status code:", response.status_code)
def main():
URL = "YOUR URL HERE"
while True:
if fetch_course_info(url):
print("Email Sent, Program exits")
sys.exit()
print("Waiting for 2 minutes...")
time.sleep(120)
if __name__ == "__main__":
main()