Renamed parsers to restaurants, add jsonify to restaurants
This commit is contained in:
parent
408e76cc96
commit
0b4ba4e0f9
12
Makefile
12
Makefile
@ -11,7 +11,7 @@ default: menuprint
|
||||
menuprint: main.o meal.o menu.o network.o $(PARSERS)
|
||||
$(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 $@ $<
|
||||
meal.o: meal.cpp meal.hpp
|
||||
$(CXX) $(CFLAGS) -c -o $@ $<
|
||||
@ -19,15 +19,15 @@ menu.o: menu.cpp menu.hpp
|
||||
$(CXX) $(CFLAGS) -c -o $@ $<
|
||||
network.o: network/network.cpp network/network.hpp
|
||||
$(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 $@ $<
|
||||
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 $@ $<
|
||||
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 $@ $<
|
||||
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 $@ $<
|
||||
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 $@ $<
|
||||
|
||||
clean:
|
||||
|
56
main.cpp
56
main.cpp
@ -1,7 +1,7 @@
|
||||
#include "parser.hpp"
|
||||
#include "restaurant.hpp"
|
||||
#include "menu.hpp"
|
||||
#include "meal.hpp"
|
||||
#include "parsers/parsers.hpp"
|
||||
#include "restaurants/restaurants.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
@ -9,23 +9,15 @@
|
||||
#include <sstream>
|
||||
|
||||
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 ) {
|
||||
std::stringstream ss{};
|
||||
bool atleastonerest = false;
|
||||
ss << "[";
|
||||
for(auto &x : parsers) {
|
||||
for(auto &x : restaurants) {
|
||||
atleastonerest = true;
|
||||
ss << "{\"restaurant\": \"" << x->getRestaurant() << "\", \"menus\": [";
|
||||
bool atleastonemenu = false;
|
||||
for(const auto &y : x->getMenus()) {
|
||||
atleastonemenu = true;
|
||||
ss << y.jsonify() << ",";
|
||||
}
|
||||
if(atleastonemenu)
|
||||
ss.seekp(-1, ss.cur);
|
||||
ss << "]},";
|
||||
ss << x->jsonify() << ",";
|
||||
}
|
||||
if(atleastonerest)
|
||||
ss.seekp(-1, ss.cur);
|
||||
@ -39,7 +31,7 @@ void get( const std::shared_ptr< restbed::Session > session ) {
|
||||
int day = -1;
|
||||
int restaurant = -1;
|
||||
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.second == "monday") {
|
||||
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) {
|
||||
std::string error = "DIDN'T SPECIFY DAY OR RESTAURANT";
|
||||
session->close(restbed::OK, error, { { "Content-Length", std::to_string(error.length()) } });
|
||||
return;
|
||||
}
|
||||
|
||||
std::stringstream ss{};
|
||||
if(day != -1 && restaurant != -1) {
|
||||
auto &x = parsers[restaurant];
|
||||
ss << "[{\"restaurant\": \"" << x->getRestaurant() << "\", \"menus\": [";
|
||||
ss << x->getMenus()[day].jsonify();
|
||||
ss << "]}]";
|
||||
ss << "[" << restaurants[restaurant]->jsonify({day}) << "]";
|
||||
} else if(day != -1) {
|
||||
ss << "[";
|
||||
bool atleastone = false;
|
||||
for(auto &x : parsers) {
|
||||
for(auto &x : restaurants) {
|
||||
atleastone = true;
|
||||
ss << "{\"restaurant\": \"" << x->getRestaurant() << "\", \"menus\": [";
|
||||
ss << x->getMenus()[day].jsonify();
|
||||
ss << "]},";
|
||||
ss << x->jsonify() << ",";
|
||||
}
|
||||
if(atleastone)
|
||||
ss.seekp(-1, ss.cur);
|
||||
ss << "]";
|
||||
} else if(restaurant != -1) {
|
||||
auto &x = parsers[restaurant];
|
||||
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 << "]}]";
|
||||
ss << "[" << restaurants[restaurant]->jsonify() << "]";
|
||||
}
|
||||
auto res = ss.str();
|
||||
session->close(restbed::OK, res, { { "Content-Length", std::to_string(res.length()) } });
|
||||
}
|
||||
|
||||
int main() {
|
||||
parsers.emplace_back(new LunchRest::UDrevakaParser());
|
||||
parsers.emplace_back(new LunchRest::PadagaliParser());
|
||||
parsers.emplace_back(new LunchRest::LightOfIndiaParser());
|
||||
parsers.emplace_back(new LunchRest::UKarlaParser());
|
||||
parsers.emplace_back(new LunchRest::AlCaponeParser());
|
||||
restaurants.emplace_back(new LunchRest::UDrevakaRestaurant());
|
||||
restaurants.emplace_back(new LunchRest::PadagaliRestaurant());
|
||||
restaurants.emplace_back(new LunchRest::LightOfIndiaRestaurant());
|
||||
restaurants.emplace_back(new LunchRest::UKarlaRestaurant());
|
||||
restaurants.emplace_back(new LunchRest::AlCaponeRestaurant());
|
||||
std::cout << "Initial parsing" << std::endl;
|
||||
for(auto &x : parsers)
|
||||
for(auto &x : restaurants)
|
||||
x->parse();
|
||||
std::cout << "Finished parsing" << std::endl;
|
||||
|
||||
|
13
meal.hpp
13
meal.hpp
@ -9,7 +9,10 @@ class Meal {
|
||||
public:
|
||||
Meal() = default;
|
||||
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 {
|
||||
return _isSoup;
|
||||
@ -37,6 +40,14 @@ public:
|
||||
}
|
||||
std::string jsonify() const;
|
||||
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;
|
||||
std::string _name;
|
||||
std::string _desc;
|
||||
|
17
menu.cpp
17
menu.cpp
@ -55,17 +55,28 @@ bool LunchRest::Menu::isValid() 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 "[]";
|
||||
std::stringstream ss{};
|
||||
bool atleastone = false;
|
||||
ss << "[";
|
||||
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++) {
|
||||
atleastone = true;
|
||||
if(i != soupInd)
|
||||
ss << getMeals()[i].jsonify() << ",";
|
||||
}
|
||||
ss.seekp(-1, ss.cur);
|
||||
for(auto &meal : permanent) {
|
||||
ss << meal.jsonify() << ",";
|
||||
}
|
||||
if(atleastone)
|
||||
ss.seekp(-1, ss.cur);
|
||||
ss << "]";
|
||||
return ss.str();
|
||||
}
|
||||
|
1
menu.hpp
1
menu.hpp
@ -22,6 +22,7 @@ public:
|
||||
void setInvalidMenu( bool invalid = true );
|
||||
bool isValid() const;
|
||||
std::string jsonify() const;
|
||||
std::string jsonify(const std::vector<Meal> &permanent) const;
|
||||
private:
|
||||
std::vector<Meal> _meals;
|
||||
bool _valid = true;
|
||||
|
33
parser.hpp
33
parser.hpp
@ -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
|
@ -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
48
restaurant.hpp
Normal 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
|
@ -1,4 +1,4 @@
|
||||
#include "parsers.hpp"
|
||||
#include "restaurants.hpp"
|
||||
#include "../network/network.hpp"
|
||||
#include "../htmlparser.hpp"
|
||||
|
||||
@ -9,7 +9,7 @@ std::string removeAlergens(const std::string &name) {
|
||||
return name.substr(0,pos);
|
||||
}
|
||||
|
||||
void LunchRest::AlCaponeParser::parse() {
|
||||
void LunchRest::AlCaponeRestaurant::parse() {
|
||||
menus.clear();
|
||||
Request r;
|
||||
auto html = r.get(_url);
|
@ -1,4 +1,4 @@
|
||||
#include "parsers.hpp"
|
||||
#include "restaurants.hpp"
|
||||
#include "../network/network.hpp"
|
||||
#include "../htmlparser.hpp"
|
||||
|
||||
@ -12,7 +12,7 @@ bool isWhiteSpaceOnly(const std::string &text) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void LunchRest::LightOfIndiaParser::parse() {
|
||||
void LunchRest::LightOfIndiaRestaurant::parse() {
|
||||
clearMenus();
|
||||
Request r;
|
||||
auto html = r.get(_url);
|
@ -1,8 +1,8 @@
|
||||
#include "parsers.hpp"
|
||||
#include "restaurants.hpp"
|
||||
#include "../network/network.hpp"
|
||||
#include "../htmlparser.hpp"
|
||||
|
||||
void LunchRest::PadagaliParser::parse() {
|
||||
void LunchRest::PadagaliRestaurant::parse() {
|
||||
clearMenus();
|
||||
int menu_index = 0;
|
||||
Request r;
|
35
restaurants/restaurants.hpp
Normal file
35
restaurants/restaurants.hpp
Normal 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
|
||||
|
@ -1,8 +1,8 @@
|
||||
#include "parsers.hpp"
|
||||
#include "restaurants.hpp"
|
||||
#include "../network/network.hpp"
|
||||
#include "../htmlparser.hpp"
|
||||
|
||||
void LunchRest::UDrevakaParser::parse() {
|
||||
void LunchRest::UDrevakaRestaurant::parse() {
|
||||
clearMenus();
|
||||
int menu_index = 0;
|
||||
Request r;
|
@ -1,8 +1,8 @@
|
||||
#include "parsers.hpp"
|
||||
#include "restaurants.hpp"
|
||||
#include "../network/network.hpp"
|
||||
#include "../htmlparser.hpp"
|
||||
|
||||
void LunchRest::UKarlaParser::parse() {
|
||||
void LunchRest::UKarlaRestaurant::parse() {
|
||||
clearMenus();
|
||||
int menu_index = 0;
|
||||
Request r;
|
||||
@ -23,6 +23,8 @@ void LunchRest::UKarlaParser::parse() {
|
||||
if(name[0] == 'P') {
|
||||
soup = true;
|
||||
name = name.substr(10);
|
||||
} else {
|
||||
name = name.substr(3);
|
||||
}
|
||||
int price = -1;
|
||||
if(texts.size() > 1)
|
Loading…
Reference in New Issue
Block a user