Add refresh

This commit is contained in:
zvon 2020-09-15 23:04:16 +02:00
parent 09b314c5b0
commit 5b67c65a03
6 changed files with 17 additions and 5 deletions

View File

@ -11,6 +11,13 @@
std::vector<std::string> days = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"}; std::vector<std::string> days = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"};
std::vector<std::unique_ptr<LunchRest::Restaurant>> restaurants; std::vector<std::unique_ptr<LunchRest::Restaurant>> restaurants;
void refresh( const std::shared_ptr< restbed::Session > session ) {
for(auto &x : restaurants)
x->parse();
std::string response = "Refreshed menus!";
session->close(restbed::OK, response, { { "Content-Length", std::to_string(response.length()) }, { "Access-Control-Allow-Origin", "*" } });
}
void get_all( const std::shared_ptr< restbed::Session > session ) { void get_all( const std::shared_ptr< restbed::Session > session ) {
std::stringstream ss{}; std::stringstream ss{};
bool atleastonerest = false; bool atleastonerest = false;
@ -108,6 +115,11 @@ int main() {
getserv->set_method_handler( "GET", get ); getserv->set_method_handler( "GET", get );
service.publish(getserv); service.publish(getserv);
auto refreshserv = std::make_shared< restbed::Resource >();
refreshserv->set_path("/refresh");
refreshserv->set_method_handler( "GET", refresh );
service.publish(refreshserv);
auto ssl_settings = std::make_shared<restbed::SSLSettings>(); auto ssl_settings = std::make_shared<restbed::SSLSettings>();
ssl_settings->set_http_disabled(true); ssl_settings->set_http_disabled(true);
ssl_settings->set_private_key(restbed::Uri("file:///home/zvon/data/programming/lunch-rest/example.key")); ssl_settings->set_private_key(restbed::Uri("file:///home/zvon/data/programming/lunch-rest/example.key"));

View File

@ -10,11 +10,11 @@ std::string removeAlergens(const std::string &name) {
} }
void LunchRest::AlCaponeRestaurant::parse() { void LunchRest::AlCaponeRestaurant::parse() {
clearMenus();
Request r; Request r;
auto html = r.get(_url); auto html = r.get(_url);
if(html == "") if(html == "")
return; return;
clearMenus();
HtmlParser hparse(html); HtmlParser hparse(html);
auto &root = hparse.getRoot(); auto &root = hparse.getRoot();
auto rows = root.find("//table[@class='table table-responsive']/tbody/tr"); auto rows = root.find("//table[@class='table table-responsive']/tbody/tr");

View File

@ -13,11 +13,11 @@ bool isWhiteSpaceOnly(const std::string &text) {
} }
void LunchRest::LightOfIndiaRestaurant::parse() { void LunchRest::LightOfIndiaRestaurant::parse() {
clearMenus();
Request r; Request r;
auto html = r.get(_url); auto html = r.get(_url);
if(html == "") if(html == "")
return; return;
clearMenus();
HtmlParser hparse(html); HtmlParser hparse(html);
auto &root = hparse.getRoot(); auto &root = hparse.getRoot();
auto container = root.find("//div[@id='content_container']")[0]; auto container = root.find("//div[@id='content_container']")[0];

View File

@ -3,12 +3,12 @@
#include "../htmlparser.hpp" #include "../htmlparser.hpp"
void LunchRest::PadagaliRestaurant::parse() { void LunchRest::PadagaliRestaurant::parse() {
clearMenus();
int menu_index = 0; int menu_index = 0;
Request r; Request r;
auto html = r.get(_url); auto html = r.get(_url);
if(html == "") if(html == "")
return; return;
clearMenus();
HtmlParser hparse(html); HtmlParser hparse(html);
auto &root = hparse.getRoot(); auto &root = hparse.getRoot();
auto days = root.find("//div[@class='glf-mor-restaurant-menu-category']"); auto days = root.find("//div[@class='glf-mor-restaurant-menu-category']");

View File

@ -3,12 +3,12 @@
#include "../htmlparser.hpp" #include "../htmlparser.hpp"
void LunchRest::UDrevakaRestaurant::parse() { void LunchRest::UDrevakaRestaurant::parse() {
clearMenus();
int menu_index = 0; int menu_index = 0;
Request r; Request r;
auto html = r.get(_url); auto html = r.get(_url);
if(html == "") if(html == "")
return; return;
clearMenus();
HtmlParser hparse(html); HtmlParser hparse(html);
auto &root = hparse.getRoot(); auto &root = hparse.getRoot();
auto days = root.find("//li[@class='item-day']"); auto days = root.find("//li[@class='item-day']");

View File

@ -3,12 +3,12 @@
#include "../htmlparser.hpp" #include "../htmlparser.hpp"
void LunchRest::UKarlaRestaurant::parse() { void LunchRest::UKarlaRestaurant::parse() {
clearMenus();
int menu_index = 0; int menu_index = 0;
Request r; Request r;
auto html = r.get(_url); auto html = r.get(_url);
if(html == "") if(html == "")
return; return;
clearMenus();
HtmlParser hparse(html); HtmlParser hparse(html);
auto &root = hparse.getRoot(); auto &root = hparse.getRoot();
auto days = root.find("//li[@class='item-day']"); auto days = root.find("//li[@class='item-day']");