minor updates
This commit is contained in:
parent
e040611ed6
commit
c564a7db6f
787
lib/today.js
787
lib/today.js
@ -1,478 +1,519 @@
|
|||||||
/**
|
/**
|
||||||
* Created by marti on 30/01/2016.
|
* 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 http = require('http'), request = require('request'), cheerio = require(
|
||||||
var jade = require('jade'), _ = require('lodash'), dateFormat = require('dateformat');
|
'cheerio'), Forecast = require('forecast.io'), util = require('util'), UltraSES = require(
|
||||||
var jsonfile = require('jsonfile'), fs = require('fs'), STRING = require('string');
|
'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 log4js = require('log4js');
|
||||||
var logger = log4js.getLogger();
|
var logger = log4js.getLogger();
|
||||||
var calHandler = require('./calHandler');
|
var calHandler = require('./calHandler');
|
||||||
var swedishWord = require('./swedishword');
|
var swedishWord = require('./swedishword');
|
||||||
|
|
||||||
var todayCache = {
|
var todayCache = {
|
||||||
last: 0,
|
last: 0, data: {
|
||||||
data: {
|
trains: {last: 0, data: []},
|
||||||
trains: {last: 0, data: []},
|
weather: {},
|
||||||
weather: {},
|
history: [],
|
||||||
history: [],
|
today: '',
|
||||||
today: '',
|
tv: {entries: []},
|
||||||
tv:{entries:[]},
|
cal: {entries: []},
|
||||||
cal:{entries:[]},
|
swedish: {}
|
||||||
swedish:{}
|
}, expire: ((60 * 1000) * 60)
|
||||||
},
|
|
||||||
expire: ((60 * 1000) * 60)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var trainList = [
|
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
|
//https://api.forecast.io/forecast/0657dc0d81c037cbc89ca88e383b6bbf/55.8582846,-4.2593033?units=uk2
|
||||||
var forecastOptions = {
|
var forecastOptions = {
|
||||||
APIKey: '0657dc0d81c037cbc89ca88e383b6bbf',
|
APIKey: '0657dc0d81c037cbc89ca88e383b6bbf', units: 'uk2'
|
||||||
units: 'uk2'
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var mailer = new UltraSES({
|
var mailer = new UltraSES({
|
||||||
aws: {
|
aws: {
|
||||||
accessKeyId: 'AKIAJWJS75F7WNCGK64A',
|
accessKeyId: 'AKIAJWJS75F7WNCGK64A',
|
||||||
secretAccessKey: '8irYxThCp4xxyrbr00HzWcODe2qdNrR7X7S5BKup',
|
secretAccessKey: '8irYxThCp4xxyrbr00HzWcODe2qdNrR7X7S5BKup',
|
||||||
"region": "eu-west-1"
|
"region": "eu-west-1"
|
||||||
},
|
}, defaults: {
|
||||||
defaults: {
|
from: 'Martin Donnelly <martind2000@gmail.com>'
|
||||||
from: 'Martin Donnelly <martind2000@gmail.com>'
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
var file = __dirname + '/' + 'newdata.json';
|
var file = __dirname + '/' + 'newdata.json';
|
||||||
var htmlfile = __dirname + '/' + 'today.html';
|
var htmlfile = __dirname + '/' + 'today.html';
|
||||||
|
|
||||||
function saveData() {
|
function saveData() {
|
||||||
logger.info('Saving...');
|
logger.info('Saving...');
|
||||||
jsonfile.writeFileSync(file, todayCache);
|
jsonfile.writeFileSync(file, todayCache);
|
||||||
}
|
}
|
||||||
|
|
||||||
function nth(d) {
|
function nth(d) {
|
||||||
if (d > 3 && d < 21) return 'th'; // thanks kennebec
|
if (d > 3 && d < 21) return 'th'; // thanks kennebec
|
||||||
switch (d % 10) {
|
switch (d % 10) {
|
||||||
case 1:
|
case 1:
|
||||||
return "st";
|
return "st";
|
||||||
case 2:
|
case 2:
|
||||||
return "nd";
|
return "nd";
|
||||||
case 3:
|
case 3:
|
||||||
return "rd";
|
return "rd";
|
||||||
default:
|
default:
|
||||||
return "th";
|
return "th";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function dayNumber() {
|
function dayNumber() {
|
||||||
var now = new Date();
|
var now = new Date();
|
||||||
var start = new Date(now.getFullYear(), 0, 0);
|
var start = new Date(now.getFullYear(), 0, 0);
|
||||||
var diff = now - start;
|
var diff = now - start;
|
||||||
var oneDay = 1000 * 60 * 60 * 24;
|
var oneDay = 1000 * 60 * 60 * 24;
|
||||||
return Math.floor(diff / oneDay);
|
return Math.floor(diff / oneDay);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return {number}
|
* @return {number}
|
||||||
*/
|
*/
|
||||||
function DayDiff(CurrentDate) {
|
function DayDiff(CurrentDate) {
|
||||||
var TYear = CurrentDate.getFullYear();
|
var TYear = CurrentDate.getFullYear();
|
||||||
var TDay = new Date("January, 01, " + (parseInt(TYear) + 1));
|
var TDay = new Date("January, 01, " + (parseInt(TYear) + 1));
|
||||||
TDay.getFullYear(TYear);
|
TDay.getFullYear(TYear);
|
||||||
var DayCount = (TDay - CurrentDate) / (1000 * 60 * 60 * 24);
|
var DayCount = (TDay - CurrentDate) / (1000 * 60 * 60 * 24);
|
||||||
DayCount = Math.round(DayCount);
|
DayCount = Math.round(DayCount);
|
||||||
return (DayCount);
|
return (DayCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
Array.prototype.indexOfOld = Array.prototype.indexOf;
|
Array.prototype.indexOfOld = Array.prototype.indexOf;
|
||||||
|
|
||||||
Array.prototype.indexOf = function (e, fn) {
|
Array.prototype.indexOf = function(e, fn) {
|
||||||
if (!fn) {
|
if (!fn) {
|
||||||
return this.indexOfOld(e)
|
return this.indexOfOld(e)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (typeof fn === 'string') {
|
if (typeof fn === 'string') {
|
||||||
var att = fn;
|
var att = fn;
|
||||||
fn = function (e) {
|
fn = function(e) {
|
||||||
return e[att];
|
return e[att];
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return this.map(fn).indexOfOld(e);
|
|
||||||
}
|
}
|
||||||
|
return this.map(fn).indexOfOld(e);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getClock: function(req, res) {
|
getClock: function(req, res) {
|
||||||
// console.log(todayCache);
|
// console.log(todayCache);
|
||||||
res.render('pages/clock', todayCache);
|
res.render('pages/clock', todayCache);
|
||||||
},
|
}, getToday: function(req, res) {
|
||||||
getToday: function (req, res) {
|
logger.info(todayCache);
|
||||||
console.log(todayCache);
|
res.render('pages/today', todayCache);
|
||||||
res.render('pages/today', todayCache);
|
}, getData: function(req, res) {
|
||||||
},
|
res.setHeader('Content-Type', 'application/json');
|
||||||
getData: function(req, res) {
|
res.end(JSON.stringify(todayCache));
|
||||||
res.setHeader('Content-Type', 'application/json');
|
}, getTodayDate: function() {
|
||||||
res.end(JSON.stringify(todayCache));
|
var s, d = new Date();
|
||||||
},
|
todayCache.data.history = [];
|
||||||
getTodayDate: function () {
|
|
||||||
var s, d = new Date();
|
|
||||||
todayCache.data.history = [];
|
|
||||||
|
|
||||||
s = '<strong>' + dateFormat(d, "mmmm d") + '</strong> - ';
|
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);
|
logger.debug(s);
|
||||||
todayCache.data.today = s;
|
todayCache.data.today = s;
|
||||||
},
|
},
|
||||||
|
|
||||||
getTechHistory: function () {
|
getTechHistory: function() {
|
||||||
var url, d, day, month, monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
|
var url, d, day, month, monthNames = [
|
||||||
|
"January",
|
||||||
|
"February",
|
||||||
|
"March",
|
||||||
|
"April",
|
||||||
|
"May",
|
||||||
|
"June",
|
||||||
|
"July",
|
||||||
|
"August",
|
||||||
|
"September",
|
||||||
|
"October",
|
||||||
|
"November",
|
||||||
|
"December"
|
||||||
|
];
|
||||||
|
|
||||||
d = new Date();
|
d = new Date();
|
||||||
|
|
||||||
month = monthNames[d.getMonth()];
|
month = monthNames[d.getMonth()];
|
||||||
|
|
||||||
day = d.getDate();
|
day = d.getDate();
|
||||||
|
|
||||||
url = ['http://www.computerhistory.org/tdih/', month, '/', day].join('');
|
url = ['http://www.computerhistory.org/tdih/', month, '/', day].join('');
|
||||||
logger.info(url);
|
logger.info(url);
|
||||||
request(url, function (err, resp, body) {
|
request(url, function(err, resp, body) {
|
||||||
if (err)
|
if (err)
|
||||||
throw err;
|
throw err;
|
||||||
|
|
||||||
$ = cheerio.load(body);
|
$ = cheerio.load(body);
|
||||||
var tdihbody = $('#tdihbody');
|
var tdihbody = $('#tdihbody');
|
||||||
|
|
||||||
var output = [];
|
var output = [];
|
||||||
|
|
||||||
tdihbody.find('.tdihevent > p').each(function (div) {
|
tdihbody.find('.tdihevent > p').each(function(div) {
|
||||||
var s = $(this).text();
|
var s = $(this).text();
|
||||||
output.push(STRING(s).collapseWhitespace().s);
|
output.push(STRING(s).collapseWhitespace().s);
|
||||||
});
|
});
|
||||||
todayCache.data.history = todayCache.data.history.concat(output);
|
todayCache.data.history = todayCache.data.history.concat(output);
|
||||||
console.log(todayCache.data.history);
|
logger.info(todayCache.data.history);
|
||||||
}, function(error, response, body) {
|
}, function(error, response, body) {
|
||||||
if(response.statusCode !== 200) {
|
if (response.statusCode !== 200) {
|
||||||
logger.error(response.statusCode);
|
logger.error(response.statusCode);
|
||||||
logger.error(body);
|
logger.error(body);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
}, getHistory: function() {
|
||||||
getHistory: function () {
|
var url, d, day, month, monthNames = [
|
||||||
var url, d, day, month, monthNames = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];
|
"january",
|
||||||
|
"february",
|
||||||
|
"march",
|
||||||
|
"april",
|
||||||
|
"may",
|
||||||
|
"june",
|
||||||
|
"july",
|
||||||
|
"august",
|
||||||
|
"september",
|
||||||
|
"october",
|
||||||
|
"november",
|
||||||
|
"december"
|
||||||
|
];
|
||||||
|
|
||||||
d = new Date();
|
d = new Date();
|
||||||
|
|
||||||
month = monthNames[d.getMonth()];
|
month = monthNames[d.getMonth()];
|
||||||
|
|
||||||
day = d.getDate();
|
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);
|
logger.debug(url);
|
||||||
request(url, function (err, resp, body) {
|
request(url, function(err, resp, body) {
|
||||||
if (err)
|
if (err)
|
||||||
throw err;
|
throw err;
|
||||||
$ = cheerio.load(body);
|
$ = cheerio.load(body);
|
||||||
|
|
||||||
var body = $('DIV#bbcPageContent').first();
|
var body = $('DIV#bbcPageContent').first();
|
||||||
var output = [];
|
var output = [];
|
||||||
|
|
||||||
body.find('.story > p').each(function (div) {
|
body.find('.story > p').each(function(div) {
|
||||||
|
|
||||||
var s = $(this).text();
|
var s = $(this).text();
|
||||||
if (s.indexOf('Today\'s recipe:') == -1) {
|
if (s.indexOf('Today\'s recipe:') == -1) {
|
||||||
output.push(s);
|
output.push(s);
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
todayCache.data.history = todayCache.data.history.concat(output);
|
|
||||||
console.log(todayCache.data.history);
|
|
||||||
module.exports.getTechHistory();
|
|
||||||
|
|
||||||
}, function(error, response, body) {
|
|
||||||
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();
|
|
||||||
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
|
|
||||||
|
|
||||||
for (var i = 0; i < outputArray.length; i++) {
|
|
||||||
var flag=false;
|
|
||||||
for (var j = 0; j < todayCache.data.trains.data.length;j++)
|
|
||||||
{
|
|
||||||
flag = _.isEqual(todayCache.data.trains.data[j], outputArray[i])
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!flag) {
|
|
||||||
todayCache.data.trains.data.push(outputArray[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
todayCache.data.trains.data = _.uniq(todayCache.data.trains.data);
|
|
||||||
|
|
||||||
}, function(error, response, body) {
|
|
||||||
if(response.statusCode !== 200) {
|
|
||||||
logger.error(response.statusCode);
|
|
||||||
logger.error(body);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
todayCache.data.trains.last = now;
|
|
||||||
|
|
||||||
},
|
|
||||||
updateTrains: function () {
|
|
||||||
console.log('Updating trains..');
|
|
||||||
|
|
||||||
todayCache.data.trains.data = [];
|
|
||||||
|
|
||||||
module.exports.getTrainUpdates(0);
|
|
||||||
module.exports.getTrainUpdates(1);
|
|
||||||
|
|
||||||
},
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
var tempMin = parseInt(data.daily.data[0].temperatureMin);
|
|
||||||
var tempMax = parseInt(data.daily.data[0].temperatureMax);
|
|
||||||
|
|
||||||
j.currently = data.currently.summary;
|
|
||||||
j.today = data.daily.data[0].summary;
|
|
||||||
j.later = data.daily.summary;
|
|
||||||
j.alerts = data.alerts || {};
|
|
||||||
j.data = data;
|
|
||||||
|
|
||||||
var fs = STRING(j.currently).endsWith('.') ? '' : '.';
|
|
||||||
if (tempMax == tempMin) {
|
|
||||||
j.currently += fs + ' Around ' + tempMin.toString() + ' degrees.';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
j.currently += fs + ' Around ' + tempMin.toString() + ' to ' + tempMax.toString() + ' degrees.';
|
|
||||||
}
|
|
||||||
todayCache.data.weather = j;
|
|
||||||
});
|
|
||||||
|
|
||||||
},
|
|
||||||
refreshTrainAndWeather:function() {
|
|
||||||
|
|
||||||
try {
|
|
||||||
module.exports.doGetWeatherOutlook();
|
|
||||||
}
|
|
||||||
catch (e) {
|
|
||||||
// statements to handle any exceptions
|
|
||||||
logger.error(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
module.exports.updateTrains();
|
|
||||||
}
|
|
||||||
catch (e) {
|
|
||||||
// statements to handle any exceptions
|
|
||||||
logger.error(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
},
|
|
||||||
preLoadToday: function () {
|
|
||||||
module.exports.getTodayDate();
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
try {
|
|
||||||
module.exports.doGetWeatherOutlook();
|
|
||||||
}
|
|
||||||
catch (e) {
|
|
||||||
// statements to handle any exceptions
|
|
||||||
logger.error(e);
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
todayCache.data.history = todayCache.data.history.concat(output);
|
||||||
module.exports.updateTrains();
|
logger.info(todayCache.data.history);
|
||||||
}
|
module.exports.getTechHistory();
|
||||||
catch (e) {
|
|
||||||
// statements to handle any exceptions
|
|
||||||
logger.error(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
}, function(error, response, body) {
|
||||||
module.exports.getHistory();
|
if (response.statusCode !== 200) {
|
||||||
}
|
logger.error(response.statusCode);
|
||||||
catch (e) {
|
logger.error(body);
|
||||||
// statements to handle any exceptions
|
}
|
||||||
logger.error(e);
|
});
|
||||||
}
|
}, processTrainUpdates: function(body) {
|
||||||
|
var outputArray = [];
|
||||||
|
var $ = cheerio.load(body);
|
||||||
|
var lu = $('DIV#LU').first();
|
||||||
|
|
||||||
try {
|
var us = lu.find('.updatesSection').first();
|
||||||
calHandler.getSimpleCalV2('http://www.pogdesign.co.uk/cat/download_ics/60cfdff469d0490545d33d7e3b5c0bcc', function(v) {
|
us.find('.updateTitle').each(function(div) {
|
||||||
todayCache.data.tv = v;
|
var wO = {title: '', description: ''};
|
||||||
});
|
title = $(this).find('A').first().text().trim();
|
||||||
}
|
wO.title = title;
|
||||||
catch (e) {
|
outputArray.push(wO);
|
||||||
logger.error(e);
|
});
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
us.find('.updateBodyStart').each(function(div) {
|
||||||
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) {
|
|
||||||
logger.error(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
var description = $(this).find('.bodyInner').first().find('.primaryStyle').first().text().trim();
|
||||||
calHandler.getSimpleCalV2('https://calendar.google.com/calendar/ical/mt5pgdhknvgoc8usfnrso9vkv0%40group.calendar.google.com/private-58876002af9f302a593acfa6fa792dcf/basic.ics', function(v) {
|
var splitDesc = description.split('\r\n');
|
||||||
todayCache.data.cal.entries = todayCache.data.cal.entries.concat(v.entries) ;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
catch (e) {
|
|
||||||
logger.error(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
var wa = [];
|
||||||
calHandler.getSimpleCalV2('https://www.tripit.com/feed/ical/private/DB96E4BB-94A9BD8F9CC1CF51C6CC0D920840F4F5/tripit.ics', function(v) {
|
for (var i = 0; i < splitDesc.length; i++) {
|
||||||
todayCache.data.cal.entries = todayCache.data.cal.entries.concat(v.entries) ;
|
var contentCheck = splitDesc[i].trim();
|
||||||
});
|
if (contentCheck.indexOf('Impact') > -1) contentCheck = '';
|
||||||
}
|
if (contentCheck.indexOf('Additional Information') > -1) contentCheck = '';
|
||||||
catch (e) {
|
if (contentCheck.indexOf('apologise for the delay') > -1) contentCheck = '';
|
||||||
logger.error(e);
|
if (contentCheck.indexOf('Delay Repay') > -1) contentCheck = '';
|
||||||
}
|
if (contentCheck.length > 0) wa.push(contentCheck);
|
||||||
|
}
|
||||||
|
description = wa.join(' ');
|
||||||
|
outputArray[div].description = description;
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
// join arrays
|
||||||
swedishWord.getSwedishWord(function(v) {
|
|
||||||
todayCache.data.swedish = v ;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
catch (e) {
|
|
||||||
logger.error(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
for (var i = 0; i < outputArray.length; i++) {
|
||||||
|
var flag = false;
|
||||||
|
for (var j = 0; j < todayCache.data.trains.data.length; j++) {
|
||||||
|
flag = _.isEqual(todayCache.data.trains.data[j], outputArray[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!flag) {
|
||||||
// word of the day http://wotd.transparent.com/rss/swedish-widget.xml?t=1455840000000
|
todayCache.data.trains.data.push(outputArray[i]);
|
||||||
// time stamp
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
todayCache.data.trains.data = _.uniq(todayCache.data.trains.data);
|
||||||
;
|
|
||||||
|
|
||||||
function sendEmailV1() {
|
}, getTrainUpdates: function(id) {
|
||||||
|
logger.info('Getting train events...');
|
||||||
|
var url = trainList[id].url;
|
||||||
|
|
||||||
var now = new Date();
|
var now = new Date();
|
||||||
|
|
||||||
var email = {
|
// if ((now - eventCache.last) > eventCache.expire) {
|
||||||
to: 'martind2000@gmail.com',
|
request(url, function(err, resp, body) {
|
||||||
subject: 'Today - ' + dateFormat(now, "dddd, mmmm dS, yyyy")
|
if (err)
|
||||||
};
|
throw err;
|
||||||
|
|
||||||
var template = {file: __dirname + '/' + 'jade/today.jade', locals: todayCache};
|
module.exports.processTrainUpdates(body);
|
||||||
|
|
||||||
logger.debug(__dirname);
|
}, function(error, response, body) {
|
||||||
logger.debug(__dirname.substr(__dirname.lastIndexOf('/'), __dirname.length));
|
if (response.statusCode !== 200) {
|
||||||
|
logger.error(response.statusCode);
|
||||||
|
logger.error(body);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
//if (__dirname.substr(__dirname.lastIndexOf('/'),__dirname.length))
|
todayCache.data.trains.last = now;
|
||||||
|
|
||||||
mailer.sendTemplate(email, template, function (err) {
|
}, updateTrains: function() {
|
||||||
if (err) throw err;
|
logger.info('Updating trains..');
|
||||||
console.log('compiled template email sent');
|
|
||||||
});
|
todayCache.data.trains.data = [];
|
||||||
|
|
||||||
|
module.exports.getTrainUpdates(0);
|
||||||
|
module.exports.getTrainUpdates(1);
|
||||||
|
|
||||||
|
},
|
||||||
|
|
||||||
|
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) {
|
||||||
|
if (err) throw err;
|
||||||
|
|
||||||
|
var tempMin = parseInt(data.daily.data[0].temperatureMin);
|
||||||
|
var tempMax = parseInt(data.daily.data[0].temperatureMax);
|
||||||
|
|
||||||
|
j.currently = data.currently.summary;
|
||||||
|
j.today = data.daily.data[0].summary;
|
||||||
|
j.later = data.daily.summary;
|
||||||
|
j.alerts = data.alerts || {};
|
||||||
|
j.data = data;
|
||||||
|
|
||||||
|
var fs = STRING(j.currently).endsWith('.') ? '' : '.';
|
||||||
|
if (tempMax == tempMin) {
|
||||||
|
j.currently += fs + ' Around ' + tempMin.toString() + ' degrees.';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
j.currently += fs + ' Around ' + tempMin.toString() + ' to ' + tempMax.toString() + ' degrees.';
|
||||||
|
}
|
||||||
|
todayCache.data.weather = j;
|
||||||
|
});
|
||||||
|
|
||||||
|
}, refreshTrainAndWeather: function() {
|
||||||
|
|
||||||
|
try {
|
||||||
|
module.exports.doGetWeatherOutlook();
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
// statements to handle any exceptions
|
||||||
|
logger.error(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
module.exports.updateTrains();
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
// statements to handle any exceptions
|
||||||
|
logger.error(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}, preLoadToday: function() {
|
||||||
|
module.exports.getTodayDate();
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
try {
|
||||||
|
module.exports.doGetWeatherOutlook();
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
// statements to handle any exceptions
|
||||||
|
logger.error(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
module.exports.updateTrains();
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
// statements to handle any exceptions
|
||||||
|
logger.error(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
module.exports.getHistory();
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
// statements to handle any exceptions
|
||||||
|
logger.error(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
calHandler.getSimpleCalV2(
|
||||||
|
'http://www.pogdesign.co.uk/cat/download_ics/60cfdff469d0490545d33d7e3b5c0bcc',
|
||||||
|
function(v) {
|
||||||
|
todayCache.data.tv = v;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
logger.error(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
logger.error(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
logger.error(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
logger.error(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
swedishWord.getSwedishWord(function(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() {
|
||||||
|
|
||||||
|
var now = new Date();
|
||||||
|
|
||||||
|
var email = {
|
||||||
|
to: 'martind2000@gmail.com',
|
||||||
|
subject: 'Today - ' + dateFormat(now, "dddd, mmmm dS, yyyy")
|
||||||
|
};
|
||||||
|
|
||||||
|
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) {
|
||||||
|
if (err) throw err;
|
||||||
|
logger.info('compiled template email sent');
|
||||||
|
});
|
||||||
|
|
||||||
// saveData();
|
// saveData();
|
||||||
var fn = jade.compileFile(template.file);
|
var fn = jade.compileFile(template.file);
|
||||||
|
|
||||||
console.log(fn(todayCache));
|
//console.log(fn(todayCache));
|
||||||
|
|
||||||
// fs.writeFileSync(htmlfile, fn(todayCache));
|
// fs.writeFileSync(htmlfile, fn(todayCache));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendEmail() {
|
function sendEmail() {
|
||||||
logger.log('Simple email');
|
logger.log('Simple email');
|
||||||
var now = new Date();
|
var now = new Date();
|
||||||
|
|
||||||
var email = {
|
var email = {
|
||||||
to: 'martind2000@gmail.com',
|
to: 'martind2000@gmail.com',
|
||||||
subject: 'Today - ' + dateFormat(now, "dddd, mmmm dS, yyyy")
|
subject: 'Today - ' + dateFormat(now, "dddd, mmmm dS, yyyy")
|
||||||
};
|
};
|
||||||
|
|
||||||
/* mailer.sendText(email, 'Look at this fantastic email body!', function (err) {
|
/* mailer.sendText(email, 'Look at this fantastic email body!', function (err) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
console.log('email sent!');
|
console.log('email sent!');
|
||||||
});
|
});
|
||||||
*/
|
*/
|
||||||
|
|
||||||
saveData();
|
saveData();
|
||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(function () {
|
setTimeout(function() {
|
||||||
module.exports.preLoadToday();
|
module.exports.preLoadToday();
|
||||||
}, 15000);
|
}, 15000);
|
||||||
|
|
||||||
|
|
||||||
/*setTimeout(function () {
|
/*setTimeout(function () {
|
||||||
sendEmailV1();
|
sendEmailV1();
|
||||||
}, 45000);*/
|
}, 45000);*/
|
||||||
|
|
||||||
cron.schedule('45 6 * * *', function () {
|
cron.schedule('45 6 * * *', function() {
|
||||||
module.exports.preLoadToday();
|
module.exports.preLoadToday();
|
||||||
return -1;
|
return -1;
|
||||||
});
|
});
|
||||||
|
|
||||||
cron.schedule('0 */1 * * *', function () {
|
cron.schedule('0 */1 * * *', function() {
|
||||||
module.exports.refreshTrainAndWeather();
|
module.exports.refreshTrainAndWeather();
|
||||||
return -1;
|
return -1;
|
||||||
});
|
});
|
||||||
|
|
||||||
cron.schedule('0 7 * * *', function () {
|
cron.schedule('0 7 * * *', function() {
|
||||||
sendEmailV1();
|
sendEmailV1();
|
||||||
// console.log('tick');
|
// console.log('tick');
|
||||||
return -1;
|
return -1;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user