This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
416 lines (360 loc) · 12.3 KB
/
index.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
const dotenv = require('dotenv').config();
const debug = require('debug')('autovoice:index');
const express = require('express');
const path = require('path');
const app = express();
const autovoice = require('./bin/lib/autovoice');
const formatContentForReading = require('./bin/lib/formatContentForReading');
const individualUUIDs = require('./bin/lib/individualUUIDs');
const fetchContent = require('./bin/lib/fetchContent');
const validateUrl = require('./bin/lib/validate-url');
const session = require('cookie-session');
const OktaMiddleware = require('@financial-times/okta-express-middleware');
const okta = new OktaMiddleware({
client_id: process.env.OKTA_CLIENT,
client_secret: process.env.OKTA_SECRET,
issuer: process.env.OKTA_ISSUER,
appBaseUrl: process.env.BASE_URL,
scope: 'openid offline_access name'
});
app.use(session({
secret: process.env.SESSION_TOKEN,
maxAge: 24 * 3600 * 1000, //24h
httpOnly: true
}));
var requestLogger = function(req, res, next) {
debug("RECEIVED REQUEST:", req.method, req.url);
next(); // Passing the request to the next handler in the stack.
}
app.use(requestLogger);
// these routes do *not* not require OKTA or token
app.use('/static', express.static('static'));
app.get('/__gtg', (req, res) => {
res.status(200).end();
});
app.get('/audio.mp3', (req, res) => {
const id = req.url.split('?')[1];
if (! id) {
res.status(400).send('This call requires an id parameter.').end();
} else {
autovoice.mp3(id)
.then(mp3Content => {
res.set('Content-Type', 'audio/mpeg');
res.send(mp3Content);
})
;
}
});
// these routes do require OKTA or token
/*
if (! process.env.hasOwnProperty('TOKEN')) {
throw 'process.env.TOKEN not defined';
}
if (process.env.BYPASS_TOKEN !== 'true') {
app.use(validateRequest);
}
*/
// Check for valid OKTA login or valid token to byass OKTA login
// This function is not in a middleware or seperate file because
// it requires the context of okta and app.use to function
app.use((req, res, next) => {
if ('token' in req.headers){
if(req.headers.token === process.env.TOKEN){
debug(`Token (header) was valid.`);
next();
} else {
debug(`The token (header) value passed was invalid.`);
res.status(401);
res.json({
status : 'err',
message : 'The token (header) value passed was invalid.'
});
}
} else if('token' in req.query ){
if(req.query.token === process.env.TOKEN){
debug(`Token (query string) was valid.`);
next();
} else {
debug(`The token (query) value passed was invalid.`);
res.status(401);
res.json({
status : 'err',
message : 'The token (query) value passed was invalid.'
});
}
} else {
debug(`No token in header or query, so defaulting to OKTA`);
// here to replicate multiple app.uses we have to do
// some gross callback stuff. You might be able to
// find a nicer way to do this
// This is the equivalent of calling this:
// app.use(okta.router);
// app.use(okta.ensureAuthenticated());
// app.use(okta.verifyJwts());
okta.router(req, res, error => {
if (error) {
return next(error);
}
okta.ensureAuthenticated()(req, res, error => {
if (error) {
return next(error);
}
okta.verifyJwts()(req, res, next);
});
});
}
});
app.get('/podcast', (req, res) => {
const rssUrl = req.query.rss;
const voice = req.query.voice;
if( ! voice ) { res.status(400).send('This call requires a voice parameter.' );
} else if( ! rssUrl ) { res.status(400).send('This call requires a rss parameter.' );
} else if( ! validateUrl(rssUrl) ) { res.status(400).send('This call requires a valid rss parameter.' );
} else {
autovoice.podcast(rssUrl, voice)
.then(feed => {
res.set('Content-Type', 'application/rss+xml');
res.send(feed);
})
;
}
});
app.get('/podcastBasedOnFirstFt/:maxResults/:voice', (req, res) => {
const maxResults = req.params.maxResults;
const voice = req.params.voice;
const token = req.query.token;
const skipFirstFtUuids = req.query.skipFirstFtUuids;
const includeFirstFtUuids = !(skipFirstFtUuids == 'true');
const requestedUrlWithToken = process.env.SERVER_ROOT + req.originalUrl;
let requestedUrl = requestedUrlWithToken.replace(/token=[^\/&]+/, 'token=...');
autovoice.firstFtBasedPodcast(maxResults, requestedUrl, includeFirstFtUuids, voice)
.then(feed => {
res.set('Content-Type', 'application/rss+xml');
res.send(feed);
})
;
});
app.get('/podcastBasedOnList/:voice', (req, res) => {
const voice = req.params.voice;
const token = req.query.token;
const requestedUrlWithToken = process.env.SERVER_ROOT + req.originalUrl;
let requestedUrl = requestedUrlWithToken.replace(/token=[^\/&]+/, 'token=...');
autovoice.listBasedPodcast(requestedUrl, voice)
.then(feed => {
res.set('Content-Type', 'application/rss+xml');
res.send(feed);
})
;
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + '/static/index.html'));
});
app.get('/format', (req, res) => {
const compare = req.query.compare;
const text = req.query.text;
const formattedText = formatContentForReading.processText(text);
res.set('Content-Type', 'text/plain');
let body = formattedText;
if (compare === "yes") {
body = `${text}\n------\n${body}`;
}
res.send(body);
});
app.get('/formatArticleForReading/:uuid', (req, res) => {
fetchContent.articleAsItem(req.params.uuid)
.then( item => {
if (item == null) {
throw `/formatArticleForReading/${req.params.uuid}: item==null, which probably means the CAPI lookup failed.`;
}
return item;
})
.then( item => { return formatContentForReading.processText(item.content) } )
.then( text => { res.send( text ); })
.catch( err => {
debug(`/formatArticleForReading/:uuid: err=${err}`);
res.status(400).send( err.toString() ).end();
})
;
});
app.get('/formatArticleForListening.mp3', (req, res) => {
const voice = req.query.voice;
const uuid = req.query.uuid;
fetchContent.articleAsItem(uuid)
.then( item => {
if (item == null) {
throw `/formatArticleForListening.mp3?uuid=${uuid}voice=${voice}: item==null, which probably means the CAPI lookup failed.`;
}
return item;
})
.then( item => { return formatContentForReading.processText(item.content) } )
.then( text => {
autovoice.snippetMp3(text, voice)
.then(mp3Content => {
res.set('Content-Type', 'audio/mpeg');
res.send(mp3Content);
})
})
.catch( err => {
debug(`/formatArticleForListening.mp3?uuid=${uuid}voice=${voice}: err=${err}`);
res.status(400).send( err.toString() ).end();
})
;
});
const AWS_POLLY_CHAR_MAX = 1500;
app.get('/snippet.mp3', (req, res) => {
let text = req.query.text;
const voice = req.query.voice;
const wrap = req.query.wrap;
if (wrap == 'yes') {
var itemData = {
content : text,
voiceId : voice,
title : "A thing happened",
author : "A. N. Author",
}
text = formatContentForReading.wrapAndProcessItemData(itemData);
}
if (text.length > AWS_POLLY_CHAR_MAX) {
const errText = `Error: Snippet text too large at ${text.length} chars. There is a ${AWS_POLLY_CHAR_MAX} char limit in AWS Polly.`;
console.error(errText);
res.status(400).send(errText);
} else {
autovoice.snippetMp3(text, voice)
.then(mp3Content => {
res.set('Content-Type', 'audio/mpeg');
res.send(mp3Content);
})
}
});
//--- access points to add/remove/list individual uuids for inclusion in Audio Articles
app.get('/uuids/add/:uuid', (req, res) => {
res.send(individualUUIDs.add(req.params.uuid));
});
app.get('/uuids/clear', (req, res) => {
res.send(individualUUIDs.clear());
});
app.get('/uuids', (req, res) => {
res.send(individualUUIDs.list());
});
//--- access points to fetch content
app.get('/content/rssItems', (req, res) => {
const url = req.query.url;
if( url === undefined || url == "" ) {
res.status(400).send(`This call requires a rss parameter.`).end();
} else if( ! validateUrl(url) ) {
res.status(400).send(`This call requires a valid rss parameter.`).end();
} else {
fetchContent.rssItems(url)
.then( items => { res.json( items ); })
.catch( err => {
res.status(400).send( debug(err) ).end();
})
;
}
});
app.get('/content/article/:uuid', (req, res) => {
fetchContent.article(req.params.uuid)
.then( article => { res.json( article ); })
.catch( err => {
res.status(400).send( debug(err) ).end();
})
;
});
app.get('/content/articleAsItem/:uuid', (req, res) => {
fetchContent.articleAsItem(req.params.uuid)
.then( item => { res.json( item ); })
.catch( err => {
res.status(400).send( debug(err) ).end();
})
;
});
app.get('/content/articlesAsItems', (req, res) => {
fetchContent.articlesAsItems( individualUUIDs.list() )
.then( items => { res.json( items ); })
.catch( err => {
res.status(400).send( debug(err) ).end();
})
;
});
//---
app.get('/validate', (req, res) => {
let isValid = false;
if (req.query.hasOwnProperty('url')) {
isValid = validateUrl(req.query.url);
}
res.json( {isValid} );
});
//---
app.get('/content/search/:uuid', (req, res) => {
fetchContent.searchByUUID(req.params.uuid)
.then( item => { res.json( item ); })
.catch( err => {
res.status(400).send( debug(err) ).end();
})
;
});
app.get('/content/searchLastFewFirstFt/:maxResults', (req, res) => {
fetchContent.searchLastFewFirstFt(req.params.maxResults)
.then( item => { res.json( item ); })
.catch( err => {
res.status(400).send( debug(err) ).end();
})
;
});
app.get('/content/getLastFewFirstFtMentionedUuids/:maxResults', (req, res) => {
fetchContent.getLastFewFirstFtMentionedUuids(req.params.maxResults)
.then( item => { res.json( item ); })
.catch( err => {
res.status(400).send( debug(err) ).end();
})
;
});
app.get('/content/getRecentWithoutAmy/:maxResults', (req, res) => {
let maxResults = parseInt( req.params.maxResults );
const offset = (req.params.maxResults>100)? req.params.maxResults-100 : 0;
maxResults = maxResults - offset;
fetchContent.getRecentArticlesWithAvailability(maxResults, offset)
.then( articles => {
const notAudioSuitable = articles.filter( a => ! a.isAudioSuitable );
const shouldHaveAudioButDont = articles.filter( a => a.isAudioSuitable && !a.hasAudio );
const availabilityStatusCounts = {};
articles.forEach( article => {
if (!availabilityStatusCounts.hasOwnProperty(article.availabilityStatus)) {
availabilityStatusCounts[article.availabilityStatus] = 0;
}
availabilityStatusCounts[article.availabilityStatus] += 1;
});
const shouldHaveAudioButDontHourCounts = {};
shouldHaveAudioButDont.forEach( article => {
const dateWithHour = article.lastPublishDateTime.split(':')[0];
if (!shouldHaveAudioButDontHourCounts.hasOwnProperty(dateWithHour)) {
shouldHaveAudioButDontHourCounts[dateWithHour] = 0;
}
shouldHaveAudioButDontHourCounts[dateWithHour] += 1;
});
return {
description: "Looking at the most recent articles published, check their audio situation on the audio-available service and display a summary. NB, only a maximum of 100 results are returned, so specifying more than that will result in an offset to the 100 oldest (e.g. specifying 1000 means you'll get the oldest 100 of the most recent 1000, i.e. 901-1000)",
maxResults: maxResults,
offset: offset,
moistRecentDate: articles[0].lastPublishDateTime,
leastRecentDate: articles[articles.length-1].lastPublishDateTime,
numFound: articles.length,
availabilityStatusCounts: availabilityStatusCounts,
numNotAudioSuitable: notAudioSuitable.length,
numAudioSuitable: articles.length - notAudioSuitable.length,
numShouldHaveAudioButDont: shouldHaveAudioButDont.length,
shouldHaveAudioButDontHourCounts: shouldHaveAudioButDontHourCounts,
shouldHaveAudioButDont: shouldHaveAudioButDont,
notAudioSuitable: notAudioSuitable
}
})
.then( item => { res.json( item ); })
.catch( err => {
res.status(400).send( debug(err) ).end();
})
;
});
//---
app.listen(process.env.PORT, function(){
debug('Server is listening on port', process.env.PORT);
});