-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetcontribsJson.py
More file actions
203 lines (164 loc) · 6.38 KB
/
getcontribsJson.py
File metadata and controls
203 lines (164 loc) · 6.38 KB
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
# Script to obtain JSON of contributions for the typesetters
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
def jsonGet(jsonFile):
try:
with open (jsonFile,"r") as read_file:
jsonData = json.load(read_file)
return jsonData
except:
signedUrl = signedContribsUrl()
print ("requesting JSON")
response = requests.get(signedUrl)
rawdata = json.loads(response.text)
print ("tidy up JSON")
contribsdata = rawdata.get('results')[0].get('contributions')
outputJsonFile(contribsdata, filename=jsonFile, timestamp=False)
return contribsdata
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 signedContribsUrl():
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': 'contributions'
}
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 = "clean_contrib-"
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 sortByBoardNum(posters):
# for poster in posters:
# try:
# # check posters have board_numbers
# except:
# # complain if there are posters without board numbers
# print (poster.get('board_number'))
posters.sort(key=lambda k: int(k['board_number']))
return posters
def sortByStartTime(talks):
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):
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,
}
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
if __name__ == '__main__':
data = jsonGet("contrib-all.json")
posterList = []
paralleltalkList = []
plenaryList = []
prizeList = []
introList = []
#prizeList, plenaryList, paralleltalkList, posterList
#this division drops any "contributions" that are outside these categories
for contrib in data :
contribType = contrib.get('type')
if contribType == 'Poster':
posterList.append(contrib)
if contribType == 'Talk' or contribType == 'Invited talk':
paralleltalkList.append(contrib)
if contribType == 'Plenary talk':
plenaryList.append(contrib)
if contribType == 'Prize lecture':
prizeList.append(contrib)
if contribType == 'Introduction':
introList.append(contrib)
contribsDict = {
"prize-PR": sortByStartTime(prizeList),
"plenary-PL": sortByStartTime(plenaryList),
"parallelsessions-PS": sortByRoomTime(paralleltalkList),
"posters": sortByBoardNum(posterList)
}
parser = argparse.ArgumentParser(description = "Gets contributions 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()
for key in contribsDict :
outputJsonFile(contribsDict[key],fileprefix="abstract_", filesuffix=key, timestamp=args.timestamp)
timetable = sortByRoomTime(prizeList + plenaryList + paralleltalkList + introList)
#outputJsonFile(timetable, fileprefix="timetable_", timestamp=args.timestamp)
TTtalkList = []
for talk in timetable:
TTtalkList.append(TTtextoutput(talk))
outputJsonFile(TTtalkList, fileprefix="timetable_titles_", timestamp=args.timestamp)
print ("done")