-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
155 lines (133 loc) · 4.4 KB
/
server.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
var express = require('express');
var request = require('request');
var app = express();
app.set('port', (process.env.PORT || 8000));
app.get('/api/metadata', function(req, res) {
request('https://spreadsheets.google.com/tq?&key=YOUR_METADATA_SHEET_KEY', function(err, response, body) {
if (err) {
console.log(err);
res.sendStatus(404);
} else {
var data = processMetadata(response.body);
res.json(data);
}
});
});
app.get('/api/regionalcontacts', function(req, res) {
request('https://spreadsheets.google.com/tq?&key=YOUR_REGIONAL_CONTACTS_SHEET_KEY', function(err, response, body) {
if (err) {
console.log(err);
res.sendStatus(404);
} else {
var data = responseToJSON(response.body)['rows'];
data = processRegionalContacts(data);
res.json(data);
}
});
});
app.get('/api/addresses', function(req, res) {
request('https://spreadsheets.google.com/tq?&key=YOUR_ADDRESS_SHEET_KEY', function(err, response, body) {
if (err) {
console.log(err);
res.sendStatus(404);
} else {
var data = responseToJSON(response.body);
data = processAddressData(data);
res.json(data);
}
});
});
app.get('/api/writing', function(req, res) {
request('https://spreadsheets.google.com/tq?&key=YOUR_LETTERTEXT_SHEET_KEY', function(err, response, body) {
if (err) {
console.log(err);
res.sendStatus(404);
} else {
var data = responseToJSON(response.body)['rows'];
data = processWritingOptionData(data);
res.json(data);
}
});
});
function responseToJSON(response) {
// switch this to regex to make more robust against response format changes
var data = response.replace('/*O_o*/\ngoogle.visualization.Query.setResponse(', '');
data = data.replace(');', '');
return JSON.parse(data)['table'];
}
function processRegionalContacts(data) {
var header = ['state', 'name', 'email'];
data = data.map(googleRowsToKeyedObjs(header));
return data.reduce(function(obj, row) {
obj[row.state] = {
name: row.name ? row.name.split(' ').map(function(str) { return str[0].toUpperCase() + str.slice(1) }).join(' ') : row.name,
email: row.email
};
return obj;
}, {});
}
function processWritingOptionData(data) {
var header = googleRowToArr(data.shift());
data = data.map(googleRowsToKeyedObjs(header));
data = data.reduce(function(obj, row) {
var paraIdx, val;
if (!obj[row.sen_state]) obj[row.sen_state] = {};
if (!obj[row.sen_state][row.name_last]) obj[row.sen_state][row.name_last] = [[], [], [], [], []];
for (var key in row) {
if (row.hasOwnProperty(key) && /para_\d_opt_\d/.test(key) && row[key] && row[key] !== '') {
paraIdx = parseInt(key[5]) - 1;
obj[row.sen_state][row.name_last][paraIdx].push(row[key]);
}
}
return obj;
}, {});
return data;
}
function processAddressData(data) {
var header = data['cols'].reduce(function(keys, col) {
if (col.label !== '') keys.push(col.label);
return keys;
}, []);
data = data['rows'].map(googleRowsToKeyedObjs(header));
data = data.reduce(function(obj, row) {
var key = row.sen_state + row.name_last;
if (!obj[key]) obj[key] = { addresses: [] };
obj[key].addresses.push(row);
return obj;
}, {});
return data;
}
function processMetadata(data) {
var metadata = data.replace('/*O_o*/\ngoogle.visualization.Query.setResponse(', ''); //'/*O_o*/\ngoogle.visualization.Query.setResponse('
metadata = metadata.replace(/\\u2013/g, '-');
metadata = metadata.replace(/\\u2013/g, "'");
metadata = metadata.replace(/\n/g, '');
metadata = metadata.slice(0, -2);
metadata = JSON.parse(metadata)['table'];
var header = metadata['cols'].reduce(function(keys, col) {
if (col.label !== '') keys.push(col.label);
return keys;
}, []);
var mappedData = metadata['rows'].map(googleRowsToKeyedObjs(header));
return mappedData.reduce(function(obj, row) {
obj[row.name_last] = row.meta;
return obj;
}, {});
}
function googleRowsToKeyedObjs(keys) {
return function(row) {
return row['c'].reduce(function(obj, val, idx) {
if (val) obj[keys[idx]] = val.v;
return obj;
}, {});
}
}
function googleRowToArr(row) {
return row['c'].map(function(obj) {
return obj ? obj.v : null;
});
}
app.use(express.static('public'));
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});