silvrgit/lib/today/history.js
2016-03-31 17:12:25 +01:00

160 lines
4.3 KiB
JavaScript

/**
* Created by Martin on 31/03/2016.
*/
var request = require('request'), cheerio = require('cheerio')
var STRING = require('string');
var logger = require('log4js').getLogger();
module.exports = {
getTechHistory: 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.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;
}
$ = 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);
});
//todayCache.data.history = todayCache.data.history.concat(output);
// logger.info(todayCache.data.history);
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, 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('');
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;
}
$ = 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);
}
});
return resolve(output);
// todayCache.data.history = todayCache.data.history.concat(output);
// logger.info(todayCache.data.history);
// module.getTechHistory();
}, 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);
});
})
}
};