silvrgit/lib/today/trains.js

132 lines
4.1 KiB
JavaScript
Raw Normal View History

2016-03-31 16:12:25 +00:00
/**
* Created by Martin on 31/03/2016.
*/
let STRING = require('string');
const logger = require('log4js').getLogger();
const request = require('request'), cheerio = require('cheerio');
const _ = require('lodash');
const trainList = [
2016-03-31 16:12:25 +00:00
{
id: 'dbeglq',
url: 'http://www.journeycheck.com/scotrail/route?from=DBE&to=GLQ&action=search&savedRoute='
},
{
2017-04-02 22:42:24 +00:00
id: 'glqhym',
url: 'http://www.journeycheck.com/scotrail/route?from=GLQ&to=HYM&action=search&savedRoute='
2016-03-31 16:12:25 +00:00
}
];
module.exports = {
processTrainUpdates: function (body) {
const outputArray = [];
const $ = cheerio.load(body);
const lu = $('DIV#LU').first();
2016-03-31 16:12:25 +00:00
const us = lu.find('.updatesSection').first();
2016-03-31 16:12:25 +00:00
us.find('.updateTitle').each(function (div) {
const wO = {title: '', description: ''};
2016-03-31 16:12:25 +00:00
title = $(this).find('A').first().text().trim();
wO.title = title;
outputArray.push(wO);
});
us.find('.updateBodyStart').each(function (div) {
let description = $(this).find('.bodyInner').first().find('.primaryStyle').first().text().trim();
const splitDesc = description.split('\r\n');
2016-03-31 16:12:25 +00:00
const wa = [];
for (let i = 0; i < splitDesc.length; i++) {
let contentCheck = splitDesc[i].trim();
2016-03-31 16:12:25 +00:00
if (contentCheck.indexOf('Impact') > -1) contentCheck = '';
if (contentCheck.indexOf('Additional Information') > -1) contentCheck = '';
if (contentCheck.indexOf('apologise for the delay') > -1) contentCheck = '';
if (contentCheck.indexOf('Delay Repay') > -1) contentCheck = '';
if (contentCheck.length > 0) wa.push(contentCheck);
}
description = wa.join(' ');
outputArray[div].description = description;
});
return outputArray;
}, getTrainUpdates: function (id, out) {
return new Promise(function (resolve, reject) {
"use strict";
logger.info('Getting train events: ', id);
const url = trainList[id].url;
2016-03-31 16:12:25 +00:00
let now = new Date();
2016-03-31 16:12:25 +00:00
// if ((now - eventCache.last) > eventCache.expire) {
request(url, function (err, resp, body) {
if (err) {
// logger.error(err);
return reject(err);
// throw err;
}
let trainData = module.exports.processTrainUpdates(body);
2016-03-31 16:12:25 +00:00
for (let i = 0; i < out.length; i++) {
let flag = false;
for (let j = 0; j < trainData.length; j++) {
2016-03-31 16:12:25 +00:00
flag = _.isEqual(trainData[j], out[i])
}
if (!flag) {
trainData.push(out[i]);
}
}
trainData = _.uniq(trainData);
return resolve(trainData);
}, function (error, response, body) {
if (response.statusCode !== 200) {
logger.error(response.statusCode);
logger.error(body);
}
if (error) {
// logger.error(err);
return reject(err);
// throw err;
}
});
});
}, updateTrains: function () {
logger.info('New Updating trains..');
let output = [];
2016-03-31 16:12:25 +00:00
return new Promise(function (resolve, reject) {
"use strict";
module.exports.getTrainUpdates(0, [])
.then((d)=> {
"use strict";
module.exports.getTrainUpdates(1, d)
.then((d)=> {
"use strict";
return resolve(d);
})
.catch((e)=> {
"use strict";
return reject(e);
});
})
.catch((e)=> {
"use strict";
return reject(e);
});
});
}
};