Created
July 11, 2009 02:19
-
-
Save safetydank/145033 to your computer and use it in GitHub Desktop.
Send out VHS upcoming event emails
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 | |
import urllib, datetime, re, StringIO, smtplib | |
def add_tag(event, tag, data): | |
if tag in ('DTSTART', 'DTEND', 'CREATED', 'LAST-MODIFIED', 'DTSTAMP'): | |
# parse a date string as a datetime object | |
(date, sep, time) = data.partition('T') | |
year, month, day = (int(s) for s in (date[0:4], date[4:6], date[6:])) | |
hour, min, sec = (int(s) for s in (time[0:2], time[2:4], time[4:])) | |
event[tag] = datetime.datetime(year, month, day, hour, min, sec) | |
else: | |
event[tag] = data | |
def parse_events(url): | |
""" | |
Parse an ical URL for upcoming events | |
url -- URL of the calendar to parse | |
Returns a list of event dicts | |
""" | |
linenum = 0 | |
events = list() | |
fp = urllib.urlopen(url) | |
while True: | |
line = fp.readline().rstrip() | |
linenum += 1 | |
if not line: | |
break # end of feed | |
elif line != 'BEGIN:VEVENT': | |
continue | |
event = dict() | |
last_tag = None | |
# parse VEVENT | |
while True: | |
line = fp.readline().rstrip() | |
linenum += 1 | |
if line == 'END:VEVENT': | |
events.append(event) | |
event = dict() | |
break | |
elif line.startswith(' '): | |
if not last_tag: | |
raise Exception('Malformed .ics file, line %d' % linenum) | |
event[last_tag] += line[1:] | |
else: | |
(tag, sep, data) = line.partition(':') | |
# new event | |
last_tag = tag | |
add_tag(event, tag, data) | |
# post-processing | |
for event in events: | |
description = event.get('DESCRIPTION') | |
if description: | |
event['DESCRIPTION'] = description.decode('string_escape') | |
return events | |
def _event_dump(event): | |
return '\n'.join(['%s : %s' % (k, v) for k,v in event.iteritems()]) | |
def _date_format(date): | |
return date.strftime('%A %d %B %Y') | |
def email_events(events, sender, recipient, mailhost='localhost'): | |
""" | |
Send out a formatted email of events using an authorized SMTP host | |
""" | |
sio = StringIO.StringIO() | |
for event in events: | |
sio.write('%s\n%s\n\n' % (event['SUMMARY'], _date_format(event['DTSTART']))) | |
sio.write('%s\n\n\n' % event['DESCRIPTION']) | |
now = datetime.datetime.now() | |
email_text = """\ | |
From: %s | |
To: %s | |
Subject: Upcoming VHS events (%s) | |
A weekly reminder of upcoming Vancouver Hack Space events. | |
%s | |
""" % (sender, | |
recipient, | |
'%d/%d/%d' % (now.day, now.month, now.year), | |
sio.getvalue()) | |
server = smtplib.SMTP(mailhost) | |
server.sendmail(sender, recipient, email_text) | |
server.quit() | |
if __name__ == '__main__': | |
events = parse_events('http://vancal.org/events.ics') | |
# Find all events in the upcoming week | |
upcoming = lambda event: 0 <= (event['DTSTART'] - datetime.datetime.now()).days <= 7 | |
upcoming_vhs = lambda event: upcoming(event) and event['SUMMARY'].startswith('VHS:') | |
upcoming_vhs_events = [ e for e in events if upcoming_vhs(e) ] | |
email_events(upcoming_vhs_events, | |
sender='Dan V. <dan@expandingbrain.com>', | |
recipient='VHS <vanhackspace@lists.uselessdegree.net>') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment