-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevents2ics
executable file
·86 lines (68 loc) · 2.82 KB
/
events2ics
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
#!/usr/bin/env python
import argparse
import datetime
import re
EVENT_RE = '(\d+)/(\d+) (\d+)(:\d+)? (am|pm) to (\d+)(:\d+)? (am|pm) (.+) @ (.+)'
TIME_RE = '(\d+):(\d+) (am|pm)'
DATE_RE = '(\d+)/(\d+)'
'''
Convert a listing of events to an .ics calendar file.
Events filename format is:
7/4 6 pm to 9:30 pm BBQ @ Home
'''
def convert_date(date, time):
time_re = re.compile(TIME_RE)
date_re = re.compile(DATE_RE)
time_match = time_re.match(time)
date_match = date_re.match(date)
time_groups = time_match.groups()
date_groups = date_match.groups()
hours = int(time_groups[0])
minutes = int(time_groups[1])
if time_groups[2].lower() == 'pm':
hours += 12
year = datetime.datetime.now().year
month = int(date_groups[0])
day = int(date_groups[1])
return '%04d%02d%02dT%02d%02d00' % (year, month, day, hours, minutes)
def print_calendar_header():
print('BEGIN:VCALENDAR')
print('PRODID:-//michaelgalloy//michaelgalloy.com 1.0//EN')
print('VERSION:2.0')
def print_calendar_footer():
print('END:VCALENDAR')
def print_event(date, start_time, end_time, timezone, title, location):
'''An event looks like:
BEGIN:VEVENT
DTSTART:20170603T160000
DTEND:20170603T210000
SUMMARY:Backyard BBQ
CATEGORIES:
DESCRIPTION:For details, click here: http://www.evite.com/event/0111CUTIWD324UWNAEPHITJHYROW7A?gid=020E64BWSS4O6I2RQEPHI5E4S7EVY4
LOCATION:Katie & Dave's Backyard 609 Marine St Boulder CO 80302
END:VEVENT'''
print('BEGIN:VEVENT')
print('DTSTART;TZID=%s:%s' % (timezone, convert_date(date, start_time)))
print('DTEND;TZID=%s:%s' % (timezone, convert_date(date, end_time)))
print('SUMMARY:%s' % title)
print('LOCATION:%s' % location)
print('END:VEVENT')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='convert event listing to ics file')
parser.add_argument('events', help='events filename')
parser.add_argument('-t', '--timezone', help='timezone, i.e., America/Denver', default='America/Denver')
args = parser.parse_args()
event_re = re.compile(EVENT_RE)
print_calendar_header()
with open(args.events, 'r') as events_file:
for event_line in events_file:
event_match = event_re.match(event_line)
if event_match:
groups = event_match.groups()
date = '%s/%s' % (groups[0], groups[1])
start_time = '%s%s %s' % (groups[2], groups[3] if groups[3] is not None else ':00', groups[4])
end_time = '%s%s %s' % (groups[5], groups[6] if groups[6] is not None else ':00', groups[7])
title = groups[8]
location = groups[9]
print_event(date, start_time, end_time, args.timezone, title, location)
print_calendar_footer()