added quotes to today

This commit is contained in:
Martin Donnelly 2016-10-23 23:52:59 +01:00
parent 2f79a85abf
commit c253462bc7
6 changed files with 111 additions and 59 deletions

View File

@ -71,6 +71,11 @@ html(lang="en")
p It is an #{data.swedish.xml.words.wordtype} which means '<em>#{data.swedish.xml.words.translation}</em>'.
p Example: #{data.swedish.xml.words.fnphrase}
p Translated: #{data.swedish.xml.words.enphrase}
.quotes
h2 Quote
p #{data.quotes.quote} <em>#{data.quotes.author}</em>
.history
h2 Today in history
if data.history.length > 0

File diff suppressed because one or more lines are too long

View File

@ -16,6 +16,7 @@ var history = require('./today/history');
var mdMailer = require('./today/mailer');
var mdFitbit = require('./today/fitbit');
var todayFTSE = require('./today/todayftse');
var quotes = require('./today/quotes');
var db_name = 'silvrgit';
var dbCouch = nano.use(db_name);
@ -24,19 +25,19 @@ require('sugar-date');
String.prototype.hashCode = function() {
if (Array.prototype.reduce) {
return this.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0);
return this.split('').reduce(function(a,b) {a = ((a << 5) - a) + b.charCodeAt(0);return a & a},0);
} else {
var hash = 0, i, chr, len;
if (this.length == 0) return hash;
for (i = 0, len = this.length; i < len; i++) {
var hash = 0, i, chr, len;
if (this.length == 0) return hash;
for (i = 0, len = this.length; i < len; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
}
return hash;
}
};
};
var todayCache = {
last: 0, data: {
@ -62,8 +63,7 @@ function runable() {
console.log('last updated', ((now - todayCache.last) / 60000));
if (now - todayCache.last < 3600000) {
return false;
}
else {
} else {
todayCache.last = now;
return true;
}
@ -141,24 +141,23 @@ function breakDay() {
};
}
function reduceTrains(d){
function reduceTrains(d) {
var titles = [], ta = [];
var titles = [], ta = [];
console.log('reducetrains',d);
for (var items in d) {
if (typeof d[items].title !== 'undefined') {
var hash = d[items].title.hashCode();
if (titles.indexOf(hash) === -1)
{
titles.push(hash);
ta.push(d[items]);
}
for (var items in d) {
if (typeof d[items].title !== 'undefined') {
var hash = d[items].title.hashCode();
if (titles.indexOf(hash) === -1) {
titles.push(hash);
ta.push(d[items]);
}
}
}
}
return ta;
}
return ta;
}
/**
* @return {number}
@ -179,8 +178,7 @@ Array.prototype.indexOfOld = Array.prototype.indexOf;
Array.prototype.indexOf = function(e, fn) {
if (!fn) {
return this.indexOfOld(e);
}
else {
} else {
if (typeof fn === 'string') {
var att = fn;
fn = function(e) {
@ -221,7 +219,7 @@ module.exports = {
logger.debug(s);
todayCache.data.today = s;
},
refreshTrain: function(){
refreshTrain: function() {
var self = this;
trains.updateTrains()
.then((d) => {
@ -241,7 +239,7 @@ module.exports = {
},
refreshWeather: function(){
refreshWeather: function() {
weather.newDoGetWeather()
.then((d) => {
todayCache.data.weather = d;
@ -256,29 +254,6 @@ module.exports = {
refreshTrainAndWeather: function() {
this.refreshTrain();
this.refreshWeather();
/*
var self = this;
weather.newDoGetWeather()
.then((d) => {
todayCache.data.weather = d;
console.log('Updating weather');
broadcastWeather();
}).catch((e) => {
logger.error(e);
});
trains.updateTrains()
.then((d) => {
'use strict';
console.log('Trains: ', d);
eventEmitter.emit('sendSocket', {id: 'trains', data: d});
todayCache.data.trains.data = d;
todayCache.data.trains.last = new Date();
})
.catch((e)=> {
'use strict';
console.error(e);
});
*/
}, preLoadToday: function() {
module.exports.getTodayDate();
todayCache.data.cal = {today: [], tomorrow: [], week: []};
@ -330,6 +305,16 @@ module.exports = {
logger.error(e);
});
quotes.GetQuotes()
.then((d) => {
todayCache.data.quotes = d;
})
.catch((e)=> {
logger.error(e);
});
for (var t = 0; t < calHandler.calendars.length; t++) {
calHandler.getAdvancedCalV3(calHandler.calendars[t])
.then((d) => {
@ -365,7 +350,7 @@ module.exports = {
});
todayCache.date = breakDay();
}, broadcast : function() {
}, broadcast: function() {
console.log('BROADCAST');
broadcastWeather();
eventEmitter.emit('sendSocket', {id: 'trains', data: todayCache.data.trains.data});
@ -378,7 +363,7 @@ setTimeout(function() {
if (runable()) {
module.exports.preLoadToday();
}
//module.exports.preLoadToday();
//Module.exports.preLoadToday();
}, 5000);
@ -390,9 +375,9 @@ setTimeout(function() {
setInterval(function() {
// eventEmitter.emit('sendSocket',{id:'weather',data:todayCache.data.weather});
// broadcastWeather();
// eventEmitter.emit('sendSocket', {id: 'trains', data: todayCache.data.trains.data});
// EventEmitter.emit('sendSocket',{id:'weather',data:todayCache.data.weather});
// broadcastWeather();
// eventEmitter.emit('sendSocket', {id: 'trains', data: todayCache.data.trains.data});
}, (60000));
@ -407,8 +392,8 @@ cron.schedule('45 6 * * *', function() {
});
cron.schedule('0 */1 * * *', function() {
// module.exports.refreshTrainAndWeather();
// this.refreshTrain();
// Module.exports.refreshTrainAndWeather();
// this.refreshTrain();
module.exports.refreshWeather();
return -1;
@ -417,7 +402,7 @@ cron.schedule('0 */1 * * *', function() {
cron.schedule('*/15 * * * *', function() {
module.exports.refreshTrain();
// module.exports.refreshWeather();
// Module.exports.refreshWeather();
return -1;
});

View File

@ -141,7 +141,7 @@ module.exports = {
.then((d) => {
output = output.concat(d);
return resolve(output)
return resolve(output);
})
.catch((e) => {
logger.error(e);

43
lib/today/quotes.js Normal file
View File

@ -0,0 +1,43 @@
/**
* Created by Martin on 31/03/2016.
*/
var https = require('https');
var STRING = require('string');
var logger = require('log4js').getLogger();
var options = {
host: 'andruxnet-random-famous-quotes.p.mashape.com',
path: '/?cat=famous',
headers: {
accept: "application/json",
"X-Mashape-Key": "5A0H980jK6mshSFL24ZmfiRrNHV2p1d1fhQjsngtx8QWuO9oe4",
'Content-Type': "application/x-www-form-urlencoded"
},
method: 'GET'
};
module.exports = {
GetQuotes: function() {
'use strict';
return new Promise(function(resolve, reject) {
https.request(options).on('response', function (response) {
var data = '';
response.on("data", function (chunk) {
data += chunk;
});
response.on('end', function () {
//console.log(data);
//callback(JSON.parse(data));
resolve(JSON.parse(data));
});
}).end();
});
}
};

19
lib/today/test.js Normal file
View File

@ -0,0 +1,19 @@
/**
*
* User: Martin Donnelly
* Date: 2016-10-23
* Time: 23:22
*
*/
var quotes = require('./quotes');
quotes.GetQuotes()
.then((d) => {
'use strict';
console.log('Quotes: ', d);
})
.catch((e)=> {
'use strict';
console.error(e);
});