Fix Suzzie's

This commit is contained in:
zvon 2021-02-11 19:19:39 +01:00
parent bcb6120199
commit 89be08e9a6
4 changed files with 47 additions and 29 deletions

View File

@ -101,7 +101,7 @@ int main(int argc, char **argv, char **env) {
restaurants["alcapone"] = std::make_unique<LunchRest::AlCaponeRestaurant>();
restaurants["plac"] = std::make_unique<LunchRest::PlacRestaurant>();
restaurants["zo"] = std::make_unique<LunchRest::ZoRestaurant>();
// restaurants["suzzies"] = std::make_unique<LunchRest::SuzziesRestaurant>();
restaurants["suzzies"] = std::make_unique<LunchRest::SuzziesRestaurant>();
std::cout << "Initial parsing" << std::endl;
for(auto &restaurant : restaurants)
restaurant.second->parse();

View File

@ -1,7 +1,6 @@
#include "restaurants.hpp"
#include "../network/network.hpp"
#include "../htmlparser.hpp"
#include <iostream>
void LunchRest::PlacRestaurant::parse() {
Request r;

View File

@ -48,7 +48,7 @@ public:
};
class SuzziesRestaurant : public Restaurant {
public:
SuzziesRestaurant();
SuzziesRestaurant() : Restaurant("http://suzies.cz/poledni-menu.html", "Suzzie's") {}
virtual ~SuzziesRestaurant() = default;
virtual void parse() override;
private:

View File

@ -1,12 +1,6 @@
#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");
}
#include "../htmlparser.hpp"
int weekDay(const std::string &date_str) {
struct tm date_tm;
@ -15,28 +9,53 @@ int weekDay(const std::string &date_str) {
}
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() )
auto html = r.get(_url);
if(html == "")
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());
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) {
auto price = std::stoi(meal["price"].GetString());
menus[week_day].addMeal(price < 100, meal["name"].GetString(), "", price);
}
}
HtmlParser hparse(html);
auto &root = hparse.getRoot();
auto days = root.find("//div[@class='day']");
for(auto &day : days) {
auto *daynum = xmlGetProp(days[0]->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);
}
}