Big updates, today, templated slack, cinemas

This commit is contained in:
martind2000 2016-02-01 08:38:16 +00:00
parent 296b79fc67
commit 40c7aa3d8f
4 changed files with 106 additions and 75 deletions

View File

@ -4,7 +4,7 @@ var eventTimer = 0;
var eventCache = {
last: 0,
data: {},
expire: ((60 * 60) * 12) * 1000,
expire: ((60 * 1000) * 60) * 12,
cinema: {}
};
// 'cwr':{data: {}, last:0};

View File

@ -10,7 +10,7 @@ var todayCache = {
trains: {last: 0, data: []},
weather: {}
},
expire: ((60 * 60) * 1) * 1000
expire: ((60 * 1000) * 60) * 1
};
var trainList = [
@ -26,6 +26,7 @@ var forecastOptions = {
module.exports = {
getToday: function (req, res) {
console.log( util.inspect(todayCache.data.trains));
res.render('pages/today', todayCache);
},
@ -34,95 +35,123 @@ module.exports = {
var url = trainList[id].url;
var now = new Date();
var outputArray = [];
// 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 us = lu.find('.updatesSection').first();
us.find('.updateTitle').each(function (div) {
var wO = {title: '', description: ''};
title = $(this).find('A').first().text().trim();
wO.title = title;
console.log(wO.title);
outputArray.push(wO);
});
us.find('.updateBodyStart').each(function (div) {
var description = $(this).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.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);
}
item.description = wa.join(' ');
todayCache.data.trains.data.push(item);
description = wa.join(' ');
outputArray[div].description = description;
});
todayCache.data.trains.last = now;
// todayCache.data.trains.data = j;
// join arrays
for (var i = 0;i < outputArray.length;i++)
{
todayCache.data.trains.data.push(outputArray[i])
}
console.log("outputArray array length:" + outputArray.length );
console.log("array length:" + todayCache.data.trains.data.length );
});
},
updateTrains: function () {
todayCache.data.trains.data = [];
var output = module.exports.getTrainUpdates(0);
output = module.exports.getTrainUpdates(1);
todayCache.data.trains.last = now;
setTimeout(function () {
module.exports.updateTrains();
}, todayCache.expire);
},
// todayCache.data.trains.data = j;
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);
}
};
,
updateTrains: function () {
todayCache.data.trains.data = [];
module.exports.getTrainUpdates(0);
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;
// console.log(j.currently);
// console.log(j.today);
// console.log(j.alerts);
});
}
,
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();
module.exports.preLoadToday();
}, 15000);

View File

@ -8,7 +8,7 @@
<title>Events</title>
<meta name="Author" content="" />
<link href="http://fonts.googleapis.com/css?family=Roboto+Slab:400,300,700" rel="stylesheet" type="text/css">
<link href="//fonts.googleapis.com/css?family=Roboto+Slab:400,300,700" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="css/mui.css">
<link rel="stylesheet" type="text/css" href="../css/mui.css">
<style>

View File

@ -1,16 +1,18 @@
<% if (data.trains.data.length > 0 ) {%>
<div id="container" class="mui-panel">
<%
for (var i = 0; i < data.trains.data.length; i++) { %>
<div class="mui-row">
<div><%= data[i].title %></div>
<p><%= data[i].description %></p>
</div>
<div class="mui-row"><div class="mui-col-md-12"><strong><%= data.trains.data[i].title %></strong></div></div>
<div class="mui-row"><div class="mui-col-md-12"><%= data.trains.data[i].description %></div></div>
<% } %>
</div>
<% } %>
<% } %>