-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
113 lines (93 loc) · 3.21 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const express = require('express');
const config = require('config');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const methodOverride = require('method-override');
const passport = require('passport');
const morgan = require('morgan');
const logger = require('./util/logger');
const Tagger = require('./tagger/tagger');
const T = new Tagger();
const port = process.env.PORT || 3000;
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(methodOverride());
app.use(session({
secret: config.get('cookie.secret'),
resave: false,
saveUninitialized: true
}));
app.use(cookieParser());
app.use(morgan('combined'));
const pocket = require('./util/auth')();
app.use(passport.initialize());
app.use(passport.session());
logger.info(`For authentification go to http://localhost:${port}`);
app.get('/', passport.authenticate('pocket'), (req, res, next) => {
if (!req.user) {
res.send('You\'re not authorized.');
logger.error('Authentication failed. Please check credentials');
return next();
}
logger.info(`User ${JSON.stringify(req.user)} authorized`);
logger.debug('Getting unreaded articles');
pocket.getUnreadItems(req.user.accessToken, (err, items) => {
if (err) return next(err);
res.send(`Welcome ${req.user.username}!`);
logger.info(`Found ${Object.keys(items.list).length} articles`);
logger.debug('Running Tagger');
/* eslint no-shadow: ["error", { "allow": ["err", "res"] }] */
T.getAll(items.list, (err, tags) => {
if (err) return next(err);
const ids = Object.keys(tags);
if (ids.length === 0) {
logger.info('Nothing to save. All your articles were already categorized');
return next();
}
logger.info(`Found tags for ${ids.length} articles`);
const chunckSize = 50;
let data = [];
logger.debug(`${Math.round(ids.length / chunckSize) + 1} requests to be sent`);
for (let i = 0; i < ids.length; i += 1) {
data.push({
action: 'tags_add',
item_id: ids[i],
tags: tags[ids[i]].join(',')
});
if (
(((i + 1) % chunckSize === 0))
|| (i === (ids.length - 1))
) {
pocket.modify(data, req.user.accessToken, (err, res) => {
if (err) return next(err);
logger.debug('Preparing to send a request');
if (res.status === 1) {
logger.info(`Data.length = ${res.action_results.length} was saved to the Pocket account`);
} else {
logger.error('An error occured while saving tags', res);
}
return res;
});
data = [];
}
}
return null;
});
return null;
});
return null;
});
app.get(
'/auth/pocket/callback',
passport.authenticate('pocket', { failureRedirect: '/' }),
(req, res) => res.redirect('/')
);
app.use((err, req, res, next) => {
logger.error('An error occured', err.stack);
if (res.headersSent) return next(err);
res.status(err.status || 500).send('Something went wrong!');
return null;
});
app.listen(port, () => logger.debug(`App: listening on ${port}`));