updated today.js

This commit is contained in:
martind2000 2016-02-14 11:34:55 +00:00
parent b084948866
commit d33143394c
2 changed files with 54 additions and 8 deletions

View File

@ -5,6 +5,8 @@ html(lang="en")
body
h1 Today
.stuff
!{data.today}
.weather
h2 Weather
p Currently:

View File

@ -3,7 +3,7 @@
*/
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');
var jsonfile = require('jsonfile'), fs=require('fs');
var log4js = require('log4js');
var logger = log4js.getLogger();
@ -12,7 +12,8 @@ var todayCache = {
data: {
trains: {last: 0, data: []},
weather: {},
history: []
history: [],
today : ''
},
expire: ((60 * 1000) * 60) * 1
};
@ -40,13 +41,43 @@ var mailer = new UltraSES({
});
var file = 'data.json';
var htmlfile = '../today.html';
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";
}
}
Array.prototype.indexOfOld = Array.prototype.indexOf
function dayNumber() {
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
return day;
}
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.indexOf = function (e, fn) {
if (!fn) {
@ -67,6 +98,16 @@ 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"];
@ -112,7 +153,7 @@ module.exports = {
url = ['http://www.bbc.co.uk/scotland/history/onthisday/', month, '/', day].join('');
todayCache.data.history = [];
console.log(url);
request(url, function (err, resp, body) {
@ -131,7 +172,7 @@ module.exports = {
}
});
todayCache.data.history = output;
todayCache.data.history = todayCache.data.history.concat(output);
console.log(todayCache.data.history);
module.exports.getTechHistory();
@ -250,11 +291,11 @@ function sendEmail() {
subject: 'Today - ' + dateFormat(now, "dddd, mmmm dS, yyyy")
};
var template = {file: 'lib/jade/today.jade', locals: todayCache};
mailer.sendTemplate(email, template, function (err) {
var template = {file: 'jade/today.jade', locals: todayCache};
/* mailer.sendTemplate(email, template, function (err) {
if (err) throw err;
console.log('compiled template email sent');
});
});*/
/* console.log(JSON.stringify(todayCache));*/
@ -262,6 +303,8 @@ function sendEmail() {
console.log(fn(todayCache));
fs.writeFileSync(htmlfile, fn(todayCache));
}
setTimeout(function () {
@ -272,6 +315,7 @@ setTimeout(function () {
setTimeout(function () {
// console.log('Pre loading trains...');
module.exports.getTodayDate();
module.exports.getHistory();