forked from simeonf/pybay
-
Notifications
You must be signed in to change notification settings - Fork 30
/
export.py
149 lines (129 loc) · 4.53 KB
/
export.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
import argparse
import functools
import inspect
import json
import logging
import requests
import sys
from hubb_client import HubbClient
def config_logging():
root = logging.getLogger()
root.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stderr)
handler.setLevel(logging.DEBUG)
root.addHandler(handler)
def translate(a, b, fields):
for new, old in fields.items():
if old:
a[new] = b[old]
return a
def reducer(x, y):
if not x:
return [y]
o = x[-1]
if o["SessionId"] == y["SessionId"]:
o[y["PropertyMetadataId"]] = y["Value"]
else:
x.append({"SessionId": y["SessionId"], y["PropertyMetadataId"]: y["Value"]})
return x
# "TrackId": 16161,
# "TimeSlotId": 92748,
# "RoomId": 45414,
EVENT_ID = 2717
def export_all_talks(hc):
session_fields = {
"additional_notes": "Id",
"title": "Title",
"description": "Description",
"start_time": "StartTime",
"end_time": "EndTime",
"room": "room",
"slot": "SlotDescription",
}
property_fields = {
47953: "talk_length",
47941: "type",
47942: "theme",
47943: "audience_level",
47944: "abstract",
47945: "what_attendees_will_learn",
}
speaker_fields = {
"speaker_email": "EmailAddress",
"first_name": "FirstName",
"last_name": "LastName",
"speaker_biography": "Biography",
"photo": "PhotoLink",
"twitter_username": "Twitter",
"website": "Website",
"linkedin": "LinkedIn",
"facebook": "Facebook",
"blog": "Blog",
}
# First get the users which is a list of objects [{"Id", ...},]
users = hc.users(event_id=EVENT_ID)
# And convert to dict of Ids {Id: {}, }
users = {user["Id"]: user for user in users}
# Next get "property values" [{"PropertyMetadataId", "SessionId", "Value" ...},]
properties = hc.propertyvalues(event_id=EVENT_ID)
# Filter down to the properties we're interested in
properties = [
p
for p in properties
if p["SessionId"] and p["PropertyMetadataId"] in property_fields
]
# Convert to dict of sessionids: {pid: value, ...}
properties = sorted(properties, key=lambda p: p["SessionId"])
properties = functools.reduce(reducer, properties, None)
properties = {p["SessionId"]: p for p in properties}
# Get the rooms and convert to {Id: Name} map
rooms = hc.rooms(event_id=EVENT_ID)
rooms = {int(r["Id"]): r["Name"] for r in rooms}
# Get the timeslots and convert to {Id: {Start: t, End: t} map
slots = hc.timeslots(event_id=EVENT_ID)
slots = {s["Id"]: s for s in slots}
# Get the time slots
export_data = []
data = hc.sessions(event_id=EVENT_ID)
for record in data:
new_record = {}
try:
record["room"] = rooms[record["RoomId"]]
except KeyError:
logging.error("No room found for {}".format(record))
continue
record["StartTime"] = slots[record["TimeSlotId"]]["StartTime"]["EventTime"]
record["EndTime"] = slots[record["TimeSlotId"]]["EndTime"]["EventTime"]
record["SlotDescription"] = slots[record["TimeSlotId"]]["Label"]
translate(new_record, record, session_fields)
new_record["additional_notes"] = str(new_record["additional_notes"])
new_record["speakers"] = []
for key in record["SpeakerOrder"]:
user = users[int(key)]
new_record["speakers"].append(translate({}, user, speaker_fields))
props = properties.get(record["Id"], {})
for num, val in props.items():
if num in property_fields:
new_record[property_fields[num]] = val
export_data.append(new_record)
return json.dumps(export_data)
if __name__ == "__main__":
config_logging()
# Setup CLI options
parser = argparse.ArgumentParser()
# Gotta have a secrets file to read API creds
txt = "Required. Json configuration file containing client_id, client_secret, and scope"
parser.add_argument("--config", help=txt, type=argparse.FileType("r"))
parser.add_argument(
"--cache", help="Cache calls to API", default=False, action='store_true'
)
args = parser.parse_args()
if not args.config:
parser.exit("Must supply secrets config file")
if args.cache:
import requests_cache
requests_cache.install_cache()
secrets = json.load(args.config)
# Make a client
hc = HubbClient(**secrets)
print(export_all_talks(hc))