S_New4/programManager.cpp

312 lines
12 KiB
C++

#include "programManager.h"
ProgramManager::ProgramManager()
{
}
ProgramManager::~ProgramManager()
{
}
int ProgramManager::start(Settings settings)
{
pageManager.setProxy(settings.proxy_ip, settings.proxy_port);
pageManager.setCookieFilePath(settings.cookieFilePath);
pageManager.setDebugMode(settings.debugMode);
switch (settings.modus) {
case Modus::DEFAULT_MODUS:
return defaultModus(&settings);
case Modus::DIRECT_LINK_MODUS:
return directLinkModus(&settings);
case Modus::Search_MODUS:
return searchModus(&settings);
default:
return -1;
}
}
int ProgramManager::defaultModus(Settings *settings)
{
AccountManager accountManager(settings->accountFilePath, settings->accountNumberPath);
if(settings->name == "") {
std::cout << "Kein Name angegeben: Missing Parameter -n [Name]." << std::endl;
return 27;
}
std::string nameInUrl =pageManager.checkName(settings->name);
if(nameInUrl == "-1") {
searchModus(settings);
return 25;
}
else if (pageManager.login(accountManager.getNextAccount()) != 0)
return 29;
pageManager.writeToFile(settings->outputFilePath, "Name: " + settings->name);
//Find out number of all seasons
Reply tmp_reply = pageManager.getServerRequest(pageManager.UrlPraefix + nameInUrl);
if(tmp_reply.html == "-1")
return 32;
int maxStaffel = pageManager.counterContains(tmp_reply.html, "/staffel-%i");
if(settings->debugMode)
std::cout << "Die Serie " << settings->name << " hat " << maxStaffel << " Staffeln." << std::endl;
//For every season
for (int staffel = settings->startSeason; staffel <= maxStaffel; ++staffel) {
//Find out number of all episodes
tmp_reply = pageManager.getServerRequest(pageManager.UrlPraefix + nameInUrl + "/staffel-" + std::to_string(staffel));
if(tmp_reply.html == "-1")
return 40;
int maxFolge = pageManager.counterContains(tmp_reply.html, "/episode-%i");
if(settings->debugMode)
std::cout << "Die Staffel " << staffel << " hat " << maxFolge << " Folgen." << std::endl;
//for every episode
for (int folge = settings->startEpisode; folge <= maxFolge; ++folge) {
tmp_reply =pageManager.getServerRequest(pageManager.UrlPraefix + nameInUrl + "/staffel-" + std::to_string(staffel) + "/episode-" + std::to_string(folge));
if(tmp_reply.html == "-1")
return 47;
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 << "'" << std::endl;
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.
return 0;
else if ( folge == settings->stopEpisode && staffel == settings->stopSeason) // stoppe wenn stopfolge = folge && stopstaffel == staffel
return 0;
}
//Setzte Startepisode zurück für nächste Staffel
settings->startEpisode = 1;
if(staffel == settings->stopSeason) {
if(settings->debugMode)
std::cout << "Stoppe, weil Staffel: " << staffel << " == StopStaffel " << settings->stopSeason << std::endl;
break;
}
}
return 0;
}
int ProgramManager::directLinkModus(Settings *settings)
{
AccountManager accountManager(settings->accountFilePath, settings->accountNumberPath);
if(settings->name == "") {
std::cout << "Kein(e) Link(s) angegeben: Missing Parameter -u [Url]." << std::endl;
return 76;
}
std::istringstream iStrStream( pageManager.replace( settings->name, ",", "\n" ) + "\n" );
std::string line;
if(pageManager.login(accountManager.getNextAccount()) != 0)
return 71;
while (getline(iStrStream, line).good()) {
if(line.find("https://s.to/redirect/") == std::string::npos) {
std::cout << "Invalid Redirect Link: '" << line << "'" << std::endl;
continue;
}
else if(convertLink(pageManager.replace(line, "https://s.to", ""), &accountManager, settings) != 0)
return 78;
}
return 0;
}
int ProgramManager::searchModus(Settings *settings)
{
if(settings->search_wantUpdate) {
int res = searchModus_update(settings) ;
if( res == 0)
std::cout << "Erfolgreich geupdatet: Die Serienliste ist nun auf dem neusten Stand." << std::endl;
else
std::cout << "Das updaten der Serienliste ist fehlgeschlagen." << std::endl;
return res;
} else if(settings->name == "") {
std::cout << "Kein Name angegeben: Missing Parameter -n [Name]." << std::endl;
return 27;
}
std::ifstream ifs(settings->serienListPath);
if(!ifs.is_open()) {
std::cout << "Keine SerienListe vorhanden. Erstelle eine neue..." << std::endl;
if(searchModus_update(settings) != 0)
return 354;
else {
ifs.open(settings->serienListPath);
if(!ifs.is_open()) {
perror("Couldn't open SerienList file after update again.");
return 434;
}
std::cout << "Erfolgreich gedownloadet." << std::endl;
}
}
//Save file in string:
std::string serienListe((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
std::string finds = pageManager.grep(serienListe, settings->name, settings->search_IgnoreUpperLower);
serienListe.clear();
if(!settings->colorless) {
for (size_t pos = pageManager.upper_string( finds ).find( pageManager.upper_string( settings->name ), 0);
pos != std::string::npos;
pos = pageManager.upper_string( finds ).find( pageManager.upper_string( settings->name ), pos + settings->name.length() + strlen("\033[37m\033[0m")))
finds.insert(pos, ( (finds.find(settings->name, pos) == pos) ? "\033[32m" : "\033[36m" ) ).insert(pos + settings->name.length() + strlen("\033[37m"), "\033[0m");
}
std::stringstream strstream(finds);
std::string line;
std::cout << "Für '" << settings->name << "' wurde(n) folgende Serie(n) gefunden: " << std::endl;
while (getline(strstream, line)) {
std::cout << " > " << line.substr(line.find("|", line.find("/")) + 1, line.length() - line.find("|", line.find("/")) -1 )
<< "\t[" << line.substr(line.find("/") + 1, line.find("|", line.find("/")) - line.find("/") - 1) << "]"
<< ( (line[0] == '|') ? "" : "\t( " + line.substr(0, line.find("|")) + " )" ) << std::endl;
}
return 0;
}
int ProgramManager::searchModus_update(Settings *settings)
{
Reply reply = pageManager.getServerRequest("https://s.to/serien");
if(reply.html == "-1")
return 21;
std::string serienListe = pageManager.replace( pageManager.grep( reply.html, "data-alternative-title" ), "</li>", "\n" );
if(reply.html.find("\" href=\"") == std::string::npos ||
reply.html.find("<li><a data-alternative-title=\"") == std::string::npos ||
reply.html.find("/serie/stream/") == std::string::npos ||
reply.html.find("</a>") == std::string::npos)
return 51;
//...\n<li><a data-alternative-title="" href="/serie/stream/2012-das-jahr-null" title="2012 - Das Jahr Null Stream anschauen">2012 - Das Jahr Null</a>\n...
serienListe = pageManager.replace(serienListe, "<li><a data-alternative-title=\"", "");
//...\n" href="/serie/stream/2012-das-jahr-null" title="2012 - Das Jahr Null Stream anschauen">2012 - Das Jahr Null</a>\n...
serienListe = pageManager.replace(serienListe, "\" href=\"", "|");
//...\n|/serie/stream/2012-das-jahr-null" title="2012 - Das Jahr Null Stream anschauen">2012 - Das Jahr Null</a>\n...
serienListe = pageManager.replace(serienListe, "|/serie/stream/", "|/");
//Performanze:
serienListe = pageManager.grep(serienListe, settings->name, true);
std::stringstream strstream(serienListe);
std::string line;
serienListe.clear();
while (getline(strstream, line)) {
if(line.find(" title=\"") == std::string::npos)
continue;
line.erase(line.find(" title="), line.find(">") - line.find(" title="));
//...\n|/serie/stream/2012-das-jahr-null"_weg_>2012 - Das Jahr Null</a>\n...
line = pageManager.replace(line, "\">", "|");
//...\n|/serie/stream/2012-das-jahr-null|2012 - Das Jahr Null</a>\n...
line = pageManager.replace(line, "</a>", "");
//...\n|/serie/stream/2012-das-jahr-null"_weg_>2012 - Das Jahr Null|\n...
serienListe += line + "\n";
} serienListe.pop_back();
std::ofstream ofs(settings->serienListPath, std::ios::trunc);
if(!ofs.is_open()) {
perror("Konnte SerienListe-Datei nicht öffnen");
return 111;
}
ofs << serienListe << std::endl;
ofs.close();
return 0;
}
int ProgramManager::convertLink(std::string redirectLink, AccountManager * accountManager,
Settings * settings, int Staffel, int Folge, std::string allLinks)
{
std::string folgenID = std::string((Staffel == -1 || Folge == -1 ) ? "" : "S" + std::string( (Staffel < 10) ? "0" : "" ) + std::to_string(Staffel)
+ "E" + std::string( (Folge < 10) ? "0" : "" ) + std::to_string( Folge ) + ": ");
std::string green = ((settings->colorless) ? "" : "\033[32m"),
red = ((settings->colorless) ? "" : "\033[31m"),
orange =((settings->colorless) ? "" : "\033[33m"),
blue = ((settings->colorless) ? "" : "\033[34m");
if(redirectLink == "" && settings->modus == Modus::DEFAULT_MODUS) {
if(allLinks == "") {
std::cout << " => " << red << "KEINEN Hoster für die Folge " << folgenID << " gefunden." << "\033[0m" << std::endl;
if(pageManager.writeToFile(settings->outputFilePath, std::string("KEINEN Hoster für die Folge ") + folgenID + std::string(" gefunden.")) != 0)
return 130;
}
else {
std::cout << " => " << orange << "Keinen PASSENDEN Hoster für die Folge " << folgenID << " gefunden." << "\033[0m" << std::endl
<< "Alle Links:" << std::endl
<< allLinks << std::endl;
if(pageManager.writeToFile(settings->outputFilePath, std::string("Keinen PASSENDEN Hoster für die Folge ") + folgenID + std::string(" gefunden.")) != 0)
return 138;
}
return 0;
}
for (int i = 1; i <= 3; ++i) {
std::string newUrl = pageManager.getUrlAfterRedirect("https://s.to" + redirectLink);
if (newUrl == "-1") {
return 102;
} else if(newUrl.find("/s.to/redirect/") != std::string::npos ) {
if(settings->debugMode)
std::cout << "Redirect Link nach umwandlung --> Neuer Account" << std::endl;
if(pageManager.login(accountManager->getNextAccount()) != 0)
return -1;
continue;
}
else {
std::cout << " => " << folgenID << green << newUrl << "\033[0m" << std::endl;
if(settings->outputFilePath != "")
if(pageManager.writeToFile(settings->outputFilePath,folgenID + newUrl) != 0)
return 108;
return 0;
}
}
std::cout << " => " << folgenID << red << "https://s.to" << redirectLink << "\033[0m" << std::endl;
if(settings->outputFilePath != "")
if(pageManager.writeToFile(settings->outputFilePath, folgenID + redirectLink) != 0)
return 114;
return 0;
}