diff --git a/parameterManager.cpp b/parameterManager.cpp index ae99d2d..b17c479 100644 --- a/parameterManager.cpp +++ b/parameterManager.cpp @@ -89,8 +89,7 @@ int unterOption_default(Settings *settings, int argc, char ** argv) int c = 0; const option long_opts[] = { {"name", required_argument, nullptr, 'n'}, - {"ip-addresse", required_argument, nullptr, 'i'}, - {"port", required_argument, nullptr, 'p'}, + {"socks5-proxy", required_argument, nullptr, 'p'}, {"genauer-hoster", required_argument, nullptr, 'g'}, {"languages", required_argument, nullptr, 'l'}, {"output-file", required_argument, nullptr, 'o'}, @@ -100,6 +99,10 @@ int unterOption_default(Settings *settings, int argc, char ** argv) {"start-season", required_argument, nullptr, 's'}, {"stop-season", required_argument, nullptr, 'S'}, + {"check-dir", required_argument, nullptr, 'C'}, + {"check-muster", required_argument, nullptr, 'm'}, + {"check-max-dirs", required_argument, nullptr, 't'}, + {"help", no_argument, nullptr, 'h'}, {"colorless", no_argument, nullptr, 'c'}, @@ -109,7 +112,7 @@ int unterOption_default(Settings *settings, int argc, char ** argv) }; - while( ( c = getopt_long (argc, argv, "n:i:p:g:l:o:e:E:s:S:hcd", long_opts, nullptr) ) != -1 ) { + while( ( c = getopt_long (argc, argv, "n:p:g:l:o:e:E:s:S:C:m:t:hcd", long_opts, nullptr) ) != -1 ) { switch(c) { case 'n': if(optarg) @@ -117,20 +120,29 @@ int unterOption_default(Settings *settings, int argc, char ** argv) if(settings->debugMode) std::cout << "Name: " << settings->name << std::endl; break; - case 'i': - if(optarg) - settings->proxy_ip = optarg; - if(settings->debugMode) - std::cout << "Proxy Ip Addresse: " << settings->proxy_ip << std::endl; - break; - case 'p': - if(optarg) - settings->proxy_port = atoi( optarg ) ; - if(std::to_string(settings->proxy_port) != optarg) { - std::cout << "Invalid Port: " << optarg << std::endl; - return 10; - } else if(settings->debugMode) - std::cout << "Proxy Port: " << settings->proxy_port << std::endl; + case 'p': { + if(!optarg || std::string(optarg) == "") + break; + std::string optarg = ::optarg; + if(optarg.find(":") == std::string::npos) { + std::cout << "Invalid Socks5 Proxy: " << optarg << std::endl; + return 31; + } + std::string ip = optarg.substr(0, optarg.find(":")); + std::string portStr = optarg.substr(optarg.find(":") + 1, optarg.length() - optarg.find(":")); + int port = atoi(portStr.c_str()); + if(std::to_string(port) != portStr || port <= 0) { + std::cout << "[-p]: Invalid Port: " << portStr << std::endl; + return 32; + } else if (ip == "") { + std::cout << "[-p]: Invalid Ip Addresse: " << ip << std::endl; + return 34; + } else if(settings->debugMode) { + std::cout << "Proxy Addresse: "<< ip << ":" << port << std::endl; + } + settings->proxy_ip = ip; + settings->proxy_port = port; + } break; case 'g': if(optarg) @@ -151,7 +163,6 @@ int unterOption_default(Settings *settings, int argc, char ** argv) std::cout << "Pfad zu Output-Datei: " << settings->outputFilePath << std::endl; break; - case 'e': if(!optarg) break; @@ -205,7 +216,31 @@ int unterOption_default(Settings *settings, int argc, char ** argv) } else if(settings->debugMode) std::cout << "StopSeason: " << settings->stopSeason << std::endl; break; + case 'm': + if(optarg) + settings->default_Searchmuster = optarg; + if(settings->debugMode) + std::cout << "SearchMuster: " << settings->default_Searchmuster << std::endl; + break; + case 't': + if(!optarg) + break; + settings->default_maxDirs = atoi(optarg); + if(std::to_string(settings->default_maxDirs) != std::string(optarg) ) { + std::cout << "[-t]: Invalid Count: " << optarg << std::endl; + return 18; + } + if(settings->debugMode) + std::cout << "SearchMuster: " << settings->default_Searchmuster << std::endl; + break; + case 'C': + if(!optarg) + break; + settings->default_checkDirPath = optarg; + if(settings->debugMode) + std::cout << "To check Dir: " << settings->default_checkDirPath << std::endl; + break; case 'c': settings->colorless = true; if(settings->debugMode) @@ -245,10 +280,8 @@ void unterOption_default_help(Settings *, char * argv0) << "\t -> Die Sprache(n) die du willst. Die wichtigsten zuerst. Default: GerDub,GerSub,Eng" << std::endl << std::endl << " > Proxy-Optionen:" << std::endl - << "\t-i [ProxyIPAddresse], --ip-addresse [ProxyIPAddresse]" << std::endl - << "\t -> Ip Addresse eines Socks5-Proxys angeben. Default: 127.0.0.1" << std::endl - << "\t-p [ProxyPort], --port [ProxyPort]" << std::endl - << "\t -> Port eines Socks5-Proxy angeben. Default: 9050" << std::endl + << "\t-p [ProxyAddresse], --socks5-proxy [ProxyAddresse]" << std::endl + << "\t -> Ip Addresse eines Socks5-Proxys angeben. Default: 127.0.0.1:9050" << std::endl << std::endl << " > Outputoptionen:" << std::endl << "\t-o [Pfad], --output-file [Pfad]" << std::endl @@ -258,6 +291,17 @@ void unterOption_default_help(Settings *, char * argv0) << "\t-d, --debug-mode" << std::endl << "\t -> Debug Nachrichten an. Default: false" << std::endl << std::endl + << " > Check-Dir Optionen:" << std::endl + << "\t-C [Pfad], --check-dir [Pfad]" << std::endl + << "\t -> Überprüfe ob in diesem, oder in einen der Unterordnern die Folge vorkommt." << std::endl + << "\t -> Wenn ja, dann überspringe diese." << std::endl + << "\t-m, --check-muster" << std::endl + << "\t -> Muster mit dem Überprüft wird, ob diese Folge in einem Ordner ist. Default: S%Staffel%E%Folge%" << std::endl + << "\t -> %Staffel% ist eine Variable für die Staffel mit 0 + Staffel, wenn Staffel < 10. " << std::endl + << "\t -> %Folge% ist eine Variable für die Folge mit 0 + Folge, wenn Folge < 10. " << std::endl + << "\t-t, --check-max-dirs" << std::endl + << "\t -> Maximale Anzahl der Ordner, die auf Dateien untersucht werden. Default: 20" << std::endl + << std::endl << " > Durchlaufoptionen:" << std::endl << "\t-e [Folge], --start-episode [Folge]" << std::endl << "\t -> Das Programm startet mit dieser Folge." << std::endl @@ -287,8 +331,7 @@ int unterOption_url(Settings *settings, int argc, char **argv) int c = 0; const option long_opts[] = { {"url", required_argument, nullptr, 'u'}, - {"ip-addresse", required_argument, nullptr, 'i'}, - {"port", required_argument, nullptr, 'p'}, + {"socks5-proxy", required_argument, nullptr, 'p'}, {"output-file", required_argument, nullptr, 'o'}, {"help", no_argument, nullptr, 'h'}, @@ -299,7 +342,7 @@ int unterOption_url(Settings *settings, int argc, char **argv) }; - while( ( c = getopt_long (argc, argv, "u:i:p:o:hcd", long_opts, nullptr) ) != -1 ) { + while( ( c = getopt_long (argc, argv, "u:p:o:hcd", long_opts, nullptr) ) != -1 ) { switch(c) { case 'u': if(optarg) @@ -308,21 +351,29 @@ int unterOption_url(Settings *settings, int argc, char **argv) std::cout << "Urls: " << settings->name << std::endl; break; - case 'i': - if(optarg) - settings->proxy_ip = optarg; - if(settings->debugMode) - std::cout << "Proxy Ip Addresse: " << settings->proxy_ip << std::endl; - break; - case 'p': - if(optarg) - settings->proxy_port = atoi( optarg ); - if(std::to_string(settings->proxy_port) != optarg) { - std::cout << "Invalid Port: " << optarg << std::endl; - return 20; + case 'p': { + if(!optarg || std::string(optarg) == "") + break; + std::string optarg = ::optarg; + if(optarg.find(":") == std::string::npos) { + std::cout << "Invalid Socks5 Proxy: " << optarg << std::endl; + return 31; + } + std::string ip = optarg.substr(0, optarg.find(":")); + std::string portStr = optarg.substr(optarg.find(":") + 1, optarg.length() - optarg.find(":")); + int port = atoi(portStr.c_str()); + if(std::to_string(port) != portStr || port <= 0) { + std::cout << "[-p]: Invalid Port: " << portStr << std::endl; + return 32; + } else if (ip == "") { + std::cout << "[-p]: Invalid Ip Addresse: " << ip << std::endl; + return 34; + } else if(settings->debugMode) { + std::cout << "Proxy Addresse: "<< ip << ":" << port << std::endl; + } + settings->proxy_ip = ip; + settings->proxy_port = port; } - if(settings->debugMode) - std::cout << "Proxy Port: " << settings->proxy_port << std::endl; break; case 'o': if(optarg) @@ -360,10 +411,8 @@ void unterOption_url_help(Settings *, char * argv0) << "Parameter:" << std::endl << "\t-u [Url1,Url2,...], --url [Url1,Url2,...]" << std::endl << "\t -> Die zu umwandelnden redirect-Links." << std::endl - << "\t-i [ProxyIPAddresse], --ip-addresse [ProxyIPAddresse]" << std::endl - << "\t -> Default: 127.0.0.1." << std::endl - << "\t-p [ProxyPort], --port [ProxyPort]" << std::endl - << "\t -> Default: 9050." << std::endl + << "\t-p [ProxyAddresse], --socks5-proxy [ProxyAddresse]" << std::endl + << "\t -> Type: Socks5, Default: 127.0.0.1:9050" << std::endl << "\t-o [Pfad], --output-file [Pfad]" << std::endl << "\t-c, --colorless" << std::endl << "\t -> Default: false ." << std::endl @@ -411,7 +460,7 @@ int unterOption_search(Settings *settings, int argc, char **argv) {nullptr, no_argument, nullptr, 0} }; - std::string optarg2; + while( ( c = getopt_long (argc, argv, "n:p:hcdeu", long_opts, nullptr) ) != -1 ) { switch(c) { case 'n': @@ -421,15 +470,15 @@ int unterOption_search(Settings *settings, int argc, char **argv) std::cout << "Name: " << settings->name << std::endl; break; case 'p': { - optarg2 = optarg; - if(!optarg || optarg2 == "") + if(!optarg || std::string(optarg) == "") break; - else if(optarg2.find(":") == std::string::npos) { + std::string optarg = ::optarg; + if(optarg.find(":") == std::string::npos) { std::cout << "Invalid Socks5 Proxy: " << optarg << std::endl; return 31; } - std::string ip = optarg2.substr(0, optarg2.find(":")); - std::string portStr = optarg2.substr(optarg2.find(":") + 1, optarg2.length() - optarg2.find(":")); + std::string ip = optarg.substr(0, optarg.find(":")); + std::string portStr = optarg.substr(optarg.find(":") + 1, optarg.length() - optarg.find(":")); int port = atoi(portStr.c_str()); if(std::to_string(port) != portStr || port <= 0) { std::cout << "[-p]: Invalid Port: " << portStr << std::endl; @@ -441,7 +490,7 @@ int unterOption_search(Settings *settings, int argc, char **argv) std::cout << "Proxy Addresse: "<< ip << ":" << port << std::endl; } settings->proxy_ip = ip; - settings->proxy_port = port; + settings->proxy_port = port; } break; case 'h': diff --git a/parameterManager.h b/parameterManager.h index 1a969cd..1cd4e55 100644 --- a/parameterManager.h +++ b/parameterManager.h @@ -11,7 +11,8 @@ enum Modus { EXIT = -1, DEFAULT_MODUS = 0, DIRECT_LINK_MODUS = 1, - Search_MODUS = 2 + Search_MODUS = 2, + }; @@ -28,7 +29,10 @@ struct Settings { languages = "GerDub,GerSub,Eng", genaueHoster = "", version = "2.0.0", - outputFilePath; + outputFilePath = "", + default_checkDirPath = "", + default_Searchmuster = "S%Staffel%E%Folge%"; + Modus modus = Modus::DEFAULT_MODUS; bool colorless = false, debugMode = false, @@ -38,7 +42,8 @@ struct Settings { stopEpisode = 0, startSeason = 1, stopSeason = 0, - proxy_port = 9050; + proxy_port = 9050, + default_maxDirs = 20; }; diff --git a/programManager.cpp b/programManager.cpp index 030a77e..b452afe 100644 --- a/programManager.cpp +++ b/programManager.cpp @@ -37,6 +37,11 @@ int ProgramManager::defaultModus(Settings *settings) return 27; } + std::string dirFiles; + if(settings->default_checkDirPath != "") + if(listDir(dirFiles, settings->default_checkDirPath, settings->default_maxDirs ) != 0) + return 28; + std::string nameInUrl =pageManager.checkName(settings->name); if(nameInUrl == "-1") { searchModus(settings); @@ -68,6 +73,15 @@ int ProgramManager::defaultModus(Settings *settings) //for every episode for (int folge = settings->startEpisode; folge <= maxFolge; ++folge) { + if(settings->default_checkDirPath != "") { + if(dirFiles.find(pageManager.replace( pageManager.replace( settings->default_Searchmuster, "%Staffel%", ((staffel < 10) ? "0" : "") + std::to_string(staffel) ), + "%Folge%", ((folge < 10) ? "0" : "") + std::to_string(folge) ) ) != std::string::npos) { + if(settings->debugMode) + std::cout << "Skipping Folge: S" << staffel << "E" << folge << std::endl; + continue; + } + } + tmp_reply =pageManager.getServerRequest(pageManager.UrlPraefix + nameInUrl + "/staffel-" + std::to_string(staffel) + "/episode-" + std::to_string(folge)); if(tmp_reply.html == "-1") return 47; @@ -232,6 +246,37 @@ int ProgramManager::searchModus_update(Settings *settings) return 0; } +int ProgramManager::listDir(std::string &list,std::string path, int maxDepth) +{ + if(maxDepth == 0) + return 0; + else if(!dirExists(path)) + return -1; + else + maxDepth--; + + if(path[path.length()-1] != '/') + path.push_back('/'); + + DIR* dirp = opendir(path.c_str()); + if(!dirp) { + perror(std::string("Cannot open directory: " + path).c_str()); + return -1; + } + + struct dirent * dp; + while ((dp = readdir(dirp)) != nullptr) { + if(strcmp( dp->d_name, "." ) == 0 || strcmp ( dp->d_name, ".." ) == 0) + continue; + else if(dp->d_type == DT_DIR) { + listDir(list, (path + dp->d_name + "/"), maxDepth); + } else + list.append( std::string(dp->d_name) + "\n" ); + } + closedir(dirp); + return 0; +} + int ProgramManager::convertLink(std::string redirectLink, AccountManager * accountManager, Settings * settings, int Staffel, int Folge, std::string allLinks) { diff --git a/programManager.h b/programManager.h index 21b7f21..437f69b 100644 --- a/programManager.h +++ b/programManager.h @@ -6,24 +6,26 @@ #include "accountManager.h" #include +#include +#include + class ProgramManager { public: ProgramManager(); ~ProgramManager(); int start(Settings setting); + int listDir(std::string &list, std::string path, int maxDepth); private: int defaultModus(Settings * settings); int directLinkModus(Settings * settings); int searchModus(Settings * settings); - int searchModus_update(Settings * settings); - PageManager pageManager; - int convertLink(std::string redirectLink, AccountManager *accountManager, Settings * settings, int Staffel = -1, int Folge = -1, std::string allLinks = "NOT_EMPTY"); + int searchModus_update(Settings * settings); };