remove useless call and add comments

This commit is contained in:
Mylloon 2021-08-24 01:51:53 +02:00
parent 67ddba74dd
commit a437d91096

37
main.py
View file

@ -16,47 +16,38 @@ class Scraper:
}
def errorFormat(self, code: int = None, message: str = "") -> str:
"""Pretty error message."""
return f"{f'[{code}]' if code else ''}{' ' if len(message) > 0 and code else ''}{message}."
def connect(self) -> CloudScraper:
"""Login to the forum using credentials."""
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, message = "Login page not available"))
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 = " ")
raise SystemError(self.errorFormat(message = "The creation of the session failed")) # called only if failed at creating the session
if self.debug: print("Connection attempt...")
reponse = session.post(f"{self.url}/ucp.php", data = self.loginData, params = {"mode": "login"}) # connect to the forum using credentials
reponse = session.post(f"{self.url}/ucp.php", data = self.loginData, params = {"mode": "login"}) # connect to the forum using credentials - params are set by default but its in case forum changing that
if reponse.status_code != 200:
raise ConnectionRefusedError(self.errorFormat(code = reponse.status_code, message = "Unable to connect"))
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"))
raise ConnectionRefusedError(self.errorFormat(code = reponse.status_code, message = "Unable to connect")) # called only status code isn't 200
return session
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"})
"""Do the research."""
if self.debug: print("Going to search page and check connection...", end = " ")
reponse = session.get(f"{self.url}/search.php", params = {"keywords": self.requested_app, "sr": "topics", "sf": "titleonly"}) # fetch results page
if "Sorry but you are not permitted to use the search system. If you're not logged in please" in reponse.text:
raise ConnectionError(self.errorFormat(message = "Connection failed, check credentials"))
raise ConnectionError(self.errorFormat(message = "Connection failed, check credentials")) # called only if login failed
if reponse.status_code != 200:
raise ConnectionError(self.errorFormat(code = reponse.status_code, message = "Impossible to make the search"))
raise ConnectionError(self.errorFormat(code = reponse.status_code, message = "Impossible to make the search")) # called only status code isn't 200
if self.debug: print(f"Connected.")
if self.debug: print(f"Results retrieval for {self.requested_app}...", end = " ")
if self.debug: print(f"Fetching results for {self.requested_app}...", end = " ")
return self.parse(reponse.text)
def parse(self, htmlPage: str) -> list:
"""Parse HTML reponse to a clean list"""
if "No suitable matches were found." in htmlPage:
return []
elements = htmlPage.split("<tr>\n<td>")[1:]
@ -80,12 +71,14 @@ class Scraper:
return elements
def work(self) -> str:
"""Call all the others methods."""
session = self.connect()
link = self.search(session)
return link
def save(elements):
"""Save all the results parsed to a CSV file."""
taille = len(elements)
if taille == 0:
print("Aucun élément n'a été trouvé avec la recherche.")