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');
Array.prototype.random = function() {
Array.prototype.random = function () {
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",
"Alaska",
"Arizona",
"Arkansas",
"California",
"Colorado",
"Connecticut",
"Delaware",
"Florida",
"Georgia",
"Idaho",
"Illinois",
"Indiana",
"Iowa",
"Kansas",
"Kentucky",
"Louisiana",
"Maine",
"Maryland",
"Michigan",
"Minnesota",
"Missouri",
"Montana",
"Nebraska",
"Nevada",
"New Hampshire",
"New Jersey",
"Mexico",
"New York",
"Carolina",
"Dakota",
"Ohio",
"Oklahoma",
"Oregon",
"Palau",
"Pennsylvania",
"Tennessee",
"Texas",
"Utah",
"Vermont",
"Virginia",
"Washington",
"Wisconsin",
"Wyoming",
"Glasgow",
"Inverness",
"Edinburgh",
"Dumbarton",
"Balloch",
"Renton",
"Helensburgh",
"Cardross",
"Dundee",
"Aberdeen",
"Paisley",
"Hamilton",
"Greenock",
"Falkirk",
"Irvine",
"Clydebank",
"Renfrew",
"Barrhead",
"Erskine",
"London",
"Hammersmith",
"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();
};
var randomAmount = function(i) {
var randomAmount = function (i) {
var str = '';
for (var t = 0; t < i; t++) {
@ -94,11 +53,9 @@ var randomAmount = function(i) {
return str;
};
module.exports = {
generate: function(req, res) {
generate: function (req, res) {
var reply = {
long: (left.random() + ' ' + right.random() + ' ' + numberCluster() + ' ' + numberCluster()).split(' ').join(whitespace.random()),
short: randomAmount(10)

View File

@ -1,14 +1,15 @@
/**
* 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 todayCache = {
last: 0,
data: {
trains: {last: 0, data: []},
weather: {}
weather: {},
history: []
},
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){
if(!fn){return this.indexOfOld(e)}
else{
if(typeof fn ==='string'){var att=fn;fn=function(e){return e[att];}}
Array.prototype.indexOf = function (e, fn) {
if (!fn) {
return this.indexOfOld(e)
}
else {
if (typeof fn === 'string') {
var att = fn;
fn = function (e) {
return e[att];
}
}
return this.map(fn).indexOfOld(e);
}
};
@ -50,6 +58,36 @@ module.exports = {
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) {
console.log('Getting train events...');
var url = trainList[id].url;
@ -106,7 +144,7 @@ module.exports = {
updateTrains: function () {
console.log('Updating trains..');
todayCache.data.trains.data= [];
todayCache.data.trains.data = [];
module.exports.getTrainUpdates(0);
module.exports.getTrainUpdates(1);
@ -160,30 +198,35 @@ function sendEmail() {
subject: 'Today'
};
var template = { file: 'jade/today.jade', locals: todayCache };
mailer.sendTemplate(email, template, function(err){
if(err) throw err;
var template = {file: 'jade/today.jade', locals: todayCache};
mailer.sendTemplate(email, template, function (err) {
if (err) throw err;
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 () {
// console.log('Pre loading trains...');
module.exports.preLoadToday();
}, 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();
// console.log('tick');
return -1;
});