2016-03-08 21:52:21 +00:00
|
|
|
/**
|
|
|
|
* Created by marti on 30/01/2016.
|
|
|
|
*/
|
2016-03-23 17:03:15 +00:00
|
|
|
var http = require('http'), request = require('request'), cheerio = require(
|
2016-03-31 16:12:25 +00:00
|
|
|
'cheerio'), util = require('util'), UltraSES = require(
|
2016-03-23 17:03:15 +00:00
|
|
|
'ultrases'), cron = require('node-cron');
|
|
|
|
var jade = require('jade'), _ = require('lodash'), dateFormat = require(
|
|
|
|
'dateformat');
|
|
|
|
var jsonfile = require('jsonfile'), fs = require('fs'), STRING = require(
|
|
|
|
'string');
|
2016-03-24 17:09:42 +00:00
|
|
|
var nano = require('nano')('http://localhost:5984');
|
|
|
|
|
2016-03-08 21:52:21 +00:00
|
|
|
var log4js = require('log4js');
|
|
|
|
var logger = log4js.getLogger();
|
2016-03-31 16:12:25 +00:00
|
|
|
var calHandler = require('./today/calHandler');
|
|
|
|
var swedishWord = require('./today/swedishword');
|
|
|
|
var weather = require('./today/weather');
|
|
|
|
var trains = require('./today/trains');
|
|
|
|
var history = require('./today/history');
|
2016-03-08 21:52:21 +00:00
|
|
|
|
2016-03-24 17:09:42 +00:00
|
|
|
var db_name = 'silvrgit';
|
|
|
|
var dbCouch = nano.use(db_name);
|
|
|
|
|
2016-03-08 21:52:21 +00:00
|
|
|
var todayCache = {
|
2016-03-31 16:12:25 +00:00
|
|
|
last: 0, data: {
|
|
|
|
trains: {last: 0, data: []},
|
|
|
|
weather: {},
|
|
|
|
history: [],
|
|
|
|
today: '',
|
|
|
|
tv: {entries: []},
|
|
|
|
cal: {entries: []},
|
|
|
|
swedish: {}
|
|
|
|
}, expire: ((60 * 1000) * 60)
|
2016-03-08 21:52:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var mailer = new UltraSES({
|
2016-03-31 16:12:25 +00:00
|
|
|
aws: {
|
|
|
|
accessKeyId: 'AKIAJWJS75F7WNCGK64A',
|
|
|
|
secretAccessKey: '8irYxThCp4xxyrbr00HzWcODe2qdNrR7X7S5BKup',
|
|
|
|
"region": "eu-west-1"
|
|
|
|
}, defaults: {
|
|
|
|
from: 'Martin Donnelly <martind2000@gmail.com>'
|
|
|
|
}
|
2016-03-08 21:52:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var file = __dirname + '/' + 'newdata.json';
|
|
|
|
var htmlfile = __dirname + '/' + 'today.html';
|
|
|
|
|
|
|
|
function saveData() {
|
2016-03-31 16:12:25 +00:00
|
|
|
logger.info('Saving...');
|
|
|
|
jsonfile.writeFileSync(file, todayCache);
|
2016-03-08 21:52:21 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 17:09:42 +00:00
|
|
|
function saveToDB(data) {
|
2016-03-31 16:12:25 +00:00
|
|
|
logger.debug('Inserting into couch...');
|
|
|
|
// logger.info(util.inspect(obj));
|
|
|
|
dbCouch.insert(data, function (err, body, header) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Error inserting into couch');
|
|
|
|
logger.error(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
2016-03-24 17:09:42 +00:00
|
|
|
}
|
|
|
|
|
2016-03-08 21:52:21 +00:00
|
|
|
function nth(d) {
|
2016-03-31 16:12:25 +00:00
|
|
|
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";
|
|
|
|
}
|
2016-03-08 21:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function dayNumber() {
|
2016-03-31 16:12:25 +00:00
|
|
|
var now = new Date();
|
|
|
|
var start = new Date(now.getFullYear(), 0, 0);
|
|
|
|
var diff = now - start;
|
|
|
|
var oneDay = 1000 * 60 * 60 * 24;
|
|
|
|
return Math.floor(diff / oneDay);
|
2016-03-08 21:52:21 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 17:09:42 +00:00
|
|
|
function breakDay() {
|
2016-03-31 16:12:25 +00:00
|
|
|
var now = new Date();
|
|
|
|
return {year: now.getFullYear(), month: parseInt(now.getMonth()) + 1, day: now.getDate()}
|
2016-03-24 17:09:42 +00:00
|
|
|
}
|
|
|
|
|
2016-03-08 21:52:21 +00:00
|
|
|
/**
|
|
|
|
* @return {number}
|
|
|
|
*/
|
|
|
|
function DayDiff(CurrentDate) {
|
2016-03-31 16:12:25 +00:00
|
|
|
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-03-08 21:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Array.prototype.indexOfOld = Array.prototype.indexOf;
|
|
|
|
|
2016-03-31 16:12:25 +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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.map(fn).indexOfOld(e);
|
2016-03-08 21:52:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
2016-03-31 16:12:25 +00:00
|
|
|
getClock: function (req, res) {
|
|
|
|
// console.log(todayCache);
|
|
|
|
res.render('pages/clock', todayCache);
|
|
|
|
}, getToday: function (req, res) {
|
|
|
|
logger.info(todayCache);
|
|
|
|
res.render('pages/today', todayCache);
|
|
|
|
}, getData: function (req, res) {
|
|
|
|
res.setHeader('Content-Type', 'application/json');
|
|
|
|
res.end(JSON.stringify(todayCache));
|
|
|
|
}, 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.';
|
|
|
|
|
|
|
|
logger.debug(s);
|
|
|
|
todayCache.data.today = s;
|
|
|
|
},
|
|
|
|
|
|
|
|
refreshTrainAndWeather: function () {
|
|
|
|
|
|
|
|
weather.newDoGetWeather()
|
|
|
|
.then((d)=> {
|
|
|
|
todayCache.data.weather = d;
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
logger.error(e);
|
|
|
|
});
|
|
|
|
|
|
|
|
trains.updateTrains()
|
|
|
|
.then((d) => {
|
|
|
|
"use strict";
|
|
|
|
console.log('Trains: ', d);
|
|
|
|
todayCache.data.trains.data = d;
|
|
|
|
todayCache.data.trains.last = new Date();
|
|
|
|
})
|
|
|
|
.catch((e)=> {
|
|
|
|
"use strict";
|
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
|
|
|
|
}, preLoadToday: function () {
|
|
|
|
module.exports.getTodayDate();
|
|
|
|
var self = this;
|
|
|
|
todayCache.data.cal.entries = [];
|
|
|
|
|
|
|
|
weather.newDoGetWeather()
|
|
|
|
.then((d)=> {
|
|
|
|
todayCache.data.weather = d;
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
logger.error(e);
|
|
|
|
});
|
|
|
|
|
|
|
|
trains.updateTrains()
|
|
|
|
.then((d) => {
|
|
|
|
"use strict";
|
|
|
|
console.log('Trains: ', d);
|
|
|
|
todayCache.data.trains.data = d;
|
|
|
|
todayCache.data.trains.last = new Date();
|
|
|
|
})
|
|
|
|
.catch((e)=> {
|
|
|
|
"use strict";
|
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
history.updateHistory()
|
|
|
|
.then((d) => {
|
|
|
|
"use strict";
|
|
|
|
console.log('History result: ' , d);
|
|
|
|
todayCache.data.history = d;
|
|
|
|
})
|
|
|
|
.catch((e)=>{
|
|
|
|
"use strict";
|
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
calHandler.getSimpleCalV3('http://www.pogdesign.co.uk/cat/download_ics/60cfdff469d0490545d33d7e3b5c0bcc')
|
|
|
|
.then((d)=>{
|
|
|
|
"use strict";
|
|
|
|
todayCache.data.tv = d;
|
|
|
|
})
|
|
|
|
.catch(e)=>{
|
|
|
|
logger.error(e);
|
2016-03-23 17:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-31 16:12:25 +00:00
|
|
|
calHandler.getSimpleCalV3(
|
|
|
|
'https://calendar.google.com/calendar/ical/martind2000%40gmail.com/private-40cfebc9f7dcfa7fde6b9bf2f0092c93/basic.ics'
|
|
|
|
)
|
|
|
|
.then((d)=>{
|
|
|
|
"use strict";
|
|
|
|
todayCache.data.cal.entries = todayCache.data.cal.entries.concat(d.entries);
|
|
|
|
})
|
|
|
|
.catch(e)=>{
|
|
|
|
logger.error(e);
|
|
|
|
}
|
2016-03-08 21:52:21 +00:00
|
|
|
|
|
|
|
|
2016-03-31 16:12:25 +00:00
|
|
|
calHandler.getSimpleCalV3(
|
|
|
|
'https://calendar.google.com/calendar/ical/mt5pgdhknvgoc8usfnrso9vkv0%40group.calendar.google.com/private-58876002af9f302a593acfa6fa792dcf/basic.ics'
|
|
|
|
)
|
|
|
|
.then((d)=>{
|
|
|
|
"use strict";
|
|
|
|
todayCache.data.cal.entries = todayCache.data.cal.entries.concat(d.entries);
|
|
|
|
})
|
|
|
|
.catch(e)=>{
|
|
|
|
logger.error(e);
|
|
|
|
}
|
2016-03-08 21:52:21 +00:00
|
|
|
|
2016-03-31 16:12:25 +00:00
|
|
|
calHandler.getSimpleCalV3(
|
|
|
|
'https://www.tripit.com/feed/ical/private/DB96E4BB-94A9BD8F9CC1CF51C6CC0D920840F4F5/tripit.ics'
|
|
|
|
)
|
|
|
|
.then((d)=>{
|
|
|
|
"use strict";
|
|
|
|
todayCache.data.cal.entries = todayCache.data.cal.entries.concat(d.entries);
|
|
|
|
})
|
|
|
|
.catch(e)=>{
|
|
|
|
logger.error(e);
|
|
|
|
}
|
2016-03-08 21:52:21 +00:00
|
|
|
|
2016-03-31 16:12:25 +00:00
|
|
|
swedishWord.getSwedishWord()
|
|
|
|
.then((d) => {
|
|
|
|
"use strict";
|
|
|
|
console.log('Swedish result: ' , d);
|
|
|
|
todayCache.data.swedish =d;
|
|
|
|
})
|
|
|
|
.catch((e)=>{
|
|
|
|
"use strict";
|
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
|
|
|
|
todayCache.date = breakDay();
|
|
|
|
// word of the day http://wotd.transparent.com/rss/swedish-widget.xml?t=1455840000000
|
|
|
|
// time stamp
|
2016-03-08 21:52:21 +00:00
|
|
|
|
2016-03-23 17:03:15 +00:00
|
|
|
}
|
|
|
|
};
|
2016-03-08 21:52:21 +00:00
|
|
|
|
|
|
|
function sendEmailV1() {
|
|
|
|
|
2016-03-31 16:12:25 +00:00
|
|
|
var now = new Date();
|
2016-03-08 21:52:21 +00:00
|
|
|
|
2016-03-31 16:12:25 +00:00
|
|
|
var email = {
|
|
|
|
to: 'martind2000@gmail.com',
|
|
|
|
subject: 'Today - ' + dateFormat(now, "dddd, mmmm dS, yyyy")
|
|
|
|
};
|
2016-03-08 21:52:21 +00:00
|
|
|
|
2016-03-31 16:12:25 +00:00
|
|
|
var template = {
|
|
|
|
file: __dirname + '/' + 'jade/today.jade',
|
|
|
|
locals: todayCache
|
|
|
|
};
|
2016-03-08 21:52:21 +00:00
|
|
|
|
2016-03-31 16:12:25 +00:00
|
|
|
logger.debug(__dirname);
|
|
|
|
logger.debug(__dirname.substr(__dirname.lastIndexOf('/'), __dirname.length));
|
2016-03-08 21:52:21 +00:00
|
|
|
|
2016-03-31 16:12:25 +00:00
|
|
|
//if (__dirname.substr(__dirname.lastIndexOf('/'),__dirname.length))
|
2016-03-08 21:52:21 +00:00
|
|
|
|
2016-03-31 16:12:25 +00:00
|
|
|
mailer.sendTemplate(email, template, function (err) {
|
|
|
|
if (err) throw err;
|
|
|
|
logger.info('compiled template email sent');
|
|
|
|
});
|
2016-03-08 21:52:21 +00:00
|
|
|
|
2016-03-31 16:12:25 +00:00
|
|
|
// saveData();
|
|
|
|
var fn = jade.compileFile(template.file);
|
2016-03-08 21:52:21 +00:00
|
|
|
|
2016-03-31 16:12:25 +00:00
|
|
|
//console.log(fn(todayCache));
|
2016-03-08 21:52:21 +00:00
|
|
|
|
2016-03-31 16:12:25 +00:00
|
|
|
// fs.writeFileSync(htmlfile, fn(todayCache));
|
2016-03-08 21:52:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendEmail() {
|
2016-03-31 16:12:25 +00:00
|
|
|
logger.log('Simple email');
|
|
|
|
var now = new Date();
|
2016-03-08 21:52:21 +00:00
|
|
|
|
2016-03-31 16:12:25 +00:00
|
|
|
var email = {
|
|
|
|
to: 'martind2000@gmail.com',
|
|
|
|
subject: 'Today - ' + dateFormat(now, "dddd, mmmm dS, yyyy")
|
|
|
|
};
|
2016-03-08 21:52:21 +00:00
|
|
|
|
2016-03-31 16:12:25 +00:00
|
|
|
/* mailer.sendText(email, 'Look at this fantastic email body!', function (err) {
|
|
|
|
if (err) throw err;
|
|
|
|
console.log('email sent!');
|
|
|
|
});
|
|
|
|
*/
|
2016-03-08 21:52:21 +00:00
|
|
|
|
2016-03-31 16:12:25 +00:00
|
|
|
saveData();
|
2016-03-08 21:52:21 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 16:12:25 +00:00
|
|
|
setTimeout(function () {
|
|
|
|
module.exports.preLoadToday();
|
|
|
|
|
|
|
|
}, 5000);
|
2016-03-08 21:52:21 +00:00
|
|
|
|
2016-03-24 17:09:42 +00:00
|
|
|
setTimeout(function () {
|
2016-03-31 16:12:25 +00:00
|
|
|
saveToDB(todayCache);
|
|
|
|
}, 45000);
|
2016-03-08 21:52:21 +00:00
|
|
|
|
2016-03-31 16:12:25 +00:00
|
|
|
cron.schedule('45 6 * * *', function () {
|
|
|
|
module.exports.preLoadToday();
|
|
|
|
return -1;
|
2016-03-08 21:52:21 +00:00
|
|
|
});
|
|
|
|
|
2016-03-31 16:12:25 +00:00
|
|
|
cron.schedule('0 */1 * * *', function () {
|
|
|
|
module.exports.refreshTrainAndWeather();
|
|
|
|
return -1;
|
2016-03-08 21:52:21 +00:00
|
|
|
});
|
|
|
|
|
2016-03-31 16:12:25 +00:00
|
|
|
cron.schedule('0 7 * * *', function () {
|
|
|
|
sendEmailV1();
|
|
|
|
saveToDB(todayCache);
|
|
|
|
// console.log('tick');
|
|
|
|
return -1;
|
2016-03-08 21:52:21 +00:00
|
|
|
});
|
|
|
|
|