-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertSessionData.js
215 lines (181 loc) · 5.61 KB
/
convertSessionData.js
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
const path = require('path');
const fs = require('fs');
const slugify = require('slugify');
let projectRoot = path.normalize(__dirname);
let archivePath = projectRoot + '/archive_data'
let dataPath = projectRoot + '/src/_data'
let year = '2012'
convertSessions();
convertLevels();
convertSpaces();
convertSpeakers();
convertTimePeriods();
convertTracks();
function convertSessions()
{
let sessionFiles = fs.readdirSync(archivePath + '/sessions');
let rawSessions = sessionFiles.map(function (filename) {
let rawData = fs.readFileSync(archivePath + '/sessions/' + filename)
let data = JSON.parse(rawData)
return parseSessionData(data);
});
writeDataFile('sessions.json', rawSessions);
}
function parseSessionData(data)
{
let track, space, level, resources = [];
if (data._links.track) {
track = data._links.track.href.replace(`/${year}/tracks/`, '');
}
if (data._links.space) {
space = data._links.space.href.replace(`/${year}/spaces/`, '');
} else {
space = 'main-hall';
}
if (data._links.level) {
level = data._links.level.href.replace(`/${year}/levels/`, '');
}
if (data._links.resource) {
resources = data._links.resource;
}
let timePeriod = data._links.timePeriod.href.replace(`/${year}/time-periods/`, '');
let session = {
slug: data.slug,
title: data.title,
description: data.description,
track: track,
space: space,
timePeriod: parseInt(timePeriod),
level: level,
speakers: [],
resources: data._links.resource,
}
if (data._links.speaker) {
data._links.speaker.forEach(function (item) {
let speaker = item.href.replace(`/${year}/speakers/`, '');
session.speakers.push(speaker);
});
}
return session;
}
function convertLevels()
{
let files = fs.readdirSync(archivePath + '/levels');
let itemArray = files.map(function (filename) {
let rawData = fs.readFileSync(archivePath + '/levels/' + filename)
let data = JSON.parse(rawData)
return {
slug: data.slug,
title: data.title,
description: data.description,
};
});
writeDataFile('levels.json', itemArray);
}
function convertSpaces()
{
let files = fs.readdirSync(archivePath + '/spaces');
let itemArray = files.map(function (filename) {
let rawData = fs.readFileSync(archivePath + '/spaces/' + filename)
let data = JSON.parse(rawData)
let space = {
slug: data.slug,
title: data.title,
order: data.order,
};
if (data._links.track) {
space.track = data._links.track.href.replace(`/${year}/tracks/`, '');
}
return space;
});
writeDataFile('spaces.json', itemArray);
}
function convertSpeakers()
{
let files = fs.readdirSync(archivePath + '/speakers');
let itemArray = files.map(function (filename) {
let rawData = fs.readFileSync(archivePath + '/speakers/' + filename)
let data = JSON.parse(rawData)
let twitter;
if (data._links.twitter) {
twitter = data._links.twitter
}
return {
slug: data.slug,
firstName: data.firstName,
lastName: data.lastName,
bio: (data.bio) ? data.bio : '',
twitter: twitter,
sessions: [],
};
});
// Sort speakers by last name
itemArray.sort(function (a, b) {
if (a.lastName < b.lastName) {
return -1;
} else if (a.lastName > b.lastName) {
return 1;
} else {
return 0;
}
})
// Convert array to keyed object
let speakers = {};
for (let item of itemArray) {
speakers[item.slug] = item;
}
// Add session slugs to speakers
let rawSessionData = fs.readFileSync(dataPath + '/sessions.json');
let sessions = JSON.parse(rawSessionData);
for (let session of Object.values(sessions)) {
for (let speakerSlug of session.speakers) {
speakers[speakerSlug].sessions.push(session.slug);
}
}
writeDataFile('speakers.json', itemArray);
}
function convertTimePeriods()
{
let files = fs.readdirSync(archivePath + '/time-periods');
let itemArray = files.map(function (filename) {
let rawData = fs.readFileSync(archivePath + '/time-periods/' + filename)
let data = JSON.parse(rawData)
return {
slug: parseInt(data.slug),
start: data.start.replace(':00.000 EDT',''),
end: data.end.replace(':00.000 EDT',''),
};
});
writeDataFile('timePeriods.json', itemArray);
}
function convertTracks()
{
if (!fs.existsSync(archivePath + '/tracks')) {
return;
}
let files = fs.readdirSync(archivePath + '/tracks');
let itemArray = files.map(function (filename) {
let rawData = fs.readFileSync(archivePath + '/tracks/' + filename)
let data = JSON.parse(rawData)
return {
slug: data.slug,
title: data.title,
};
});
writeDataFile('tracks.json', itemArray);
}
function writeDataFile(filename, array)
{
let object = {};
for (let item of array) {
object[item.slug] = item;
}
let file = `${projectRoot}/src/_data/${filename}`;
let content = JSON.stringify(object, null, 4);
fs.writeFileSync(file, content, function(err) {
if(err) {
return console.log(err);
}
console.log(`Data written to ${filename}`);
});
}