-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetsessionsJson.py
233 lines (184 loc) · 7.31 KB
/
getsessionsJson.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
# Script to obtain JSON of sessions for the typesetters to put into the program pages
import json
import hashlib
import hmac
import urllib
import time
import requests
from ConfigParser import SafeConfigParser as ConfigParser
from datetime import datetime as dt
import argparse
from collections import OrderedDict
def jsonGet(jsonFile):
try:
with open (jsonFile,"r") as read_file:
jsonData = json.load(read_file)
return jsonData
except:
signedUrl = signedSessionsUrl()
print signedUrl
print ("requesting JSON")
response = requests.get(signedUrl)
rawdata = json.loads(response.text)
print ("tidy up JSON")
sessionsdata = rawdata.get('results')[0].get('sessions')
outputJsonFile(sessionsdata, filename=jsonFile, timestamp=False)
return sessionsdata
def build_indico_request(path, params, api_key=None, secret_key=None, only_public=False, persistent=False):
items = params.items() if hasattr(params, 'items') else list(params)
if api_key:
items.append(('apikey', api_key))
if only_public:
items.append(('onlypublic', 'yes'))
if secret_key:
if not persistent:
items.append(('timestamp', str(int(time.time()))))
items = sorted(items, key=lambda x: x[0].lower())
url = '%s?%s' % (path, urllib.urlencode(items))
signature = hmac.new(secret_key, url, hashlib.sha1).hexdigest()
items.append(('signature', signature))
if not items:
return path
return '%s?%s' % (path, urllib.urlencode(items))
def signedSessionsUrl():
config = ConfigParser()
config.read('config.ini')
API_KEY = config.get('default','API_KEY')
SECRET_KEY = config.get('default','SECRET_KEY')
PATH = config.get('default','PATH')
PREFIX = config.get('default','PREFIX')
PARAMS = {
'pretty': 'yes',
'detail': 'sessions'
}
return PREFIX+build_indico_request(PATH, PARAMS, API_KEY, SECRET_KEY)
def outputJsonFile(data,fileprefix=None, filesuffix="all", timestamp=True, filename=None):
if not filename:
if not fileprefix:
fileprefix = "sessions-"
filename = fileprefix + filesuffix
if timestamp:
now = dt.now()
timestamp = now.strftime("%Y%m%d-%H%M")
filename = filename + "_" + timestamp
filename += ".json"
print ("writing "+filename)
with open (filename,"w") as write_file:
json.dump(data, write_file)
def sortByStartTime(talks): # works also for sessions
for talk in talks:
try:
starttimeDict = talk.get("startDate")
starttimeStr = starttimeDict["date"] + " " + starttimeDict["time"]
except:
print ("Warning: unscheduled talk in exported JSON list.")
return
#print (starttimeStr)
talks.sort(key=lambda t: dt.strptime(t['startDate']['date']+' '+t['startDate']['time'],"%Y-%m-%d %H:%M:%S"))
return talks
# def checkRoomStartTime(paralleltalks):
# for talk in paralleltalks:
# try:
# room = talk.get("room")
# starttimeDict = talk.get("startDate")
# starttimeStr = starttimeDict["date"] + " " + starttimeDict["time"]
# except:
# print ("Warning: something wrong.")
# return
# print (room, starttimeStr)
def sortByRoomTime(paralleltalks): # works also for sessions
for talk in paralleltalks:
try:
room = talk.get("room")
except:
print ("Warning: talk without assigned room in exported JSON list.")
return
#print (room)
sortRoomOrder = {
"Max Kade Auditorium": 0,
"Lecture Hall A": 1,
"Lecture Hall B": 2,
"Lecture Hall C": 3,
"Lecture Hall D": 4,
"": 5
}
paralleltalks.sort(key=lambda t: sortRoomOrder[str(t['room'])])
paralleltalks = sortByStartTime(paralleltalks)
return paralleltalks
# def TTtextoutput(x):
# textList = []
# textList.append(x["startDate"]["date"]+" "+x["startDate"]["time"]+" - "+x["endDate"]["time"])
# textList.append(x["room"])
# textList.append(x["session"])
# try:
# textList.append(x["speakers"][0]["first_name"]+" "+x["speakers"][0]["last_name"])
# except:
# textList.append("no speaker")
# pass
# textList.append(x["title"])
# return textList
def parseDateTime(item, timepoint):
# item - structured JSON data containing startDate, endDate
# timepoint - str, for Indico JSON export: starDate or endDate
dtDict = item.get(timepoint)
dtStr = dtDict["date"] + " " + dtDict["time"]
return dtStr[:-3] # strip seconds
def parseTime(item, timepoint):
# item - structured JSON data containing startDate, endDate
# timepoint - str, for Indico JSON export: starDate or endDate
dtDict = item.get(timepoint)
dtStr = dtDict["time"]
return dtStr[:-3] # strip seconds
def processTitle(title):
sep = ":"
titleNoSessionNum = title.rsplit(sep,1)[0]
return str(titleNoSessionNum)
def prettyStartEnd(data, start, end):
return parseDateTime(data, start) + "-" + parseTime(data, end)
def getInitials(name):
initials = ''.join([x[0].upper()+'.' for x in name.split(' ')])
initials = initials+' '
return initials
def parseSession(data):
parsedData = OrderedDict({})
#print processTitle(data["title"])
parsedData["session-title"] = processTitle(data["title"])
parsedData["session-time"] = prettyStartEnd(data, "startDate", "endDate")
parsedData["room"] = data["room"]
parsedContribs = []
contribsUnsorted = data["contributions"]
contribs = sortByStartTime(contribsUnsorted)
for talk in contribs:
parsedTalk = OrderedDict({})
if data["session"]["type"] != u"Plenary lecture":
try:
speaker = talk["speakers"][0]
parsedTalk["speaker"] = getInitials(speaker["first_name"])+speaker["last_name"]
except:
# posters
pass
else:
try:
speaker = talk["speakers"][0]
parsedTalk["speaker"] = speaker["first_name"]+' '+speaker["last_name"]
except:
# prizes to be announced
pass
parsedTalk["title"] = talk["title"]
parsedTalk["time"] = prettyStartEnd(talk, "startDate", "endDate")
parsedContribs.append(parsedTalk)
parsedData["talks"] = parsedContribs
return parsedData
if __name__ == '__main__':
data = jsonGet("session-all.json")
sorteddata = sortByRoomTime(data) # sessions are sorted, but talks within a session aren't
simplifiedSessions = []
for session in sorteddata:
simpleSession = parseSession(session)
simplifiedSessions.append(simpleSession)
parser = argparse.ArgumentParser(description = "Gets sessions from one event using HTTP API, then sort into JSON files for typesetting. See github: selkieupsilon/euroismar-indicoscripts")
parser.add_argument('--timestamp', action='store_true', help='add timestamp to output files')
args = parser.parse_args()
#outputJsonFile(sorteddata, fileprefix="sessionSorted_", timestamp=args.timestamp)
outputJsonFile(simplifiedSessions, fileprefix="sessionSimple_", timestamp=args.timestamp)
print ("done")