silvrgit/lib/today/history.js

153 lines
3.3 KiB
JavaScript
Raw Normal View History

const request = require('request');
const cheerio = require('cheerio');
const STRING = require('string');
const logger = require('log4js').getLogger('history');
2016-03-31 16:12:25 +00:00
module.exports = {
'getTechHistory': function() {
let url;
let d;
let day;
let month;
const monthNames = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
2016-03-31 22:00:47 +00:00
];
d = new Date();
month = monthNames[d.getMonth()];
day = d.getDate();
url = ['http://www.computerhistory.org/tdih/', month, '/', day].join('');
logger.debug(url);
2016-03-31 22:00:47 +00:00
return new Promise(function(resolve, reject) {
'use strict';
request(url, function(err, resp, body) {
if (err)
2016-03-31 22:00:47 +00:00
// Logger.error(err);
return reject(err);
// Throw err;
2016-03-31 22:00:47 +00:00
const $ = cheerio.load(body);
const tdihbody = $('#tdihbody .tdihevent');
const output = [];
2016-03-31 16:12:25 +00:00
tdihbody.find('p').each(function(div) {
const s = $(this).text();
2016-03-31 22:00:47 +00:00
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);
2016-03-31 22:00:47 +00:00
return reject(error);
}
});
});
}, 'getHistory': function() {
let url;
const d = new Date();
let day;
let month;
const monthNames = [
'january',
'february',
'march',
'april',
'may',
'june',
'july',
'august',
'september',
'october',
'november',
'december'
2016-03-31 22:00:47 +00:00
];
month = monthNames[d.getMonth()];
day = d.getDate();
url = [
'http://www.bbc.co.uk/scotland/history/onthisday/', month, '/',
day
2016-03-31 22:00:47 +00:00
].join('');
logger.debug(url);
return new Promise(function(resolve, reject) {
request(url, function(err, resp, body) {
if (err)
2016-03-31 22:00:47 +00:00
// Logger.error(err);
return reject(err);
// Throw err;
2016-03-31 22:00:47 +00:00
const $ = cheerio.load(body);
2016-03-31 22:00:47 +00:00
const nbody = $('DIV#bbcPageContent').first();
const output = [];
2016-03-31 16:12:25 +00:00
2016-03-31 22:00:47 +00:00
nbody.find('.story > p').each(function(div) {
const s = $(this).text();
if (s.indexOf('Today\'s recipe:') == -1)
2016-03-31 22:00:47 +00:00
output.push(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);
2016-03-31 22:00:47 +00:00
return reject(error);
}
});
});
},
'updateHistory': function() {
2016-03-31 22:00:47 +00:00
'use strict';
let output = [];
2016-03-31 22:00:47 +00:00
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);
});
});
2016-03-31 22:00:47 +00:00
}
};