silvrgit/lib/today/history.js

159 lines
3.4 KiB
JavaScript
Raw Normal View History

2016-03-31 22:00:47 +00:00
var request = require('request');
var cheerio = require('cheerio');
2016-03-31 16:12:25 +00:00
var STRING = require('string');
var logger = require('log4js').getLogger();
module.exports = {
2016-03-31 22:00:47 +00:00
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');
2016-03-31 16:12:25 +00:00
2016-03-31 22:00:47 +00:00
var output = [];
2016-03-31 16:12:25 +00:00
2016-03-31 22:00:47 +00:00
tdihbody.find('.tdihevent > p').each(function(div) {
var s = $(this).text();
output.push(STRING(s).collapseWhitespace().s);
});
2016-03-31 16:12:25 +00:00
2016-03-31 22:00:47 +00:00
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 = [];
2016-03-31 16:12:25 +00:00
2016-03-31 22:00:47 +00:00
nbody.find('.story > p').each(function(div) {
2016-03-31 16:12:25 +00:00
2016-03-31 22:00:47 +00:00
var s = $(this).text();
if (s.indexOf('Today\'s recipe:') == -1) {
output.push(s);
}
});
2016-03-31 16:12:25 +00:00
2016-03-31 22:00:47 +00:00
return resolve(output);
2016-03-31 16:12:25 +00:00
2016-03-31 22:00:47 +00:00
}, function(error, response, body) {
if (response.statusCode !== 200) {
logger.error(response.statusCode);
logger.error(body);
return reject(error);
}
});
2016-03-31 16:12:25 +00:00
2016-03-31 22:00:47 +00:00
});
2016-03-31 16:12:25 +00:00
2016-03-31 22:00:47 +00:00
},
updateHistory: function() {
'use strict';
var output = [];
return new Promise(function(resolve, reject) {
module.exports.getHistory()
2016-03-31 16:12:25 +00:00
.then((d)=> {
2016-03-31 22:00:47 +00:00
output = d;
2016-03-31 16:12:25 +00:00
2016-03-31 22:00:47 +00:00
module.exports.getTechHistory()
2016-03-31 16:12:25 +00:00
.then((d) => {
2016-03-31 22:00:47 +00:00
output = output.concat(d);
2016-03-31 16:12:25 +00:00
2016-03-31 22:00:47 +00:00
return resolve(output)
2016-03-31 16:12:25 +00:00
})
.catch((e) => {
2016-03-31 22:00:47 +00:00
logger.error(e);
return reject(e);
2016-03-31 16:12:25 +00:00
});
})
.catch((e) => {
2016-03-31 22:00:47 +00:00
logger.error(e);
return reject(e);
2016-03-31 16:12:25 +00:00
});
2016-03-31 22:00:47 +00:00
})
}
2016-03-31 16:12:25 +00:00
};