check-osmcal-for-paris-events/check.py
2024-10-18 08:52:41 +01:00

63 lines
1.6 KiB
Python
Executable file

#!/usr/bin/python3
"""Watch https://osmcal.org/ for another Paris OSM hack weekend."""
import email.message
import email.utils
import smtplib
import sys
import requests
MAIL_FROM = "edward@4angle.com"
MAIL_TO = "edward@4angle.com"
SMTP_HOST = "4angle.com"
URL = "https://osmcal.org/api/v2/events/?in=France"
def send_mail(subject: str, body: str) -> None:
"""Send an e-mail."""
msg = email.message.EmailMessage()
msg["Subject"] = subject
msg["To"] = f"Edward Betts <{MAIL_TO}>"
msg["From"] = f"OSM Calendar alert <{MAIL_FROM}>"
msg["Date"] = email.utils.formatdate()
msg["Message-ID"] = email.utils.make_msgid()
msg.set_content(body)
s = smtplib.SMTP(SMTP_HOST)
s.sendmail(MAIL_FROM, [MAIL_TO], msg.as_string())
s.quit()
def check_for_paris_events() -> None:
"""Check for upcoming OSM events in Paris and send an email if found."""
r = requests.get(URL)
events = r.json()
paris_events = [
event for event in events if "paris" in event["location"]["detailed"].lower()
]
if not paris_events:
if sys.stdin.isatty():
print(f"{len(events)} events found, none in Paris.")
return
subject = "Upcoming Paris OSM Hack Weekend Found!"
count = len(paris_events)
body = f"Found {count} {'event' if count == 1 else 'events'} in Paris:\n\n"
for event in paris_events:
body += (
f"{event['name']} - {event['url']}\n"
+ f"Date: {event['date']['human']}\n"
+ f"Location: {event['location']['detailed']}\n\n"
)
send_mail(subject, body)
if __name__ == "__main__":
check_for_paris_events()