minor updates

This commit is contained in:
Martin Donnelly 2016-03-23 17:03:15 +00:00
parent e040611ed6
commit c564a7db6f

View File

@ -1,37 +1,44 @@
/**
* Created by marti on 30/01/2016.
*/
var http = require('http'), request = require('request'), cheerio = require('cheerio'), Forecast = require('forecast.io'), util = require('util'), UltraSES = require('ultrases'), cron = require('node-cron');
var jade = require('jade'), _ = require('lodash'), dateFormat = require('dateformat');
var jsonfile = require('jsonfile'), fs = require('fs'), STRING = require('string');
var http = require('http'), request = require('request'), cheerio = require(
'cheerio'), Forecast = require('forecast.io'), util = require('util'), UltraSES = require(
'ultrases'), cron = require('node-cron');
var jade = require('jade'), _ = require('lodash'), dateFormat = require(
'dateformat');
var jsonfile = require('jsonfile'), fs = require('fs'), STRING = require(
'string');
var log4js = require('log4js');
var logger = log4js.getLogger();
var calHandler = require('./calHandler');
var swedishWord = require('./swedishword');
var todayCache = {
last: 0,
data: {
last: 0, data: {
trains: {last: 0, data: []},
weather: {},
history: [],
today: '',
tv:{entries:[]},
cal:{entries:[]},
swedish:{}
},
expire: ((60 * 1000) * 60)
tv: {entries: []},
cal: {entries: []},
swedish: {}
}, 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='}
{
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'
APIKey: '0657dc0d81c037cbc89ca88e383b6bbf', units: 'uk2'
};
var mailer = new UltraSES({
@ -39,8 +46,7 @@ var mailer = new UltraSES({
accessKeyId: 'AKIAJWJS75F7WNCGK64A',
secretAccessKey: '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',
"region": "eu-west-1"
},
defaults: {
}, defaults: {
from: 'Martin Donnelly <martind2000@gmail.com>'
}
});
@ -89,14 +95,14 @@ function DayDiff(CurrentDate) {
Array.prototype.indexOfOld = Array.prototype.indexOf;
Array.prototype.indexOf = function (e, fn) {
Array.prototype.indexOf = function(e, fn) {
if (!fn) {
return this.indexOfOld(e)
}
else {
if (typeof fn === 'string') {
var att = fn;
fn = function (e) {
fn = function(e) {
return e[att];
}
}
@ -108,28 +114,39 @@ module.exports = {
getClock: function(req, res) {
// console.log(todayCache);
res.render('pages/clock', todayCache);
},
getToday: function (req, res) {
console.log(todayCache);
}, getToday: function(req, res) {
logger.info(todayCache);
res.render('pages/today', todayCache);
},
getData: function(req, res) {
}, getData: function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(todayCache));
},
getTodayDate: function () {
}, getTodayDate: function() {
var s, d = new Date();
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.';
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;
},
getTechHistory: function () {
var url, d, day, month, monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
getTechHistory: function() {
var url, d, day, month, monthNames = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
d = new Date();
@ -139,7 +156,7 @@ module.exports = {
url = ['http://www.computerhistory.org/tdih/', month, '/', day].join('');
logger.info(url);
request(url, function (err, resp, body) {
request(url, function(err, resp, body) {
if (err)
throw err;
@ -148,21 +165,33 @@ module.exports = {
var output = [];
tdihbody.find('.tdihevent > p').each(function (div) {
tdihbody.find('.tdihevent > p').each(function(div) {
var s = $(this).text();
output.push(STRING(s).collapseWhitespace().s);
});
todayCache.data.history = todayCache.data.history.concat(output);
console.log(todayCache.data.history);
logger.info(todayCache.data.history);
}, function(error, response, body) {
if(response.statusCode !== 200) {
if (response.statusCode !== 200) {
logger.error(response.statusCode);
logger.error(body);
}
});
},
getHistory: function () {
var url, d, day, month, monthNames = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];
}, getHistory: function() {
var url, d, day, month, monthNames = [
"january",
"february",
"march",
"april",
"may",
"june",
"july",
"august",
"september",
"october",
"november",
"december"
];
d = new Date();
@ -170,10 +199,13 @@ module.exports = {
day = d.getDate();
url = ['http://www.bbc.co.uk/scotland/history/onthisday/', month, '/', day].join('');
url = [
'http://www.bbc.co.uk/scotland/history/onthisday/', month, '/',
day
].join('');
console.log(url);
request(url, function (err, resp, body) {
logger.debug(url);
request(url, function(err, resp, body) {
if (err)
throw err;
$ = cheerio.load(body);
@ -181,7 +213,7 @@ module.exports = {
var body = $('DIV#bbcPageContent').first();
var output = [];
body.find('.story > p').each(function (div) {
body.find('.story > p').each(function(div) {
var s = $(this).text();
if (s.indexOf('Today\'s recipe:') == -1) {
@ -190,38 +222,29 @@ module.exports = {
});
todayCache.data.history = todayCache.data.history.concat(output);
console.log(todayCache.data.history);
logger.info(todayCache.data.history);
module.exports.getTechHistory();
}, function(error, response, body) {
if(response.statusCode !== 200) {
if (response.statusCode !== 200) {
logger.error(response.statusCode);
logger.error(body);
}
});
},
getTrainUpdates: function (id) {
console.log('Getting train events...');
var url = trainList[id].url;
var now = new Date();
}, processTrainUpdates: function(body) {
var outputArray = [];
// if ((now - eventCache.last) > eventCache.expire) {
request(url, function (err, resp, body) {
if (err)
throw err;
$ = cheerio.load(body);
var $ = cheerio.load(body);
var lu = $('DIV#LU').first();
var us = lu.find('.updatesSection').first();
us.find('.updateTitle').each(function (div) {
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) {
us.find('.updateBodyStart').each(function(div) {
var description = $(this).find('.bodyInner').first().find('.primaryStyle').first().text().trim();
var splitDesc = description.split('\r\n');
@ -242,9 +265,8 @@ module.exports = {
// join arrays
for (var i = 0; i < outputArray.length; i++) {
var flag=false;
for (var j = 0; j < todayCache.data.trains.data.length;j++)
{
var flag = false;
for (var j = 0; j < todayCache.data.trains.data.length; j++) {
flag = _.isEqual(todayCache.data.trains.data[j], outputArray[i])
}
@ -254,8 +276,21 @@ module.exports = {
}
todayCache.data.trains.data = _.uniq(todayCache.data.trains.data);
}, getTrainUpdates: function(id) {
logger.info('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;
module.exports.processTrainUpdates(body);
}, function(error, response, body) {
if(response.statusCode !== 200) {
if (response.statusCode !== 200) {
logger.error(response.statusCode);
logger.error(body);
}
@ -263,9 +298,8 @@ module.exports = {
todayCache.data.trains.last = now;
},
updateTrains: function () {
console.log('Updating trains..');
}, updateTrains: function() {
logger.info('Updating trains..');
todayCache.data.trains.data = [];
@ -274,11 +308,14 @@ module.exports = {
},
doGetWeatherOutlook: function () {
console.log('Retrieving weather..');
doGetWeatherOutlook: function() {
logger.info('Retrieving weather..');
var j = {};
var forecast = new Forecast(forecastOptions);
forecast.get(55.8582846, -4.2593033, {units: 'uk2'}, function (err, res, data) {
forecast.get(55.8582846,
-4.2593033,
{units: 'uk2'},
function(err, res, data) {
if (err) throw err;
var tempMin = parseInt(data.daily.data[0].temperatureMin);
@ -294,15 +331,13 @@ module.exports = {
if (tempMax == tempMin) {
j.currently += fs + ' Around ' + tempMin.toString() + ' degrees.';
}
else
{
else {
j.currently += fs + ' Around ' + tempMin.toString() + ' to ' + tempMax.toString() + ' degrees.';
}
todayCache.data.weather = j;
});
},
refreshTrainAndWeather:function() {
}, refreshTrainAndWeather: function() {
try {
module.exports.doGetWeatherOutlook();
@ -320,8 +355,7 @@ module.exports = {
logger.error(e);
}
},
preLoadToday: function () {
}, preLoadToday: function() {
module.exports.getTodayDate();
var self = this;
@ -350,7 +384,9 @@ module.exports = {
}
try {
calHandler.getSimpleCalV2('http://www.pogdesign.co.uk/cat/download_ics/60cfdff469d0490545d33d7e3b5c0bcc', function(v) {
calHandler.getSimpleCalV2(
'http://www.pogdesign.co.uk/cat/download_ics/60cfdff469d0490545d33d7e3b5c0bcc',
function(v) {
todayCache.data.tv = v;
});
}
@ -359,8 +395,10 @@ module.exports = {
}
try {
calHandler.getSimpleCalV2('https://calendar.google.com/calendar/ical/martind2000%40gmail.com/private-40cfebc9f7dcfa7fde6b9bf2f0092c93/basic.ics', function(v) {
todayCache.data.cal.entries = todayCache.data.cal.entries.concat(v.entries) ;
calHandler.getSimpleCalV2(
'https://calendar.google.com/calendar/ical/martind2000%40gmail.com/private-40cfebc9f7dcfa7fde6b9bf2f0092c93/basic.ics',
function(v) {
todayCache.data.cal.entries = todayCache.data.cal.entries.concat(v.entries);
});
}
catch (e) {
@ -368,8 +406,10 @@ module.exports = {
}
try {
calHandler.getSimpleCalV2('https://calendar.google.com/calendar/ical/mt5pgdhknvgoc8usfnrso9vkv0%40group.calendar.google.com/private-58876002af9f302a593acfa6fa792dcf/basic.ics', function(v) {
todayCache.data.cal.entries = todayCache.data.cal.entries.concat(v.entries) ;
calHandler.getSimpleCalV2(
'https://calendar.google.com/calendar/ical/mt5pgdhknvgoc8usfnrso9vkv0%40group.calendar.google.com/private-58876002af9f302a593acfa6fa792dcf/basic.ics',
function(v) {
todayCache.data.cal.entries = todayCache.data.cal.entries.concat(v.entries);
});
}
catch (e) {
@ -377,8 +417,10 @@ module.exports = {
}
try {
calHandler.getSimpleCalV2('https://www.tripit.com/feed/ical/private/DB96E4BB-94A9BD8F9CC1CF51C6CC0D920840F4F5/tripit.ics', function(v) {
todayCache.data.cal.entries = todayCache.data.cal.entries.concat(v.entries) ;
calHandler.getSimpleCalV2(
'https://www.tripit.com/feed/ical/private/DB96E4BB-94A9BD8F9CC1CF51C6CC0D920840F4F5/tripit.ics',
function(v) {
todayCache.data.cal.entries = todayCache.data.cal.entries.concat(v.entries);
});
}
catch (e) {
@ -387,21 +429,18 @@ module.exports = {
try {
swedishWord.getSwedishWord(function(v) {
todayCache.data.swedish = v ;
todayCache.data.swedish = v;
});
}
catch (e) {
logger.error(e);
}
// word of the day http://wotd.transparent.com/rss/swedish-widget.xml?t=1455840000000
// time stamp
}
}
;
};
function sendEmailV1() {
@ -412,22 +451,25 @@ function sendEmailV1() {
subject: 'Today - ' + dateFormat(now, "dddd, mmmm dS, yyyy")
};
var template = {file: __dirname + '/' + 'jade/today.jade', locals: todayCache};
var template = {
file: __dirname + '/' + 'jade/today.jade',
locals: todayCache
};
logger.debug(__dirname);
logger.debug(__dirname.substr(__dirname.lastIndexOf('/'), __dirname.length));
//if (__dirname.substr(__dirname.lastIndexOf('/'),__dirname.length))
mailer.sendTemplate(email, template, function (err) {
mailer.sendTemplate(email, template, function(err) {
if (err) throw err;
console.log('compiled template email sent');
logger.info('compiled template email sent');
});
// saveData();
var fn = jade.compileFile(template.file);
console.log(fn(todayCache));
//console.log(fn(todayCache));
// fs.writeFileSync(htmlfile, fn(todayCache));
@ -446,31 +488,30 @@ function sendEmail() {
if (err) throw err;
console.log('email sent!');
});
*/
*/
saveData();
}
setTimeout(function () {
setTimeout(function() {
module.exports.preLoadToday();
}, 15000);
/*setTimeout(function () {
sendEmailV1();
}, 45000);*/
}, 45000);*/
cron.schedule('45 6 * * *', function () {
cron.schedule('45 6 * * *', function() {
module.exports.preLoadToday();
return -1;
});
cron.schedule('0 */1 * * *', function () {
cron.schedule('0 */1 * * *', function() {
module.exports.refreshTrainAndWeather();
return -1;
});
cron.schedule('0 7 * * *', function () {
cron.schedule('0 7 * * *', function() {
sendEmailV1();
// console.log('tick');
return -1;