diff --git a/main.py b/main.py index 9e0859e..d9790fe 100644 --- a/main.py +++ b/main.py @@ -16,15 +16,17 @@ class Scraper: } def errorFormat(self, code: int = None, message: str = "") -> str: - return f"{f'[{code}]' if code else ''}{' ' if len(message) > 0 and code else ''}{message}" + return f"{f'[{code}]' if code else ''}{' ' if len(message) > 0 and code else ''}{message}." def connect(self) -> CloudScraper: session = create_scraper(browser = {"browser": "chrome", "platform": "windows"}) # connect with cloudflare bypasser with a chrome browser on windows + if not session: + raise SystemError(self.errorFormat(message = "The creation of the session failed")) if self.debug: print("Retrieval of the login SID...", end = " ") 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(code = reponse.status_code)) + raise ConnectionError(self.errorFormat(code = reponse.status_code, message = "Login page not available")) try: self.loginData["sid"] = reponse.cookies.get_dict()["ppcw_29d3s_sid"] # register "sid" except: @@ -34,27 +36,28 @@ class Scraper: 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(code = reponse.status_code)) + raise ConnectionRefusedError(self.errorFormat(code = reponse.status_code, message = "Unable to connect")) if self.debug: print("Connection done.") reponse = session.get(f"{self.url}/index.php", cookies = reponse.cookies, params = {"sid": self.loginData["sid"]}) # back to index page + if reponse.status_code != 200: + raise ConnectionError(self.errorFormat(code = reponse.status_code, message = "Unable to get to the index page")) return session - def search(self, session) -> str: + def search(self, session) -> list: if self.debug: print("Going to search page...", end = " ") reponse = session.get(f"{self.url}/search.php", params = {"keywords": self.requested_app, "sr": "topics", "sf": "titleonly"}) + if reponse.status_code != 200: + raise ConnectionError(self.errorFormat(code = reponse.status_code, message = "Impossible to make the search")) - if self.debug: print("Results retrieval...", end = " ") - linkList = self.parse(reponse.text) - # if self.debug: print(reponse.status_code, reponse.url) - # with open("temp2.log", "w") as f: # debug - # f.writelines(res) + if self.debug: print(f"Results retrieval for {self.requested_app}...", end = " ") - link = "No link for your application was found." - return link + return self.parse(reponse.text) def parse(self, htmlPage: str) -> list: + if "No suitable matches were found." in htmlPage: + return [] elements = htmlPage.split("