Make suzie's work

This commit is contained in:
zv0n 2022-02-24 16:25:21 +01:00
parent 5a097a5ce7
commit 4d7586168e
5 changed files with 161 additions and 151 deletions

View File

@ -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 suzzies.o PARSERS = udrevaka.o padagali.o lightofindia.o ukarla.o alcapone.o plac.o zo.o suzies.o
.PHONY: default .PHONY: default
default: menuprint default: menuprint
@ -37,7 +37,7 @@ 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 $@ $<
suzzies.o: restaurants/suzzies.cpp restaurants/restaurants.hpp network/network.hpp environment.hpp suzies.o: restaurants/suzies.cpp restaurants/restaurants.hpp network/network.hpp environment.hpp
$(CXX) $(CFLAGS) -c -o $@ $< $(CXX) $(CFLAGS) -c -o $@ $<
clean: clean:

View File

@ -1,7 +1,7 @@
#include "environment.hpp" #include "environment.hpp"
#include "restaurant.hpp"
#include "menu.hpp"
#include "meal.hpp" #include "meal.hpp"
#include "menu.hpp"
#include "restaurant.hpp"
#include "restaurants/restaurants.hpp" #include "restaurants/restaurants.hpp"
#include <fstream> #include <fstream>
@ -14,11 +14,13 @@
std::map<std::string, std::unique_ptr<LunchRest::Restaurant>> restaurants; std::map<std::string, std::unique_ptr<LunchRest::Restaurant>> restaurants;
std::string file_info_name = "data_backup"; void sendResponse(const std::string &response, int status_code,
struct tm last_save; const std::shared_ptr<restbed::Session> session,
const std::string &content_type = "text/plain") {
void sendResponse(const std::string &response, int status_code, const std::shared_ptr< restbed::Session > session) { session->close(status_code, response,
session->close(status_code, response, { { "Content-Length", std::to_string(response.length()) }, { "Access-Control-Allow-Origin", "*" } }); { { "Content-Length", std::to_string(response.length()) },
{ "Access-Control-Allow-Origin", "*" },
{ "Content-Type", content_type } });
} }
void refresh(const std::shared_ptr<restbed::Session> session) { void refresh(const std::shared_ptr<restbed::Session> session) {
@ -28,13 +30,14 @@ void refresh( const std::shared_ptr< restbed::Session > session ) {
sendResponse("Refreshed menus!", restbed::OK, session); sendResponse("Refreshed menus!", restbed::OK, session);
} }
std::string get_all_json() { std::string get_all_json(const std::vector<int> &days = { 0, 1, 2, 3, 4, 5,
6 }) {
std::stringstream ss{}; std::stringstream ss{};
bool atleastonerest = false; bool atleastonerest = false;
ss << "["; ss << "[";
for (auto &restaurant : restaurants) { for (auto &restaurant : restaurants) {
atleastonerest = true; atleastonerest = true;
ss << restaurant.second->jsonify() << ","; ss << restaurant.second->jsonify(days) << ",";
} }
if (atleastonerest) if (atleastonerest)
ss.seekp(-1, ss.cur); ss.seekp(-1, ss.cur);
@ -42,25 +45,8 @@ std::string get_all_json() {
return ss.str(); return ss.str();
} }
void backupData() {
time_t now = time(NULL);
struct tm *local = localtime(&now);
if(last_save.tm_year == local->tm_year && last_save.tm_mon == local->tm_mon && last_save.tm_mday == local->tm_mday)
return;
std::ofstream output_file;
output_file.open(file_info_name, std::ios_base::app);
auto write_buffer = get_all_json();
write_buffer += "\n";
output_file.write(write_buffer.c_str(), write_buffer.size());
last_save.tm_year = local->tm_year;
last_save.tm_mon = local->tm_mon;
last_save.tm_mday = local->tm_mday;
}
void get_all(const std::shared_ptr<restbed::Session> session) { void get_all(const std::shared_ptr<restbed::Session> session) {
sendResponse(get_all_json(), restbed::OK, session); sendResponse(get_all_json(), restbed::OK, session, "application/json");
} }
void get(const std::shared_ptr<restbed::Session> session) { void get(const std::shared_ptr<restbed::Session> session) {
@ -90,52 +76,43 @@ void get( const std::shared_ptr< restbed::Session > session ) {
restaurant = query_param.second; restaurant = query_param.second;
} }
if (day == -1 && restaurant == "") { if (day == -1 && restaurant == "") {
sendResponse("DIDN'T SPECIFY DAY OR RESTAURANT", restbed::BAD_REQUEST, session); sendResponse("DIDN'T SPECIFY DAY OR RESTAURANT", restbed::BAD_REQUEST,
session);
return; return;
} }
if (restaurant != "" && restaurants.find(restaurant) == restaurants.end()) { if (restaurant != "" && restaurants.find(restaurant) == restaurants.end()) {
sendResponse("YOU DIDN'T SPECIFY A VALID RESTAURANT!", restbed::BAD_REQUEST, session); sendResponse("YOU DIDN'T SPECIFY A VALID RESTAURANT!",
restbed::BAD_REQUEST, session);
return; return;
} }
std::stringstream ss{}; std::string response{};
if (day != -1 && restaurant != "") { if (day != -1 && restaurant != "") {
ss << "[" << restaurants[restaurant]->jsonify({day}) << "]"; response = "[" + restaurants[restaurant]->jsonify({ day }) + "]";
} else if (day != -1) { } else if (day != -1) {
ss << "["; response = get_all_json({ day });
bool atleastone = false;
for(auto &restaurant : restaurants) {
atleastone = true;
ss << restaurant.second->jsonify({day}) << ",";
}
if(atleastone)
ss.seekp(-1, ss.cur);
ss << "]";
} else if (restaurant != "") { } else if (restaurant != "") {
ss << "[" << restaurants[restaurant]->jsonify() << "]"; response = "[" + restaurants[restaurant]->jsonify() + "]";
} }
sendResponse(ss.str(), restbed::OK, session); sendResponse(response, restbed::OK, session, "application/json");
} }
int main(int argc, char **argv, char **env) { int main(int /*UNUSED*/, char ** /*UNUSED*/, char **env) {
setupEnv(env); setupEnv(env);
restaurants["udrevaka"] = std::make_unique<LunchRest::UDrevakaRestaurant>(); restaurants["udrevaka"] = std::make_unique<LunchRest::UDrevakaRestaurant>();
restaurants["padagali"] = std::make_unique<LunchRest::PadagaliRestaurant>(); restaurants["padagali"] = std::make_unique<LunchRest::PadagaliRestaurant>();
restaurants["lightofindia"] = std::make_unique<LunchRest::LightOfIndiaRestaurant>(); restaurants["lightofindia"] =
std::make_unique<LunchRest::LightOfIndiaRestaurant>();
restaurants["ukarla"] = std::make_unique<LunchRest::UKarlaRestaurant>(); restaurants["ukarla"] = std::make_unique<LunchRest::UKarlaRestaurant>();
restaurants["alcapone"] = std::make_unique<LunchRest::AlCaponeRestaurant>(); restaurants["alcapone"] = std::make_unique<LunchRest::AlCaponeRestaurant>();
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["suzzies"] = std::make_unique<LunchRest::SuzziesRestaurant>(); restaurants["suzies"] = std::make_unique<LunchRest::SuziesRestaurant>();
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();
std::cout << "Finished parsing" << std::endl; std::cout << "Finished parsing" << std::endl;
last_save.tm_year = 0;
last_save.tm_mon = 0;
last_save.tm_mday = 0;
backupData();
restbed::Service service; restbed::Service service;
@ -154,17 +131,20 @@ int main(int argc, char **argv, 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("file:///home/zvon/data/programming/lunch-rest/example.key")); ssl_settings->set_private_key(restbed::Uri(
ssl_settings->set_certificate(restbed::Uri("file:///home/zvon/data/programming/lunch-rest/example.crt")); "file:///home/zvon/data/programming/lunch-rest/example.key"));
ssl_settings->set_temporary_diffie_hellman(restbed::Uri("file:///home/zvon/data/programming/lunch-rest/dhparams.pem")); ssl_settings->set_certificate(restbed::Uri(
ssl_settings->set_port(1985); "file:///home/zvon/data/programming/lunch-rest/example.crt"));
ssl_settings->set_temporary_diffie_hellman(restbed::Uri(
"file:///home/zvon/data/programming/lunch-rest/dhparams.pem"));
ssl_settings->set_port(1985);*/
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);

View File

@ -46,10 +46,10 @@ public:
virtual ~ZoRestaurant() = default; virtual ~ZoRestaurant() = default;
virtual void parse() override {}; virtual void parse() override {};
}; };
class SuzziesRestaurant : public Restaurant { class SuziesRestaurant : public Restaurant {
public: public:
SuzziesRestaurant() : Restaurant("http://suzies.cz/poledni-menu.html", "Suzzie's") {} SuziesRestaurant() : Restaurant("https://www.suzies.cz/poledni-menu", "Suzie's") {}
virtual ~SuzziesRestaurant() = default; virtual ~SuziesRestaurant() = default;
virtual void parse() override; virtual void parse() override;
private: private:
std::string api_key; std::string api_key;

91
restaurants/suzies.cpp Normal file
View File

@ -0,0 +1,91 @@
#include "restaurants.hpp"
#include "../network/network.hpp"
#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);
if(html == "")
return;
clearMenus();
HtmlParser hparse(html);
auto &root = hparse.getRoot();
auto days = root.find("//div[@class='uk-card-body item']");
std::cout << "IMMA TRY" << std::endl;
for(auto &day : days) {
auto daytext = nodeToText(day->find("./h2/text()")[0]);
int cur_day = 0;
switch(daytext[0]) {
case 'P':
if(daytext[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;
}
std::cout << "DAY: " << cur_day << std::endl;
bool working_on_meal = false;
bool working_on_soup = false;
Meal meal{};
for(auto &child : day->get_children()) {
if(child->get_name() == "h3" && child->find("./text()").size() != 0) {
std::cout << "MEAL" << std::endl;
auto meal_type = trim(nodeToText(child->find("./text()")[0]));
std::cout << meal_type << std::endl;
meal = Meal();
working_on_meal = true;
if(meal_type == "Polévka") {
working_on_soup = true;
meal.setSoup();
} else {
meal.setDesc(meal_type);
}
}
if(working_on_meal && child->get_name() == "div") {
if(!working_on_soup) {
auto price_elements = child->find("./div[contains(@class, 'price')]/text()");
if(price_elements.size() == 0) {
goto end;
}
auto price = nodeToText(price_elements[0]);
std::cout << "PRICE: " << price << std::endl;
meal.setPrice(std::stoi(price));
auto namelen = child->find("./div[contains(@class, 'uk-width-expand')]/text()").size();
std::string name = "";
if(namelen == 1) {
name = trim(nodeToText(child->find("./div[contains(@class, 'uk-width-expand')]/text()")[0]));
} else {
name = trim(nodeToText(child->find("./div[contains(@class, 'uk-width-expand')]/span/text()")[0])) + " " + trim(nodeToText(child->find("./div[contains(@class, 'uk-width-expand')]/text()")[1]));
}
std::cout << name << std::endl;
meal.setName(name);
} else {
auto name = trim(nodeToText(child->find("./div[contains(@class, 'uk-width-expand')]/text()")[0]));
std::cout << name << std::endl;
meal.setName(name);
}
menus[cur_day].addMeal(meal);
end:
working_on_meal = false;
working_on_soup = false;
}
}
menus[cur_day].setInvalidMenu(false);
}
}

View File

@ -1,61 +0,0 @@
#include "restaurants.hpp"
#include "../network/network.hpp"
#include "../htmlparser.hpp"
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::SuzziesRestaurant::parse() {
Request r;
auto html = r.get(_url);
if(html == "")
return;
clearMenus();
HtmlParser hparse(html);
auto &root = hparse.getRoot();
auto days = root.find("//div[@class='day']");
for(auto &day : days) {
auto *daynum = xmlGetProp(day->cobj(), (xmlChar*)"data-day");
int cur_day = 0;
switch(daynum[0]) {
case '2':
cur_day = 1;
break;
case '3':
cur_day = 2;
break;
case '4':
cur_day = 3;
break;
case '5':
cur_day = 4;
default:
break;
}
for(auto &meal : day->find(".//div[@class='item']")) {
Meal meal_obj{};
auto type = trim(nodeToText(meal->find(".//h6/text()")[0]));
auto name_nodes = meal->find(".//div[@class='title']/text()");
auto text_nodes = meal->find(".//div[@class='text']/text()");
auto price_nodes = meal->find(".//div[@class='price']/text()");
if(price_nodes.size() > 0) {
// not soup
auto name = type;
if(name_nodes.size() > 0)
name += " - " + trim(nodeToText(name_nodes[0]));
meal_obj.setName(name);
if(text_nodes.size() > 0)
meal_obj.setDesc(trim(nodeToText(text_nodes[0])));
meal_obj.setPrice(std::stoi(trim(nodeToText(price_nodes[0]))));
} else {
meal_obj.setName(trim(nodeToText(name_nodes[0])));
meal_obj.setSoup();
}
menus[cur_day].addMeal(meal_obj);
}
menus[cur_day].setInvalidMenu(false);
}
}