2022-03-03 07:49:58 +00:00
|
|
|
#include "functions.hpp"
|
2022-03-02 07:35:36 +00:00
|
|
|
#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);
|
|
|
|
}
|
2022-04-11 07:02:00 +00:00
|
|
|
if(price_pos == 0 || price_pos == std::string::npos) {
|
|
|
|
return {-1, -1};
|
|
|
|
}
|
2022-03-02 07:35:36 +00:00
|
|
|
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();
|
2022-09-24 21:13:42 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-03-02 07:35:36 +00:00
|
|
|
|
2022-09-24 21:13:42 +00:00
|
|
|
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)));
|
2022-04-11 07:02:00 +00:00
|
|
|
}
|
2022-03-02 07:35:36 +00:00
|
|
|
}
|
2022-09-24 21:13:42 +00:00
|
|
|
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);
|
2022-03-02 07:35:36 +00:00
|
|
|
|
2022-09-24 21:13:42 +00:00
|
|
|
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;
|
2022-03-02 07:35:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-24 21:13:42 +00:00
|
|
|
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);
|
2022-03-02 07:35:36 +00:00
|
|
|
}
|
2022-09-24 21:13:42 +00:00
|
|
|
}
|
|
|
|
} catch (std::exception &/*UNUSED*/) {
|
|
|
|
clearMenus();
|
|
|
|
for(int i = 0; i < menus.size(); i++) {
|
|
|
|
menus[i].addMeal(false, parseError, parseInfo, 0);
|
2022-03-02 07:35:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|