aboutsummaryrefslogtreecommitdiffstats
path: root/src/GetEssen.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/GetEssen.cpp')
-rw-r--r--src/GetEssen.cpp135
1 files changed, 55 insertions, 80 deletions
diff --git a/src/GetEssen.cpp b/src/GetEssen.cpp
index 3825c5c..28a54c9 100644
--- a/src/GetEssen.cpp
+++ b/src/GetEssen.cpp
@@ -1,59 +1,50 @@
+//
+// Created by max on 18.12.18.
+//
+
#include "GetEssen.hpp"
-#include "sqdb.hpp"
-#include "Utilities.hpp"
+using namespace boost::posix_time;
+using namespace std;
-using std::string;
+#define SQLITEPATH "../../main.sqlite"
-bool
-Handler::MensaHandler::db_update_mensa_message(const int &mensaID, const ptime &mensaDatum, const string &message) {
- sqdb::Db db("../main.sqlite");
- string newTime = to_simple_string(ptime(boost::posix_time::second_clock::local_time()));
- string mensaDatumString =
- std::to_string(mensaDatum.date().year()) + "-" + mensaDatum.date().month().as_short_string() + "-" +
- std::to_string(mensaDatum.date().day().as_number());
+bool Handler::MensaHandler::db_update_mensa_message(const int& mensaID, const ptime& mensaDatum, const string& message){
+ sqdb::Db db(SQLITEPATH);
+ string newTime = to_simple_string(ptime(second_clock::local_time()));
+ string mensaDatumString = to_string(mensaDatum.date().year()) + "-" + mensaDatum.date().month().as_short_string() + "-" + to_string(mensaDatum.date().day().as_number());
sqdb::QueryStr str;
- db.Query(str.Format(
- SQDB_MAKE_TEXT("update mensa set Message = '%s', LastModified = '%s' where ID = %i and Datum = '%s';"),
- message.c_str(), newTime.c_str(), mensaID, mensaDatumString.c_str())).Next();
+ db.Query(str.Format(SQDB_MAKE_TEXT("update mensa set Message = '%s', LastModified = '%s' where ID = %i and Datum = '%s';"), message.c_str(), newTime.c_str(), mensaID, mensaDatumString.c_str())).Next();
return true;
}
-bool
-Handler::MensaHandler::db_insert_mensa_message(const int &mensaID, const ptime &mensaDatum, const string &message) {
- sqdb::Db db("../main.sqlite");
- string newTime = to_simple_string(ptime(boost::posix_time::second_clock::local_time()));
- string mensaDatumString =
- std::to_string(mensaDatum.date().year()) + "-" + mensaDatum.date().month().as_short_string() + "-" +
- std::to_string(mensaDatum.date().day().as_number());
+bool Handler::MensaHandler::db_insert_mensa_message(const int& mensaID, const ptime& mensaDatum, const string& message){
+ sqdb::Db db(SQLITEPATH);
+ string newTime = to_simple_string(ptime(second_clock::local_time()));
+ string mensaDatumString = to_string(mensaDatum.date().year()) + "-" + mensaDatum.date().month().as_short_string() + "-" + to_string(mensaDatum.date().day().as_number());
sqdb::QueryStr str;
- db.Query(str.Format(SQDB_MAKE_TEXT("insert into mensa values(%i, '%s', '%s', '%s');"), mensaID, message.c_str(),
- mensaDatumString.c_str(), newTime.c_str())).Next();
+ db.Query(str.Format(SQDB_MAKE_TEXT("insert into mensa values(%i, '%s', '%s', '%s');"), mensaID, message.c_str(), mensaDatumString.c_str(), newTime.c_str())).Next();
return true;
}
-Handler::MensaHandler::MensaEssen
-Handler::MensaHandler::db_get_mensa_message(const int &mensaID, const ptime &mensaDatum) {
- MensaEssen out;
- sqdb::Db db("../main.sqlite");
+Handler::MensaHandler::MensaEssen Handler::MensaHandler::db_get_mensa_message(const int& mensaID, const ptime& mensaDatum) {
+ Handler::MensaHandler::MensaEssen out;
+ sqdb::Db db(SQLITEPATH);
sqdb::QueryStr str;
- string mensaDatumString =
- std::to_string(mensaDatum.date().year()) + "-" + mensaDatum.date().month().as_short_string() + "-" +
- std::to_string(mensaDatum.date().day().as_number());
- sqdb::Statement s = db.Query(
- str.Format(SQDB_MAKE_TEXT("SELECT * from mensa WHERE Datum = '%s' AND ID = %i;"), mensaDatumString.c_str(),
- mensaID));
+ string mensaDatumString = to_string(mensaDatum.date().year()) + "-" + mensaDatum.date().month().as_short_string() + "-" + to_string(mensaDatum.date().day().as_number());
try {
- if (s.Next()) {
+ sqdb::Statement s = db.Query(str.Format(SQDB_MAKE_TEXT("SELECT * from mensa WHERE Datum = '%s' AND ID = %i;"), mensaDatumString.c_str(), mensaID ));
+ //sqdb::Statement s = db.Query(str.Format(SQDB_MAKE_TEXT("SELECT * from mensa"))); // WHERE Datum = '%s' AND ID = %i;"), mensaDatumString.c_str(), mensaID ));
+ if(s.Next()) {
int ID = s.GetField(0);
string Message = s.GetField(1);
string Datum = s.GetField(2);
string LastModified = s.GetField(3);
- ptime LMDate(boost::posix_time::time_from_string(LastModified));
+ ptime LMDate(time_from_string(LastModified));
out.ID = ID;
out.Message = Message;
@@ -61,31 +52,31 @@ Handler::MensaHandler::db_get_mensa_message(const int &mensaID, const ptime &men
out.LastModified = LMDate;
return out;
}
- } catch (sqdb::Exception &exception) {
- std::cerr << "Error code: " << exception.GetErrorCode() << " Error Message: " << exception.GetErrorMsg()
- << std::endl;
+ } catch (sqdb::Exception &exception){
+ std::cerr << "Error code: " << exception.GetErrorCode() << " Error Message: " << exception.GetErrorMsg() << "\n";
}
+
out.ID = -2;
return out;
}
string Handler::MensaHandler::getEssen(const int mensa_ID, const int offset) {
- MensaEssen Essen;
+ Handler::MensaHandler::MensaEssen Essen;
if (offset >= 0) {
if (offset > 28) {
return "Ich fühle mich geschmeichelt, dass du denkst, dass ich so weit in die Zukunft gucken kann (0 bis 28 Tage)";
}
- ptime datumNow = boost::posix_time::second_clock::local_time();
+ ptime datumNow = second_clock::local_time();
ptime datumEssen = datumNow + (boost::gregorian::days(offset));
Essen = db_get_mensa_message(mensa_ID, datumEssen);
if (Essen.ID == -2) { // SQL got nothing back :/
//log("sql got nothing back!");
- if (downloadMensaFile(mensa_ID, datumEssen) != CURLE_OK) {
- std::cerr << "Error downloading file" << std::endl;
+ if (downloadMensaFile(mensa_ID, datumEssen) != CURLE_OK){
+ std::cerr << "Error downloading file\n";
return "Error downloading file!";
}
db_insert_mensa_message(mensa_ID, datumEssen, read_mensa_message_from_file(mensa_ID));
@@ -94,34 +85,25 @@ string Handler::MensaHandler::getEssen(const int mensa_ID, const int offset) {
string out;
switch (Essen.ID) {
case mensa::Adlershof :
- out = "<b>Mensa Adlershof</b> am " + std::to_string(Essen.Datum.date().day().as_number()) + "." +
- std::to_string(Essen.Datum.date().month().as_number()) + "." +
- std::to_string(Essen.Datum.date().year()) + ":";
+ out = "Mensa Adlershof am " + to_string(Essen.Datum.date().day().as_number()) + "." + to_string(Essen.Datum.date().month().as_number()) + "." + to_string(Essen.Datum.date().year()) + ":";
break;
- case mensa::Nord:
- out = "<b>Mensa Nord</b> am " + std::to_string(Essen.Datum.date().day().as_number()) + "." +
- std::to_string(Essen.Datum.date().month().as_number()) + "." +
- std::to_string(Essen.Datum.date().year()) + ":";
+ case mensa ::Nord:
+ out = "Mensa Nord am " + to_string(Essen.Datum.date().day().as_number()) + "." + to_string(Essen.Datum.date().month().as_number()) + "." + to_string(Essen.Datum.date().year()) + ":";
break;
case mensa::Sued :
- out = "<b>Mensa Sued</b> am " + std::to_string(Essen.Datum.date().day().as_number()) + "." +
- std::to_string(Essen.Datum.date().month().as_number()) + "." +
- std::to_string(Essen.Datum.date().year()) + ":";
+ out = "Mensa Sued am " + to_string(Essen.Datum.date().day().as_number()) + "." + to_string(Essen.Datum.date().month().as_number()) + "." + to_string(Essen.Datum.date().year()) + ":";
break;
default:
- out = "Mensa Unbekannt am " + std::to_string(Essen.Datum.date().day().as_number()) + "." +
- std::to_string(Essen.Datum.date().month().as_number()) + "." +
- std::to_string(Essen.Datum.date().year()) + ":";
+ out = "Mensa Unbekannt am " + to_string(Essen.Datum.date().day().as_number()) + "." + to_string(Essen.Datum.date().month().as_number()) + "." + to_string(Essen.Datum.date().year()) + ":";
break;
}
out += Essen.Message;
out += "\n (Stand: " + to_simple_string(Essen.LastModified) + ")\n";
-
return out;
}
- if (Essen.LastModified + boost::posix_time::hours(2) < datumNow) { //LastModified is more than 2 hours old
+ if (Essen.LastModified + hours(2) < datumNow) { //LastModified is more than 2 hours old
if (downloadMensaFile(mensa_ID, datumEssen) != CURLE_OK) {
return "Error downloading file!";
}
@@ -132,40 +114,28 @@ string Handler::MensaHandler::getEssen(const int mensa_ID, const int offset) {
string out;
switch (Essen.ID) {
case mensa::Adlershof :
- out = "<b>Mensa Adlershof</b> am " + std::to_string(Essen.Datum.date().day().as_number()) + "." +
- std::to_string(Essen.Datum.date().month().as_number()) + "." +
- std::to_string(Essen.Datum.date().year()) +
- ":";
+ out = "Mensa Adlershof am " + to_string(Essen.Datum.date().day().as_number()) + "." + to_string(Essen.Datum.date().month().as_number()) + "." + to_string(Essen.Datum.date().year()) + ":";
break;
case mensa::Nord :
- out = "<b>Mensa Nord</b> am " + std::to_string(Essen.Datum.date().day().as_number()) + "." +
- std::to_string(Essen.Datum.date().month().as_number()) + "." +
- std::to_string(Essen.Datum.date().year()) +
- ":";
+ out = "Mensa Nord am " + to_string(Essen.Datum.date().day().as_number()) + "." + to_string(Essen.Datum.date().month().as_number()) + "." + to_string(Essen.Datum.date().year()) + ":";
break;
case mensa::Sued :
- out = "<b>Mensa Sued</b> am " + std::to_string(Essen.Datum.date().day().as_number()) + "." +
- std::to_string(Essen.Datum.date().month().as_number()) + "." +
- std::to_string(Essen.Datum.date().year()) +
- ":";
+ out = "Mensa Sued am " + to_string(Essen.Datum.date().day().as_number()) + "." + to_string(Essen.Datum.date().month().as_number()) + "." + to_string(Essen.Datum.date().year()) + ":";
break;
default:
- out = "Mensa Unbekannt am " + std::to_string(Essen.Datum.date().day().as_number()) + "." +
- std::to_string(Essen.Datum.date().month().as_number()) + "." +
- std::to_string(Essen.Datum.date().year()) +
- ":";
+ out = "Mensa Unbekannt am " + to_string(Essen.Datum.date().day().as_number()) + "." + to_string(Essen.Datum.date().month().as_number()) + "." + to_string(Essen.Datum.date().year()) + ":";
break;
}
out += Essen.Message;
out += "\n (Stand: " + to_simple_string(Essen.LastModified) + ")\n";
+ std::cerr << out;
return out;
- }
+ } else return "Wer denkst du bin ich? Ein Zeitreisender?\nIch kann zwar in die Zukunft gucken, aber nur 28 Tage";
- return "Wer denkst du bin ich? Ein Zeitreisender?\nIch kann zwar in die Zukunft gucken, aber nur 28 Tage";
}
-string Handler::MensaHandler::read_mensa_message_from_file(const int &mensa_id_file) {
+string Handler::MensaHandler::read_mensa_message_from_file(const int& mensa_id_file) {
string s;
string out;
bool serious = false;
@@ -218,14 +188,14 @@ string Handler::MensaHandler::read_mensa_message_from_file(const int &mensa_id_f
}
serious = (s == "Aktionen" || s == "Essen" || s == "Beilagen");
if (serious) {
- out += "\n<b>" + s + "</b>\n";
+ out += "\n" + s + "\n";
}
}
if (s.find("class=\"bold\"") != string::npos && serious) { //Getting the Meal
for (int i = 0, j = 0; (unsigned) i < s.length(); ++i) {
if (s.at(i) == '>') {
j = i + 1;
- //logStatus("Zeichen > gefunden in Position" + std::std::to_string(i));
+ //logStatus("Zeichen > gefunden in Position" + std::to_string(i));
}
if (s.at(i) == '<' && j != 0) {
s = s.substr(j, i - j);
@@ -247,7 +217,7 @@ string Handler::MensaHandler::read_mensa_message_from_file(const int &mensa_id_f
j = 0;
}
}
- out += "\t<b>" + s + "</b>€\n";
+ out += "\t" + s + "€\n";
}
}
@@ -258,12 +228,16 @@ string Handler::MensaHandler::read_mensa_message_from_file(const int &mensa_id_f
}
+
Handler::json Handler::mensaHandler(const std::string &arguments, const std::string &session, void *payload) {
+ (void) payload;
Handler::MensaHandler::mensa Mensa_ID;
+ //return Response::simple_response("Mensafunktion gerade nicht verfügbar. Bitte versuchen Sie es Später erneut.");
+
auto args = tokenizeArguments(arguments);
- std::string mensa_name = (args.size() > 0 ? args.at(0) : "adlershof");
+ std::string mensa_name = (!args.empty() ? args.at(0) : "adlershof");
std::string offset_request = (args.size() > 1 ? args.at(1) : "0");
if (mensa_name == "adlershof") {
@@ -278,5 +252,6 @@ Handler::json Handler::mensaHandler(const std::string &arguments, const std::str
int offset = std::stoi(offset_request);
- return Response::simple_response(getEssen(Mensa_ID, offset));
+ return Response::simple_response(getEssen(Mensa_ID, offset), session, true);
+
}