Renamed parsers to restaurants, add jsonify to restaurants

This commit is contained in:
zvon 2020-09-15 17:25:27 +02:00
parent 408e76cc96
commit 0b4ba4e0f9
14 changed files with 145 additions and 127 deletions

View File

@ -11,7 +11,7 @@ default: menuprint
menuprint: main.o meal.o menu.o network.o $(PARSERS) menuprint: main.o meal.o menu.o network.o $(PARSERS)
$(CXX) $(CFLAGS) -o $@ $^ ${LDFLAGS} $(CXX) $(CFLAGS) -o $@ $^ ${LDFLAGS}
main.o: main.cpp parser.hpp menu.hpp meal.hpp parsers/parsers.hpp main.o: main.cpp restaurant.hpp menu.hpp meal.hpp restaurants/restaurants.hpp
$(CXX) $(CFLAGS) -c -o $@ $< $(CXX) $(CFLAGS) -c -o $@ $<
meal.o: meal.cpp meal.hpp meal.o: meal.cpp meal.hpp
$(CXX) $(CFLAGS) -c -o $@ $< $(CXX) $(CFLAGS) -c -o $@ $<
@ -19,15 +19,15 @@ menu.o: menu.cpp menu.hpp
$(CXX) $(CFLAGS) -c -o $@ $< $(CXX) $(CFLAGS) -c -o $@ $<
network.o: network/network.cpp network/network.hpp network.o: network/network.cpp network/network.hpp
$(CXX) $(CFLAGS) -c -o $@ $< $(CXX) $(CFLAGS) -c -o $@ $<
udrevaka.o: parsers/udrevaka.cpp parsers/parsers.hpp network/network.hpp htmlparser.hpp udrevaka.o: restaurants/udrevaka.cpp restaurants/restaurants.hpp network/network.hpp htmlparser.hpp
$(CXX) $(CFLAGS) -c -o $@ $< $(CXX) $(CFLAGS) -c -o $@ $<
padagali.o: parsers/padagali.cpp parsers/parsers.hpp network/network.hpp htmlparser.hpp padagali.o: restaurants/padagali.cpp restaurants/restaurants.hpp network/network.hpp htmlparser.hpp
$(CXX) $(CFLAGS) -c -o $@ $< $(CXX) $(CFLAGS) -c -o $@ $<
lightofindia.o: parsers/lightofindia.cpp parsers/parsers.hpp network/network.hpp htmlparser.hpp lightofindia.o: restaurants/lightofindia.cpp restaurants/restaurants.hpp network/network.hpp htmlparser.hpp
$(CXX) $(CFLAGS) -c -o $@ $< $(CXX) $(CFLAGS) -c -o $@ $<
ukarla.o: parsers/ukarla.cpp parsers/parsers.hpp network/network.hpp htmlparser.hpp ukarla.o: restaurants/ukarla.cpp restaurants/restaurants.hpp network/network.hpp htmlparser.hpp
$(CXX) $(CFLAGS) -c -o $@ $< $(CXX) $(CFLAGS) -c -o $@ $<
alcapone.o: parsers/alcapone.cpp parsers/parsers.hpp network/network.hpp htmlparser.hpp alcapone.o: restaurants/alcapone.cpp restaurants/restaurants.hpp network/network.hpp htmlparser.hpp
$(CXX) $(CFLAGS) -c -o $@ $< $(CXX) $(CFLAGS) -c -o $@ $<
clean: clean:

View File

@ -1,7 +1,7 @@
#include "parser.hpp" #include "restaurant.hpp"
#include "menu.hpp" #include "menu.hpp"
#include "meal.hpp" #include "meal.hpp"
#include "parsers/parsers.hpp" #include "restaurants/restaurants.hpp"
#include <iostream> #include <iostream>
#include <memory> #include <memory>
@ -9,23 +9,15 @@
#include <sstream> #include <sstream>
std::vector<std::string> days = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"}; std::vector<std::string> days = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"};
std::vector<std::unique_ptr<LunchRest::Parser>> parsers; std::vector<std::unique_ptr<LunchRest::Restaurant>> restaurants;
void get_all( const std::shared_ptr< restbed::Session > session ) { void get_all( const std::shared_ptr< restbed::Session > session ) {
std::stringstream ss{}; std::stringstream ss{};
bool atleastonerest = false; bool atleastonerest = false;
ss << "["; ss << "[";
for(auto &x : parsers) { for(auto &x : restaurants) {
atleastonerest = true; atleastonerest = true;
ss << "{\"restaurant\": \"" << x->getRestaurant() << "\", \"menus\": ["; ss << x->jsonify() << ",";
bool atleastonemenu = false;
for(const auto &y : x->getMenus()) {
atleastonemenu = true;
ss << y.jsonify() << ",";
}
if(atleastonemenu)
ss.seekp(-1, ss.cur);
ss << "]},";
} }
if(atleastonerest) if(atleastonerest)
ss.seekp(-1, ss.cur); ss.seekp(-1, ss.cur);
@ -39,7 +31,7 @@ void get( const std::shared_ptr< restbed::Session > session ) {
int day = -1; int day = -1;
int restaurant = -1; int restaurant = -1;
auto request = session->get_request(); auto request = session->get_request();
for(const auto query_param : request->get_query_parameters()) { for(const auto &query_param : request->get_query_parameters()) {
if(query_param.first == "day") { if(query_param.first == "day") {
if(query_param.second == "monday") { if(query_param.second == "monday") {
day = 0; day = 0;
@ -67,54 +59,40 @@ void get( const std::shared_ptr< restbed::Session > session ) {
} }
} }
} }
std::string error = "DIDN'T SPECIFY DAY OR RESTAURANT";
if(day == -1 && restaurant == -1) { if(day == -1 && restaurant == -1) {
std::string error = "DIDN'T SPECIFY DAY OR RESTAURANT";
session->close(restbed::OK, error, { { "Content-Length", std::to_string(error.length()) } }); session->close(restbed::OK, error, { { "Content-Length", std::to_string(error.length()) } });
return; return;
} }
std::stringstream ss{}; std::stringstream ss{};
if(day != -1 && restaurant != -1) { if(day != -1 && restaurant != -1) {
auto &x = parsers[restaurant]; ss << "[" << restaurants[restaurant]->jsonify({day}) << "]";
ss << "[{\"restaurant\": \"" << x->getRestaurant() << "\", \"menus\": [";
ss << x->getMenus()[day].jsonify();
ss << "]}]";
} else if(day != -1) { } else if(day != -1) {
ss << "["; ss << "[";
bool atleastone = false; bool atleastone = false;
for(auto &x : parsers) { for(auto &x : restaurants) {
atleastone = true; atleastone = true;
ss << "{\"restaurant\": \"" << x->getRestaurant() << "\", \"menus\": ["; ss << x->jsonify() << ",";
ss << x->getMenus()[day].jsonify();
ss << "]},";
} }
if(atleastone) if(atleastone)
ss.seekp(-1, ss.cur); ss.seekp(-1, ss.cur);
ss << "]"; ss << "]";
} else if(restaurant != -1) { } else if(restaurant != -1) {
auto &x = parsers[restaurant]; ss << "[" << restaurants[restaurant]->jsonify() << "]";
ss << "[{\"restaurant\": \"" << x->getRestaurant() << "\", \"menus\": [";
bool atleastone = false;
for(auto &y : x->getMenus()) {
atleastone = true;
ss << y.jsonify() << ",";
}
if(atleastone)
ss.seekp(-1, ss.cur);
ss << "]}]";
} }
auto res = ss.str(); auto res = ss.str();
session->close(restbed::OK, res, { { "Content-Length", std::to_string(res.length()) } }); session->close(restbed::OK, res, { { "Content-Length", std::to_string(res.length()) } });
} }
int main() { int main() {
parsers.emplace_back(new LunchRest::UDrevakaParser()); restaurants.emplace_back(new LunchRest::UDrevakaRestaurant());
parsers.emplace_back(new LunchRest::PadagaliParser()); restaurants.emplace_back(new LunchRest::PadagaliRestaurant());
parsers.emplace_back(new LunchRest::LightOfIndiaParser()); restaurants.emplace_back(new LunchRest::LightOfIndiaRestaurant());
parsers.emplace_back(new LunchRest::UKarlaParser()); restaurants.emplace_back(new LunchRest::UKarlaRestaurant());
parsers.emplace_back(new LunchRest::AlCaponeParser()); restaurants.emplace_back(new LunchRest::AlCaponeRestaurant());
std::cout << "Initial parsing" << std::endl; std::cout << "Initial parsing" << std::endl;
for(auto &x : parsers) for(auto &x : restaurants)
x->parse(); x->parse();
std::cout << "Finished parsing" << std::endl; std::cout << "Finished parsing" << std::endl;

View File

@ -9,7 +9,10 @@ class Meal {
public: public:
Meal() = default; Meal() = default;
Meal(bool soup, const std::string &name, const std::string &desc, int price) : Meal(bool soup, const std::string &name, const std::string &desc, int price) :
_isSoup(soup), _name(name), _desc(desc), _price(price) {} _isSoup(soup), _name(name), _desc(desc), _price(price) {
replaceStr(_name, "\"", "\\\"");
replaceStr(_desc, "\"", "\\\"");
}
bool isSoup() const { bool isSoup() const {
return _isSoup; return _isSoup;
@ -37,6 +40,14 @@ public:
} }
std::string jsonify() const; std::string jsonify() const;
private: private:
void replaceStr(std::string &str, const std::string &to_replace, const std::string &replacement) {
auto pos = str.find(to_replace);
while(pos != std::string::npos) {
str.replace(pos, to_replace.length(), replacement);
pos += replacement.length();
pos = str.find(to_replace, pos);
}
}
bool _isSoup = false; bool _isSoup = false;
std::string _name; std::string _name;
std::string _desc; std::string _desc;

View File

@ -55,17 +55,28 @@ bool LunchRest::Menu::isValid() const {
} }
std::string LunchRest::Menu::jsonify() const { std::string LunchRest::Menu::jsonify() const {
if(!isValid()) return jsonify({});
}
std::string LunchRest::Menu::jsonify(const std::vector<Meal> &permanent) const {
if(!isValid() && permanent.empty())
return "[]"; return "[]";
std::stringstream ss{}; std::stringstream ss{};
bool atleastone = false;
ss << "["; ss << "[";
auto soupInd = getSoupIndex(); auto soupInd = getSoupIndex();
ss << getMeals()[soupInd].jsonify() << ","; if(soupInd != (unsigned long int)-1)
ss << getMeals()[soupInd].jsonify() << ",";
for(unsigned long int i = 0; i < getMeals().size(); i++) { for(unsigned long int i = 0; i < getMeals().size(); i++) {
atleastone = true;
if(i != soupInd) if(i != soupInd)
ss << getMeals()[i].jsonify() << ","; ss << getMeals()[i].jsonify() << ",";
} }
ss.seekp(-1, ss.cur); for(auto &meal : permanent) {
ss << meal.jsonify() << ",";
}
if(atleastone)
ss.seekp(-1, ss.cur);
ss << "]"; ss << "]";
return ss.str(); return ss.str();
} }

View File

@ -22,6 +22,7 @@ public:
void setInvalidMenu( bool invalid = true ); void setInvalidMenu( bool invalid = true );
bool isValid() const; bool isValid() const;
std::string jsonify() const; std::string jsonify() const;
std::string jsonify(const std::vector<Meal> &permanent) const;
private: private:
std::vector<Meal> _meals; std::vector<Meal> _meals;
bool _valid = true; bool _valid = true;

View File

@ -1,33 +0,0 @@
#ifndef LUNCH_REST_PARSER_H
#define LUNCH_REST_PARSER_H
#include "menu.hpp"
namespace LunchRest {
class Parser {
public:
Parser() = delete;
Parser(const std::string &url, const std::string &restaurant) :
_url(url), _restaurant(restaurant) {}
virtual ~Parser() = default;
const std::vector<Menu> &getMenus() {
return menus;
}
const std::string &getRestaurant() {
return _restaurant;
}
virtual void parse() = 0;
void clearMenus() {
menus.clear();
menus.resize(5);
for(auto &x : menus)
x.setInvalidMenu();
}
protected:
std::string _url;
std::string _restaurant;
std::vector<Menu> menus;
};
} // end of namespace LunchRest
#endif

View File

@ -1,35 +0,0 @@
#include "../parser.hpp"
namespace LunchRest {
class UDrevakaParser : public Parser {
public:
UDrevakaParser() : Parser("https://www.udrevaka.cz/denni-menu/", "U Dřeváka") {}
virtual ~UDrevakaParser() = default;
virtual void parse() override;
};
class PadagaliParser : public Parser {
public:
PadagaliParser() : Parser("https://padagali.cz/denni-menu/", "Padagali") {}
virtual ~PadagaliParser() = default;
virtual void parse() override;
};
class LightOfIndiaParser : public Parser {
public:
LightOfIndiaParser() : Parser("http://lightofindia.cz/lang-en/denni-menu", "Light of India") {}
virtual ~LightOfIndiaParser() = default;
virtual void parse() override;
};
class UKarlaParser : public Parser {
public:
UKarlaParser() : Parser("https://www.ukarlabrno.cz/denni-menu/", "U Karla") {}
virtual ~UKarlaParser() = default;
virtual void parse() override;
};
class AlCaponeParser : public Parser {
public:
AlCaponeParser() : Parser("https://www.pizzaalcapone.cz/cz/poledni-menu", "Al Capone") {}
virtual ~AlCaponeParser() = default;
virtual void parse() override;
};
} // end of namespace LunchRest

48
restaurant.hpp Normal file
View File

@ -0,0 +1,48 @@
#ifndef LUNCH_REST_PARSER_H
#define LUNCH_REST_PARSER_H
#include "menu.hpp"
#include <sstream>
namespace LunchRest {
class Restaurant {
public:
Restaurant() = delete;
Restaurant(const std::string &url, const std::string &restaurant) :
_url(url), _restaurant(restaurant) {
menus.resize(5);
}
virtual ~Restaurant() = default;
const std::vector<Menu> &getMenus() {
return menus;
}
const std::string &getRestaurant() {
return _restaurant;
}
virtual void parse() = 0;
void clearMenus() {
menus.clear();
menus.resize(5);
for(auto &x : menus)
x.setInvalidMenu();
}
std::string jsonify(const std::vector<int> &days = {0,1,2,3,4}) {
std::stringstream ss{};
ss << "{\"restaurant\": \"" << getRestaurant() << "\", \"menus\": [";
for(auto &day : days) {
ss << menus[day].jsonify(permanent) << ",";
}
if(!days.empty())
ss.seekp(-1, ss.cur);
ss << "]}";
return ss.str();
}
protected:
std::string _url;
std::string _restaurant;
std::vector<Menu> menus;
std::vector<Meal> permanent;
};
} // end of namespace LunchRest
#endif

View File

@ -1,4 +1,4 @@
#include "parsers.hpp" #include "restaurants.hpp"
#include "../network/network.hpp" #include "../network/network.hpp"
#include "../htmlparser.hpp" #include "../htmlparser.hpp"
@ -9,7 +9,7 @@ std::string removeAlergens(const std::string &name) {
return name.substr(0,pos); return name.substr(0,pos);
} }
void LunchRest::AlCaponeParser::parse() { void LunchRest::AlCaponeRestaurant::parse() {
menus.clear(); menus.clear();
Request r; Request r;
auto html = r.get(_url); auto html = r.get(_url);

View File

@ -1,4 +1,4 @@
#include "parsers.hpp" #include "restaurants.hpp"
#include "../network/network.hpp" #include "../network/network.hpp"
#include "../htmlparser.hpp" #include "../htmlparser.hpp"
@ -12,7 +12,7 @@ bool isWhiteSpaceOnly(const std::string &text) {
return true; return true;
} }
void LunchRest::LightOfIndiaParser::parse() { void LunchRest::LightOfIndiaRestaurant::parse() {
clearMenus(); clearMenus();
Request r; Request r;
auto html = r.get(_url); auto html = r.get(_url);

View File

@ -1,8 +1,8 @@
#include "parsers.hpp" #include "restaurants.hpp"
#include "../network/network.hpp" #include "../network/network.hpp"
#include "../htmlparser.hpp" #include "../htmlparser.hpp"
void LunchRest::PadagaliParser::parse() { void LunchRest::PadagaliRestaurant::parse() {
clearMenus(); clearMenus();
int menu_index = 0; int menu_index = 0;
Request r; Request r;

View File

@ -0,0 +1,35 @@
#include "../restaurant.hpp"
namespace LunchRest {
class UDrevakaRestaurant : public Restaurant {
public:
UDrevakaRestaurant() : Restaurant("https://www.udrevaka.cz/denni-menu/", "U Dřeváka") {}
virtual ~UDrevakaRestaurant() = default;
virtual void parse() override;
};
class PadagaliRestaurant : public Restaurant {
public:
PadagaliRestaurant() : Restaurant("https://padagali.cz/denni-menu/", "Padagali") {}
virtual ~PadagaliRestaurant() = default;
virtual void parse() override;
};
class LightOfIndiaRestaurant : public Restaurant {
public:
LightOfIndiaRestaurant() : Restaurant("http://lightofindia.cz/lang-en/denni-menu", "Light of India") {}
virtual ~LightOfIndiaRestaurant() = default;
virtual void parse() override;
};
class UKarlaRestaurant : public Restaurant {
public:
UKarlaRestaurant() : Restaurant("https://www.ukarlabrno.cz/denni-menu/", "U Karla") {}
virtual ~UKarlaRestaurant() = default;
virtual void parse() override;
};
class AlCaponeRestaurant : public Restaurant {
public:
AlCaponeRestaurant() : Restaurant("https://www.pizzaalcapone.cz/cz/poledni-menu", "Al Capone") {}
virtual ~AlCaponeRestaurant() = default;
virtual void parse() override;
};
} // end of namespace LunchRest

View File

@ -1,8 +1,8 @@
#include "parsers.hpp" #include "restaurants.hpp"
#include "../network/network.hpp" #include "../network/network.hpp"
#include "../htmlparser.hpp" #include "../htmlparser.hpp"
void LunchRest::UDrevakaParser::parse() { void LunchRest::UDrevakaRestaurant::parse() {
clearMenus(); clearMenus();
int menu_index = 0; int menu_index = 0;
Request r; Request r;

View File

@ -1,8 +1,8 @@
#include "parsers.hpp" #include "restaurants.hpp"
#include "../network/network.hpp" #include "../network/network.hpp"
#include "../htmlparser.hpp" #include "../htmlparser.hpp"
void LunchRest::UKarlaParser::parse() { void LunchRest::UKarlaRestaurant::parse() {
clearMenus(); clearMenus();
int menu_index = 0; int menu_index = 0;
Request r; Request r;
@ -23,6 +23,8 @@ void LunchRest::UKarlaParser::parse() {
if(name[0] == 'P') { if(name[0] == 'P') {
soup = true; soup = true;
name = name.substr(10); name = name.substr(10);
} else {
name = name.substr(3);
} }
int price = -1; int price = -1;
if(texts.size() > 1) if(texts.size() > 1)