old-silvrgit/lib/today.js

345 lines
9.6 KiB
JavaScript
Raw Normal View History

/**
* Created by marti on 30/01/2016.
*/
2016-02-11 14:48:05 +00:00
var http = require('http'), request = require('request'), cheerio = require('cheerio'), Forecast = require('forecast.io'), util = require('util'), UltraSES = require('ultrases'), cron = require('node-cron');
2016-02-12 10:47:52 +00:00
var jade = require('jade'), _ = require('lodash'), dateFormat = require('dateformat');
2016-02-14 11:34:55 +00:00
var jsonfile = require('jsonfile'), fs=require('fs');
2016-02-13 18:07:20 +00:00
var log4js = require('log4js');
var logger = log4js.getLogger();
var todayCache = {
last: 0,
data: {
trains: {last: 0, data: []},
2016-02-11 14:48:05 +00:00
weather: {},
2016-02-14 11:34:55 +00:00
history: [],
today : ''
},
2016-02-14 11:39:47 +00:00
expire: ((60 * 1000) * 60)
};
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'
};
2016-02-11 11:40:10 +00:00
var mailer = new UltraSES({
aws: {
accessKeyId: 'AKIAJWJS75F7WNCGK64A',
secretAccessKey: '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',
"region": "eu-west-1"
},
defaults: {
from: 'Martin Donnelly <martind2000@gmail.com>'
}
});
2016-02-12 10:23:43 +00:00
var file = 'data.json';
2016-02-14 11:34:55 +00:00
var htmlfile = '../today.html';
2016-02-12 10:23:43 +00:00
function saveData() {
jsonfile.writeFileSync(file, todayCache);
}
2016-02-14 11:34:55 +00:00
function nth(d) {
if(d>3 && d<21) return 'th'; // thanks kennebec
switch (d % 10) {
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
}
}
function dayNumber() {
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
2016-02-14 11:39:47 +00:00
return Math.floor(diff / oneDay);
2016-02-14 11:34:55 +00:00
}
2016-02-14 11:39:47 +00:00
/**
* @return {number}
*/
2016-02-14 11:34:55 +00:00
function DayDiff(CurrentDate)
{
var TYear=CurrentDate.getFullYear();
var TDay=new Date("January, 01, " + (parseInt(TYear)+1));
TDay.getFullYear(TYear);
var DayCount=(TDay-CurrentDate)/(1000*60*60*24);
DayCount=Math.round(DayCount);
return(DayCount);
}
2016-02-12 10:23:43 +00:00
2016-02-14 11:34:55 +00:00
Array.prototype.indexOfOld = Array.prototype.indexOf;
2016-02-11 11:40:10 +00:00
2016-02-11 14:48:05 +00:00
Array.prototype.indexOf = function (e, fn) {
if (!fn) {
return this.indexOfOld(e)
}
else {
if (typeof fn === 'string') {
var att = fn;
fn = function (e) {
return e[att];
}
}
2016-02-11 11:40:10 +00:00
return this.map(fn).indexOfOld(e);
}
};
module.exports = {
getToday: function (req, res) {
res.render('pages/today', todayCache);
},
2016-02-14 11:34:55 +00:00
getTodayDate: function() {
2016-02-14 11:39:47 +00:00
var s, d = new Date();
2016-02-14 11:34:55 +00:00
todayCache.data.history = [];
s = '<strong>' + dateFormat(d, "mmmm d") + '</strong> - ';
s = s + 'The ' + dayNumber() + nth(dayNumber) + ' day of ' + dateFormat(d, "yyyy") + ', and there are ' + DayDiff(d) + ' days left until the end of the year.';
logger.debug(s);
todayCache.data.today = s;
},
2016-02-13 18:07:20 +00:00
getTechHistory: function () {
var url, d, day, month, monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
d = new Date();
month = monthNames[d.getMonth()];
day = d.getDate();
url = ['http://www.computerhistory.org/tdih/', month, '/', day].join('');
logger.info(url);
request(url, function (err, resp, body) {
if (err)
throw err;
$ = cheerio.load(body);
var tdihbody = $('#tdihbody');
var output = [];
tdihbody.find('.tdihevent > p').each(function (div) {
2016-02-13 18:13:47 +00:00
// logger.info($(this));
2016-02-13 18:07:20 +00:00
var s = $(this).text();
if (s.indexOf('Today\'s recipe:') == -1) {
output.push(s);
}
});
todayCache.data.history = todayCache.data.history.concat(output);
console.log(todayCache.data.history);
});
},
2016-02-11 14:48:05 +00:00
getHistory: function () {
var url, d, day, month, monthNames = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];
d = new Date();
month = monthNames[d.getMonth()];
day = d.getDate();
url = ['http://www.bbc.co.uk/scotland/history/onthisday/', month, '/', day].join('');
2016-02-13 18:07:20 +00:00
2016-02-14 11:34:55 +00:00
2016-02-13 18:07:20 +00:00
2016-02-12 10:23:43 +00:00
console.log(url);
2016-02-11 14:48:05 +00:00
request(url, function (err, resp, body) {
if (err)
throw err;
$ = cheerio.load(body);
var body = $('DIV#bbcPageContent').first();
var output = [];
body.find('.story > p').each(function (div) {
var s = $(this).text();
if (s.indexOf('Today\'s recipe:') == -1) {
output.push(s);
}
});
2016-02-14 11:34:55 +00:00
todayCache.data.history = todayCache.data.history.concat(output);
2016-02-12 10:23:43 +00:00
console.log(todayCache.data.history);
2016-02-13 18:07:20 +00:00
module.exports.getTechHistory();
2016-02-11 14:48:05 +00:00
});
},
getTrainUpdates: function (id) {
console.log('Getting train events...');
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();
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;
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');
var wa = [];
for (var i = 0; i < splitDesc.length; i++) {
var contentCheck = splitDesc[i].trim();
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;
});
// join arrays
2016-02-01 08:40:42 +00:00
for (var i = 0; i < outputArray.length; i++) {
2016-02-11 11:40:10 +00:00
var p = todayCache.data.trains.data.indexOf(outputArray[i].title);
console.log('P: ' + p);
todayCache.data.trains.data.push(outputArray[i])
}
2016-02-11 11:40:10 +00:00
todayCache.data.trains.data = _.uniq(todayCache.data.trains.data);
2016-02-01 14:09:58 +00:00
});
todayCache.data.trains.last = now;
2016-02-01 10:23:47 +00:00
},
2016-02-01 08:40:42 +00:00
updateTrains: function () {
2016-02-01 14:09:58 +00:00
console.log('Updating trains..');
2016-02-11 14:48:05 +00:00
todayCache.data.trains.data = [];
2016-02-01 14:09:58 +00:00
2016-02-01 08:40:42 +00:00
module.exports.getTrainUpdates(0);
module.exports.getTrainUpdates(1);
2016-02-01 10:23:47 +00:00
},
2016-02-01 08:40:42 +00:00
doGetWeatherOutlook: function () {
console.log('Retrieving weather..');
var j = {};
var forecast = new Forecast(forecastOptions);
forecast.get(55.8582846, -4.2593033, {units: 'uk2'}, function (err, res, data) {
if (err) throw err;
2016-02-01 08:40:42 +00:00
j.currently = data.currently.summary;
j.today = data.daily.summary;
j.alerts = data.alerts || {};
2016-02-01 08:40:42 +00:00
todayCache.data.weather = j;
});
2016-02-01 10:23:47 +00:00
},
2016-02-01 08:40:42 +00:00
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);
}
}
;
2016-02-11 11:40:10 +00:00
function sendEmail() {
2016-02-12 10:47:52 +00:00
var now = new Date();
2016-02-11 11:40:10 +00:00
var email = {
to: 'martind2000@gmail.com',
2016-02-12 10:47:52 +00:00
subject: 'Today - ' + dateFormat(now, "dddd, mmmm dS, yyyy")
2016-02-11 11:40:10 +00:00
};
2016-02-14 11:34:55 +00:00
var template = {file: 'jade/today.jade', locals: todayCache};
2016-02-14 11:40:40 +00:00
mailer.sendTemplate(email, template, function (err) {
2016-02-11 14:48:05 +00:00
if (err) throw err;
2016-02-11 11:40:10 +00:00
console.log('compiled template email sent');
2016-02-14 11:40:40 +00:00
});
2016-02-11 11:40:10 +00:00
2016-02-12 10:23:43 +00:00
/* console.log(JSON.stringify(todayCache));*/
2016-02-11 14:48:05 +00:00
var fn = jade.compileFile(template.file);
2016-02-11 11:40:10 +00:00
2016-02-11 14:48:05 +00:00
console.log(fn(todayCache));
2016-02-11 11:40:10 +00:00
2016-02-14 11:34:55 +00:00
fs.writeFileSync(htmlfile, fn(todayCache));
2016-02-11 11:40:10 +00:00
}
setTimeout(function () {
// console.log('Pre loading trains...');
module.exports.preLoadToday();
2016-02-11 11:40:10 +00:00
}, 15000);
2016-02-12 10:23:43 +00:00
setTimeout(function () {
// console.log('Pre loading trains...');
2016-02-14 11:34:55 +00:00
module.exports.getTodayDate();
2016-02-13 18:07:20 +00:00
module.exports.getHistory();
2016-02-12 10:23:43 +00:00
}, 25000);
setTimeout(function () {
// console.log('Pre loading trains...');
saveData();
sendEmail();
}, 45000);
cron.schedule('45 6 * * *', function () {
2016-02-11 14:48:05 +00:00
module.exports.getHistory();
return -1;
});
2016-02-12 10:23:43 +00:00
cron.schedule('0 7 * * *', function () {
2016-02-11 11:40:10 +00:00
sendEmail();
// console.log('tick');
return -1;
2016-02-11 14:48:05 +00:00
});