This commit is contained in:
Martin Donnelly 2016-02-11 14:48:05 +00:00
parent 881f366337
commit 9fcfc7ac05
2 changed files with 68 additions and 68 deletions

View File

@ -1,6 +1,6 @@
var http = require('http'); var http = require('http');
Array.prototype.random = function() { Array.prototype.random = function () {
return this[Math.floor((Math.random() * this.length))]; return this[Math.floor((Math.random() * this.length))];
}; };
@ -10,81 +10,40 @@ var numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
var left = ["Alabama", var left = ["Alabama",
"Alaska", "Alaska",
"Arizona", "Arizona",
"Arkansas",
"California",
"Colorado",
"Connecticut",
"Delaware",
"Florida",
"Georgia",
"Idaho",
"Illinois",
"Indiana",
"Iowa",
"Kansas",
"Kentucky",
"Louisiana",
"Maine",
"Maryland", "Maryland",
"Michigan",
"Minnesota",
"Missouri",
"Montana",
"Nebraska",
"Nevada", "Nevada",
"New Hampshire",
"New Jersey",
"Mexico", "Mexico",
"New York",
"Carolina",
"Dakota",
"Ohio",
"Oklahoma",
"Oregon",
"Palau",
"Pennsylvania",
"Tennessee",
"Texas", "Texas",
"Utah", "Utah",
"Vermont",
"Virginia",
"Washington",
"Wisconsin",
"Wyoming",
"Glasgow", "Glasgow",
"Inverness", "Inverness",
"Edinburgh", "Edinburgh",
"Dumbarton", "Dumbarton",
"Balloch", "Balloch",
"Renton", "Renton",
"Helensburgh",
"Cardross", "Cardross",
"Dundee", "Dundee",
"Aberdeen",
"Paisley", "Paisley",
"Hamilton", "Hamilton",
"Greenock", "Greenock",
"Falkirk", "Falkirk",
"Irvine", "Irvine",
"Clydebank",
"Renfrew", "Renfrew",
"Barrhead",
"Erskine", "Erskine",
"London", "London",
"Hammersmith", "Hammersmith",
"Islington", "Islington",
"Silver", "Black", "Yellow", "Purple" "Silver", "Black", "Yellow", "Purple", 'White', 'Pink', 'Red', 'Orange','Brown','Green','Blue','Amber','Aqua','Azure','Bronze','Coral','Copper','Crimson','Cyan','Ginger','Gold','Indigo','Jade'
]; ];
var right = ['Aganju', 'Cygni', 'Akeron', 'Antares', 'Aragoth', 'Arduinne', 'Ardus', 'Aquitaine', 'Carpenter', 'Beta Hydri', 'Cooper', 'Dahin', 'Capella', 'Endriago', 'Gallina', 'Fenris', 'Freya', 'Glenn', 'Grissom', 'Tau Ceti', 'Jotunheim', 'Kailaasa', 'Lagarto', 'Muspelheim', 'Nifleheim', 'Odin Rex', 'Primus', 'Vega', 'Ragnarok', 'Shepard', 'Slayton', 'Tarsis', 'Mercury', 'Venus', 'Mars', 'Earth', 'Terra', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto', 'Europa', 'Ganymede', 'Callisto', 'Titan', 'Juno', 'Eridanus', 'Cassiopeia', 'Scorpius', 'Crux', 'Cancer', 'Taurus', 'Lyra', 'Andromeda', 'Virgo', 'Aquarius', 'Cygnus', 'Corvus', 'Taurus', 'Draco', 'Perseus', 'Pegasus', 'Gemini', 'Columbia', 'Bootes', 'Orion', 'Deneb']; var right = ['Aganju', 'Cygni', 'Akeron', 'Antares', 'Aragoth', 'Ardus', 'Carpenter', 'Cooper', 'Dahin', 'Capella', 'Endriago', 'Gallina', 'Fenris', 'Freya', 'Glenn', 'Grissom', 'Jotunheim', 'Kailaasa', 'Lagarto', 'Muspelheim', 'Nifleheim', 'Primus', 'Vega', 'Ragnarok', 'Shepard', 'Slayton', 'Tarsis', 'Mercury', 'Venus', 'Mars', 'Earth', 'Terra', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto', 'Europa', 'Ganymede', 'Callisto', 'Titan', 'Juno', 'Eridanus', 'Scorpius', 'Crux', 'Cancer', 'Taurus', 'Lyra', 'Andromeda', 'Virgo', 'Aquarius', 'Cygnus', 'Corvus', 'Taurus', 'Draco', 'Perseus', 'Pegasus', 'Gemini', 'Columbia', 'Bootes', 'Orion', 'Deneb', 'Merope'];
var numberCluster = function () {
var numberCluster = function() {
return numbers.random() + numbers.random() + numbers.random(); return numbers.random() + numbers.random() + numbers.random();
}; };
var randomAmount = function(i) { var randomAmount = function (i) {
var str = ''; var str = '';
for (var t = 0; t < i; t++) { for (var t = 0; t < i; t++) {
@ -94,11 +53,9 @@ var randomAmount = function(i) {
return str; return str;
}; };
module.exports = { module.exports = {
generate: function (req, res) {
generate: function(req, res) {
var reply = { var reply = {
long: (left.random() + ' ' + right.random() + ' ' + numberCluster() + ' ' + numberCluster()).split(' ').join(whitespace.random()), long: (left.random() + ' ' + right.random() + ' ' + numberCluster() + ' ' + numberCluster()).split(' ').join(whitespace.random()),
short: randomAmount(10) short: randomAmount(10)

View File

@ -1,14 +1,15 @@
/** /**
* Created by marti on 30/01/2016. * 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 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 jade = require('jade'), _ = require('lodash');
var todayCache = { var todayCache = {
last: 0, last: 0,
data: { data: {
trains: {last: 0, data: []}, trains: {last: 0, data: []},
weather: {} weather: {},
history: []
}, },
expire: ((60 * 1000) * 60) * 1 expire: ((60 * 1000) * 60) * 1
}; };
@ -35,12 +36,19 @@ var mailer = new UltraSES({
} }
}); });
Array.prototype.indexOfOld=Array.prototype.indexOf Array.prototype.indexOfOld = Array.prototype.indexOf
Array.prototype.indexOf=function(e,fn){ Array.prototype.indexOf = function (e, fn) {
if(!fn){return this.indexOfOld(e)} if (!fn) {
else{ return this.indexOfOld(e)
if(typeof fn ==='string'){var att=fn;fn=function(e){return e[att];}} }
else {
if (typeof fn === 'string') {
var att = fn;
fn = function (e) {
return e[att];
}
}
return this.map(fn).indexOfOld(e); return this.map(fn).indexOfOld(e);
} }
}; };
@ -50,6 +58,36 @@ module.exports = {
res.render('pages/today', todayCache); res.render('pages/today', todayCache);
}, },
getHistory: 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.bbc.co.uk/scotland/history/onthisday/', month, '/', day].join('');
request(url, function (err, resp, body) {
if (err)
throw err;
$ = cheerio.load(body);
var body = $('DIV#bbcPageContent').first();
var output = [];
body.find('.story > p').each(function (div) {
var s = $(this).text();
if (s.indexOf('Today\'s recipe:') == -1) {
output.push(s);
}
});
todayCache.data.history = output;
});
},
getTrainUpdates: function (id) { getTrainUpdates: function (id) {
console.log('Getting train events...'); console.log('Getting train events...');
var url = trainList[id].url; var url = trainList[id].url;
@ -106,7 +144,7 @@ module.exports = {
updateTrains: function () { updateTrains: function () {
console.log('Updating trains..'); console.log('Updating trains..');
todayCache.data.trains.data= []; todayCache.data.trains.data = [];
module.exports.getTrainUpdates(0); module.exports.getTrainUpdates(0);
module.exports.getTrainUpdates(1); module.exports.getTrainUpdates(1);
@ -160,30 +198,35 @@ function sendEmail() {
subject: 'Today' subject: 'Today'
}; };
var template = {file: 'jade/today.jade', locals: todayCache};
var template = { file: 'jade/today.jade', locals: todayCache }; 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');
}); });
/* /*
var fn = jade.compileFile(template.file); var fn = jade.compileFile(template.file);
console.log(fn(todayCache)); console.log(fn(todayCache));
*/ */
} }
setTimeout(function () { setTimeout(function () {
// console.log('Pre loading trains...'); // console.log('Pre loading trains...');
module.exports.preLoadToday(); module.exports.preLoadToday();
}, 15000); }, 15000);
cron.schedule('1 00 7 * * *', function(){ cron.schedule('1 45 6 * * *', function () {
module.exports.getHistory();
return -1;
});
cron.schedule('1 00 7 * * *', function () {
sendEmail(); sendEmail();
// console.log('tick'); // console.log('tick');
return -1; return -1;
}); });