Fix udrevaka and ukarla

This commit is contained in:
zv0n 2021-02-09 23:23:37 +01:00
parent b03fceb3dd
commit 560cd28e78
6 changed files with 25 additions and 10 deletions

View File

@ -99,9 +99,9 @@ int main(int argc, char **argv, char **env) {
restaurants["lightofindia"] = std::make_unique<LunchRest::LightOfIndiaRestaurant>(); restaurants["lightofindia"] = std::make_unique<LunchRest::LightOfIndiaRestaurant>();
restaurants["ukarla"] = std::make_unique<LunchRest::UKarlaRestaurant>(); restaurants["ukarla"] = std::make_unique<LunchRest::UKarlaRestaurant>();
restaurants["alcapone"] = std::make_unique<LunchRest::AlCaponeRestaurant>(); restaurants["alcapone"] = std::make_unique<LunchRest::AlCaponeRestaurant>();
restaurants["plac"] = std::make_unique<LunchRest::PlacRestaurant>(); // restaurants["plac"] = std::make_unique<LunchRest::PlacRestaurant>();
restaurants["zo"] = std::make_unique<LunchRest::ZoRestaurant>(); restaurants["zo"] = std::make_unique<LunchRest::ZoRestaurant>();
restaurants["suzzies"] = std::make_unique<LunchRest::SuzziesRestaurant>(); // restaurants["suzzies"] = std::make_unique<LunchRest::SuzziesRestaurant>();
std::cout << "Initial parsing" << std::endl; std::cout << "Initial parsing" << std::endl;
for(auto &restaurant : restaurants) for(auto &restaurant : restaurants)
restaurant.second->parse(); restaurant.second->parse();

View File

@ -20,7 +20,9 @@ void LunchRest::PlacRestaurant::parse() {
clearMenus(); clearMenus();
HtmlParser hparse(html); HtmlParser hparse(html);
auto &root = hparse.getRoot(); auto &root = hparse.getRoot();
auto pizzas = root.find("//div[@class='section-inner']//div[@class='mt-i cf']"); auto pizzas = root.find("//div[@class='mt-c cf']//div[@class='mt-i cf']");
if(pizzas.size() == 0)
return;
auto soups = pizzas[0]; auto soups = pizzas[0];
int soup_price = std::stoi(nodeToText(pizzas[1]->find(".//strong/text()")[0])); int soup_price = std::stoi(nodeToText(pizzas[1]->find(".//strong/text()")[0]));

View File

@ -3,3 +3,15 @@
std::string LunchRest::nodeToText(xmlpp::Node *node) { std::string LunchRest::nodeToText(xmlpp::Node *node) {
return dynamic_cast<const xmlpp::ContentNode *>(node)->get_content(); return dynamic_cast<const xmlpp::ContentNode *>(node)->get_content();
} }
std::string LunchRest::trim(const std::string &input) {
const std::string whitespace = "\t\n\v\f\r ";
std::string ret = input;
ret.erase(0, ret.find_first_not_of(whitespace));
ret.erase(ret.find_last_not_of(whitespace) + 1);
for(size_t i = 0; i < ret.length(); i++) {
if(ret[i] == '\n')
ret[i] = ' ';
}
return ret;
}

View File

@ -3,9 +3,10 @@
namespace LunchRest { namespace LunchRest {
std::string nodeToText(xmlpp::Node *node); std::string nodeToText(xmlpp::Node *node);
std::string trim(const std::string &input);
class UDrevakaRestaurant : public Restaurant { class UDrevakaRestaurant : public Restaurant {
public: public:
UDrevakaRestaurant() : Restaurant("https://www.udrevaka.cz/denni-menu/", "U Dřeváka") {} UDrevakaRestaurant() : Restaurant("https://udrevaka.cz/denni-menu/", "U Dřeváka") {}
virtual ~UDrevakaRestaurant() = default; virtual ~UDrevakaRestaurant() = default;
virtual void parse() override; virtual void parse() override;
}; };
@ -23,7 +24,7 @@ public:
}; };
class UKarlaRestaurant : public Restaurant { class UKarlaRestaurant : public Restaurant {
public: public:
UKarlaRestaurant() : Restaurant("https://www.ukarlabrno.cz/denni-menu/", "U Karla") {} UKarlaRestaurant() : Restaurant("https://ukarlabrno.cz/denni-menu/", "U Karla") {}
virtual ~UKarlaRestaurant() = default; virtual ~UKarlaRestaurant() = default;
virtual void parse() override; virtual void parse() override;
}; };

View File

@ -14,10 +14,10 @@ void LunchRest::UDrevakaRestaurant::parse() {
auto days = root.find("//li[@class='item-day']"); auto days = root.find("//li[@class='item-day']");
for(auto &day : days) { for(auto &day : days) {
auto meals = day->find("./div[@class='row']"); auto meals = day->find("./div[@class='row']");
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 = nodeToText(divs[0]); std::string name = trim(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,7 +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) {
meal_obj.setPrice(std::stoi(nodeToText(divs[1]))); meal_obj.setPrice(std::stoi(trim(nodeToText(divs[1]))));
} }
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 = nodeToText(texts[0]); std::string name = trim(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(nodeToText(texts[1])); price = std::stoi(trim(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);
} }