Added Tao restaurant

This commit is contained in:
zv0n 2022-03-02 08:35:36 +01:00
parent a635d7673a
commit e3ac4495f9
5 changed files with 123 additions and 20 deletions

View File

@ -3,7 +3,7 @@ CFLAGS ?= -O2 -Wall -Wextra `pkg-config libxml-2.0 --cflags` `pkg-config libxml+
PREFIX ?= /usr/local/bin
LDFLAGS ?= -lcurl -lrestbed `pkg-config libxml-2.0 --libs` `pkg-config libxml++-3.0 --libs`
PARSERS = udrevaka.o padagali.o lightofindia.o ukarla.o alcapone.o plac.o zo.o suzies.o
PARSERS = udrevaka.o padagali.o lightofindia.o ukarla.o alcapone.o plac.o zo.o suzies.o tao.o
.PHONY: default
default: menuprint
@ -37,7 +37,9 @@ plac.o: restaurants/plac.cpp restaurants/restaurants.hpp network/network.hpp htm
$(CXX) $(CFLAGS) -c -o $@ $<
zo.o: restaurants/zo.cpp restaurants/restaurants.hpp network/network.hpp htmlparser.hpp
$(CXX) $(CFLAGS) -c -o $@ $<
suzies.o: restaurants/suzies.cpp restaurants/restaurants.hpp network/network.hpp environment.hpp
suzies.o: restaurants/suzies.cpp restaurants/restaurants.hpp network/network.hpp htmlparser.hpp
$(CXX) $(CFLAGS) -c -o $@ $<
tao.o: restaurants/tao.cpp restaurants/restaurants.hpp network/network.hpp htmlparser.hpp
$(CXX) $(CFLAGS) -c -o $@ $<
clean:

View File

@ -109,6 +109,7 @@ int main(int /*UNUSED*/, char ** /*UNUSED*/, char **env) {
restaurants["plac"] = std::make_unique<LunchRest::PlacRestaurant>();
restaurants["zo"] = std::make_unique<LunchRest::ZoRestaurant>();
restaurants["suzies"] = std::make_unique<LunchRest::SuziesRestaurant>();
restaurants["tao"] = std::make_unique<LunchRest::TaoRestaurant>();
std::cout << "Initial parsing" << std::endl;
for (auto &restaurant : restaurants)
restaurant.second->parse();

View File

@ -51,8 +51,12 @@ public:
SuziesRestaurant() : Restaurant("https://www.suzies.cz/poledni-menu", "Suzie's") {}
virtual ~SuziesRestaurant() = default;
virtual void parse() override;
private:
std::string api_key;
};
class TaoRestaurant : public Restaurant {
public:
TaoRestaurant() : Restaurant("https://www.taorestaurant.cz/tydenni_menu/nabidka/", "Táo Viet Nam") {}
virtual ~TaoRestaurant() = default;
virtual void parse() override;
};
} // end of namespace LunchRest

View File

@ -3,12 +3,6 @@
#include "../htmlparser.hpp"
#include <iostream>
int weekDay(const std::string &date_str) {
struct tm date_tm;
strptime(date_str.c_str(), "%Y-%m-%d %H:%M:%S", &date_tm);
return (date_tm.tm_wday + 6) % 7;
}
void LunchRest::SuziesRestaurant::parse() {
Request r;
auto html = r.get(_url);

102
restaurants/tao.cpp Normal file
View File

@ -0,0 +1,102 @@
#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);
}
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();
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;
}
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;
}
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);
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);
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);
}
}
}