Added Tao restaurant
This commit is contained in:
parent
a635d7673a
commit
e3ac4495f9
6
Makefile
6
Makefile
@ -3,7 +3,7 @@ CFLAGS ?= -O2 -Wall -Wextra `pkg-config libxml-2.0 --cflags` `pkg-config libxml+
|
|||||||
PREFIX ?= /usr/local/bin
|
PREFIX ?= /usr/local/bin
|
||||||
LDFLAGS ?= -lcurl -lrestbed `pkg-config libxml-2.0 --libs` `pkg-config libxml++-3.0 --libs`
|
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
|
.PHONY: default
|
||||||
default: menuprint
|
default: menuprint
|
||||||
@ -37,7 +37,9 @@ plac.o: restaurants/plac.cpp restaurants/restaurants.hpp network/network.hpp htm
|
|||||||
$(CXX) $(CFLAGS) -c -o $@ $<
|
$(CXX) $(CFLAGS) -c -o $@ $<
|
||||||
zo.o: restaurants/zo.cpp restaurants/restaurants.hpp network/network.hpp htmlparser.hpp
|
zo.o: restaurants/zo.cpp restaurants/restaurants.hpp network/network.hpp htmlparser.hpp
|
||||||
$(CXX) $(CFLAGS) -c -o $@ $<
|
$(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 $@ $<
|
$(CXX) $(CFLAGS) -c -o $@ $<
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
|
5
main.cpp
5
main.cpp
@ -109,6 +109,7 @@ int main(int /*UNUSED*/, char ** /*UNUSED*/, char **env) {
|
|||||||
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["suzies"] = std::make_unique<LunchRest::SuziesRestaurant>();
|
restaurants["suzies"] = std::make_unique<LunchRest::SuziesRestaurant>();
|
||||||
|
restaurants["tao"] = std::make_unique<LunchRest::TaoRestaurant>();
|
||||||
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();
|
||||||
@ -131,7 +132,7 @@ int main(int /*UNUSED*/, char ** /*UNUSED*/, char **env) {
|
|||||||
refreshserv->set_method_handler("GET", refresh);
|
refreshserv->set_method_handler("GET", refresh);
|
||||||
service.publish(refreshserv);
|
service.publish(refreshserv);
|
||||||
|
|
||||||
/* auto ssl_settings = std::make_shared<restbed::SSLSettings>();
|
/* auto ssl_settings = std::make_shared<restbed::SSLSettings>();
|
||||||
ssl_settings->set_http_disabled(true);
|
ssl_settings->set_http_disabled(true);
|
||||||
ssl_settings->set_private_key(restbed::Uri(
|
ssl_settings->set_private_key(restbed::Uri(
|
||||||
"file:///home/zvon/data/programming/lunch-rest/example.key"));
|
"file:///home/zvon/data/programming/lunch-rest/example.key"));
|
||||||
@ -144,7 +145,7 @@ int main(int /*UNUSED*/, char ** /*UNUSED*/, char **env) {
|
|||||||
auto settings = std::make_shared<restbed::Settings>();
|
auto settings = std::make_shared<restbed::Settings>();
|
||||||
settings->set_port(1984);
|
settings->set_port(1984);
|
||||||
settings->set_default_header("Connection", "close");
|
settings->set_default_header("Connection", "close");
|
||||||
// settings->set_ssl_settings(ssl_settings);
|
// settings->set_ssl_settings(ssl_settings);
|
||||||
|
|
||||||
service.start(settings);
|
service.start(settings);
|
||||||
|
|
||||||
|
@ -51,8 +51,12 @@ public:
|
|||||||
SuziesRestaurant() : Restaurant("https://www.suzies.cz/poledni-menu", "Suzie's") {}
|
SuziesRestaurant() : Restaurant("https://www.suzies.cz/poledni-menu", "Suzie's") {}
|
||||||
virtual ~SuziesRestaurant() = default;
|
virtual ~SuziesRestaurant() = default;
|
||||||
virtual void parse() override;
|
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
|
} // end of namespace LunchRest
|
||||||
|
|
||||||
|
@ -3,12 +3,6 @@
|
|||||||
#include "../htmlparser.hpp"
|
#include "../htmlparser.hpp"
|
||||||
#include <iostream>
|
#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() {
|
void LunchRest::SuziesRestaurant::parse() {
|
||||||
Request r;
|
Request r;
|
||||||
auto html = r.get(_url);
|
auto html = r.get(_url);
|
||||||
|
102
restaurants/tao.cpp
Normal file
102
restaurants/tao.cpp
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user