traintimesPWA/server/lib/twitter.js
2020-08-20 15:34:55 +01:00

81 lines
1.7 KiB
JavaScript

/**
* Created by WebStorm.
* User: martin
* Date: 30/04/2020
* Time: 16:19
*/
const logger = require('log4js').getLogger('Twitter');
const dbmanager = require('../db/dbmanager');
const rawTwitterList = require('./twitterList.json');
let twitterList = [];
let intervalID = 0;
let eventEmitter;
logger.level = process.env.LOGGER_LEVEL || 'debug';
function doTweetUpdates() {
logger.info('doTweetUpdates');
dbmanager.getLatestTweets(10000)
.then((data) => {
data.forEach((item) => {
if(twitterList.indexOf(item.userID) !== -1)
eventEmitter.emit('sendTweet', `t-${item.userIDStr}`, item);
});
})
.catch((err) => {
logger.error(err.message);
});
}
function buildList() {
twitterList = rawTwitterList.map((item) => {
return item.id;
});
logger.debug(twitterList);
}
function startTweetUpdates() {
logger.warn('StartTweetUpdates');
intervalID = setInterval(() => {
doTweetUpdates();
}, 10000);
}
exports.top = (req, res) => {
dbmanager.getTopOne()
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
'message': err.message || 'Some error occurred while querying the database.'
});
});
};
exports.all = (req, res) => {
const _list = req.body.list || [];
dbmanager.getAll(_list)
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
'message': err.message || 'Some error occurred while querying the database.'
});
});
};
exports.updates = (newEmitter) => {
logger.warn('Setting events');
eventEmitter = newEmitter;
buildList();
setTimeout(() => {
startTweetUpdates();
}, 30000);
};