Use nodeToText, fix padagali

This commit is contained in:
zvon 2020-09-16 17:04:19 +02:00
parent dd16942150
commit 24dea4b537
5 changed files with 31 additions and 15 deletions

View File

@ -21,7 +21,7 @@ void LunchRest::AlCaponeRestaurant::parse() {
int cur_day = -1; int cur_day = -1;
for(auto &row : rows) { for(auto &row : rows) {
if(row->find("./td[@class='bg1']").size() != 0) { if(row->find("./td[@class='bg1']").size() != 0) {
std::string day = dynamic_cast<const xmlpp::ContentNode *>(row->find("./td[@class='bg1']/text()")[0])->get_content(); std::string day = nodeToText(row->find("./td[@class='bg1']/text()")[0]);
if(day.find("pondělí") != std::string::npos) if(day.find("pondělí") != std::string::npos)
cur_day = 0; cur_day = 0;
else if(day.find("úterý") != std::string::npos) else if(day.find("úterý") != std::string::npos)
@ -39,13 +39,13 @@ void LunchRest::AlCaponeRestaurant::parse() {
continue; continue;
auto menu_info = row->find("./td/text()"); auto menu_info = row->find("./td/text()");
auto meal_data = row->find("./td/h3/text()"); auto meal_data = row->find("./td/h3/text()");
std::string menu = dynamic_cast<const xmlpp::ContentNode *>(menu_info[0])->get_content(); std::string menu = nodeToText(menu_info[0]);
std::string name = dynamic_cast<const xmlpp::ContentNode *>(meal_data[0])->get_content(); std::string name = nodeToText(meal_data[0]);
name = removeAlergens(name); name = removeAlergens(name);
if(menu == "Polévka") { if(menu == "Polévka") {
menus[cur_day].addMeal(true, name, "", 0); menus[cur_day].addMeal(true, name, "", 0);
} else { } else {
menus[cur_day].addMeal(false, name, "", std::stoi(dynamic_cast<const xmlpp::ContentNode *>(meal_data[1])->get_content())); menus[cur_day].addMeal(false, name, "", std::stoi(nodeToText(meal_data[1])));
} }
} }
} }

View File

@ -24,7 +24,7 @@ void LunchRest::LightOfIndiaRestaurant::parse() {
auto texts = container->find(".//td/text()"); auto texts = container->find(".//td/text()");
int index = -1; int index = -1;
for(auto text : texts) { for(auto text : texts) {
std::string text_text = dynamic_cast<const xmlpp::ContentNode *>(text)->get_content(); std::string text_text = nodeToText(text);
if(isWhiteSpaceOnly(text_text) || text_text.find("Week") != std::string::npos) if(isWhiteSpaceOnly(text_text) || text_text.find("Week") != std::string::npos)
continue; continue;
if(text_text[0] == '1') if(text_text[0] == '1')

View File

@ -2,8 +2,22 @@
#include "../network/network.hpp" #include "../network/network.hpp"
#include "../htmlparser.hpp" #include "../htmlparser.hpp"
int dayToNum(const std::string &day) {
if(day.find("POND") != std::string::npos) {
return 0;
} else if (day.find("ÚTER") != std::string::npos) {
return 1;
} else if (day.find("STŘE") != std::string::npos) {
return 2;
} else if (day.find("ČTVR") != std::string::npos) {
return 3;
} else if (day.find("PÁTE") != std::string::npos) {
return 4;
}
return -1;
}
void LunchRest::PadagaliRestaurant::parse() { void LunchRest::PadagaliRestaurant::parse() {
int menu_index = 0;
Request r; Request r;
auto html = r.get(_url); auto html = r.get(_url);
if(html == "") if(html == "")
@ -12,14 +26,17 @@ void LunchRest::PadagaliRestaurant::parse() {
HtmlParser hparse(html); HtmlParser hparse(html);
auto &root = hparse.getRoot(); auto &root = hparse.getRoot();
auto days = root.find("//div[@class='glf-mor-restaurant-menu-category']"); auto days = root.find("//div[@class='glf-mor-restaurant-menu-category']");
for(int i = 0; i < 5; i++) { auto menu_index = dayToNum(nodeToText(days[0]->find("./h3/text()")[0]));
if(menu_index == -1)
return;
for(int i = 0; i < 5-menu_index; i++) {
auto day = days[i]; auto day = days[i];
auto meals = day->find("./div"); auto meals = day->find("./div");
for(auto &meal : meals) { for(auto &meal : meals) {
auto info = meal->find("./div/div/div"); auto info = meal->find("./div/div/div");
std::string desc = dynamic_cast<const xmlpp::ContentNode *>(info[1]->find("./text()")[0])->get_content(); std::string desc = nodeToText(info[1]->find("./text()")[0]);
std::string name = dynamic_cast<const xmlpp::ContentNode *>(info[0]->find("./h5/text()")[0])->get_content(); std::string name = nodeToText(info[0]->find("./h5/text()")[0]);
int price = std::stoi(dynamic_cast<const xmlpp::ContentNode *>(info[0]->find("./div/text()")[0])->get_content()); int price = std::stoi(nodeToText(info[0]->find("./div/text()")[0]));
bool soup = name.find("Soup") == std::string::npos ? false : true; bool soup = name.find("Soup") == std::string::npos ? false : true;
menus[menu_index].addMeal(soup, name, desc, price); menus[menu_index].addMeal(soup, name, desc, price);
menus[menu_index].setInvalidMenu(false); menus[menu_index].setInvalidMenu(false);

View File

@ -17,7 +17,7 @@ void LunchRest::UDrevakaRestaurant::parse() {
for(auto meal : meals) { for(auto meal : meals) {
auto divs = meal->find(".//div/text()"); auto divs = meal->find(".//div/text()");
Meal meal_obj{}; Meal meal_obj{};
std::string name = dynamic_cast<const xmlpp::ContentNode *>(divs[0])->get_content();; std::string name = nodeToText(divs[0]);
auto soup_pos = name.find("Polévka"); auto soup_pos = name.find("Polévka");
if(soup_pos != std::string::npos) { if(soup_pos != std::string::npos) {
meal_obj.setSoup(); meal_obj.setSoup();
@ -26,8 +26,7 @@ void LunchRest::UDrevakaRestaurant::parse() {
meal_obj.setName(name.substr(3, name.find('(') - 4)); meal_obj.setName(name.substr(3, name.find('(') - 4));
} }
if(divs.size() > 1) { if(divs.size() > 1) {
std::string price = dynamic_cast<const xmlpp::ContentNode *>(divs[1])->get_content();; meal_obj.setPrice(std::stoi(nodeToText(divs[1])));
meal_obj.setPrice(std::stoi(price));
} }
menus[menu_index].addMeal(meal_obj); menus[menu_index].addMeal(meal_obj);
menus[menu_index].setInvalidMenu(false); menus[menu_index].setInvalidMenu(false);

View File

@ -19,7 +19,7 @@ void LunchRest::UKarlaRestaurant::parse() {
for(auto &meal : meals) { for(auto &meal : meals) {
auto soup = false; auto soup = false;
auto texts = meal->find("./div/text()"); auto texts = meal->find("./div/text()");
std::string name = dynamic_cast<const xmlpp::ContentNode *>(texts[0])->get_content(); std::string name = nodeToText(texts[0]);
if(name[0] == 'P') { if(name[0] == 'P') {
soup = true; soup = true;
name = name.substr(10); name = name.substr(10);
@ -28,7 +28,7 @@ void LunchRest::UKarlaRestaurant::parse() {
} }
int price = -1; int price = -1;
if(texts.size() > 1) if(texts.size() > 1)
price = std::stoi(dynamic_cast<const xmlpp::ContentNode *>(texts[1])->get_content()); price = std::stoi(nodeToText(texts[1]));
menus[menu_index].addMeal(soup, name, "", price); menus[menu_index].addMeal(soup, name, "", price);
menus[menu_index].setInvalidMenu(false); menus[menu_index].setInvalidMenu(false);
} }