big enhancements

This commit is contained in:
Mylloon 2021-08-23 13:56:36 +02:00
parent 3e0daa93b6
commit fcc4af88af

40
main.py
View file

@ -1,7 +1,7 @@
from sys import argv
from os import environ
from dotenv import load_dotenv
from cloudscraper import create_scraper
from cloudscraper import CloudScraper, create_scraper
class Scraper:
def __init__(self, pseudo, password, app, debug = False):
@ -14,38 +14,42 @@ class Scraper:
"login": "Login"
}
def errorFormat(self, code: int, message: str = "") -> str:
return f"Error: [{code}]{' ' if len(message) > 0 else ''}{message}"
def errorFormat(self, code: int = None, message: str = "") -> str:
return f"{f'[{code}]' if code else ''}{' ' if len(message) > 0 and code else ''}{message}"
def connect(self):
def connect(self) -> CloudScraper:
session = create_scraper(browser = {"browser": "chrome", "platform": "windows"}) # connect with cloudflare bypasser with a chrome browser on windows
if self.debug: print("Retrieval of the login SID...", end = " ")
reponse = session.get(f"{self.url}/ucp.php?mode=login") # get login page to get "sid"
reponse = session.get(f"{self.url}/ucp.php", params = {"mode": "login"}) # get login page to get "sid"
if reponse.status_code != 200:
raise ConnectionError(self.errorFormat(reponse.status_code))
self.loginData["sid"] = reponse.cookies.get_dict()["ppcw_29d3s_sid"] # register "sid"
raise ConnectionError(self.errorFormat(code = reponse.status_code))
try:
self.loginData["sid"] = reponse.cookies.get_dict()["ppcw_29d3s_sid"] # register "sid"
except:
raise ValueError(self.errorFormat(message = "Cookie containing the SID not found."))
if self.debug: print("SID retrieval done,", end = " ")
if self.debug: print("SID retrieval done, connection attempt...", end = " ")
reponse = session.post(f"{self.url}/ucp.php?mode=login", data = self.loginData) # connect to the forum using credentials
if self.debug: print("connection attempt...", end = " ")
reponse = session.post(f"{self.url}/ucp.php", data = self.loginData, params = {"mode": "login"}) # connect to the forum using credentials
if reponse.status_code != 200:
raise ConnectionRefusedError(self.errorFormat(reponse.status_code))
raise ConnectionRefusedError(self.errorFormat(code = reponse.status_code))
if self.debug: print("Connection done.")
reponse = session.get(f"{self.url}/index.php", cookies = reponse.cookies, params = {"sid": self.loginData["sid"]})
if self.debug: print(reponse.status_code, reponse.url)
with open("temp.html", "w") as f: # debug
f.writelines(reponse.text)
reponse = session.get(f"{self.url}/index.php", cookies = reponse.cookies, params = {"sid": self.loginData["sid"]}) # back to index page
return session
def search(self, session):
def search(self, session) -> str:
if self.debug: print("Going to search page...", end = " ")
reponse = session.get(f"{self.url}/index.php")
with open("temp.html", "w") as f: # debug
f.writelines(reponse.text)
link = "No link for your application was found."
return link
def work(self):
def work(self) -> str:
session = self.connect()
link = self.search(session)
@ -63,5 +67,5 @@ if __name__ == "__main__":
except:
debug = False
print(Scraper(environ["PSEUDO_MOBILISM"], environ["PASSWORD_MOBILISM"], environ["APP_MOBILISM"], debug).work())
except:
except KeyError:
print('Please fill in the username and password (with ") by args or with .env file.')