-
Notifications
You must be signed in to change notification settings - Fork 1
/
sexport.py
executable file
·307 lines (245 loc) · 8.67 KB
/
sexport.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python
# miniLAC16 schedule exporter
# ===========================
# Copyright (C) 2016 riot <riot@c-base.org>
#
# GPLv3
from datetime import timedelta, datetime
from pprint import pprint
import argparse
import requests
import json
import sys
import pytz
import calendar
import uuid
__author__ = 'riot'
DEBUG = False
OFFLINE = False
baseurl = "http://minilac.linuxaudio.org/api.php?action=parse&page=%s&prop" \
"=wikitext&format=json"
conference = {
'conference': {
'acronym': 'miniLAC16',
'title': 'Mini Linux Audio Conference 2016',
'basedate': '2016-04-09',
'timezone': 'Europe/Berlin',
'license': 'CC-BY-SA-4.0',
'language': 'en'
},
'events': {}
}
rooms = [
'Mainhall',
'Weltenbaulab',
'Seminar room',
'Upper-deck',
'Soundlab'
]
webdatacache = {}
def strip_tags(markup):
markup = markup.lstrip().rstrip()
markup = markup.replace("'''", "")
if "[[" not in markup:
return markup
wobble = markup
ham = ""
while '[[' in wobble:
wibble, wobble = wobble.split("[[", maxsplit=1)
ham += wibble
spam, wobble = wobble.split("]]", maxsplit=1)
if spam.startswith('User'):
if DEBUG:
print("Found a user: ", spam)
ham += str(spam.split("|")[1])
if spam in rooms:
if DEBUG:
print("Room encountered")
ham += spam
elif spam.startswith('Image'):
if DEBUG:
print("Found an image: ", spam)
ham += wobble
return ham
def get_events(eventtype="Lecture"):
if eventtype not in webdatacache:
if DEBUG:
print("Object not in cache!")
if not OFFLINE:
webdata = requests.get(baseurl % eventtype)
else:
print("Cannot get object! Offline!")
sys.exit(-1)
if DEBUG:
pprint(webdata.json(), indent=1)
wikidata = "".join(list(webdata.json()['parse']['wikitext']['*']))
webdatacache[eventtype] = wikidata
else:
wikidata = webdatacache[eventtype]
rawevents = []
events = []
for no, part in enumerate(wikidata.split("{{Template:" + eventtype)):
if not no == 0:
if DEBUG:
print("#" * 5)
part = part.split("}}")[0]
if DEBUG:
print(part)
rawevents.append(part)
for rawevent in rawevents:
rawevent = rawevent.split("\n")
if DEBUG:
print(rawevent)
event = {
'title': '',
'type': eventtype,
'id': 1,
'room': '',
# only valid when basedate is given, use full datetime in
# 'start' otherwise
'day': 0,
# can be a whole ISO datetime, otherwise, 'basedate' is used as
# a base
'start': '',
'duration': '01:00', # alternative: 'end'
'people': '', # can be an array. alias: 'persons'
'type': eventtype, # optional. default: lecture
'license': 'CC-BY-SA', # optional
'description': '', # optional
}
for no, line in enumerate(rawevent):
if DEBUG:
print(line)
split = line[1:].split(
"=") # Cut off pipe, then split into k,v pair
if len(split) > 2:
if DEBUG:
print(
"Oh, my, a raw event line with more than key and "
"value!")
if DEBUG:
pprint(split)
if split[0] == 'description':
description = " ".join(rawevent[no:]).split("|description=")[1]
description = description.lstrip().rstrip()
description = strip_tags(description)
event['description'] = description
break
if split[0] in event.keys():
if split[0] in ('id', 'day'):
try:
if split[0] == 'id':
split[1] = int(split[1]) + 1
else:
split[1] = int(split[1])
except TypeError:
if DEBUG:
print("Malformed ID or day in event! "
"Appending string for inspection.")
event[split[0]] = split[1]
else:
event[split[0]] = strip_tags(split[1])
if DEBUG:
pprint(event)
events.append(event)
# pprint(rawevents)
return events
def get_infobeamer_events():
lectures = get_events('Lecture')
workshops = get_events('Workshop')
hacksessions = get_events('Hacking')
all_events = lectures + workshops + hacksessions
infobeamer_events = []
for event in all_events:
hours = event['start'].split(":")
duration = event['duration'].split(":")
# TODO: Fetch static parts from conference metadata above
start = datetime(2016, 4, 9 + int(event['day']), hour=int(hours[0]),
minute=int(hours[1]), tzinfo=pytz.utc) #pytz.timezone(
#"Europe/Berlin"))
#print(start)
#print(hours)
#print(start)
unixtime = int(calendar.timegm(start.timetuple()))
# TODO: Mean hack, we should do that better:
unixtime -= (120*60)
#print("This is the timestamp:", unixtime)
duration = (int(duration[0]) * 60 + int(duration[1]))
stop = unixtime + int(duration) * 60
if DEBUG:
print(event['room'], start, stop, event['title'])
new = {
'title': event['title'],
'event_id': event['id'],
'place': event['room'],
'unix_end': stop,
'unix': unixtime,
'duration': duration,
'speakers': event['people'].split(", "),
'lines': event['description'][:200].split("."),
'lang': 'en',
'start': event['start']
}
if len(new['lines']) == 100:
new['lines'] += " - for more information, check the wiki."
infobeamer_events.append(new)
sortedlist = sorted(infobeamer_events, key=lambda k: k['unix'])
return sortedlist
def get_voc_events():
lectures = get_events('Lecture')
workshops = get_events('Workshop')
hacksessions = get_events('Hacking')
all_events = lectures + workshops + hacksessions
for event in all_events:
event['day'] += 1
event['guid'] = str(uuid.uuid4())
return all_events
def generate_schedule(scheduletype):
if DEBUG:
print("Generating for %s" % scheduletype)
if scheduletype == 'voc':
events = get_voc_events()
conference['events'] = events
return conference
elif scheduletype == 'infobeamer':
events = get_infobeamer_events()
return events
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--outputfile",
help="Specify output filename",
default="schedule.json")
parser.add_argument("--cachefile",
help="Read from given cache file",
default="schedule.cache.json")
parser.add_argument("--debug",
help="Printout debug info to STDOUT(!)",
action="store_true")
parser.add_argument("--writecache",
help="Store retrieved data to a file",
action="store_true")
parser.add_argument("--readcache",
help="Read cache from file",
action="store_true")
parser.add_argument("--offline",
help="Read cache and stay offline, do not write cache",
action="store_true")
parser.add_argument("--scheduletype",
default='voc',
help="Either 'voc' or 'infobeamer' - infobeamer "
"generates minilac-room-next-node compatible "
"json, voc generates for c3voc's tracker.")
args = parser.parse_args()
if args.debug:
DEBUG = True
if args.offline:
OFFLINE = True
if args.readcache or args.offline:
with open(args.cachefile, "r") as f:
webdatacache = json.load(f)
with open(args.outputfile, "w") as f:
json.dump(generate_schedule(args.scheduletype), f, indent=4,
sort_keys=True)
if args.writecache and not args.offline:
with open(args.cachefile, "w") as f:
json.dump(webdatacache, f)