S_New4/pageManager.cpp

348 lines
13 KiB
C++
Raw Permalink Normal View History

2019-07-06 18:47:29 +00:00
#include "pageManager.h"
2019-08-31 18:35:11 +00:00
PageManager::PageManager(std::string sock5ProxyOnlyAddress, std::string cookieFilePath)
: sock5Proxy("socks5://" + sock5ProxyOnlyAddress), cookieFilePath(cookieFilePath)
2019-07-06 18:47:29 +00:00
{
curl_global_init(CURL_GLOBAL_ALL);
2019-07-06 18:47:29 +00:00
}
PageManager::~PageManager()
{
curl_global_cleanup();
2019-07-06 18:47:29 +00:00
}
void PageManager::setProxy(std::string ip, int port)
2019-07-07 16:30:37 +00:00
{
this->sock5Proxy = "socks5://" + ip + ":" + std::to_string(port);
2019-07-07 16:30:37 +00:00
}
2019-07-08 18:17:11 +00:00
void PageManager::setCookieFilePath(std::string path)
{
this->cookieFilePath = path;
}
void PageManager::setDebugMode(bool status)
{
this->debugMode = status;
}
2019-07-06 18:47:29 +00:00
size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
//Function für CURL
static_cast<std::string*>(userp)->append(static_cast<char*>(contents),size * nmemb);
return size * nmemb;
}
Reply PageManager::getServerRequest(std::string Url, bool useCookies, std::string data, bool generateCookieFile)
{
CURL *curl;
CURLcode res;
std::string readBuffer;
char *url;
std::string returnUrl;
std::cout << ( "\33[2K\rLade: '" + Url + "'..." ) << std::flush;
2019-07-06 18:47:29 +00:00
curl = curl_easy_init();
if(!curl) {
2019-08-11 14:52:23 +00:00
perror("\33[2K\r => Error: Curl easy init failed");
2019-07-06 18:47:29 +00:00
return Reply("-1");
}
//Settings
curl_easy_setopt(curl, CURLOPT_URL, Url.c_str()); //Url für Curl
curl_easy_setopt(curl, CURLOPT_PROXY, sock5Proxy.c_str() ); //Sock5Proxy für Curl
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); //follows redirection
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); // Funktion zum Speichern des outputs in einem string
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); //Legt die Variable readbuffer fest
2019-07-07 16:30:37 +00:00
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0");
2019-07-06 18:47:29 +00:00
if(useCookies)
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFilePath.c_str());
if(data != "")
curl_easy_setopt (curl, CURLOPT_POSTFIELDS, data.c_str());
if(generateCookieFile)
curl_easy_setopt (curl, CURLOPT_COOKIEJAR, cookieFilePath.c_str());
2019-07-08 18:17:11 +00:00
int maxTimeout = 10;
for (int timeout = 1; timeout <= maxTimeout; ++timeout) {
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
std::cout << "\33[2K\r" << std::flush;
2019-07-08 18:17:11 +00:00
if(res != CURLE_OK) {
if(timeout == maxTimeout) {
perror((std::string("\33[2K\r => Error: curl_easy_perform() failed: ") + curl_easy_strerror(res)).c_str());
2019-07-08 18:17:11 +00:00
return Reply("-1");
} else {
std::cout << std::string( "\33[2K\r => Warning: Versuch " + std::to_string(timeout) + " von " + std::to_string(maxTimeout) + ": curl_easy_perform() failed: " + curl_easy_strerror(res) )<< std::flush;
2019-07-08 18:17:11 +00:00
sleep(1);
}
} else {
break;
}
2019-07-06 18:47:29 +00:00
}
//Get Url
res = curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
if( res != CURLE_OK || !url ) {
perror((std::string("\33[2K\r => Error: curl_easy_getinfo failed: ") + curl_easy_strerror(res)).c_str());
2019-07-06 18:47:29 +00:00
return Reply("-1");
} else
returnUrl=url;
/* always cleanup */ /* Mach den Griff zu, schreib die Kekse! */
curl_easy_cleanup(curl);
return Reply(readBuffer, returnUrl);
}
int PageManager::login(Account account)
{
2019-07-08 18:17:11 +00:00
if(debugMode)
2019-08-11 14:52:23 +00:00
std::cout << " > Melde mit neuem Account an: Email: " << account.Email << " Passowort: " << account.Password << std::endl;
2019-07-08 18:17:11 +00:00
2019-07-06 18:47:29 +00:00
std::string html = getServerRequest("https://s.to/login", false, std::string("email=" + account.Email + "&password=" + account.Password), true ).html;
if(html == "" )
return 0;
else if (html.find("Das Feld Email muss eine gültige E-Mail-Adresse enthalten.") != std::string::npos)
2019-08-11 14:52:23 +00:00
std::cout << " => Error: Login failed: Das Feld Email muss eine gültige E-Mail-Adresse enthalten." << std::endl
2019-07-06 18:47:29 +00:00
<< " Email: '" << account.Email << "' Passwort: '" << account.Password << "'" << std::endl;
else if (html.find("Das Passwort ist nicht korrekt") != std::string::npos)
2019-08-11 14:52:23 +00:00
std::cout << " => Error: Login failed: Das Passwort ist nicht korrekt." << std::endl
2019-07-06 18:47:29 +00:00
<< " Email: '" << account.Email << "' Passwort: '" << account.Password << "'" << std::endl;
else if (html.find("Ein Account mit dieser E-Mail Adresse wurde nicht gefunden.") != std::string::npos)
2019-08-11 14:52:23 +00:00
std::cout << " => Error: Login failed: Ein Account mit dieser E-Mail Adresse wurde nicht gefunden." << std::endl
2019-07-06 18:47:29 +00:00
<< " Email: '" << account.Email << "' Passwort: '" << account.Password << "'" << std::endl;
2019-07-07 16:30:37 +00:00
else if(html == "-1")
return -1;
2019-07-06 18:47:29 +00:00
else
2019-08-11 14:52:23 +00:00
std::cout << " => Error: Login failed: Keine Weiterleitung bei Login." << std::endl
2019-07-06 18:47:29 +00:00
<< " Email: '" << account.Email << "' Passwort: '" << account.Password << "'" << std::endl;
return -1;
}
std::string PageManager::getUrlAfterRedirect(std::string Url)
{
return getServerRequest(Url, true).url;
}
std::string PageManager::checkName(std::string Name)
{
std::string name = replace(Name, " ", "-");
std::string html = getServerRequest("https://s.to/serie/stream/" + name).html;
if(html.find("Die gewünschte Serie wurde nicht gefunden oder ist im Moment deaktiviert.") != std::string::npos) {
std::cout << "\33[2K\r => Error: Die gewünschte Serie wurde nicht gefunden oder ist im Moment deaktiviert: " << Name << std::endl;
2019-07-06 18:47:29 +00:00
return "-1";
} else if (html.find("404 - Seite nicht gefunden") != std::string::npos) {
std::cout << "\33[2K\r => Error: Ungültiger Name: " << Name << std::endl;
2019-07-06 18:47:29 +00:00
return "-1";
2019-07-07 16:30:37 +00:00
} else if (html == "-1") {
return "-1";
}
else {
2019-08-17 11:54:24 +00:00
std::cout << "\33[2K\r > Name: " << name << std::endl;
2019-07-06 18:47:29 +00:00
return name;
}
}
std::string PageManager::getLinks(std::string HTML)
{
size_t pos = HTML.find("<ul class=\"row\">");
if(pos == std::string::npos) {
2019-08-11 14:52:23 +00:00
std::cout << " => Error: Konnte Position von \"" << "<ul class=\"row\">" << " nicht finden" <<std::endl;
2019-07-06 18:47:29 +00:00
return "";
}
HTML.erase(0,pos);
pos = HTML.find("<script async=");
if(pos == std::string::npos) {
2019-08-11 14:52:23 +00:00
std::cout << " => Error: Konnte Position von \"" << "<script async=" << "\" nicht finden" <<std::endl;
2019-07-06 18:47:29 +00:00
return "";
}
HTML.erase(pos,HTML.length() - pos);
HTML = replace(HTML, "\n", ""); //HTML.replace("\n","").replace("</span>", "\n");
HTML = replace(HTML, "</span>", "\n");
HTML = grep(HTML,"href=\"/redirect/");
std::istringstream iStrStream( HTML + "\n" );
std::string line, ReturnValue;
size_t pos2;
while (getline(iStrStream, line).good()) {
2019-07-07 16:30:37 +00:00
if(line == "")
break;
2019-07-06 18:47:29 +00:00
pos=line.find("data-lang-key=");
if(pos == std::string::npos) {
2019-08-22 11:11:00 +00:00
std::cout << " => Error: Konnte Position von \"data-lang-key=\" nicht finden." << std::endl;
2019-07-06 18:47:29 +00:00
continue;
}
//entferne alles bis pos
line.erase(0,pos);
pos=line.find("data-link-id=");
if(pos == std::string::npos) {
2019-08-22 11:11:00 +00:00
std::cout << " => Error: Konnte Position von \"data-link-id=\" nicht finden." << std::endl;
2019-07-06 18:47:29 +00:00
continue;
}
pos2=line.find("href=\"");
if(pos2 == std::string::npos) {
2019-08-22 11:11:00 +00:00
std::cout << " => Error: Konnte Position von 'href=\"' nicht finden." << std::endl;
2019-07-06 18:47:29 +00:00
continue;
}
//Entferne alles von pos bis pos2
line.erase(pos,pos2-pos);
pos=line.find("target=");
if(pos == std::string::npos) {
2019-08-22 11:11:00 +00:00
std::cout << " => Error: Konnte Position von \"target=\" nicht finden." << std::endl;
2019-07-06 18:47:29 +00:00
continue;
}
pos2=line.find("title=");
if(pos2 == std::string::npos) {
2019-08-22 11:11:00 +00:00
std::cout << " => Error: Konnte Position von \"title=\" nicht finden." << std::endl;
2019-07-06 18:47:29 +00:00
continue;
}
//entferne alles von pos bis pos2
line.erase(pos,pos2-pos);
pos=line.find("><");
if(pos == std::string::npos) {
2019-08-22 11:11:00 +00:00
std::cout << " => Error: Konnte Position von \"><\" nicht finden." << std::endl;
2019-07-06 18:47:29 +00:00
continue;
}
//entferne alles von pos bis zum ende
line.erase(pos,line.length()-pos);
line = replace(line, "title=\"Hoster ", "hoster=\"");
ReturnValue+=line+"\n";
}
2019-08-11 14:52:23 +00:00
if(ReturnValue.length() > 0)
2019-07-06 18:47:29 +00:00
return ReturnValue.erase( ReturnValue.size()-1 , 1);
else
return "";
}
std::string PageManager::replace(std::string str, std::string substr1, std::string substr2)
{
2019-07-07 16:30:37 +00:00
if(substr1 == "")
return str;
2019-07-06 18:47:29 +00:00
size_t index = 0;
for (index = str.find(substr1, index); index != std::string::npos; index = str.find(substr1, index + strlen(substr1.c_str())) )
str.replace(index, strlen(substr1.c_str()), substr2);
return str;
}
int PageManager::counterContains(std::string text, std::string substring_with_prozent_i_for_number, int starte_mit_dieser_Zahl)
{
int i = starte_mit_dieser_Zahl;
for (; text.find( replace(substring_with_prozent_i_for_number, "%i", std::to_string(i)) ) != std::string::npos; i++);
return i-1;
}
std::string PageManager::grep(std::string text, std::string substring, bool IgnoreCaseSensetifity)
2019-07-06 18:47:29 +00:00
{
std::istringstream iStrStream(text + "\n");
std::string line, returnValue;
while( std::getline(iStrStream, line).good() ) //auto start_of_line_position = begin( line ); //auto end_of_line_position = end( line );
if(line.find(substring) != std::string::npos || ( IgnoreCaseSensetifity && upper_string(line).find(upper_string(substring)) != std::string::npos) )
2019-07-06 18:47:29 +00:00
returnValue += line + "\n";
if(returnValue.length() >= 1)
return returnValue.erase(returnValue.length()-1,1);
else
return "";
}
std::string PageManager::upper_string(const std::string &str)
{
std::string upper;
transform(str.begin(), str.end(), std::back_inserter(upper), toupper);
return upper;
}
2019-07-07 16:30:37 +00:00
int PageManager::writeToFile(std::string path, std::string text)
{
if(path == "")
return 0;
std::ofstream of;
of.open(path, std::ios::out | std::ios::app);
if(!of.is_open()) {
2019-08-11 17:15:33 +00:00
perror(" => Error: Konnte Output Datei nicht öffnen");
2019-07-07 16:30:37 +00:00
return -1;
}
of << text << std::endl;
of.close();
return 0;
}
2019-07-06 18:47:29 +00:00
std::string PageManager::chooseHosterLink(std::string HosterList, std::string Hoster_with_Highst_Priority_at_First, std::string languages_with_highst_priority_at_first)
{
std::istringstream SListLang ( replace( languages_with_highst_priority_at_first, ",", "\n") + "\n" );
std::istringstream SListHoster( replace( Hoster_with_Highst_Priority_at_First, ",", "\n") + "\n" );
int LangId = 0;
size_t pos = 0;
std::string LanguageSortedHoster, Line, langId, hoster;
//QTextStream stream();
//Für jede Sprache:
while (getline(SListLang, langId).good()) {
if ( langId == "GerDub" )
LangId=1;
else if ( langId == "Eng" )
LangId=2;
else if ( langId == "GerSub" )
LangId=3;
else {
2019-08-11 14:52:23 +00:00
std::cout << " => Error: Unbekannte Sprache: " << langId << std::endl;
2019-07-06 18:47:29 +00:00
continue;
}
//Liste aller Links mit der Sprache des durchgangs der schleife
LanguageSortedHoster = grep(HosterList, ( "data-lang-key=\"" + std::to_string(LangId) + "\"" ) );
//std::cout << "Alle Folgen mi der Sprache " << langId << ":\n'" << LanguageSortedHoster << "'" << std::endl;
2019-07-08 18:17:11 +00:00
//Wenn keine Links zu der Sprache gefunden worden sind
if(LanguageSortedHoster == "")
continue;
2019-07-06 18:47:29 +00:00
//Für jeden Angegebenen Hoster:
while (getline(SListHoster, hoster).good()) {
//Wenn es den hoster bei dieser prache nicht gibt, wähle nächsten
if(LanguageSortedHoster.find("hoster=\"" + hoster + "\"") == std::string::npos) {
//std::cout << "Hoster " << hoster << " gibt es bei der sprache" << langId << " nicht " << std::endl;
continue;
}
Line = grep(LanguageSortedHoster, ("hoster=\"" + hoster + "\"" ) );
pos = Line.find("href=\"");
if(pos == std::string::npos) {
2019-08-22 11:11:00 +00:00
std::cout << " => Error: Konnte 'href=\"' in chooseHosterLink() nicht finden." << std::endl;
2019-07-06 18:47:29 +00:00
continue;
}
Line.erase(0, pos + static_cast<int>(strlen("href=\"")));
pos = Line.find("\"");
if(pos == std::string::npos) {
2019-08-22 11:11:00 +00:00
std::cout << " => Error: Konnte '\"' in chooseHosterLink() nicht finden." << std::endl;
2019-07-06 18:47:29 +00:00
continue;
}
return Line.erase(pos, Line.length()-pos);
}
}
return "";
}