traintimesPWA/twitter.js
Martin Donnelly da5d1384c5 * Twitter now cleans db on restart
* Server now handles websockets and sends tweets to websocket

* Added new ecosystem for pm2 to control server/twitter
2020-08-19 11:25:53 +01:00

142 lines
4.1 KiB
JavaScript

/**
* Created by WebStorm.
* User: martin
* Date: 30/04/2020
* Time: 12:24
*/
const Twitter = require('twitter-lite');
require('dotenv').config();
const dbmanager = require('./server/db/dbmanager');
const client = new Twitter({
'consumer_key' : process.env.TWITTER_CONSUMER_KEY,
'consumer_secret' : process.env.TWITTER_CONSUMER_SECRET,
'access_token_key' : process.env.TWITTER_ACCESS_TOKEN_KEY,
'access_token_secret' : process.env.TWITTER_ACCESS_TOKEN_SECRET
});
const accounts = [
{'name':'nationalrailenq', 'id':33546465},
{'name':'networkrail', 'id':365344176},
{'name':'NetworkRailSCOT', 'id':402687948},
{'name':'AvantiWestCoast', 'id':1143560758476906497},
{'name':'CalSleeper', 'id':2870293725},
{'name':'CrossCountryUK', 'id':153368708},
{'name':'Eurostar', 'id':98412169},
{'name':'EurostarUK', 'id':59742254},
{'name':'GatwickExpress', 'id':163816182},
{'name':'GlasgowSubway', 'id':224607925},
{'name':'GWRHelp', 'id':15589815},
{'name':'HeathrowExpress', 'id':20240678},
{'name':'LNER', 'id':313306238},
{'name':'LNRailway', 'id':910487328627535872},
{'name':'northernassist', 'id':194512268},
{'name':'ScotRail', 'id':61569136},
{'name':'Stansted_Exp', 'id':257511611},
{'name':'TfL', 'id':47319664},
{'name':'WestMidRailway', 'id':915554470175657984},
{'name':'NetworkRailBHM', 'id':583910976},
{'name':'NetworkRailEDB', 'id':586614081},
{'name':'NetworkRailEUS', 'id':581807264},
{'name':'NetworkRailGLC', 'id':421061171},
{'name':'NetworkRailKGX', 'id':459192871},
{'name':'NetworkRailLST', 'id':581826097},
{'name':'NetworkRailMAN', 'id':583895871},
{'name':'NetworkRailVIC', 'id':587354752},
{'name':'BTP', 'id':266094415},
{'name':'BTPAvonSomerset', 'id':738664125132345344},
{'name':'BTPBhm', 'id':952003488},
{'name':'BTPBlackCountry', 'id':767698362866999297},
{'name':'BTPCambs', 'id':2574726074},
{'name':'BTPCardiff_NWP', 'id':951714852},
{'name':'BTPEAnglia', 'id':4479942923},
{'name':'BTPEssex', 'id':2949032015},
{'name':'BTPGtrMcr', 'id':1670204977},
{'name':'BTPLeics', 'id':761147194598711296},
{'name':'BTPLiverpoolSt', 'id':951912242},
{'name':'BTPLondon', 'id':957226980},
{'name':'BTPLondonBridge', 'id':3346645594},
{'name':'BTPMersey', 'id':951748434},
{'name':'BTPNorthScot', 'id':2238888007},
{'name':'BTPNorthWales', 'id':951487338},
{'name':'BTPOxon', 'id':741228701791178753},
{'name':'BTPPontypridd', 'id':1672678292},
{'name':'BTPScotland', 'id':957256160},
{'name':'BTPSouthYorks', 'id':3384315676},
{'name':'BTPTeesValley', 'id':802182849872936962},
{'name':'BTPUnderground', 'id':986236195049897985},
{'name':'BTPWales', 'id':1430734374},
{'name':'BTPWaterloo', 'id':951997044},
{'name':'BTPWestScot', 'id':951757261}
];
// {'name':'', 'id':},
(async function(){
/* const response = await user.getBearerToken();
const app = new Twitter({
bearer_token: response.access_token
});*/
const cleanDB = async function(){
const aDay = 86400000;
const now = new Date().getTime() ;
const oldDate = now - (15 * aDay);
console.log('Clean up to:', oldDate );
await dbmanager.deleteUpTo(oldDate).then((v) => {
console.log(v);
}).catch((err) => {
console.error(err);
});
await dbmanager.vacuum().then((v) => {
console.log(v);
}).catch((err) => {
console.error(err);
});
};
const validAccounts = accounts.map(item => {
return item.id;
})
const parameters = {
follow: validAccounts.join(',') // Scotrail, networkrailScot, networkrail, nationalrail, tfl, avantiwestcoast
}
console.log('Starting:', parameters);
await cleanDB();
const stream = client.stream("statuses/filter", parameters)
.on("start", response => console.log('Started!'))
.on("data", tweet => {
console.log('>> data', tweet);
dbmanager.insertOne(tweet).then((v) => {
console.log(v);
}).catch((err) => {
console.error(err);
});
})
.on("ping", () => console.log('>> ping'))
.on("error", error => console.error(error))
.on("end", response => console.log('Stopped!'));
})();