Add try/catch
This commit is contained in:
parent
ae57b4c06e
commit
913c8fd168
@ -10,30 +10,37 @@ void LunchRest::MahostinaRestaurant::parse() {
|
||||
if (html == "")
|
||||
return;
|
||||
clearMenus();
|
||||
HtmlParser hparse(html);
|
||||
auto &root = hparse.getRoot();
|
||||
auto today_lists = root.find(
|
||||
"//div[@id='dnesnibasta-section']//ul[@data-rte-list='default']");
|
||||
if (today_lists.empty()) {
|
||||
std::cout << "No meals :(" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
time_t t = time(nullptr);
|
||||
tm *timePtr = localtime(&t);
|
||||
auto day = (timePtr->tm_wday + 6) % 7;
|
||||
|
||||
for (auto &meal : today_lists[0]->find("./li/p/text()")) {
|
||||
auto text = nodeToText(meal);
|
||||
auto price_end = text.find(",-");
|
||||
auto price_start = price_end - 1;
|
||||
while (text[price_start] >= '0' && text[price_start] <= '9') {
|
||||
price_start -= 1;
|
||||
try {
|
||||
HtmlParser hparse(html);
|
||||
auto &root = hparse.getRoot();
|
||||
auto today_lists = root.find(
|
||||
"//div[@id='dnesnibasta-section']//ul[@data-rte-list='default']");
|
||||
if (today_lists.empty()) {
|
||||
std::cout << "No meals :(" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
time_t t = time(nullptr);
|
||||
tm *timePtr = localtime(&t);
|
||||
auto day = (timePtr->tm_wday + 6) % 7;
|
||||
|
||||
for (auto &meal : today_lists[0]->find("./li/p/text()")) {
|
||||
auto text = nodeToText(meal);
|
||||
auto price_end = text.find(",-");
|
||||
auto price_start = price_end - 1;
|
||||
while (text[price_start] >= '0' && text[price_start] <= '9') {
|
||||
price_start -= 1;
|
||||
}
|
||||
price_start += 1;
|
||||
menus[day].addMeal(
|
||||
false, text.substr(0, price_start - 1), "",
|
||||
std::stoi(text.substr(price_start, price_end - price_start)));
|
||||
}
|
||||
menus[day].setInvalidMenu(false);
|
||||
} catch (std::exception &/*UNUSED*/) {
|
||||
clearMenus();
|
||||
for(int i = 0; i < menus.size(); i++) {
|
||||
menus[i].addMeal(false, parseError, parseInfo, 0);
|
||||
}
|
||||
price_start += 1;
|
||||
menus[day].addMeal(
|
||||
false, text.substr(0, price_start - 1), "",
|
||||
std::stoi(text.substr(price_start, price_end - price_start)));
|
||||
}
|
||||
menus[day].setInvalidMenu(false);
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "menicka.hpp"
|
||||
#include "restaurants.hpp"
|
||||
#include "../network/network.hpp"
|
||||
#include "../htmlparser.hpp"
|
||||
#include "functions.hpp"
|
||||
@ -24,50 +25,57 @@ void LunchRest::MenickaRestaurant::parse() {
|
||||
if(html == "")
|
||||
return;
|
||||
clearMenus();
|
||||
HtmlParser hparse(html);
|
||||
auto &root = hparse.getRoot();
|
||||
auto days = root.find("//div[@class='menicka']");
|
||||
for(auto &day : days) {
|
||||
auto daytext = day->find("./div[@class='nadpis']/text()");
|
||||
auto soup = day->find("./ul/li[@class='polevka']");
|
||||
auto meals = day->find("./ul/li[@class='jidlo']");
|
||||
if(daytext.size() == 0) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
HtmlParser hparse(html);
|
||||
auto &root = hparse.getRoot();
|
||||
auto days = root.find("//div[@class='menicka']");
|
||||
for(auto &day : days) {
|
||||
auto daytext = day->find("./div[@class='nadpis']/text()");
|
||||
auto soup = day->find("./ul/li[@class='polevka']");
|
||||
auto meals = day->find("./ul/li[@class='jidlo']");
|
||||
if(daytext.size() == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto daystr = nodeToText(daytext[0]);
|
||||
int cur_day = 0;
|
||||
switch (daystr[0]) {
|
||||
case 'P':
|
||||
if (daystr[1] != 'o') {
|
||||
cur_day = 4;
|
||||
auto daystr = nodeToText(daytext[0]);
|
||||
int cur_day = 0;
|
||||
switch (daystr[0]) {
|
||||
case 'P':
|
||||
if (daystr[1] != 'o') {
|
||||
cur_day = 4;
|
||||
}
|
||||
break;
|
||||
case "Ú"[0]:
|
||||
cur_day = 1;
|
||||
break;
|
||||
case 'S':
|
||||
cur_day = 2;
|
||||
break;
|
||||
case "Č"[0]:
|
||||
cur_day = 3;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case "Ú"[0]:
|
||||
cur_day = 1;
|
||||
break;
|
||||
case 'S':
|
||||
cur_day = 2;
|
||||
break;
|
||||
case "Č"[0]:
|
||||
cur_day = 3;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if(soup.size() != 0) {
|
||||
auto soupData = getNameAndPrice(soup[0]);
|
||||
if(!soupData.first.empty()) {
|
||||
menus[cur_day].addMeal(Meal(true, soupData.first, "", soupData.second));
|
||||
if(soup.size() != 0) {
|
||||
auto soupData = getNameAndPrice(soup[0]);
|
||||
if(!soupData.first.empty()) {
|
||||
menus[cur_day].addMeal(Meal(true, soupData.first, "", soupData.second));
|
||||
}
|
||||
}
|
||||
for(auto &meal : meals) {
|
||||
auto mealData = getNameAndPrice(meal);
|
||||
if(!mealData.first.empty()) {
|
||||
menus[cur_day].addMeal(Meal(false, mealData.first, "", mealData.second));
|
||||
}
|
||||
}
|
||||
if(!menus[cur_day].getMeals().empty()) {
|
||||
menus[cur_day].setInvalidMenu(false);
|
||||
}
|
||||
}
|
||||
for(auto &meal : meals) {
|
||||
auto mealData = getNameAndPrice(meal);
|
||||
if(!mealData.first.empty()) {
|
||||
menus[cur_day].addMeal(Meal(false, mealData.first, "", mealData.second));
|
||||
}
|
||||
}
|
||||
if(!menus[cur_day].getMeals().empty()) {
|
||||
menus[cur_day].setInvalidMenu(false);
|
||||
} catch (std::exception &/*UNUSED*/) {
|
||||
clearMenus();
|
||||
for(int i = 0; i < menus.size(); i++) {
|
||||
menus[i].addMeal(false, parseError, parseInfo, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,8 @@
|
||||
#include <libxml++/libxml++.h>
|
||||
|
||||
namespace LunchRest {
|
||||
const std::string parseError = "Could not retreive menu";
|
||||
const std::string parseInfo = "Please contact the developer, likely the restaurant website changed and requires a new parser";
|
||||
class UKarlaRestaurant : public Restaurant {
|
||||
public:
|
||||
UKarlaRestaurant() : Restaurant("https://ukarlabrno.cz/denni-menu/", "U Karla") {}
|
||||
|
@ -37,79 +37,86 @@ void LunchRest::TaoRestaurant::parse() {
|
||||
if (html == "")
|
||||
return;
|
||||
clearMenus();
|
||||
HtmlParser hparse(html);
|
||||
auto &root = hparse.getRoot();
|
||||
auto week_meals_html =
|
||||
root.find("//div[@class='ct-div-block']/div[@class='ct-div-block "
|
||||
"tydenni-menu-div']");
|
||||
if (week_meals_html.empty()) {
|
||||
std::cout << "No week meals :(" << std::endl;
|
||||
return;
|
||||
}
|
||||
auto daily_meals_html =
|
||||
root.find("//div[@class='ct-section-inner-wrap']/"
|
||||
"div[@class='ct-div-block tydenni-menu-div']");
|
||||
if (daily_meals_html.empty()) {
|
||||
std::cout << "No daily meals :(" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<Meal> week_meals{};
|
||||
for (auto &meal : week_meals_html) {
|
||||
auto texts = meal->find(".//span/text()");
|
||||
if (!texts.empty()) {
|
||||
auto text = nodeToText(texts[0]);
|
||||
auto price_positions = getPricePosFromText(text);
|
||||
if(price_positions.first == (size_t)-1) {
|
||||
week_meals.emplace_back(false, text, "", 0);
|
||||
continue;
|
||||
}
|
||||
auto end_pos = getEndOfTextPos(text, price_positions.first);
|
||||
week_meals.emplace_back(
|
||||
false, text.substr(0, end_pos), "",
|
||||
std::stoi(text.substr(price_positions.first,
|
||||
price_positions.second)));
|
||||
try {
|
||||
HtmlParser hparse(html);
|
||||
auto &root = hparse.getRoot();
|
||||
auto week_meals_html =
|
||||
root.find("//div[@class='ct-div-block']/div[@class='ct-div-block "
|
||||
"tydenni-menu-div']");
|
||||
if (week_meals_html.empty()) {
|
||||
std::cout << "No week meals :(" << std::endl;
|
||||
return;
|
||||
}
|
||||
auto daily_meals_html =
|
||||
root.find("//div[@class='ct-section-inner-wrap']/"
|
||||
"div[@class='ct-div-block tydenni-menu-div']");
|
||||
if (daily_meals_html.empty()) {
|
||||
std::cout << "No daily meals :(" << std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
for (auto &meal : daily_meals_html) {
|
||||
auto texts = meal->find(".//span/text()");
|
||||
auto days = meal->find(".//div/b/text()");
|
||||
if (!texts.empty() && !days.empty()) {
|
||||
auto text = nodeToText(texts[0]);
|
||||
auto day = nodeToText(days[0]);
|
||||
auto price_positions = getPricePosFromText(text);
|
||||
if(price_positions.first == (size_t)-1) {
|
||||
continue;
|
||||
}
|
||||
auto end_pos = getEndOfTextPos(text, price_positions.first);
|
||||
|
||||
int cur_day = 0;
|
||||
switch (day[0]) {
|
||||
case 'P':
|
||||
if (day[1] != 'o') {
|
||||
cur_day = 4;
|
||||
std::vector<Meal> week_meals{};
|
||||
for (auto &meal : week_meals_html) {
|
||||
auto texts = meal->find(".//span/text()");
|
||||
if (!texts.empty()) {
|
||||
auto text = nodeToText(texts[0]);
|
||||
auto price_positions = getPricePosFromText(text);
|
||||
if(price_positions.first == (size_t)-1) {
|
||||
week_meals.emplace_back(false, text, "", 0);
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
case "Ú"[0]:
|
||||
cur_day = 1;
|
||||
break;
|
||||
case 'S':
|
||||
cur_day = 2;
|
||||
break;
|
||||
case "Č"[0]:
|
||||
cur_day = 3;
|
||||
default:
|
||||
break;
|
||||
auto end_pos = getEndOfTextPos(text, price_positions.first);
|
||||
week_meals.emplace_back(
|
||||
false, text.substr(0, end_pos), "",
|
||||
std::stoi(text.substr(price_positions.first,
|
||||
price_positions.second)));
|
||||
}
|
||||
}
|
||||
for (auto &meal : daily_meals_html) {
|
||||
auto texts = meal->find(".//span/text()");
|
||||
auto days = meal->find(".//div/b/text()");
|
||||
if (!texts.empty() && !days.empty()) {
|
||||
auto text = nodeToText(texts[0]);
|
||||
auto day = nodeToText(days[0]);
|
||||
auto price_positions = getPricePosFromText(text);
|
||||
if(price_positions.first == (size_t)-1) {
|
||||
continue;
|
||||
}
|
||||
auto end_pos = getEndOfTextPos(text, price_positions.first);
|
||||
|
||||
Meal cur_meal(false, text.substr(0, end_pos), "",
|
||||
std::stoi(text.substr(price_positions.first,
|
||||
price_positions.second)));
|
||||
menus[cur_day].addMeal(cur_meal);
|
||||
for (auto &meal : week_meals) {
|
||||
menus[cur_day].addMeal(meal);
|
||||
int cur_day = 0;
|
||||
switch (day[0]) {
|
||||
case 'P':
|
||||
if (day[1] != 'o') {
|
||||
cur_day = 4;
|
||||
}
|
||||
break;
|
||||
case "Ú"[0]:
|
||||
cur_day = 1;
|
||||
break;
|
||||
case 'S':
|
||||
cur_day = 2;
|
||||
break;
|
||||
case "Č"[0]:
|
||||
cur_day = 3;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
Meal cur_meal(false, text.substr(0, end_pos), "",
|
||||
std::stoi(text.substr(price_positions.first,
|
||||
price_positions.second)));
|
||||
menus[cur_day].addMeal(cur_meal);
|
||||
for (auto &meal : week_meals) {
|
||||
menus[cur_day].addMeal(meal);
|
||||
}
|
||||
menus[cur_day].setInvalidMenu(false);
|
||||
}
|
||||
menus[cur_day].setInvalidMenu(false);
|
||||
}
|
||||
} catch (std::exception &/*UNUSED*/) {
|
||||
clearMenus();
|
||||
for(int i = 0; i < menus.size(); i++) {
|
||||
menus[i].addMeal(false, parseError, parseInfo, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,29 +10,40 @@ void LunchRest::UKarlaRestaurant::parse() {
|
||||
if(html == "")
|
||||
return;
|
||||
clearMenus();
|
||||
HtmlParser hparse(html);
|
||||
auto &root = hparse.getRoot();
|
||||
auto days = root.find("//li[@class='item-day']");
|
||||
int validdays = 0;
|
||||
for(auto &day : days) {
|
||||
validdays++;
|
||||
auto meals = day->find("./div[@class='row']");
|
||||
for(auto &meal : meals) {
|
||||
auto soup = false;
|
||||
auto texts = meal->find("./div/text()");
|
||||
std::string name = trim(nodeToText(texts[0]));
|
||||
if(name[0] == 'P') {
|
||||
soup = true;
|
||||
name = name.substr(10);
|
||||
} else {
|
||||
name = name.substr(3);
|
||||
try {
|
||||
HtmlParser hparse(html);
|
||||
auto &root = hparse.getRoot();
|
||||
auto days = root.find("//li[@class='item-day']");
|
||||
int validdays = 0;
|
||||
for(auto &day : days) {
|
||||
validdays++;
|
||||
auto meals = day->find("./div[@class='row']");
|
||||
for(auto &meal : meals) {
|
||||
auto soup = false;
|
||||
auto texts = meal->find("./div/text()");
|
||||
if(texts.empty()) {
|
||||
menus[menu_index].addMeal(false, parseError, parseInfo, 0);
|
||||
continue;
|
||||
}
|
||||
std::string name = trim(nodeToText(texts[0]));
|
||||
if(name[0] == 'P') {
|
||||
soup = true;
|
||||
name = name.substr(10);
|
||||
} else {
|
||||
name = name.substr(3);
|
||||
}
|
||||
int price = -1;
|
||||
if(texts.size() > 1)
|
||||
price = std::stoi(trim(nodeToText(texts[1])));
|
||||
menus[menu_index].addMeal(soup, name, "", price);
|
||||
menus[menu_index].setInvalidMenu(false);
|
||||
}
|
||||
int price = -1;
|
||||
if(texts.size() > 1)
|
||||
price = std::stoi(trim(nodeToText(texts[1])));
|
||||
menus[menu_index].addMeal(soup, name, "", price);
|
||||
menus[menu_index].setInvalidMenu(false);
|
||||
menu_index++;
|
||||
}
|
||||
} catch (std::exception &/*UNUSED*/) {
|
||||
clearMenus();
|
||||
for(int i = 0; i < menus.size(); i++) {
|
||||
menus[i].addMeal(false, parseError, parseInfo, 0);
|
||||
}
|
||||
menu_index++;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user