-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_data.js
243 lines (225 loc) · 7.18 KB
/
get_data.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
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
const request = require('request');
const babyparse = require('babyparse');
const yaml = require('js-yaml');
const fs = require('fs-extra');
const escape = require('escape-html');
// sheet to update
// uggcf://qbpf.tbbtyr.pbz/fcernqfurrgf/q/1r5l9ULLd-qghTeUkIzfRvrL2wO3RReJbILklgGZaNUj/rqvg#tvq=863043106
var oldurl = 'https://docs.google.com/spreadsheets/d/16RfbdrnDHhRgP2iZwNw6AVSyWy5VoKn0nB0CpyMa658/pub?';
var jun16dri = 'https://docs.google.com/spreadsheets/d/1e5y9HYYq-dtuGrHxVmsEieY2jB3EErWoVYxytTMnAHw/pub?';
var dataDir = '_data/';
var outExt = 'json';
// var outExt = 'yml';
getData({
gdocUrlBase: jun16dri,
gdocSheet: '863043106',
outFile: 'overview.' + outExt,
outDir: dataDir
});
getData({
gdocUrlBase: jun16dri,
gdocSheet: '55408582',
outFile: 'outcomes.' + outExt,
outDir: dataDir
});
getData({
gdocUrlBase: jun16dri,
gdocSheet: '585110058',
outFile: 'workshops.' + outExt,
outDir: dataDir
});
getData({
gdocUrlBase: jun16dri,
gdocSheet: '1411565774',
outFile: 'people.' + outExt,
outDir: dataDir,
processRows: function (rows) {
var outData = {};
for (var c = 0; c < rows.length; c++) {
if (rows[c].bio) {
rows[c].bio = '<p>' + escape(rows[c].bio) + '</p>';
rows[c].bio = rows[c].bio.replace(/(\n)/g, '</p><p>');
}
if (rows[c].id) {
rows[c].id = rows[c].name.replace(/\s/g, '-').toLowerCase();
}
if (rows[c].shortname) {
outData[rows[c].shortname] = rows[c];
}
}
return outData;
}
});
getData({
gdocUrlBase: jun16dri,
gdocSheet: '2001419383',
outFile: 'partners.' + outExt,
outDir: dataDir
});
getData({
gdocUrlBase: jun16dri,
gdocSheet: '1809179717',
outFile: 'rooms.' + outExt,
outDir: dataDir,
processRows: function (rows) {
var outData = {};
for (var c = 0; c < rows.length; c++) {
var row = rows[c];
if (row.short) {
outData[row.short] = {
name: row.name,
capacity: row.capacity,
location: row.location
};
}
}
return outData;
}
});
getData({
gdocUrlBase: jun16dri,
gdocSheet: '470059533',
outFile: 'schedule.' + outExt,
outDir: dataDir,
processRows: function (rows) {
var outData = [];
var timeslot;
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
timeslot = row.Time || timeslot;
if (timeslot && timeslot.indexOf('Day') > -1) {
outData.push({day: row.Time, date: row.Session, timeslots: []});
// } else if (row.Session) {
} else {
outData[outData.length - 1].timeslots.push({
time: timeslot || '',
session: row.Session || '',
title: row.Title || '',
room: row.Room || '',
instructor: row.Instructors || '',
// instructorlink: linkFromNames(row.Instructors, '/instructors'),
link: row.link || '',
class: row.class || ''
});
}
// else {
// outData[outData.length - 1].timeslots.push({
// time: row.Time,
// title: row.Title,
// room: row.Room
// });
// }
}
return outData;
}
});
getData({
gdocUrlBase: oldurl,
gdocSheet: '585110058',
outFile: 'oldworkshops.' + outExt,
outDir: dataDir
});
getData({
gdocUrlBase: oldurl,
gdocSheet: '470059533',
outFile: 'oldschedule.' + outExt,
outDir: dataDir,
processRows: function (rows) {
var outData = [];
var timeslot;
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
timeslot = row.Time || timeslot;
if (timeslot && timeslot.indexOf('Day') > -1) {
outData.push({day: row.Time, date: row.Session, timeslots: []});
} else if (row.Session) {
outData[outData.length - 1].timeslots.push({
time: timeslot,
session: row.Session,
title: row.Title,
room: row.Room,
instructor: row.Instructors,
instructorlink: linkFromNames(row.Instructors, '/instructors'),
link: row.link
});
} else {
outData[outData.length - 1].timeslots.push({
time: row.Time,
title: row.Title,
room: row.Room
});
}
}
return outData;
}
});
function linkFromNames(names, urlPrefix) {
var nameList = names.split(',');
var links = '';
for (var c = 0; c < nameList.length; c++) {
var name = nameList[c].trim();
if (links !== '') {
links += ', ';
}
links += '<a href="' + urlPrefix + '/#';
name = name.replace(/\s/g, '-').toLowerCase();
links += name + '\">' + nameList[c].trim() + '</a>';
}
return links;
}
function getData(options) {
// Gets data from a google spreadsheet and saves it locally as json/yml file
// The sheet needs to be published to the web as a csv:
// => File > publish to the web > link > csv
//
// options as follows:
// gdocUrlBase (string): the base url of the google spreadsheet everything
// before the worksheet id #
// gdocSheet (string): the worksheet id # from the url. separated into a
// separate variable so you can run this using the same base url for
// multiple sheets in the same document. Default is '0' (first sheet)
// outFile (string): filename for the output file, including appropriate
// extension of either .json or .yml. If neither extension is given, it
// defaults to outputting a json file regardless of file extension.
// Default value is 'gdocdata.json'
// outDir (string): pathname for the output file, relative to this script.
// Default value is the current directory.
// parseOptions (dictionary): options to pass to the csv parser.
// see https://github.com/Rich-Harris/BabyParse and http://papaparse.com/
// Default is to use a header row, skip empty lines, and treat lines
// that start with '//' as comments (ignored)
// processRows: callback function to process the csv data prior to writing.
// takes one parameter, the parse output from babyparse (see link above)
// should return a similarly formatted array.
options = options || {};
var gdocUrlBase = options.gdocUrlBase;
var gdocSheet = options.gdocSheet || '0';
var outFile = options.outFile || 'gdocdata.json';
var outDir = options.outDir || './';
var parseOptions = options.parseOptions || {
header: true,
skipEmptyLines: true,
comments: '//'
};
var processRows = options.processRows;
var gdocUrl = gdocUrlBase + 'gid=' + gdocSheet + '&single=true&output=csv';
var format = (outFile.indexOf('yml') > -1 ? 'yaml' : 'json');
request(gdocUrl, function (error, response, body) {
if (!error && response.statusCode === 200) {
console.log('Successfully read ' + outFile + ' from Google Docs.');
var rows = babyparse.parse(body, parseOptions).data;
if (typeof processRows === 'function') {
console.log('Processing data for ' + outFile);
rows = processRows(rows);
}
if (format === 'yaml') {
fs.writeFile(outDir + outFile, yaml.safeDump(rows));
} else {
fs.writeFile(outDir + outFile, JSON.stringify(rows));
}
console.log('Wrote data to ' + outFile);
} else {
console.error('Failed to get ' + outFile + ' data from Google Docs. \n Error: ' + error + '\nResponse: ' + response);
}
});
}