From 50a7e12f80ec9a5ad8a26e2bb3acfeb6537279c8 Mon Sep 17 00:00:00 2001 From: Markus Date: Fri, 16 Aug 2019 13:32:13 +0200 Subject: [PATCH] works ok; but needs tests --- pageManager.cpp | 2 +- parameterManager.cpp | 1 - parameterManager.h | 2 +- programManager.cpp | 171 ++++++++++++++++++++----------------------- programManager.h | 5 +- 5 files changed, 87 insertions(+), 94 deletions(-) diff --git a/pageManager.cpp b/pageManager.cpp index 2bd6a47..4d190a4 100644 --- a/pageManager.cpp +++ b/pageManager.cpp @@ -43,7 +43,7 @@ Reply PageManager::getServerRequest(std::string Url, bool useCookies, std::strin char *url; std::string returnUrl; - std::cout << "Lade: '" << Url << "'..." << std::flush; + std::cout << "\33[2K\r" << "Lade: '" << Url << "'..." << std::flush; curl = curl_easy_init(); if(!curl) { diff --git a/parameterManager.cpp b/parameterManager.cpp index 2367128..c56df48 100644 --- a/parameterManager.cpp +++ b/parameterManager.cpp @@ -4,7 +4,6 @@ void setPaths(Settings &settings) { //Path settings std::string executablePathTo = getexepath(); - std::cout << "Path " << executablePathTo << std::endl; executablePathTo.erase(executablePathTo.find_last_of(settings.pathSymbol) + 1 , executablePathTo.length() - ( executablePathTo.find_last_of(settings.pathSymbol) + 1) ); diff --git a/parameterManager.h b/parameterManager.h index ea05108..26dc80b 100644 --- a/parameterManager.h +++ b/parameterManager.h @@ -53,7 +53,7 @@ struct Settings { proxy_port = 9050, default_maxDirs = 20; char pathSymbol = '/'; - unsigned maxThreads = 4; + unsigned maxThreads = 5; }; diff --git a/programManager.cpp b/programManager.cpp index b4a41fd..600fdc6 100644 --- a/programManager.cpp +++ b/programManager.cpp @@ -103,12 +103,10 @@ void * threadFunction(void * KA) { } - int ProgramManager::defaultModus(Settings *settings) { AccountManager accountManager(settings->accountFilePath, settings->accountNumberPath); - //Wenn kein Name mit -n Angegeben wurde: if(settings->name == "") { if(settings->default_checkDirPath != "") { @@ -157,28 +155,30 @@ int ProgramManager::defaultModus(Settings *settings) return 25; } + //Wenn multihtreading seaktiviert ist, normal login sonst bereite threads vor + if(settings->maxThreads == 0) { + // melde bei s.to an und speicher cookies. + if (pageManager.login(accountManager.getNextAccount()) != 0) //---------------------------------------- + return 29; + } else { + for (unsigned i = 0; i < settings->maxThreads; ++i) { + ka * newKa = new ka(i); + PageManager * pm = new PageManager; + pm->setProxy(settings->proxy_ip, settings->proxy_port); + pm->setCookieFilePath(settings->cookieFilePath + std::to_string( i )); + pm->setDebugMode(settings->debugMode); + newKa->pageManager = pm; - for (unsigned i = 0; i < settings->maxThreads; ++i) { - ka * newKa = new ka(i); - PageManager * pm = new PageManager; - pm->setProxy(settings->proxy_ip, settings->proxy_port); - pm->setCookieFilePath(settings->cookieFilePath + std::to_string( i )); - pm->setDebugMode(settings->debugMode); - newKa->pageManager = pm; - - newKa->settings = settings; - newKa->accountManager = &accountManager; - newKa->nameInUrl = nameInUrl; - threadList.push_back(newKa); + newKa->settings = settings; + newKa->accountManager = &accountManager; + newKa->nameInUrl = nameInUrl; + newKa->thread = 0; // Sonst error wenn join & nicht gesetzt + newKa->exitState = -1; + threadList.push_back(newKa); + } } - - - - - // melde bei s.to an und speicher cookies. - if (pageManager.login(accountManager.getNextAccount()) != 0) //---------------------------------------- - return 29; + //Write Name to File if -o is set pageManager.writeToFile(settings->outputFilePath, "Name: " + settings->name); //Finde die anzahl der staffel heraus: @@ -217,70 +217,49 @@ int ProgramManager::defaultModus(Settings *settings) } } - - //Wenn nex Thread noch in den Vector passt(weniger Threads als Max), dann Starte neuen mit data aus dem Vector... - if(nextThread < threadList.size()) { - threadList[nextThread]->setData(staffel, folge); - if(pthread_create(&threadList[nextThread]->thread, nullptr, threadFunction, reinterpret_cast(threadList[nextThread])) != 0 ) { - perror("pthread_creat failed"); - return 48; - } - nextThread++; - } else { // Sonnst warte bis alle Fertig sind und restarte die Folge - for( auto &e : threadList) { - pthread_join(e->thread, nullptr); - if(e->exitState != 0) { - std::cout << "Error: Thread gab error zurück." << std::endl; - return e->exitState; + //Multihreading Mode + if(settings->maxThreads > 0) { + //Wenn nex Thread noch in den Vector passt(weniger Threads als Max), dann Starte neuen mit data aus dem Vector... + if(nextThread < threadList.size()) { + threadList[nextThread]->exitState = 0; + threadList[nextThread]->setData(staffel, folge); + if(pthread_create(&threadList[nextThread]->thread, nullptr, threadFunction, reinterpret_cast(threadList[nextThread])) != 0 ) { + perror("pthread_creat failed"); + return 48; } - std::cout << "NachThread: " << e->returnValue << std::endl; + nextThread++; + } else { // Sonnst warte bis alle Fertig sind und restarte die Folge + if(waitForThreads() != 0) + return 231; + nextThread=0; + folge--; + continue; } - nextThread=0; - folge--; - continue; + + } else { // Default Mode + tmp_reply =pageManager.getServerRequest(pageManager.UrlPraefix + nameInUrl + "/staffel-" + std::to_string(staffel) + "/episode-" + std::to_string(folge)); + if(tmp_reply.html == "-1") + return 50; + std::string allLinks = pageManager.getLinks(tmp_reply.html); + std::string Link = pageManager.chooseHosterLink(allLinks, settings->genaueHoster, settings->languages); + + if(settings->debugMode) + std::cout << allLinks << std::endl << ( (Link == "") ? "" : " -> Link: 'https://s.to") << Link << ( (Link == "") ? "" : "'\n" ); + if(convertLink(Link, &accountManager, settings, staffel, folge, allLinks) != 0) + return 51; + } - - - /* - tmp_reply =pageManager.getServerRequest(pageManager.UrlPraefix + nameInUrl + "/staffel-" + std::to_string(staffel) + "/episode-" + std::to_string(folge)); - if(tmp_reply.html == "-1") - return 50; - std::string allLinks = pageManager.getLinks(tmp_reply.html); - std::string Link = pageManager.chooseHosterLink(allLinks, settings->genaueHoster, settings->languages); - - if(settings->debugMode) - std::cout << allLinks << std::endl << ( (Link == "") ? "" : " -> Link: 'https://s.to") << Link << ( (Link == "") ? "" : "'\n" ); - if(convertLink(Link, &accountManager, settings, staffel, folge, allLinks) != 0) - return 51; - - - */ - - - - - if(folge == settings->stopEpisode && settings->stopSeason < 1) { // stoppe wenn stopfolge gleich der folge ist und stopstaffel nicht gesetzt wurde. - for( auto &e : threadList) { - pthread_join(e->thread, nullptr); - if(e->exitState != 0) { - std::cout << "Error: Thread gab error zurück." << std::endl; - return e->exitState; - } - std::cout << "NachThread: " << e->returnValue << std::endl; - } + if(settings->maxThreads != 0) + if(waitForThreads() != 0) + return 261; return 0; - } - else if ( folge == settings->stopEpisode && staffel == settings->stopSeason) { // stoppe wenn stopfolge = folge && stopstaffel == staffel - for( auto &e : threadList) { - pthread_join(e->thread, nullptr); - if(e->exitState != 0) { - std::cout << "Error: Thread gab error zurück." << std::endl; - return e->exitState; - } - std::cout << "NachThread: " << e->returnValue << std::endl; - } + + } else if ( folge == settings->stopEpisode && staffel == settings->stopSeason) { // stoppe wenn stopfolge = folge && stopstaffel == staffel + if(settings->maxThreads != 0) + if(waitForThreads() != 0) + return 267; return 0; } } @@ -294,22 +273,34 @@ int ProgramManager::defaultModus(Settings *settings) } } - - for( auto &e : threadList) { - pthread_join(e->thread, nullptr); - if(e->exitState != 0) { - std::cout << "Error: Thread gab error zurück." << std::endl; - return e->exitState; - } - std::cout << "NachThread: " << e->returnValue << std::endl; - } - - + if(settings->maxThreads != 0) + if(waitForThreads() != 0) + return 292; std::cout << " > Fertig" << std::endl; return 0; } +int ProgramManager::waitForThreads() +{ + for( auto &e : threadList) { + if( e->exitState != -1 && pthread_join(e->thread, nullptr) == 0) { + if(e->exitState != 0) { + std::cout << "Error: Thread gab error zurück." << std::endl; + return e->exitState; + } else + if(e->returnValue != "") + std::cout << "(T" << e->id << ")" << e->returnValue << std::endl; + } + e->exitState = -1; + e->returnValue = ""; + e->setData(0, 0); + e->thread = 0; + } + return 0; +} + + int ProgramManager::directLinkModus(Settings *settings) { AccountManager accountManager(settings->accountFilePath, settings->accountNumberPath); diff --git a/programManager.h b/programManager.h index f2bafa5..8067407 100644 --- a/programManager.h +++ b/programManager.h @@ -15,6 +15,8 @@ struct ka { ka(unsigned id) : id(id) {} + ~ka() { std::cout << "Deskrtuktor" << std::endl; } + unsigned id; pthread_t thread; std::string nameInUrl, returnValue; @@ -29,7 +31,6 @@ struct ka } void setData(int staffel, int folge) { this->staffel = staffel; this->folge = folge; } - }; @@ -54,6 +55,8 @@ private: 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); + int waitForThreads(); + };