Last active
August 27, 2015 16:38
-
-
Save lrivallain/e3939c78494e2db4d06f to your computer and use it in GitHub Desktop.
A simple ICS events notifier by email
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
{ | |
"url": "http://an/ics/file/uri.ics", | |
"mailfile": "/tmp/ics-notifier.email.txt", | |
"subject": "Évènement", | |
"tzone": "Europe/Paris", | |
"mails": [ | |
"[email protected]", | |
"[email protected]" | |
] | |
} |
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from ics import Calendar | |
from urllib2 import urlopen | |
import arrow | |
import subprocess | |
import codecs | |
import json | |
# simple mail sending function | |
def send_message(recipient, subject, mailbodyfile): | |
try: | |
command = "cat %s | mail -s '%s' '%s'" % (mailbodyfile, subject, recipient) | |
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) | |
except Exception, error: | |
print error | |
def main(): | |
# read config | |
json_data=json.load(open('./ics-notifier.conf.json')) | |
url = json_data["url"] | |
mailfile = json_data["mailfile"] | |
maildest = json_data["mails"] | |
tzone = json_data["tzone"] | |
subject = json_data["subject"] | |
# init calendar | |
c = Calendar(urlopen(url).read().decode('utf-8')) | |
# limits | |
today = arrow.now(tzone) | |
tomorrow = today.replace(days=+1) | |
# process events | |
for event in c.events: | |
if event.begin > arrow.now(tzone).replace(minutes=-50): | |
event_delay = None | |
# get events for each our delay | |
if event.begin < arrow.now(tzone).replace(hours=+3): | |
event_delay = "dans moins de 3 heures" | |
if event.begin < arrow.now(tzone).replace(hours=+1): | |
event_delay = "dans moins de 60 minutes" | |
if event.begin < arrow.now(tzone): | |
event_delay = "maintenant !" | |
if event_delay: | |
subject = u"%s %s" % (subject, event_delay) | |
# init file | |
mailbody = codecs.open(mailfile, "w", "utf-8") | |
# fill mail | |
mailbody.write(subject) | |
mailbody.write(u" %s\n" % event.name) | |
mailbody.write(u"\nà %s\n" % event.begin.to('local')) | |
mailbody.write(u"\n--------------\n") | |
mailbody.write(event.description + u"\n") | |
mailbody.close() | |
# send alerts | |
for user in maildest: | |
send_message(user, subject, mailfile) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment