mirror of
https://gitlab.silvrtree.co.uk/martind2000/old-silvrgit.git
synced 2025-01-10 21:55:08 +00:00
updated today.js
This commit is contained in:
parent
5d0d3ad34a
commit
a0abd7c5b4
@ -1,10 +1,12 @@
|
||||
doctype html
|
||||
html(lang="en")
|
||||
head
|
||||
title= Today
|
||||
|
||||
title
|
||||
Today
|
||||
body
|
||||
h1 Today
|
||||
.stuff
|
||||
| !{data.today}
|
||||
.weather
|
||||
h2 Weather
|
||||
p Currently:
|
||||
|
143
lib/today.js
143
lib/today.js
@ -2,17 +2,20 @@
|
||||
* Created by marti on 30/01/2016.
|
||||
*/
|
||||
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');
|
||||
var jsonfile = require('jsonfile');
|
||||
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 todayCache = {
|
||||
last: 0,
|
||||
data: {
|
||||
trains: {last: 0, data: []},
|
||||
weather: {},
|
||||
history: []
|
||||
history: [],
|
||||
today: ''
|
||||
},
|
||||
expire: ((60 * 1000) * 60) * 1
|
||||
expire: ((60 * 1000) * 60)
|
||||
};
|
||||
|
||||
var trainList = [
|
||||
@ -37,14 +40,48 @@ var mailer = new UltraSES({
|
||||
}
|
||||
});
|
||||
|
||||
var file = 'data.json';
|
||||
var file = __dirname + '/' + 'data.json';
|
||||
var htmlfile = __dirname + '/' + '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;
|
||||
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) {
|
||||
if (!fn) {
|
||||
@ -65,7 +102,50 @@ 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"];
|
||||
|
||||
@ -76,6 +156,7 @@ 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)
|
||||
@ -93,8 +174,9 @@ module.exports = {
|
||||
}
|
||||
});
|
||||
|
||||
todayCache.data.history = output;
|
||||
todayCache.data.history = todayCache.data.history.concat(output);
|
||||
console.log(todayCache.data.history);
|
||||
module.exports.getTechHistory();
|
||||
|
||||
});
|
||||
},
|
||||
@ -202,23 +284,52 @@ module.exports = {
|
||||
}
|
||||
;
|
||||
|
||||
function sendEmail() {
|
||||
function sendEmailV1() {
|
||||
|
||||
var now = new Date();
|
||||
|
||||
var email = {
|
||||
to: 'martind2000@gmail.com',
|
||||
subject: 'Today'
|
||||
subject: 'Today - ' + dateFormat(now, "dddd, mmmm dS, yyyy")
|
||||
};
|
||||
|
||||
var template = {file: 'jade/today.jade', locals: todayCache};
|
||||
mailer.sendTemplate(email, template, function (err) {
|
||||
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('compiled template email sent');
|
||||
console.log('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));
|
||||
|
||||
}
|
||||
|
||||
function sendEmail() {
|
||||
logger.log('Simple email');
|
||||
var now = new Date();
|
||||
|
||||
var email = {
|
||||
to: 'martind2000@gmail.com',
|
||||
subject: 'Today - ' + dateFormat(now, "dddd, mmmm dS, yyyy")
|
||||
};
|
||||
|
||||
mailer.sendText(email, 'Look at this fantastic email body!', function (err) {
|
||||
if (err) throw err;
|
||||
console.log('email sent!');
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@ -230,6 +341,7 @@ setTimeout(function () {
|
||||
|
||||
setTimeout(function () {
|
||||
// console.log('Pre loading trains...');
|
||||
module.exports.getTodayDate();
|
||||
module.exports.getHistory();
|
||||
|
||||
}, 25000);
|
||||
@ -253,4 +365,3 @@ cron.schedule('0 7 * * *', function () {
|
||||
return -1;
|
||||
});
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user