Sauter la navigation.
Accueil
Groupe d'Utilisateurs de GNU/Linux et de Logiciels Libres Francilien

Lire un flux ical et l'envoyer par mail

Pour ceux qui souhaitent tester, voici un script python récupérant un flux ical et envoyant un mail pour le prochain événement.

Vous aurez besoin de http://pypi.python.org/pypi/icalendar


#!/usr/bin/env python
#
# Vincent-Xavier Jumel (c) 2011
# Released under the terms of the WTFPL 2
#

# -*- coding: utf-8 -*-

from curl import urllib
from icalendar import Calendar, Event
from datetime import datetime,timedelta
from pytz import timezone

import smtplib
from email.mime.text import MIMEText

from netrc import netrc

today=datetime.now()
day=timedelta(days=1)
yesterday=today-day
tomorrow=today+day

cal=Calendar.from_string(urllib.urlopen("http://www.parinux.org/calendar/ical/"+today.strftime("%Y-%m")).read())

i=0
prochain_evenement=cal.walk('vevent')[i]
date_prochain_evenement=prochain_evenement.decoded('dtstart')
while date_prochain_evenement.replace(tzinfo=None).date() != tomorrow.date():
i=i+1
if i == len(cal.walk('vevent')):
exit(0)

prochain_evenement=cal.walk('vevent')[i]
date_prochain_evenement=prochain_evenement.decoded('dtstart')

date_locale=prochain_evenement.decoded('dtstart').astimezone(timezone('Europe/Paris'))

#print(prochain_evenement.decoded('summary').encode('utf-8'))
#print(date_locale)
#print("\n")
#print(prochain_evenement.decoded('description').encode('utf-8'))

msg = MIMEText(prochain_evenement.decoded('description').encode('utf-8'),'plain','UTF-8')

msg['Subject'] = prochain_evenement.decoded('summary').encode('utf-8')
msg['From'] = "robot@parinux.org"
msg['To'] = "yourmail@example.com"

s = smtplib.SMTP()
s.connect(host='mail.example.com')
s.starttls()
login = netrc().authenticators("mail.example.com")[0]
password = netrc().authenticators("mail.example.com")[2]
s.login(login,password)
s.sendmail("robot@parinux.org", "yourmail@example.com", msg.as_string())
s.quit()

exit(0)