Fix Suzzie's
This commit is contained in:
parent
bcb6120199
commit
89be08e9a6
2
main.cpp
2
main.cpp
@ -101,7 +101,7 @@ int main(int argc, char **argv, char **env) {
|
|||||||
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>();
|
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();
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
#include "restaurants.hpp"
|
#include "restaurants.hpp"
|
||||||
#include "../network/network.hpp"
|
#include "../network/network.hpp"
|
||||||
#include "../htmlparser.hpp"
|
#include "../htmlparser.hpp"
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
void LunchRest::PlacRestaurant::parse() {
|
void LunchRest::PlacRestaurant::parse() {
|
||||||
Request r;
|
Request r;
|
||||||
|
@ -48,7 +48,7 @@ public:
|
|||||||
};
|
};
|
||||||
class SuzziesRestaurant : public Restaurant {
|
class SuzziesRestaurant : public Restaurant {
|
||||||
public:
|
public:
|
||||||
SuzziesRestaurant();
|
SuzziesRestaurant() : Restaurant("http://suzies.cz/poledni-menu.html", "Suzzie's") {}
|
||||||
virtual ~SuzziesRestaurant() = default;
|
virtual ~SuzziesRestaurant() = default;
|
||||||
virtual void parse() override;
|
virtual void parse() override;
|
||||||
private:
|
private:
|
||||||
|
@ -1,12 +1,6 @@
|
|||||||
#include "../environment.hpp"
|
|
||||||
#include "restaurants.hpp"
|
#include "restaurants.hpp"
|
||||||
#include "../network/network.hpp"
|
#include "../network/network.hpp"
|
||||||
#include <ctime>
|
#include "../htmlparser.hpp"
|
||||||
#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) {
|
int weekDay(const std::string &date_str) {
|
||||||
struct tm date_tm;
|
struct tm date_tm;
|
||||||
@ -15,28 +9,53 @@ int weekDay(const std::string &date_str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void LunchRest::SuzziesRestaurant::parse() {
|
void LunchRest::SuzziesRestaurant::parse() {
|
||||||
rapidjson::Document json;
|
|
||||||
Request r;
|
Request r;
|
||||||
r.addHeader("Accept: application/json");
|
auto html = r.get(_url);
|
||||||
r.addHeader("user_key: " + api_key);
|
if(html == "")
|
||||||
json.Parse(r.get(_url).c_str());
|
|
||||||
if( json.HasParseError() )
|
|
||||||
return;
|
|
||||||
const auto &json_menus = json["daily_menus"];
|
|
||||||
if( !json_menus.IsArray() )
|
|
||||||
return;
|
return;
|
||||||
clearMenus();
|
clearMenus();
|
||||||
for(size_t i = 0; i < json_menus.Size(); i++) {
|
HtmlParser hparse(html);
|
||||||
const auto &menu = json_menus[i]["daily_menu"];
|
auto &root = hparse.getRoot();
|
||||||
auto week_day = weekDay(menu["start_date"].GetString());
|
auto days = root.find("//div[@class='day']");
|
||||||
menus[week_day].setInvalidMenu(false);
|
for(auto &day : days) {
|
||||||
const auto &meals = menu["dishes"];
|
auto *daynum = xmlGetProp(days[0]->cobj(), (xmlChar*)"data-day");
|
||||||
for(size_t j = 0; j < meals.Size(); j++) {
|
int cur_day = 0;
|
||||||
const auto &meal = meals[j]["dish"];
|
switch(daynum[0]) {
|
||||||
if(strlen(meal["price"].GetString()) != 0) {
|
case '2':
|
||||||
auto price = std::stoi(meal["price"].GetString());
|
cur_day = 1;
|
||||||
menus[week_day].addMeal(price < 100, meal["name"].GetString(), "", price);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user