works ok; but needs tests

This commit is contained in:
Markus 2019-08-16 13:32:13 +02:00
parent 34ec60e6a2
commit 50a7e12f80
5 changed files with 87 additions and 94 deletions

View File

@ -43,7 +43,7 @@ Reply PageManager::getServerRequest(std::string Url, bool useCookies, std::strin
char *url; char *url;
std::string returnUrl; std::string returnUrl;


std::cout << "Lade: '" << Url << "'..." << std::flush; std::cout << "\33[2K\r" << "Lade: '" << Url << "'..." << std::flush;


curl = curl_easy_init(); curl = curl_easy_init();
if(!curl) { if(!curl) {

View File

@ -4,7 +4,6 @@ void setPaths(Settings &settings)
{ {
//Path settings //Path settings
std::string executablePathTo = getexepath(); 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) ); executablePathTo.erase(executablePathTo.find_last_of(settings.pathSymbol) + 1 , executablePathTo.length() - ( executablePathTo.find_last_of(settings.pathSymbol) + 1) );



View File

@ -53,7 +53,7 @@ struct Settings {
proxy_port = 9050, proxy_port = 9050,
default_maxDirs = 20; default_maxDirs = 20;
char pathSymbol = '/'; char pathSymbol = '/';
unsigned maxThreads = 4; unsigned maxThreads = 5;


}; };



View File

@ -103,12 +103,10 @@ void * threadFunction(void * KA) {
} }





int ProgramManager::defaultModus(Settings *settings) int ProgramManager::defaultModus(Settings *settings)
{ {
AccountManager accountManager(settings->accountFilePath, settings->accountNumberPath); AccountManager accountManager(settings->accountFilePath, settings->accountNumberPath);



//Wenn kein Name mit -n Angegeben wurde: //Wenn kein Name mit -n Angegeben wurde:
if(settings->name == "") { if(settings->name == "") {
if(settings->default_checkDirPath != "") { if(settings->default_checkDirPath != "") {
@ -157,7 +155,12 @@ int ProgramManager::defaultModus(Settings *settings)
return 25; 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) { for (unsigned i = 0; i < settings->maxThreads; ++i) {
ka * newKa = new ka(i); ka * newKa = new ka(i);
PageManager * pm = new PageManager; PageManager * pm = new PageManager;
@ -169,16 +172,13 @@ int ProgramManager::defaultModus(Settings *settings)
newKa->settings = settings; newKa->settings = settings;
newKa->accountManager = &accountManager; newKa->accountManager = &accountManager;
newKa->nameInUrl = nameInUrl; newKa->nameInUrl = nameInUrl;
newKa->thread = 0; // Sonst error wenn join & nicht gesetzt
newKa->exitState = -1;
threadList.push_back(newKa); threadList.push_back(newKa);
} }
}



//Write Name to File if -o is set



// melde bei s.to an und speicher cookies.
if (pageManager.login(accountManager.getNextAccount()) != 0) //----------------------------------------
return 29;
pageManager.writeToFile(settings->outputFilePath, "Name: " + settings->name); pageManager.writeToFile(settings->outputFilePath, "Name: " + settings->name);


//Finde die anzahl der staffel heraus: //Finde die anzahl der staffel heraus:
@ -217,9 +217,11 @@ int ProgramManager::defaultModus(Settings *settings)
} }
} }



//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... //Wenn nex Thread noch in den Vector passt(weniger Threads als Max), dann Starte neuen mit data aus dem Vector...
if(nextThread < threadList.size()) { if(nextThread < threadList.size()) {
threadList[nextThread]->exitState = 0;
threadList[nextThread]->setData(staffel, folge); threadList[nextThread]->setData(staffel, folge);
if(pthread_create(&threadList[nextThread]->thread, nullptr, threadFunction, reinterpret_cast<void*>(threadList[nextThread])) != 0 ) { if(pthread_create(&threadList[nextThread]->thread, nullptr, threadFunction, reinterpret_cast<void*>(threadList[nextThread])) != 0 ) {
perror("pthread_creat failed"); perror("pthread_creat failed");
@ -227,22 +229,14 @@ int ProgramManager::defaultModus(Settings *settings)
} }
nextThread++; nextThread++;
} else { // Sonnst warte bis alle Fertig sind und restarte die Folge } else { // Sonnst warte bis alle Fertig sind und restarte die Folge
for( auto &e : threadList) { if(waitForThreads() != 0)
pthread_join(e->thread, nullptr); return 231;
if(e->exitState != 0) {
std::cout << "Error: Thread gab error zurück." << std::endl;
return e->exitState;
}
std::cout << "NachThread: " << e->returnValue << std::endl;
}
nextThread=0; nextThread=0;
folge--; folge--;
continue; continue;
} }



} else { // Default Mode

/*
tmp_reply =pageManager.getServerRequest(pageManager.UrlPraefix + nameInUrl + "/staffel-" + std::to_string(staffel) + "/episode-" + std::to_string(folge)); tmp_reply =pageManager.getServerRequest(pageManager.UrlPraefix + nameInUrl + "/staffel-" + std::to_string(staffel) + "/episode-" + std::to_string(folge));
if(tmp_reply.html == "-1") if(tmp_reply.html == "-1")
return 50; return 50;
@ -254,33 +248,18 @@ int ProgramManager::defaultModus(Settings *settings)
if(convertLink(Link, &accountManager, settings, staffel, folge, allLinks) != 0) if(convertLink(Link, &accountManager, settings, staffel, folge, allLinks) != 0)
return 51; return 51;



}
*/






if(folge == settings->stopEpisode && settings->stopSeason < 1) { // stoppe wenn stopfolge gleich der folge ist und stopstaffel nicht gesetzt wurde. if(folge == settings->stopEpisode && settings->stopSeason < 1) { // stoppe wenn stopfolge gleich der folge ist und stopstaffel nicht gesetzt wurde.
for( auto &e : threadList) { if(settings->maxThreads != 0)
pthread_join(e->thread, nullptr); if(waitForThreads() != 0)
if(e->exitState != 0) { return 261;
std::cout << "Error: Thread gab error zurück." << std::endl;
return e->exitState;
}
std::cout << "NachThread: " << e->returnValue << std::endl;
}
return 0; return 0;
}
else if ( folge == settings->stopEpisode && staffel == settings->stopSeason) { // stoppe wenn stopfolge = folge && stopstaffel == staffel } else if ( folge == settings->stopEpisode && staffel == settings->stopSeason) { // stoppe wenn stopfolge = folge && stopstaffel == staffel
for( auto &e : threadList) { if(settings->maxThreads != 0)
pthread_join(e->thread, nullptr); if(waitForThreads() != 0)
if(e->exitState != 0) { return 267;
std::cout << "Error: Thread gab error zurück." << std::endl;
return e->exitState;
}
std::cout << "NachThread: " << e->returnValue << std::endl;
}
return 0; return 0;
} }
} }
@ -294,22 +273,34 @@ int ProgramManager::defaultModus(Settings *settings)
} }
} }



if(settings->maxThreads != 0)
for( auto &e : threadList) { if(waitForThreads() != 0)
pthread_join(e->thread, nullptr); return 292;
if(e->exitState != 0) {
std::cout << "Error: Thread gab error zurück." << std::endl;
return e->exitState;
}
std::cout << "NachThread: " << e->returnValue << std::endl;
}




std::cout << " > Fertig" << std::endl; std::cout << " > Fertig" << std::endl;
return 0; 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) int ProgramManager::directLinkModus(Settings *settings)
{ {
AccountManager accountManager(settings->accountFilePath, settings->accountNumberPath); AccountManager accountManager(settings->accountFilePath, settings->accountNumberPath);

View File

@ -15,6 +15,8 @@
struct ka struct ka
{ {
ka(unsigned id) : id(id) {} ka(unsigned id) : id(id) {}
~ka() { std::cout << "Deskrtuktor" << std::endl; }

unsigned id; unsigned id;
pthread_t thread; pthread_t thread;
std::string nameInUrl, returnValue; std::string nameInUrl, returnValue;
@ -29,7 +31,6 @@ struct ka
} }
void setData(int staffel, int folge) { this->staffel = staffel; this->folge = folge; } 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 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 searchModus_update(Settings * settings);
int waitForThreads();



}; };