@democracy-deutschland/bt-named-polls
Version:
scraper for the german "Bundestag" (Namentliche Abstimmungen)
71 lines (57 loc) • 2.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
const request = require('request');
// fix Date.parse
const MONTH = [{ string: 'März', fix: 'March' }, { string: 'Mai', fix: 'May' }, { string: 'Oktober', fix: 'October' }, { string: 'Dezember', fix: 'December' }];
class Browser {
constructor() {
this.cookie = null;
this.initialize = uri => this.request(_extends({}, this.defReqOpt, {
uri
}));
this.request = opts => {
const reqOptions = _extends({
timeout: 15000,
method: 'GET',
jar: this.cookie
}, opts);
return new Promise((resolve, reject) => {
request(reqOptions, (error, res, body) => {
if (!error && res.statusCode === 200) {
resolve({ res, body });
} else {
reject(error);
}
});
});
};
this.getHead = body => {
const [, head] = body.match(/<article class="bt-artikel col-xs-12 bt-standard-content">(.*?)<\/article>/s);
let [, date] = head.match(/<span class="bt-dachzeile">(.*?)<\/span>/s);
const [, title] = head.match(/<br\/>(.*?)<\/h3>/s);
const documentsData = head.match(/<i class="icon-doc"><\/i>(\d{1,3}\/\d{1,10})<\/a>/g);
MONTH.forEach(({ string, fix }) => {
date = date.replace(`${string}`, `${fix}`);
});
let documents = [];
if (documentsData) {
documents = documentsData.map(doc => doc.replace('<i class="icon-doc"></i>', '').replace('</a>', ''));
}
return { date: new Date(Date.parse(date)), title, documents };
};
this.getPartyVotes = body => {
const partyVotesHtml = body.match(/<div class="col-xs-12 col-sm-3">(.*?)<\/a>\s*<\/div>/gs);
const partyVotes = partyVotesHtml.reduce((prev, html) => {
const [, party] = html.match(/<h4 class="bt-chart-fraktion">(.*?)<br\/>/s);
const voteResults = html.match(/<li class="bt-legend-(?:ja|nein|enthalten|na)">(.*?)<\/li>/gs).map(voteResultHtml => voteResultHtml.match(/>(.*?)</)[1].split(' ')).reduce((prev, [votes, decision]) => _extends({}, prev, { [decision]: votes }), {});
return _extends({}, prev, { [party]: voteResults });
}, {});
return partyVotes;
};
this.cookie = request.jar();
}
}
exports.default = Browser;