-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
110 lines (99 loc) · 3.52 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
const {google} = require('googleapis'); // include google api
const keys = require('./keys.json'); // import API keys
const express = require('express');
const fs = require('fs');
// const normalizeForSearch = require('normalize-for-search');
const app = express();
const port = process.env.PORT || 9001;
const host = process.env.HOST || "localhost";
/**
* Returns a copy of dataSet after removing any rows
* containing empty and undefined values.
* @param {*} dataSet A 2D array of data
*/
function cleanData(dataSet){
found = [];
cleaned = [];
for(i in dataSet){
if(dataSet[i][3] == undefined)
cleaned.push(dataSet[i])
else{
found.push(dataSet[i])
}
}
console.log('Removed %d rows with missing data', cleaned.length);
console.log('Found %d valid rows', found.length);
return found;
};
/**
* Gets all data from a given Google Sheet.
* @param {*} client Authorized connection to Google Sheets API
* @param {*} sheet Which sheet to access, 'STATE' or 'FEDERAL'
* @returns A 2D array of data
*/
async function getSheetData(client,sheet) {
const gsAPI = google.sheets({version:"v4", auth:client});
const opt = {
spreadsheetId: keys.sheet_id,
range: sheet
};
const data = await gsAPI.spreadsheets.values.get(opt);
return data;
}
/**
* Gets data and metadata from both 'FEDERAL' and 'STATE' sheets.
* Defines responseObject.
* @param {*} client Authorized connection to Google Sheets API
*/
async function getData(client) { // get data from both federal and state sheets
const federalSheetData = await getSheetData(client, 'FEDERAL');
const federalArray = federalSheetData.data.values;
const federalInfo = federalArray[1];
const federalData = federalArray.slice(2);
const stateSheetData = await getSheetData(client, 'STATE');
const stateArray = stateSheetData.data.values;
const stateInfo = stateArray[1];
const stateData = stateArray.slice(2);
const metadataSheetData = await getSheetData(client, 'METADATA');
const metadataArray = metadataSheetData.data.values;
const metadataObject = {};
for (r in metadataArray) {
const row = metadataArray[r];
metadataObject[row[0]] = row[1];
}
const staticFiles = fs.readdirSync('public/static');
responseObject = {
metadata: metadataObject,
states: stateInfo[0].split(/\s*;\s*/),
agencies: federalInfo[0].split(/\s*;\s*/),
types: stateInfo[3].split(/\s*;\s*/),
topics: stateInfo[4].split(/\s*;\s*/),
outcomes: stateInfo[7].split(/\s*;\s*/),
data: cleanData(federalData.concat(stateData)),
helpFiles: staticFiles
};
}
async function startup() {
const client = new google.auth.JWT( // create client object, which holds the private key and service acc address
keys.client_email, // service acc
null,
keys.private_key, // private key
['https://www.googleapis.com/auth/spreadsheets'] // api address
);
// call the authorize method, which will reach out to the api address and attempt a connection
client.authorize(function(err,tokens){
if(err){
console.log(err);
return;
} else {
console.log("connected to google cloud API")
}
});
await getData(client);
app.use(express.static('public'));
app.get('/info', async (req,res) => {
res.status(200).json(responseObject);
});
app.listen(port, () => {console.log(host + ":" + port)});
}
startup();