-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
298 lines (273 loc) · 9.02 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"use strict";
var fs = require('fs');
var readline = require('readline');
var google = require('googleapis');
var googleAuth = require('google-auth-library');
var express = require('express');
var app = express();
var router = express.Router();
//var path = __dirname + '/views/';
var today = Array(); //{start: '', end: '', summary: ''};
var labmanagers = Array(); //{start: '', end: '', summary: ''};
var workshops = Array(); // {start: '', end: '', summary: ''};
var clientSecrets = Array();
var lmDB = Array();
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/calendar-nodejs-quickstart.json
var SCOPES = ['https://www.googleapis.com/auth/calendar.readonly'];
var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE) + '/.credentials/';
var TOKEN_PATH = TOKEN_DIR + 'calendar-nodejs-quickstart.json';
// Load client secrets from a local file.
fs.readFile('client_secret.json', function processClientSecrets(err, content) {
if (err) {
console.log('Error loading client secret file: ' + err);
return;
}
clientSecrets = JSON.parse(content);
// Authorize a client with the loaded credentials, then call the
// Google Calendar API to read Events and Workshops.
authorize(clientSecrets, listEvents);
});
setInterval(function getGoogleCallendars() {
fs.readFile('client_secret.json', function processClientSecrets(err, content) {
if (err) {
console.log('Error loading client secret file: ' + err);
return;
}
authorize(JSON.parse(content), listEvents);
});
}, 1000 * 60 * 15); // every 15 minutes
// Load LabManager DB from local file.
fs.readFile('labmanagers.json', function processLabManagers(err, content) {
if (err) {
console.log('Error loading labmanagers file: ' + err);
return;
}
lmDB = JSON.parse(content);
});
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
*
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
function authorize(credentials, callback) {
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris[0];
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, function(err, token) {
if (err) {
getNewToken(oauth2Client, callback);
} else {
oauth2Client.credentials = JSON.parse(token);
callback(oauth2Client);
}
});
}
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
*
* @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback to call with the authorized
* client.
*/
function getNewToken(oauth2Client, callback) {
var authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
});
console.log('Authorize this app by visiting this url: ', authUrl);
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter the code from that page here: ', function(code) {
rl.close();
oauth2Client.getToken(code, function(err, token) {
if (err) {
console.log('Error while trying to retrieve access token', err);
return;
}
oauth2Client.credentials = token;
storeToken(token);
callback(oauth2Client);
});
});
}
/**
* Store token to disk be used in later program executions.
*
* @param {Object} token The token to store to disk.
*/
function storeToken(token) {
try {
fs.mkdirSync(TOKEN_DIR);
} catch (err) {
if (err.code != 'EEXIST') {
throw err;
}
}
fs.writeFile(TOKEN_PATH, JSON.stringify(token));
console.log('Token stored to ' + TOKEN_PATH);
}
/**
* Lists all user's calendars.
*
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
function listCalendars(auth) {
var calendar = google.calendar('v3');
console.log('%s', JSON.stringify(calendar));
calendar.calendarList.list({
auth: auth
}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}
console.log(calendars);
for (var i = 0; i < calendars.length; i++) {
console.log('%s', JSON.stringify(calendars[i]));
//console.log('%s', calendar[i].calendarId);
}
});
}
/**
* Lists the next 6 events on the user's primary calendar.
*
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
function listEvents(auth) {
var calendar = google.calendar('v3');
calendar.events.list({
auth: auth,
calendarId: 'primary',
timeMin: new Date().toISOString(),
maxResults: 6,
singleEvents: true,
orderBy: 'startTime'
}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}
var events = response.items;
labmanagers = Array();
if (events.length == 0) {
console.log('No upcoming events found.');
} else {
console.log('Got ' + events.length +' upcoming LabManagers from Google Calendar.');
var firstToday = 1;
var startUTC = new Date(events[0].start.dateTime);
if (startUTC.getDate() == new Date().getDate()){
firstToday = 0;
}
for (var i = 0; i < (events.length - firstToday); i++) {
var event = events[i];
var startUTC = new Date(event.start.dateTime);
var start = formatStartDate(startUTC);
var endUTC = new Date(event.end.dateTime);
var end = formatEndTime(endUTC);
var summary = event.summary;
var startName = event.summary.search(/LabManager:/i) + 11;
var name, lm;
if (startName > 11) {
name = event.summary.substr(startName).trim();
lm = lmDB[name];
if (typeof(lm) === 'undefined'){
} else {
if (startUTC.getDate() == new Date().getDate()){
start = formatEndTime(startUTC);
today = {start: start, end: end, lm: lm};
} else {
labmanagers.push({start: start, end: end, lm: lm});
}
}
}
console.log('%s bis %s : %s (%s)', start, end, lm.name, lm.image);
}
}
});
listWorkshops(auth);
}
/**
* Lists the next 15 events on the "termine" calendar.
*
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
function listWorkshops(auth) {
var calendar = google.calendar('v3');
calendar.events.list({
auth: auth,
calendarId: '6mi8quogdni3bvnjhe787ci7i4@group.calendar.google.com',
timeMin: (new Date()).toISOString(),
maxResults: 4,
singleEvents: true,
orderBy: 'startTime'
}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}
var events = response.items;
workshops = Array();
//workshops = events;
if (events.length == 0) {
console.log('No upcoming events found.');
} else {
console.log('Got ' + events.length +' upcoming Workshops from Google Calendar.');
for (var i = 0; i < events.length; i++) {
var event = events[i];
var startUTC = new Date(event.start.dateTime);
var start = formatStartDate(startUTC);
var endUTC = new Date(event.end.dateTime);
var end = formatEndTime(endUTC);
var summary = event.summary;
workshops.push({start: start, end: end, summary: summary});
console.log('%s - %s : %s', start, end, summary);
}
}
});
}
function formatStartDate(utc){
var day = utc.getDate();
var month = utc.getMonth()+1;
var year = utc.getFullYear();
var hours = utc.getHours();
var minutes = utc.getMinutes();
if (day<10) day = '0' + day;
if (month<10) month = '0' + month;
if (hours<10) hours = '0' + hours;
if (minutes<10) minutes = '0' + minutes;
return day + '.' + month + '.' + year + ' ' + hours + ':' + minutes;
}
function formatEndTime(utc){
var hours = utc.getHours();
var minutes = utc.getMinutes();
if (hours<10) hours = '0' + hours;
if (minutes<10) minutes = '0' + minutes;
return hours + ':' + minutes;
}
// serve static content
app.use(express.static('public'));
// set view engine and views folder
app.set('view engine', 'pug');
app.set('views', './views');
app.use('/', router);
router.get('/',function(req, res){
var now = new Date();
var curdate = now.getDate() + '.' + (now.getMonth()+1) + '.' + now.getFullYear();
res.render('index', { title: 'FabLab InfoDisplay', message: 'InfoDisplay', curdate: curdate, today: today, labmanagers: labmanagers, workshops: workshops});
//res.sendFile(path + 'index.html');
});
app.use('*',function(req, res){
res.send('Error 404: Not Found!');
});
app.listen(3030,function(){
console.log('Server running at Port 3030');
});