-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
96 lines (86 loc) · 3.57 KB
/
app.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
// Hawaii Non-Profit Grant Tracker
// (c) 2021 James Thesken
// james@kauaitechgroup.com
// This code is licensed under MIT license
// -------------------------------------------------- //
// Module Dependencies
// -------------------------------------------------- //
require('dotenv').config()
var express = require('express');
var http = require('http');
var config = require('./config.js'); // Get our config info (app id and app secret)
var app = express();
const { GoogleSpreadsheet } = require('google-spreadsheet');
// -------------------------------------------------- //
// Variables
// -------------------------------------------------- //
const doc = new GoogleSpreadsheet('13cpPI2-ZbmFUqImPXWul49Nnl_UbOzQWpCSC5MccS4Q');
// -------------------------------------------------- //
// Initialize Auth - see more available options at https://theoephraim.github.io/node-google-spreadsheet/#/getting-started/authentication
// -------------------------------------------------- //
(async function() {
await doc.useServiceAccountAuth({
client_email: process.env.client_email,
private_key: process.env.private_key.replace(/\\n/g, '\n'),
});
await doc.loadInfo(); // loads document properties and worksheets
console.log(doc.title);
const sheet = doc.sheetsByIndex[0]; // or use doc.sheetsById[id] or doc.sheetsByTitle[title]
const rows = await sheet.getRows(); // can pass in { limit, offset }
}());
// -------------------------------------------------- //
// Express set-up and middleware
// -------------------------------------------------- //
app.set('port', (process.env.PORT || config.PORT));
app.use(express.static(__dirname + '/public'));
// -------------------------------------------------- //
// Routes
// -------------------------------------------------- //
app.get('/', function(req, res) {
console.log("got here");
res.redirect('/index.html');
});
app.get('/data', async function(req, res) {
await doc.useServiceAccountAuth({
client_email: process.env.client_email,
private_key: process.env.private_key.replace(/\\n/g, '\n'),
});
await doc.loadInfo(); // loads document properties and worksheets
const sheet = doc.sheetsByIndex[0]; // or use doc.sheetsById[id] or doc.sheetsByTitle[title]
const rows = await sheet.getRows(); // can pass in { limit, offset }
const result = []
for (i = 0; i < sheet.rowCount; i++) {
if (typeof(rows[i]) !== "undefined"){
const a = {
"lastUpdated": rows[i]["Timestamp"],
"funder": rows[i]['Funder'],
"federal": rows[i]['Federal Opportunity'],
"title": rows[i]['Program Title'],
"description": rows[i]['Program Description'],
"eligibility": rows[i]['Eligibility'],
"programFunding": rows[i]["Funding Total"],
"minAward": rows[i]["Minimum Award"],
"maxAward": rows[i]['Maximum Award'],
"dueDate": rows[i]["Due Date"],
"dueDateOther": rows[i]["Due Date (Other)"],
"url": rows[i]["Link"]
}
const today = new Date();
if (new Date(rows[i]["Due Date"]) < today){
a["isClosed"] = "Closed"
} else if (new Date(rows[i]["Due Date"]) > today){
a["isClosed"] = "Open"
} else if (rows[i]["Due Date"] === ""){
a["isClosed"] = rows[i]["Due Date Type"]
}
result.push(a)
}
}
res.send(result);
})
// -------------------------------------------------- //
// Create and start our server
// -------------------------------------------------- //
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});