mirror of
https://gitlab.silvrtree.co.uk/martind2000/old-silvrgit.git
synced 2025-01-11 00:35:08 +00:00
updated today.js
This commit is contained in:
parent
043068b407
commit
023cfb182e
@ -1,11 +1,10 @@
|
|||||||
doctype html
|
doctype html
|
||||||
html(lang="en")
|
html(lang="en")
|
||||||
head
|
head
|
||||||
title
|
title= Today
|
||||||
Today
|
|
||||||
body
|
body
|
||||||
h1 Today
|
h1 Today
|
||||||
.stuff !{data.today}
|
|
||||||
.weather
|
.weather
|
||||||
h2 Weather
|
h2 Weather
|
||||||
p Currently:
|
p Currently:
|
||||||
|
142
lib/today.js
142
lib/today.js
@ -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 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');
|
||||||
var log4js = require('log4js');
|
|
||||||
var logger = log4js.getLogger();
|
|
||||||
|
|
||||||
var todayCache = {
|
var todayCache = {
|
||||||
last: 0,
|
last: 0,
|
||||||
data: {
|
data: {
|
||||||
trains: {last: 0, data: []},
|
trains: {last: 0, data: []},
|
||||||
weather: {},
|
weather: {},
|
||||||
history: [],
|
history: []
|
||||||
today: ''
|
|
||||||
},
|
},
|
||||||
expire: ((60 * 1000) * 60)
|
expire: ((60 * 1000) * 60) * 1
|
||||||
};
|
};
|
||||||
|
|
||||||
var trainList = [
|
var trainList = [
|
||||||
@ -40,48 +37,14 @@ var mailer = new UltraSES({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var file = __dirname + '/' + 'data.json';
|
var file = 'data.json';
|
||||||
var htmlfile = __dirname + '/' + 'today.html';
|
|
||||||
|
|
||||||
function saveData() {
|
function saveData() {
|
||||||
jsonfile.writeFileSync(file, todayCache);
|
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() {
|
Array.prototype.indexOfOld = Array.prototype.indexOf
|
||||||
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.indexOf = function (e, fn) {
|
Array.prototype.indexOf = function (e, fn) {
|
||||||
if (!fn) {
|
if (!fn) {
|
||||||
@ -102,50 +65,7 @@ module.exports = {
|
|||||||
getToday: function (req, res) {
|
getToday: function (req, res) {
|
||||||
res.render('pages/today', todayCache);
|
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 () {
|
getHistory: 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"];
|
||||||
|
|
||||||
@ -156,7 +76,6 @@ module.exports = {
|
|||||||
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);
|
console.log(url);
|
||||||
request(url, function (err, resp, body) {
|
request(url, function (err, resp, body) {
|
||||||
if (err)
|
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);
|
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() {
|
function sendEmail() {
|
||||||
logger.info('Simple email');
|
|
||||||
var now = new Date();
|
var now = new Date();
|
||||||
|
|
||||||
var email = {
|
var email = {
|
||||||
@ -326,11 +211,18 @@ function sendEmail() {
|
|||||||
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) {
|
var template = {file: 'lib/jade/today.jade', locals: todayCache};
|
||||||
|
mailer.sendTemplate(email, template, function (err) {
|
||||||
if (err) throw 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 () {
|
setTimeout(function () {
|
||||||
@ -341,7 +233,6 @@ setTimeout(function () {
|
|||||||
|
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
// console.log('Pre loading trains...');
|
// console.log('Pre loading trains...');
|
||||||
module.exports.getTodayDate();
|
|
||||||
module.exports.getHistory();
|
module.exports.getHistory();
|
||||||
|
|
||||||
}, 25000);
|
}, 25000);
|
||||||
@ -365,3 +256,4 @@ cron.schedule('0 7 * * *', function () {
|
|||||||
return -1;
|
return -1;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user