-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_ical.py
197 lines (120 loc) · 4.73 KB
/
export_ical.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# coding=utf-8
"""Online Scout Manager Interface.
Usage:
export_ical.py [-d] <apiid> <token> <outdir> <section>...
export_ical.py (-h | --help)
export_ical.py --version
Options:
<section> Section to export.
<outdir> Output directory for ical files.
-d,--debug Turn on debug output.
-h,--help Show this screen.
--version Show version.
"""
import os.path
import logging
from docopt import docopt
import datetime
import osm
from icalendar import Calendar, Event, Timezone,\
TimezoneStandard, TimezoneDaylight
import pytz
tz = pytz.timezone('Europe/London')
NOW = datetime.datetime.now()
from pprint import pprint
from group import Group
log = logging.getLogger(__name__)
DEF_CACHE = "osm.cache"
DEF_CREDS = "osm.creds"
def orn(i):
if i is None or i == 'None':
return ""
return i
def meeting2ical(section, event, i):
e = Event()
e['summary'] = "{}: {}".format(section, orn(event['title']))
e['description'] = orn(event['notesforparents'])
if event.start_time.time() != datetime.time(0, 0, 0):
e.add('dtstart', event.start_time.astimezone(tz))
else:
e.add('dtstart', event.start_time.date())
if event.end_time.time() != datetime.time(0, 0, 0):
e.add('dtend', event.end_time.astimezone(tz))
else:
e.add('dtend', event.end_time.date())
e.add('dtstamp', NOW)
i.add_component(e)
def event2ical(section, event, i):
e = Event()
e['summary'] = "{}: {}".format(section, orn(event['name']))
e['location'] = orn(event['location'])
if event.start_time.time() != datetime.time(0, 0, 0):
e.add('dtstart', event.start_time.astimezone(tz))
else:
e.add('dtstart', event.start_time.date())
if event.end_time.time() != datetime.time(0, 0, 0):
e.add('dtend', event.end_time.astimezone(tz))
else:
e.add('dtend', event.end_time.date())
e.add('dtstamp', NOW)
i.add_component(e)
def _main(osm, auth, sections, outdir):
assert os.path.exists(outdir) and os.path.isdir(outdir)
for section in sections:
assert section in list(Group.SECTIONIDS.keys()) + ['Group'], \
"section must be in {!r}.".format(Group.SECTIONIDS.keys())
osm_sections = osm.OSM(auth, Group.SECTIONIDS.values())
tzc = Timezone()
tzc.add('tzid', 'Europe/London')
tzc.add('x-lic-location', 'Europe/London')
tzs = TimezoneStandard()
tzs.add('tzname', 'GMT')
tzs.add('dtstart', datetime.datetime(1970, 10, 25, 2, 0, 0))
tzs.add('rrule', {'freq': 'yearly', 'bymonth': 10, 'byday': '-1su', 'interval': '1'})
tzd = TimezoneDaylight()
tzd.add('tzname', 'BST')
tzd.add('dtstart', datetime.datetime(1970, 3, 29, 1, 0, 0))
tzd.add('rrule', {'freq': 'yearly', 'bymonth': 3, 'byday': '-1su', 'interval': '1'})
tzd.add('TZOFFSETFROM', datetime.timedelta(hours=0))
tzd.add('TZOFFSETTO', datetime.timedelta(hours=1))
for section in sections:
i = Calendar()
tzc.add_component(tzs)
tzc.add_component(tzd)
i.add_component(tzc)
if section == "Group":
i['x-wr-calname'] = '7th Lichfield: Group Calendar'
i['X-WR-CALDESC'] = 'Current Programme'
i['calscale'] = 'GREGORIAN'
i['X-WR-TIMEZONE'] = 'Europe/London'
for s in Group.SECTIONIDS.keys():
section_obj = osm_sections.sections[Group.SECTIONIDS[s]]
[meeting2ical(s, event, i) for
event in section_obj.programme.events_by_date()]
[event2ical(s, event, i) for
event in section_obj.events]
open(os.path.join(outdir, section + ".ical"),
'w').write(i.to_ical().decode())
else:
section_obj = osm_sections.sections[Group.SECTIONIDS[section]]
i['x-wr-calname'] = '7th Lichfield: {}'.format(section)
i['X-WR-CALDESC'] = '{} Programme'.format(section_obj.term['name'])
i['calscale'] = 'GREGORIAN'
i['X-WR-TIMEZONE'] = 'Europe/London'
[meeting2ical(section, event, i) for
event in section_obj.programme.events_by_date()]
[event2ical(section, event, i) for
event in section_obj.events]
open(os.path.join(outdir, section + ".ical"),
'w').write(i.to_ical().decode())
if __name__ == '__main__':
args = docopt(__doc__, version='OSM 2.0')
if args['--debug']:
level = logging.DEBUG
else:
level = logging.INFO
logging.basicConfig(level=level)
log.debug("Debug On\n")
auth = osm.Authorisor(args['<apiid>'], args['<token>'])
auth.load_from_file(open(DEF_CREDS, 'r'))
_main(osm, auth, args['<section>'], args['<outdir>'])