Skip to content

Commit

Permalink
alfa parsing updated (without tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
yohny committed Mar 28, 2017
1 parent 993c4d8 commit bd7d84b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2,788 deletions.
49 changes: 30 additions & 19 deletions parsers/alfa.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,52 @@ module.exports.parse = function(html, date, callback) {
var $ = cheerio.load(html);
var dayMenu = [];

var soupPattern = /^0[\.,]\d+\s?l/;
var dateStr = date.format("DD.MM.YYYY");

var denneMenu = $('.dnesne_menu, .ostatne_menu').filter(function(){
var denneMenuElem = $('.dnesne_menu, .ostatne_menu').filter(function(){
var nadpis = $(this).find('h2').text();
return nadpis.indexOf(dateStr) > -1;
});

denneMenu.first().find('.jedlo_polozka').each(function() {
if($(this).find('.left>b').length === 0) {
dayMenu.push(this);
}
});
var soupElems = [];
var mealElems = [];
var pushingSoups = false;
denneMenuElem.first().find('.jedlo_polozka').each(function() {
var elem = $(this);
var txt = elem.text().trim();

// assumption - first one is soup
var soupsElement = dayMenu[0];
var soupNames = normalize($('.left', soupsElement).text().trim()).split(',');
var soupPrice = parseFloat($('.right', soupsElement).text());
if(txt === "Polievka"){
pushingSoups = true;
return;
}
if(txt === "Hlavné jedlá"){
pushingSoups = false;
return;
}

var soupMenu = soupNames.map(function(item) { return { isSoup: true, text: item.trim(), price: soupPrice }; });
if(pushingSoups) {
soupElems.push(elem);
}
else {
mealElems.push(elem);
}
});

//convert to menu item object
dayMenu = dayMenu.slice(1).map(function(item) {
var label = $('.left', item).text();
var price = $('.right', item).text();
return { isSoup: soupPattern.test(label.trim()), text: normalize(label), price: parseFloat(price) };
soupElems.forEach((elem) => {
var text = normalize($('.left', elem).text());
dayMenu.push({ isSoup: true, text: text, price: NaN });
});

dayMenu = soupMenu.concat(dayMenu);
mealElems.forEach((elem) => {
var text = normalize($('.left', elem).text());
var price = parseFloat($('.right', elem).text().trim());
dayMenu.push({ isSoup: false, text: text, price: price });
});

callback(dayMenu);

function normalize(str) {
return str.normalizeWhitespace()
.replace(soupPattern, '')
.removeMetrics()
.correctCommaSpacing()
.removeItemNumbering();
Expand Down
115 changes: 0 additions & 115 deletions test/alfa.js

This file was deleted.

Loading

0 comments on commit bd7d84b

Please sign in to comment.