-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifier.py
More file actions
112 lines (92 loc) · 4.02 KB
/
Copy pathnotifier.py
File metadata and controls
112 lines (92 loc) · 4.02 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import requests
from bs4 import BeautifulSoup
import os
import smtplib
from email.mime.text import MIMEText
# Configuration
URL = "https://msc-ai.iit.demokritos.gr/el/announcements"
FILE_NAME = "last_announcement.txt"
EMAIL_SENDER = os.environ.get("EMAIL_SENDER")
EMAIL_PASSWORD = os.environ.get("EMAIL_PASSWORD")
EMAIL_RECEIVER = os.environ.get("EMAIL_RECEIVER")
def send_email(subject, items):
# list_items will now be a list of dicts with 'title', 'date', and 'link'
formatted_items = ""
for item in items:
formatted_items += f"""
<li style="margin-bottom: 15px; border-bottom: 1px solid #eee; padding-bottom: 10px; list-style: none;">
<strong style="font-size: 1.1em; color: #003366;">{item['title']}</strong><br>
<span style="color: #666; font-size: 0.9em;">📅 {item['date']}</span><br>
<a href="{item['link']}" style="color: #3498db; text-decoration: none; font-size: 0.9em;">Read announcement →</a>
</li>
"""
html_body = f"""
<html>
<body style="font-family: Arial, sans-serif; line-height: 1.6; color: #333;">
<div style="max-width: 600px; margin: auto; border: 1px solid #ddd; padding: 20px; border-radius: 8px;">
<h2 style="color: #003366; text-align: center; border-bottom: 2px solid #003366; padding-bottom: 10px;">
MSc AI Announcements
</h2>
<ul style="padding-left: 0;">
{formatted_items}
</ul>
<div style="text-align: center; margin-top: 20px;">
<a href="{URL}" style="background-color: #003366; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px;">
Open All Announcements
</a>
</div>
</div>
</body>
</html>
"""
print(html_body)
msg = MIMEText(html_body, "html")
msg["Subject"] = subject
msg["From"] = EMAIL_SENDER
msg["To"] = EMAIL_RECEIVER
try:
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
server.login(EMAIL_SENDER, EMAIL_PASSWORD)
server.sendmail(EMAIL_SENDER, EMAIL_RECEIVER, msg.as_string())
print("Email sent successfully.")
except Exception as e:
print(f"SMTP Error: {e}")
def scrape():
response = requests.get(URL)
soup = BeautifulSoup(response.text, "html.parser")
# Target the individual announcement blocks within the grid
announcement_blocks = soup.find_all("div", class_="node-announcements")
if not announcement_blocks:
print("No announcements found. Check if selectors need updating.")
return
announcements_data = []
for block in announcement_blocks:
# Extract Title and Link
title_tag = block.find("h3").find("a") if block.find("h3") else None
title = title_tag.get_text(strip=True) if title_tag else "No Title"
link = f"https://msc-ai.iit.demokritos.gr{title_tag['href']}" if title_tag else URL
# Extract Date
date_tag = block.find("div", class_="field-name-post-date")
date = date_tag.get_text(strip=True) if date_tag else "No Date"
announcements_data.append({
"title": title,
"date": date,
"link": link
})
# Sort announcements by date - https://stackoverflow.com/a/652347
announcements_data.sort(key=lambda item:item["date"], reverse=True)
# Use the unique link of the first announcement to check for changes
latest_id = announcements_data[0]["link"]
last_known = ""
if os.path.exists(FILE_NAME):
with open(FILE_NAME, "r", encoding="utf-8") as f:
last_known = f.read().strip()
if latest_id != last_known:
print("New announcement detected!")
send_email("Hermes: New MSc AI Announcement Found", announcements_data)
with open(FILE_NAME, "w", encoding="utf-8") as f:
f.write(latest_id)
else:
print("Everything is up to date.")
if __name__ == "__main__":
scrape()