74 lines
2.1 KiB
C++
74 lines
2.1 KiB
C++
#include "menicka.hpp"
|
|
#include "../network/network.hpp"
|
|
#include "../htmlparser.hpp"
|
|
#include "functions.hpp"
|
|
|
|
std::pair<std::string, int> getNameAndPrice(xmlpp::Node *meal) {
|
|
auto souptext = meal->find("./div[@class='polozka']/text()");
|
|
auto soupprice = meal->find("./div[@class='cena']/text()");
|
|
std::string name = "";
|
|
int price = -1;
|
|
if(souptext.size() != 0) {
|
|
name = LunchRest::nodeToText(souptext[0]);
|
|
}
|
|
if(soupprice.size() != 0) {
|
|
price = std::stoi(LunchRest::nodeToText(soupprice[0]));
|
|
}
|
|
|
|
return {name, price};
|
|
}
|
|
|
|
void LunchRest::MenickaRestaurant::parse() {
|
|
Request r;
|
|
auto html = r.get(_url);
|
|
if(html == "")
|
|
return;
|
|
clearMenus();
|
|
HtmlParser hparse(html);
|
|
auto &root = hparse.getRoot();
|
|
auto days = root.find("//div[@class='menicka']");
|
|
for(auto &day : days) {
|
|
auto daytext = day->find("./div[@class='nadpis']/text()");
|
|
auto soup = day->find("./ul/li[@class='polevka']");
|
|
auto meals = day->find("./ul/li[@class='jidlo']");
|
|
if(daytext.size() == 0) {
|
|
continue;
|
|
}
|
|
|
|
auto daystr = nodeToText(daytext[0]);
|
|
int cur_day = 0;
|
|
switch (daystr[0]) {
|
|
case 'P':
|
|
if (daystr[1] != 'o') {
|
|
cur_day = 4;
|
|
}
|
|
break;
|
|
case "Ú"[0]:
|
|
cur_day = 1;
|
|
break;
|
|
case 'S':
|
|
cur_day = 2;
|
|
break;
|
|
case "Č"[0]:
|
|
cur_day = 3;
|
|
default:
|
|
break;
|
|
}
|
|
if(soup.size() != 0) {
|
|
auto soupData = getNameAndPrice(soup[0]);
|
|
if(!soupData.first.empty()) {
|
|
menus[cur_day].addMeal(Meal(true, soupData.first, "", soupData.second));
|
|
}
|
|
}
|
|
for(auto &meal : meals) {
|
|
auto mealData = getNameAndPrice(meal);
|
|
if(!mealData.first.empty()) {
|
|
menus[cur_day].addMeal(Meal(false, mealData.first, "", mealData.second));
|
|
}
|
|
}
|
|
if(!menus[cur_day].getMeals().empty()) {
|
|
menus[cur_day].setInvalidMenu(false);
|
|
}
|
|
}
|
|
}
|