aboutsummaryrefslogblamecommitdiffstats
path: root/include/Utilities.hpp
blob: 781b9fb000d8063d82daafa566bbd8bf60584b1a (plain) (tree)


















































































































































                                                                                                                                                                                                                                                               
#pragma once

#include <ctime>
#include <string>
#include <curl/curl.h>
#include <boost/date_time.hpp>

#define NOW std::time(0)
#define LOG_TIME_FORMAT "%y-%m-%d %H:%M:%S"
#define HR_TIME_FORMAT "%d.%m.%y, %H:%M:%S UHR"
#define NOW_STRING TimeStampToHReadble(NOW, false)

static std::string TimeStampToHReadble(const time_t rawTime, bool HR_READABLE) {
    struct tm *dt;
    char buffer[30];
    dt = localtime(&rawTime);
    if (HR_READABLE) {
        strftime(buffer, sizeof(buffer), HR_TIME_FORMAT, dt);
    } else {
        strftime(buffer, sizeof(buffer), LOG_TIME_FORMAT, dt);
    }
    return std::string(buffer);
}

#include <fstream>

static std::string getApiTokenFromFile(const std::string &filePath) {
    std::string token = "NO-TOKEN";

    std::ifstream inFile(filePath);

    if (inFile) {
        // TODO: validate, at least a little - currently we just assume the file contains the correct data
        std::getline(inFile, token);
    } else {
        return "";
    }

    return token;
}

#ifdef NDEBUG

static std::string getProductionApiToken() {
    return getApiTokenFromFile("../release.api");
}

#else

static std::string getDebugApiToken() {
    return getApiTokenFromFile("../debug.api");
}

#endif

static CURLcode downloadFile(const std::string &url, const std::string &fileName) {

    CURL *curl;
    FILE *fp;
    CURLcode res;
    curl = curl_easy_init();

    if (curl) {
        fp = fopen(fileName.c_str(), "wb");

        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_USERAGENT,
                         "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"); //so apparently the Website stw.berlin blocks the IPwhen accessing it very often with the bot...
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        res = curl_easy_perform(curl);
        if (res != CURLE_OK) {
            //TODO: return int as status instead of boolean - maybe even this curl error?
            return CURLE_COULDNT_CONNECT;
        }
        curl_easy_cleanup(curl);
        fclose(fp);
        return CURLE_OK;
    } else {
        return CURLE_COULDNT_CONNECT;
    }
}

static CURLcode downloadMensaFile(const int &ID, const boost::posix_time::ptime &datum) {

    std::string datumString =
            std::to_string(datum.date().year()) + "-" + std::to_string(datum.date().month().as_number()) + "-" +
            std::to_string(datum.date().day());

    std::string postfield = "resources_id=" + std::to_string(ID) + "&date=" + datumString;

    CURL *curl;
    FILE *fp;
    CURLcode res;
    curl = curl_easy_init();

    if (curl) {
        fp = fopen(std::to_string(ID).c_str(), "wb");

        struct curl_slist *slist1;

        slist1 = nullptr;
        slist1 = curl_slist_append(slist1,
                                   "Cookie: seitenbereich=mensen; _ga=GA1.2.1719763776.1545837849; filterkennz=[]; PHPSESSID=9fa00137a2ad065fe595196ddb34e0ba; _gid=GA1.2.226483670.1549982576; loadDataForResource=191; storedPathname=/mensen.html; _gat=1");
        slist1 = curl_slist_append(slist1, "Origin: https://www.stw.berlin");
        slist1 = curl_slist_append(slist1, "Accept-Encoding: gzip, deflate, br");
        slist1 = curl_slist_append(slist1, "Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7");
        slist1 = curl_slist_append(slist1,
                                   "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36");
        slist1 = curl_slist_append(slist1, "Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
        slist1 = curl_slist_append(slist1, "Accept: */*");
        slist1 = curl_slist_append(slist1, "Referer: https://www.stw.berlin/mensen.html");
        slist1 = curl_slist_append(slist1, "X-Requested-With: XMLHttpRequest");
        slist1 = curl_slist_append(slist1, "Connection: keep-alive");
        slist1 = curl_slist_append(slist1, "DNT: 1");

        curl = curl_easy_init();
        curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 102400L);
        curl_easy_setopt(curl, CURLOPT_URL, "https://www.stw.berlin/xhr/speiseplan-wochentag.html");
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5L);
        curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postfield.c_str());
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t) 32);
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist1);
        curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.63.0");
        curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
        curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, (long) CURL_HTTP_VERSION_2TLS);
        curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
        curl_easy_setopt(curl, CURLOPT_SSH_KNOWNHOSTS, "/home/max/.ssh/known_hosts");
        curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);

        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        res = curl_easy_perform(curl);
        if (res != CURLE_OK) {
            //TODO: return int as status instead of boolean - maybe even this curl error?
            return res;
        }
        curl_easy_cleanup(curl);
        curl = nullptr;
        curl_slist_free_all(slist1);
        slist1 = nullptr;
        fclose(fp);
        return CURLE_OK;
    } else {
        return CURLE_FAILED_INIT;
    }
}