lunch-rest/restaurants/plac.cpp

59 lines
1.9 KiB
C++
Raw Normal View History

2020-09-16 09:20:27 +00:00
#include "restaurants.hpp"
#include "../network/network.hpp"
#include "../htmlparser.hpp"
void trim(std::string &str) {
int start = 0;
int end = str.length() - 1;
while(std::isspace(str[start]))
start++;
while(std::isspace(str[end]))
end--;
str = str.substr(start, end - start);
}
void LunchRest::PlacRestaurant::parse() {
Request r;
auto html = r.get(_url);
if(html == "")
return;
clearMenus();
HtmlParser hparse(html);
auto &root = hparse.getRoot();
auto pizzas = root.find("//div[@class='section-inner']//div[@class='mt-i cf']");
auto soups = pizzas[0];
int soup_price = std::stoi(nodeToText(pizzas[1]->find(".//strong/text()")[0]));
int cur_day = -1;
for(auto &soup : soups->find(".//text()")) {
std::string soup_text = nodeToText(soup);
auto soup_day = soup_text.substr(0,2);
if(soup_day == "Po")
cur_day = 0;
if(cur_day < 0)
continue;
menus[cur_day].setInvalidMenu(false);
auto soupname = soup_text.substr(5);
while(std::isspace(soupname[0]))
soupname = soupname.substr(1);
menus[cur_day].addMeal(true, soupname, "", soup_price);
cur_day++;
if(cur_day > 4)
break;
}
for(unsigned long int i = 2; i < pizzas.size(); i++) {
auto content = pizzas[i]->find(".//div[@class='b-c b-text-c b-s b-s-t60 b-s-b60 cf']");
auto name_candidates = content[0]->find(".//h3/text()");
std::string name = nodeToText(name_candidates[0]);
auto desc_candidates = content[0]->find(".//p//text()");
std::string desc = nodeToText(desc_candidates[0]);
auto price_candidates = content[1]->find(".//p/strong/text()");
int price = std::stoi(nodeToText(price_candidates[0]));
trim(name);
trim(desc);
addPermanent(false, name, desc, price);
}
}