old-silvrgit/lib/today.js
2016-02-01 00:22:50 +00:00

128 lines
4.0 KiB
JavaScript

/**
* Created by marti on 30/01/2016.
*/
var http = require('http'), request = require('request'), cheerio = require('cheerio'), Forecast = require('forecast.io'), util = require('util');
var todayCache = {
last: 0,
data: {
trains: {last: 0, data: []},
weather: {}
},
expire: ((60 * 60) * 1) * 1000
};
var trainList = [
{id: 'dbeglq', url: 'http://www.journeycheck.com/scotrail/route?from=DBE&to=GLQ&action=search&savedRoute='},
{id: 'glqdbe', url: 'http://www.journeycheck.com/scotrail/route?from=GLQ&to=DBE&action=search&savedRoute='}
];
//https://api.forecast.io/forecast/0657dc0d81c037cbc89ca88e383b6bbf/55.8582846,-4.2593033?units=uk2
var forecastOptions = {
APIKey: '0657dc0d81c037cbc89ca88e383b6bbf',
units: 'uk2'
};
module.exports = {
getToday: function (req, res) {
res.render('pages/today', todayCache);
},
getTrainUpdates: function (id) {
console.log('Getting train events...');
var url = trainList[id].url;
var now = new Date();
// if ((now - eventCache.last) > eventCache.expire) {
request(url, function (err, resp, body) {
if (err)
throw err;
$ = cheerio.load(body);
var lu = $('DIV#LU').first();
lu.find('.updatesSection').each(function (div) {
var item = {};
var title = $(this).find('.updateTitle').first().find('A').first().text().trim();
var description = $(this).find('.updateBodyStart').first().find('.bodyInner').first().find('.primaryStyle').first().text().trim();
var splitDesc = description.split('\r\n');
item.title = title;
var wa = [];
for (var i = 0; i < splitDesc.length; i++) {
var contentCheck = splitDesc[i].trim();
if (contentCheck.indexOf('Impact') > 0) contentCheck = '';
if (contentCheck.indexOf('Additional Information') > 0) contentCheck = '';
if (contentCheck.indexOf('apologise for the delay') > 0) contentCheck = '';
if (contentCheck.indexOf('Delay Repay') > 0) contentCheck = '';
if (contentCheck.length > 0) wa.push(contentCheck);
}
item.description = wa.join(' ');
todayCache.data.trains.data.push(item);
});
todayCache.data.trains.last = now;
// todayCache.data.trains.data = j;
});
},
updateTrains: function () {
todayCache.data.trains.data = [];
var output = module.exports.getTrainUpdates(0);
output = module.exports.getTrainUpdates(1);
setTimeout(function () {
module.exports.updateTrains();
}, todayCache.expire);
},
doGetWeatherOutlook: function () {
console.log('Retrieving weather..');
var j = {};
var forecast = new Forecast(forecastOptions);
//55.8582846,-4.2593033
forecast.get(55.8582846, -4.2593033, {units: 'uk2'}, function (err, res, data) {
if (err) throw err;
//console.log('data: ' + util.inspect(data));
j.currently = data.currently.summary;
j.today = data.daily.summary;
j.alerts = data.alerts || {};
todayCache.data.weather = j;
});
},
preLoadToday: function () {
try {
module.exports.doGetWeatherOutlook();
}
catch (e) {
// statements to handle any exceptions
console.log('ERROR:');
console.log(e);
}
try {
module.exports.updateTrains();
}
catch (e) {
// statements to handle any exceptions
console.log('ERROR:');
console.log(e);
}
setTimeout(function () {
module.exports.preLoadToday();
}, todayCache.expire);
}
};
setTimeout(function () {
// console.log('Pre loading trains...');
module.exports.preLoadToday();
}, 15000);