/
CalendarGenerator.py
52 lines (43 loc) · 1.41 KB
/
CalendarGenerator.py
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
from datetime import timedelta
import logging
from icalendar import Calendar, Event, Alarm
import ScheduleProcessor
def eventFromMatch(match, team, reminder=None):
if team == None:
color = match.white + ' (white)'
opponent = match.dark + ' (dark)'
elif match.white == team:
color = 'White'
opponent = match.dark
elif match.dark == team:
color = 'Dark'
opponent = match.white
else:
return None
evt = Event()
evt.add('summary', 'Soccer - {0} vs. {1}'.format(color, opponent))
evt.add('dtstart', match.time)
evt.add('dtend', match.time + timedelta(hours = 1))
if reminder:
evt.add_component(createReminder(reminder))
return evt
def calendarFromMatches(matches, team, reminder=None):
cal = Calendar()
cal.add('version', '2.0')
cal.add('prodid', '-//com/appspot/SambaSchedule//NONSGML v1.0//EN')
for match in matches:
evt = eventFromMatch(match, team, reminder)
if evt:
cal.add_component(evt)
return cal
def createReminder(minutes):
a = Alarm()
a.add('ACTION', 'DISPLAY')
a.add('DESCRIPTION', 'starting soon...')
a.add('TRIGGER', timedelta(minutes = -minutes))
return a
if __name__ == '__main__':
html = open('sample.html').read()
matches = ScheduleProcessor.processPage(html)
cal = calendarFromMatches(matches, 'The Captain')
a = createReminder(35)