From 2116c20519d4964cfe99983465a8919642ed3ad7 Mon Sep 17 00:00:00 2001 From: zvon Date: Sun, 20 Sep 2020 16:27:49 +0200 Subject: [PATCH] Add suzzie's --- Makefile | 8 +++++-- environment.cpp | 18 ++++++++++++++++ environment.hpp | 9 ++++++++ main.cpp | 6 ++++-- restaurant.hpp | 6 +++++- restaurants/restaurants.hpp | 8 +++++++ restaurants/suzzies.cpp | 43 +++++++++++++++++++++++++++++++++++++ 7 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 environment.cpp create mode 100644 environment.hpp create mode 100644 restaurants/suzzies.cpp diff --git a/Makefile b/Makefile index f086802..31a3e93 100644 --- a/Makefile +++ b/Makefile @@ -3,12 +3,12 @@ CFLAGS ?= -O2 -Wall -Wextra `pkg-config libxml-2.0 --cflags` `pkg-config libxml+ PREFIX ?= /usr/local/bin 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 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} 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 $@ $< restaurants.o: restaurants/restaurants.cpp restaurants/restaurants.hpp $(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 $(CXX) $(CFLAGS) -c -o $@ $< 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 $@ $< zo.o: restaurants/zo.cpp restaurants/restaurants.hpp network/network.hpp htmlparser.hpp $(CXX) $(CFLAGS) -c -o $@ $< +suzzies.o: restaurants/suzzies.cpp restaurants/restaurants.hpp network/network.hpp environment.hpp + $(CXX) $(CFLAGS) -c -o $@ $< clean: rm -Rf *.o menuprint diff --git a/environment.cpp b/environment.cpp new file mode 100644 index 0000000..7136310 --- /dev/null +++ b/environment.cpp @@ -0,0 +1,18 @@ +#include "environment.hpp" +#include + +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 ""; +} diff --git a/environment.hpp b/environment.hpp new file mode 100644 index 0000000..8ca4c6c --- /dev/null +++ b/environment.hpp @@ -0,0 +1,9 @@ +#ifndef ENVIRONMENT_H +#define ENVIRONMENT_H + +#include + +void setupEnv(char **env); +std::string getEnv(const std::string &env); + +#endif diff --git a/main.cpp b/main.cpp index 6af60a1..5969a27 100644 --- a/main.cpp +++ b/main.cpp @@ -1,3 +1,4 @@ +#include "environment.hpp" #include "restaurant.hpp" #include "menu.hpp" #include "meal.hpp" @@ -9,7 +10,6 @@ #include #include -std::vector days = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"}; std::map> restaurants; 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); } -int main() { +int main(int argc, char **argv, char **env) { + setupEnv(env); restaurants["udrevaka"] = std::make_unique(); restaurants["padagali"] = std::make_unique(); restaurants["lightofindia"] = std::make_unique(); @@ -100,6 +101,7 @@ int main() { restaurants["alcapone"] = std::make_unique(); restaurants["plac"] = std::make_unique(); restaurants["zo"] = std::make_unique(); + restaurants["suzzies"] = std::make_unique(); std::cout << "Initial parsing" << std::endl; for(auto &restaurant : restaurants) restaurant.second->parse(); diff --git a/restaurant.hpp b/restaurant.hpp index 08eef38..4ed0d04 100644 --- a/restaurant.hpp +++ b/restaurant.hpp @@ -34,10 +34,14 @@ public: std::string jsonify(const std::vector &days = {0,1,2,3,4}) { std::stringstream ss{}; ss << "{\"restaurant\": \"" << getRestaurant() << "\", \"dailymenus\": ["; + bool comma = false; for(auto &day : days) { + if(static_cast(day) > menus.size()) + continue; + comma = true; ss << menus[day].jsonify() << ","; } - if(!days.empty()) + if(comma) ss.seekp(-1, ss.cur); ss << "], \"permanentmeals\": ["; for(auto &meal : permanent) { diff --git a/restaurants/restaurants.hpp b/restaurants/restaurants.hpp index 1db66e2..273b421 100644 --- a/restaurants/restaurants.hpp +++ b/restaurants/restaurants.hpp @@ -45,5 +45,13 @@ public: virtual ~ZoRestaurant() = default; 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 diff --git a/restaurants/suzzies.cpp b/restaurants/suzzies.cpp new file mode 100644 index 0000000..89a7fe4 --- /dev/null +++ b/restaurants/suzzies.cpp @@ -0,0 +1,43 @@ +#include "../environment.hpp" +#include "restaurants.hpp" +#include "../network/network.hpp" +#include +#include + +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())); + } + } + } +}