updated today.js

This commit is contained in:
martind2000 2016-02-14 21:11:22 +00:00
parent 043068b407
commit 023cfb182e
2 changed files with 19 additions and 128 deletions

View File

@ -1,11 +1,10 @@
doctype html
html(lang="en")
head
title
Today
title= Today
body
h1 Today
.stuff !{data.today}
.weather
h2 Weather
p Currently:

View File

@ -3,19 +3,16 @@
*/
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');
var log4js = require('log4js');
var logger = log4js.getLogger();
var jsonfile = require('jsonfile');
var todayCache = {
last: 0,
data: {
trains: {last: 0, data: []},
weather: {},
history: [],
today: ''
history: []
},
expire: ((60 * 1000) * 60)
expire: ((60 * 1000) * 60) * 1
};
var trainList = [
@ -40,48 +37,14 @@ var mailer = new UltraSES({
}
});
var file = __dirname + '/' + 'data.json';
var htmlfile = __dirname + '/' + 'today.html';
var file = 'data.json';
function saveData() {
jsonfile.writeFileSync(file, todayCache);
}
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;
return Math.floor(diff / oneDay);
}
/**
* @return {number}
*/
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);
}
Array.prototype.indexOfOld = Array.prototype.indexOf;
Array.prototype.indexOfOld = Array.prototype.indexOf
Array.prototype.indexOf = function (e, fn) {
if (!fn) {
@ -102,50 +65,7 @@ module.exports = {
getToday: function (req, res) {
res.render('pages/today', 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;
},
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) {
// logger.info($(this));
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);
});
},
getHistory: function () {
var url, d, day, month, monthNames = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];
@ -156,7 +76,6 @@ module.exports = {
day = d.getDate();
url = ['http://www.bbc.co.uk/scotland/history/onthisday/', month, '/', day].join('');
console.log(url);
request(url, function (err, resp, body) {
if (err)
@ -174,9 +93,8 @@ module.exports = {
}
});
todayCache.data.history = todayCache.data.history.concat(output);
todayCache.data.history = output;
console.log(todayCache.data.history);
module.exports.getTechHistory();
});
},
@ -284,41 +202,8 @@ module.exports = {
}
;
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/test.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;
console.log('compiled template email sent');
});*/
mailer.sendText(email, 'Look at this fantastic email body!', function (err) {
if (err) throw err;
console.log('email sent!');
});
var fn = jade.compileFile(template.file);
console.log(fn(todayCache));
fs.writeFileSync(htmlfile, fn(todayCache));
}
function sendEmail() {
logger.info('Simple email');
var now = new Date();
var email = {
@ -326,11 +211,18 @@ function sendEmail() {
subject: 'Today - ' + dateFormat(now, "dddd, mmmm dS, yyyy")
};
mailer.sendText(email, 'Look at this fantastic email body!', function (err) {
var template = {file: 'lib/jade/today.jade', locals: todayCache};
mailer.sendTemplate(email, template, function (err) {
if (err) throw err;
console.log('email sent!');
console.log('compiled template email sent');
});
/* console.log(JSON.stringify(todayCache));*/
var fn = jade.compileFile(template.file);
console.log(fn(todayCache));
}
setTimeout(function () {
@ -341,7 +233,6 @@ setTimeout(function () {
setTimeout(function () {
// console.log('Pre loading trains...');
module.exports.getTodayDate();
module.exports.getHistory();
}, 25000);
@ -365,3 +256,4 @@ cron.schedule('0 7 * * *', function () {
return -1;
});