silvrgit/lib/today/history.js
2016-03-31 23:00:47 +01:00

159 lines
3.4 KiB
JavaScript

var request = require('request');
var cheerio = require('cheerio');
var STRING = require('string');
var logger = require('log4js').getLogger();
module.exports = {
getTechHistory: function() {
var url;
var d;
var day;
var month;
var 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.debug(url);
return new Promise(function(resolve, reject) {
'use strict';
request(url, function(err, resp, body) {
if (err) {
// Logger.error(err);
return reject(err);
// Throw err;
}
var $ = cheerio.load(body);
var tdihbody = $('#tdihbody');
var output = [];
tdihbody.find('.tdihevent > p').each(function(div) {
var s = $(this).text();
output.push(STRING(s).collapseWhitespace().s);
});
return resolve(output);
}, function(error, response, body) {
if (response.statusCode !== 200) {
logger.error(response.statusCode);
logger.error(body);
return reject(error);
}
});
});
}, getHistory: function() {
var url;
var d = new Date();
var day;
var month;
var monthNames = [
'january',
'february',
'march',
'april',
'may',
'june',
'july',
'august',
'september',
'october',
'november',
'december'
];
month = monthNames[d.getMonth()];
day = d.getDate();
url = [
'http://www.bbc.co.uk/scotland/history/onthisday/', month, '/',
day
].join('');
logger.debug(url);
return new Promise(function(resolve, reject) {
request(url, function(err, resp, body) {
if (err) {
// Logger.error(err);
return reject(err);
// Throw err;
}
var $ = cheerio.load(body);
var nbody = $('DIV#bbcPageContent').first();
var output = [];
nbody.find('.story > p').each(function(div) {
var s = $(this).text();
if (s.indexOf('Today\'s recipe:') == -1) {
output.push(s);
}
});
return resolve(output);
}, function(error, response, body) {
if (response.statusCode !== 200) {
logger.error(response.statusCode);
logger.error(body);
return reject(error);
}
});
});
},
updateHistory: function() {
'use strict';
var output = [];
return new Promise(function(resolve, reject) {
module.exports.getHistory()
.then((d)=> {
output = d;
module.exports.getTechHistory()
.then((d) => {
output = output.concat(d);
return resolve(output)
})
.catch((e) => {
logger.error(e);
return reject(e);
});
})
.catch((e) => {
logger.error(e);
return reject(e);
});
})
}
};