updated today.js

This commit is contained in:
martind2000 2016-02-14 14:41:30 +00:00
parent 609f028a47
commit 6805b955c5
2 changed files with 33 additions and 32 deletions

View File

@ -1,12 +1,11 @@
doctype html doctype html
html(lang="en") html(lang="en")
head head
title= Today title
Today
body body
h1 Today h1 Today
.stuff .stuff !{data.today}
!{data.today}
.weather .weather
h2 Weather h2 Weather
p Currently: 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 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 jade = require('jade'), _ = require('lodash'), dateFormat = require('dateformat');
var jsonfile = require('jsonfile'), fs=require('fs'); var jsonfile = require('jsonfile'), fs = require('fs');
var log4js = require('log4js'); var log4js = require('log4js');
var logger = log4js.getLogger(); var logger = log4js.getLogger();
@ -13,7 +13,7 @@ var todayCache = {
trains: {last: 0, data: []}, trains: {last: 0, data: []},
weather: {}, weather: {},
history: [], history: [],
today : '' today: ''
}, },
expire: ((60 * 1000) * 60) expire: ((60 * 1000) * 60)
}; };
@ -40,20 +40,24 @@ var mailer = new UltraSES({
} }
}); });
var file = 'data.json'; var file = __dirname + '/' + 'data.json';
var htmlfile = '../today.html'; var htmlfile = __dirname + '/' + 'today.html';
function saveData() { function saveData() {
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: return "st"; case 1:
case 2: return "nd"; return "st";
case 3: return "rd"; case 2:
default: return "th"; return "nd";
case 3:
return "rd";
default:
return "th";
} }
} }
@ -68,17 +72,15 @@ function dayNumber() {
/** /**
* @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) {
@ -100,7 +102,7 @@ module.exports = {
getToday: function (req, res) { getToday: function (req, res) {
res.render('pages/today', todayCache); res.render('pages/today', todayCache);
}, },
getTodayDate: function() { getTodayDate: function () {
var s, d = new Date(); var s, d = new Date();
todayCache.data.history = []; todayCache.data.history = [];
@ -133,7 +135,7 @@ module.exports = {
tdihbody.find('.tdihevent > p').each(function (div) { tdihbody.find('.tdihevent > p').each(function (div) {
// logger.info($(this)); // logger.info($(this));
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);
@ -155,8 +157,6 @@ module.exports = {
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); console.log(url);
request(url, function (err, resp, body) { request(url, function (err, resp, body) {
if (err) if (err)
@ -293,17 +293,20 @@ function sendEmail() {
subject: 'Today - ' + dateFormat(now, "dddd, mmmm dS, yyyy") subject: 'Today - ' + dateFormat(now, "dddd, mmmm dS, yyyy")
}; };
var template = {file: '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; if (err) throw err;
console.log('compiled template email sent'); console.log('compiled template email sent');
}); });
/* console.log(JSON.stringify(todayCache));*/ 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));
@ -318,8 +321,7 @@ setTimeout(function () {
setTimeout(function () { setTimeout(function () {
// console.log('Pre loading trains...'); // console.log('Pre loading trains...');
module.exports.getTodayDate(); module.exports.getTodayDate();
module.exports.getHistory(); module.exports.getHistory();
}, 25000); }, 25000);