silvrgit/lib/today/history.js
2017-10-13 10:39:58 +01:00

153 lines
3.3 KiB
JavaScript

const request = require('request');
const cheerio = require('cheerio');
const STRING = require('string');
const logger = require('log4js').getLogger('history');
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'
];
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;
const $ = cheerio.load(body);
const tdihbody = $('#tdihbody .tdihevent');
const output = [];
tdihbody.find('p').each(function(div) {
const 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() {
let url;
const d = new Date();
let day;
let month;
const 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;
const $ = cheerio.load(body);
const nbody = $('DIV#bbcPageContent').first();
const output = [];
nbody.find('.story > p').each(function(div) {
const 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';
let 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);
});
});
}
};