From 43aff4b348346a0efa5ce16a625e40aee184c08d Mon Sep 17 00:00:00 2001 From: Markus Date: Fri, 4 Oct 2019 20:11:01 +0200 Subject: [PATCH] add update warn system => V3.0.0 --- pageManager.cpp | 8 ++++++++ pageManager.h | 2 ++ parameterManager.cpp | 12 +++++++++++- parameterManager.h | 10 +++++++--- programManager.cpp | 42 +++++++++++++++++++++++++++++++++++++++++- programManager.h | 1 + 6 files changed, 70 insertions(+), 5 deletions(-) diff --git a/pageManager.cpp b/pageManager.cpp index 8ea6fb9..251181c 100644 --- a/pageManager.cpp +++ b/pageManager.cpp @@ -267,6 +267,14 @@ std::string PageManager::upper_string(const std::string &str) return upper; } +size_t PageManager::getDate() +{ + std::time_t now = std::time(nullptr); + struct tm *tm_now = localtime(&now); + return static_cast( static_cast(1900 + tm_now->tm_year) * 365.24220 + + static_cast(tm_now->tm_mon +1) * 30.43685 + tm_now->tm_mday); +} + int PageManager::writeToFile(std::string path, std::string text) { if(path == "") diff --git a/pageManager.h b/pageManager.h index 30d15d2..e5636e2 100644 --- a/pageManager.h +++ b/pageManager.h @@ -7,6 +7,7 @@ #include #include // sleep #include +#include // std::time, localtime ... struct Reply { @@ -39,6 +40,7 @@ public: int counterContains(std::string text, std::string substring_with_prozent_i_for_number, int starte_mit_dieser_Zahl = 1); std::string grep(std::string text, std::string substring, bool IgnoreCaseSensetifity = false); std::string upper_string(const std::string& str); + size_t getDate(); int writeToFile(std::string path, std::string text); diff --git a/parameterManager.cpp b/parameterManager.cpp index 6f34778..6b5ba85 100644 --- a/parameterManager.cpp +++ b/parameterManager.cpp @@ -37,6 +37,7 @@ int setPaths(Settings &settings) settings.cookieFilePath = CacheDir + "S_New4_cookies"; settings.accountNumberPath = CacheDir + "Account_Number"; + settings.lastUpdateDateFilePath = CacheDir + "LastUpdateDate"; settings.accountFilePath = SettingsDir + "Accounts"; settings.serienListPath = SettingsDir + "SerienListe"; @@ -150,6 +151,7 @@ int loadDefaulOptions(Settings &settings) ofs << "#default-maxThreads=0" << std::endl; ofs << std::endl; ofs << "#search-AchteAufGroßUndKleinschreibung=false" << std::endl; + ofs << "#search-UpdateWarnungNachTagen=10" << std::endl; ofs.close(); @@ -258,7 +260,15 @@ int loadDefaulOptions(Settings &settings) if(settings.debugMode) std::cout << " > Defaults: OutputFile: " << settings.outputFilePath << std::endl; - } else if (what == "") { + } else if (what == "search-UpdateWarnungNachTagen") { + if(!isNumber(data)) { + std::cout << " Error_Defaults: search-UpdateWarnungNachTagen: Wert ist keine Zahl." << std::endl; + return 228; + } else { + settings.updateWarningDays = atoi(data.c_str()); + } + if(settings.debugMode) + std::cout << " > Defaults: search-UpdateWarnungNachTagen: " << settings.updateWarningDays << std::endl; } else if (what == "") { diff --git a/parameterManager.h b/parameterManager.h index 5fc1e1d..25d248c 100644 --- a/parameterManager.h +++ b/parameterManager.h @@ -18,6 +18,7 @@ #include // PATH_MAX #include // readlink() + #ifdef _WIN32 #include #endif @@ -43,12 +44,14 @@ struct Settings { accountNumberPath= "/tmp/a_n", cookieFilePath = "/tmp/S_New4_cookies", serienListPath = "/tmp/SerienListe", + lastUpdateDateFilePath = "/tmp/lastUpdateDateFile", + defaultsFilePath = "/tmp/defaults", proxy_ip = "127.0.0.1", languages = "GerDub,GerSub,Eng", genaueHoster = "", - version = "2.9.0", - defaultFileVersion="1.3", + version = "3.0.0", + defaultFileVersion="1.4", outputFilePath = "", default_checkPath = "", default_Searchmuster = "S%Staffel%E%Folge%"; @@ -63,7 +66,8 @@ struct Settings { startSeason = 1, stopSeason = 0, proxy_port = 9050, - default_maxDirs = 20; + default_maxDirs = 20, + updateWarningDays = 10; char pathSymbol = '/'; unsigned maxThreads = 0; diff --git a/programManager.cpp b/programManager.cpp index c611d77..b285c6c 100644 --- a/programManager.cpp +++ b/programManager.cpp @@ -156,6 +156,29 @@ int ProgramManager::sucheNach_1_Serien(Settings *settings, PageManager &pageMana return 0; } +int ProgramManager::checkLastUpdate(Settings *settings) +{ + if(settings->updateWarningDays == 0) + return 0; + + std::ifstream ifs(settings->lastUpdateDateFilePath); + if(!ifs.is_open()) { + perror(("Konnte die Datei '" + settings->lastUpdateDateFilePath + "' nicht öffnen").c_str()); + return 1; + } + std::string line; + if(!std::getline(ifs, line).good()) { + perror("GetLine from Date failed"); + return 2; + + } else if( ( static_cast(pageManager.getDate()) - atoll(line.c_str()) ) >= settings->updateWarningDays) { + std::cout << ((settings->colorless) ? "" : "\033[93m") << " => Warnung: SerienListe zuletzt vor " << settings->updateWarningDays << " Tagen aktualisiert." + << std::endl << " Mit '" << settings->programName << " search -u' kannst du die Liste aktualisieren." << "\033[0m" << std::endl;; + } + + return 0; +} + int ProgramManager::defaultModus(Settings *settings) { AccountManager accountManager(settings->accountFilePath, settings->accountNumberPath); @@ -389,7 +412,7 @@ int ProgramManager::searchModus(Settings *settings, std::string *saveTo) std::ifstream ifs(settings->serienListPath); if(!ifs.is_open()) { - std::cout << "Keine SerienListe vorhanden. Erstelle eine neue..." << std::endl; + std::cout << " => Keine SerienListe vorhanden. Erstelle eine neue..." << std::endl; if(searchModus_update(settings) != 0) return 354; else { @@ -402,6 +425,10 @@ int ProgramManager::searchModus(Settings *settings, std::string *saveTo) } } + //Check for last Update.. + if(checkLastUpdate(settings) != 0) + return -1; + //Save file in string: std::string serienListe((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); @@ -789,6 +816,19 @@ int ProgramManager::searchModus_update(Settings *settings) } std::cout << ((settings->colorless) ? "" : "\033[93m") << "Serienunterschied: " << ( ((countAf - countBef) > 0) ? "+" : "") << countAf - countBef << " Serien." << "\033[0m" << std::endl; + + //Speicher Dateim des Updates... + ofs.open(settings->lastUpdateDateFilePath, std::ios::trunc); + if(!ofs.is_open()) { + perror("Konnte UpdateDate-Datei nicht öffnen"); + return 113; + } else { + //Schreibe Liste in das TextFile + ofs << pageManager.getDate() << std::endl; + ofs.close(); + } + + return 0; } diff --git a/programManager.h b/programManager.h index 5bf8aaf..1f24e5f 100644 --- a/programManager.h +++ b/programManager.h @@ -56,6 +56,7 @@ private: int searchModus_update(Settings * settings); int waitForThreads(); int sucheNach_1_Serien(Settings *settings, PageManager &pageManager, std::string &newName); + int checkLastUpdate(Settings * settings); };