Add suzzie's

This commit is contained in:
zvon 2020-09-20 16:27:49 +02:00
parent 71f33bd9e3
commit 2116c20519
7 changed files with 93 additions and 5 deletions

View File

@ -3,12 +3,12 @@ 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 PARSERS = udrevaka.o padagali.o lightofindia.o ukarla.o alcapone.o plac.o zo.o suzzies.o
.PHONY: default .PHONY: default
default: menuprint default: menuprint
menuprint: main.o meal.o menu.o network.o restaurants.o $(PARSERS) menuprint: main.o meal.o menu.o network.o restaurants.o environment.o $(PARSERS)
$(CXX) $(CFLAGS) -o $@ $^ ${LDFLAGS} $(CXX) $(CFLAGS) -o $@ $^ ${LDFLAGS}
main.o: main.cpp restaurant.hpp menu.hpp meal.hpp restaurants/restaurants.hpp main.o: main.cpp restaurant.hpp menu.hpp meal.hpp restaurants/restaurants.hpp
@ -21,6 +21,8 @@ network.o: network/network.cpp network/network.hpp
$(CXX) $(CFLAGS) -c -o $@ $< $(CXX) $(CFLAGS) -c -o $@ $<
restaurants.o: restaurants/restaurants.cpp restaurants/restaurants.hpp restaurants.o: restaurants/restaurants.cpp restaurants/restaurants.hpp
$(CXX) $(CFLAGS) -c -o $@ $< $(CXX) $(CFLAGS) -c -o $@ $<
environment.o: environment.cpp environment.hpp
$(CXX) $(CFLAGS) -c -o $@ $<
udrevaka.o: restaurants/udrevaka.cpp restaurants.o restaurants/restaurants.hpp network/network.hpp htmlparser.hpp udrevaka.o: restaurants/udrevaka.cpp restaurants.o restaurants/restaurants.hpp network/network.hpp htmlparser.hpp
$(CXX) $(CFLAGS) -c -o $@ $< $(CXX) $(CFLAGS) -c -o $@ $<
padagali.o: restaurants/padagali.cpp restaurants.o restaurants/restaurants.hpp network/network.hpp htmlparser.hpp padagali.o: restaurants/padagali.cpp restaurants.o restaurants/restaurants.hpp network/network.hpp htmlparser.hpp
@ -35,6 +37,8 @@ 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
$(CXX) $(CFLAGS) -c -o $@ $<
clean: clean:
rm -Rf *.o menuprint rm -Rf *.o menuprint

18
environment.cpp Normal file
View File

@ -0,0 +1,18 @@
#include "environment.hpp"
#include <cstring>
char **environment_env_ptr = nullptr;
void setupEnv(char **env) {
environment_env_ptr = env;
}
std::string getEnv(const std::string &env) {
auto env_ptr = environment_env_ptr;
while(*env_ptr != NULL) {
if(!std::strncmp(env.c_str(), *env_ptr, env.length()) && (*env_ptr)[env.length()] == '=')
return std::strchr(*env_ptr,'=')+1;
env_ptr++;
}
return "";
}

9
environment.hpp Normal file
View File

@ -0,0 +1,9 @@
#ifndef ENVIRONMENT_H
#define ENVIRONMENT_H
#include <string>
void setupEnv(char **env);
std::string getEnv(const std::string &env);
#endif

View File

@ -1,3 +1,4 @@
#include "environment.hpp"
#include "restaurant.hpp" #include "restaurant.hpp"
#include "menu.hpp" #include "menu.hpp"
#include "meal.hpp" #include "meal.hpp"
@ -9,7 +10,6 @@
#include <restbed> #include <restbed>
#include <sstream> #include <sstream>
std::vector<std::string> days = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"};
std::map<std::string, std::unique_ptr<LunchRest::Restaurant>> restaurants; std::map<std::string, std::unique_ptr<LunchRest::Restaurant>> restaurants;
void sendResponse(const std::string &response, int status_code, const std::shared_ptr< restbed::Session > session) { void sendResponse(const std::string &response, int status_code, const std::shared_ptr< restbed::Session > session) {
@ -92,7 +92,8 @@ void get( const std::shared_ptr< restbed::Session > session ) {
sendResponse(ss.str(), restbed::OK, session); sendResponse(ss.str(), restbed::OK, session);
} }
int main() { int main(int argc, char **argv, char **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>();
@ -100,6 +101,7 @@ int main() {
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>();
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();

View File

@ -34,10 +34,14 @@ public:
std::string jsonify(const std::vector<int> &days = {0,1,2,3,4}) { std::string jsonify(const std::vector<int> &days = {0,1,2,3,4}) {
std::stringstream ss{}; std::stringstream ss{};
ss << "{\"restaurant\": \"" << getRestaurant() << "\", \"dailymenus\": ["; ss << "{\"restaurant\": \"" << getRestaurant() << "\", \"dailymenus\": [";
bool comma = false;
for(auto &day : days) { for(auto &day : days) {
if(static_cast<size_t>(day) > menus.size())
continue;
comma = true;
ss << menus[day].jsonify() << ","; ss << menus[day].jsonify() << ",";
} }
if(!days.empty()) if(comma)
ss.seekp(-1, ss.cur); ss.seekp(-1, ss.cur);
ss << "], \"permanentmeals\": ["; ss << "], \"permanentmeals\": [";
for(auto &meal : permanent) { for(auto &meal : permanent) {

View File

@ -45,5 +45,13 @@ public:
virtual ~ZoRestaurant() = default; virtual ~ZoRestaurant() = default;
virtual void parse() override {}; virtual void parse() override {};
}; };
class SuzziesRestaurant : public Restaurant {
public:
SuzziesRestaurant();
virtual ~SuzziesRestaurant() = default;
virtual void parse() override;
private:
std::string api_key;
};
} // end of namespace LunchRest } // end of namespace LunchRest

43
restaurants/suzzies.cpp Normal file
View File

@ -0,0 +1,43 @@
#include "../environment.hpp"
#include "restaurants.hpp"
#include "../network/network.hpp"
#include <ctime>
#include <rapidjson/document.h>
LunchRest::SuzziesRestaurant::SuzziesRestaurant() : Restaurant("https://developers.zomato.com/api/v2.1/dailymenu?res_id=18126190", "Suzzie's") {
api_key = getEnv("ZOMATO_APIKEY");
}
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 + 1) % 7;
}
void LunchRest::SuzziesRestaurant::parse() {
rapidjson::Document json;
Request r;
r.addHeader("Accept: application/json");
r.addHeader("user_key: " + api_key);
json.Parse(r.get(_url).c_str());
if( json.HasParseError() )
return;
const auto &json_menus = json["daily_menus"];
if( !json_menus.IsArray() )
return;
clearMenus();
for(size_t i = 0; i < json_menus.Size(); i++) {
const auto &menu = json_menus[i]["daily_menu"];
auto week_day = weekDay(menu["start_date"].GetString());
if(week_day > 4)
continue;
menus[week_day].setInvalidMenu(false);
const auto &meals = menu["dishes"];
for(size_t j = 0; j < meals.Size(); j++) {
const auto &meal = meals[j]["dish"];
if(strlen(meal["price"].GetString()) != 0) {
menus[week_day].addMeal(false, meal["name"].GetString(), "", std::stoi(meal["price"].GetString()));
}
}
}
}