lunch-rest/restaurants/tao.cpp
2022-09-24 23:13:42 +02:00

123 lines
4.2 KiB
C++

#include "functions.hpp"
#include "restaurants.hpp"
#include "../network/network.hpp"
#include "../htmlparser.hpp"
#include <iostream>
std::pair<size_t, size_t> getPricePosFromText(const std::string &text) {
size_t price_pos = 0, possible_price = 0;
while (possible_price != std::string::npos) {
price_pos = possible_price;
possible_price = text.find('k', possible_price + 1);
}
if(price_pos == 0 || price_pos == std::string::npos) {
return {-1, -1};
}
auto end_pos = price_pos;
price_pos -= 1;
while (text[price_pos] >= '0' && text[price_pos] <= '9') {
price_pos -= 1;
}
price_pos += 1;
return { price_pos, end_pos - price_pos };
}
size_t getEndOfTextPos(const std::string &text, size_t price_pos) {
auto end_pos = price_pos -= 1;
while (text[end_pos] == '.' || text[end_pos] == ' ') {
end_pos -= 1;
}
end_pos += 1;
return end_pos;
}
void LunchRest::TaoRestaurant::parse() {
Request r;
auto html = r.get(_url);
if (html == "")
return;
clearMenus();
try {
HtmlParser hparse(html);
auto &root = hparse.getRoot();
auto week_meals_html =
root.find("//div[@class='ct-div-block']/div[@class='ct-div-block "
"tydenni-menu-div']");
if (week_meals_html.empty()) {
std::cout << "No week meals :(" << std::endl;
return;
}
auto daily_meals_html =
root.find("//div[@class='ct-section-inner-wrap']/"
"div[@class='ct-div-block tydenni-menu-div']");
if (daily_meals_html.empty()) {
std::cout << "No daily meals :(" << std::endl;
return;
}
std::vector<Meal> week_meals{};
for (auto &meal : week_meals_html) {
auto texts = meal->find(".//span/text()");
if (!texts.empty()) {
auto text = nodeToText(texts[0]);
auto price_positions = getPricePosFromText(text);
if(price_positions.first == (size_t)-1) {
week_meals.emplace_back(false, text, "", 0);
continue;
}
auto end_pos = getEndOfTextPos(text, price_positions.first);
week_meals.emplace_back(
false, text.substr(0, end_pos), "",
std::stoi(text.substr(price_positions.first,
price_positions.second)));
}
}
for (auto &meal : daily_meals_html) {
auto texts = meal->find(".//span/text()");
auto days = meal->find(".//div/b/text()");
if (!texts.empty() && !days.empty()) {
auto text = nodeToText(texts[0]);
auto day = nodeToText(days[0]);
auto price_positions = getPricePosFromText(text);
if(price_positions.first == (size_t)-1) {
continue;
}
auto end_pos = getEndOfTextPos(text, price_positions.first);
int cur_day = 0;
switch (day[0]) {
case 'P':
if (day[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;
}
Meal cur_meal(false, text.substr(0, end_pos), "",
std::stoi(text.substr(price_positions.first,
price_positions.second)));
menus[cur_day].addMeal(cur_meal);
for (auto &meal : week_meals) {
menus[cur_day].addMeal(meal);
}
menus[cur_day].setInvalidMenu(false);
}
}
} catch (std::exception &/*UNUSED*/) {
clearMenus();
for(int i = 0; i < menus.size(); i++) {
menus[i].addMeal(false, parseError, parseInfo, 0);
}
}
}