from sys import argv
from requests_html import BaseSession
class Universite:
def __init__(self, url: str, pseudo: str, motDePasse: str):
self.url = url
self.loginData = {
"username": pseudo,
"password": motDePasse,
"_eventId": "submit",
"submit": "SE CONNECTER"
}
def ecrirePageHTML(self, texte: str):
"""Affiche la page HTML pour le debug."""
with open("page.html", 'w') as f:
f.write(texte)
def recuperationNotes(self) -> str:
"""Récupère les notes."""
with BaseSession() as session:
reponse = session.get(self.url)
# login
self.loginData["lt"] = [element.attrs["value"] for element in reponse.html.find("input") if element.attrs["name"] == "lt"][0]
self.loginData["execution"] = [element.attrs["value"] for element in reponse.html.find("input") if element.attrs["name"] == "execution"][0]
reponse = session.post(self.url, data = self.loginData)
# page des résultats intermédiaire
try:
url = [element.attrs["href"] for element in reponse.html.find("a") if "id" in element.attrs if element.attrs["id"] == "service-407"][0]
except IndexError: # Arrive quand "An Error Has Occurred"
raise TimeoutError("Le site a prit trop de temps pour répondre, veuillez réessayez plus tard.")
reponse = session.get(url, allow_redirects = False)
url = reponse.headers["Location"]
reponse = session.get(url)
# choix des années
url = f"{url}?{[element.attrs['action'] for element in reponse.html.find('form') if 'enctype' in element.attrs if element.attrs['enctype'] == 'application/x-www-form-urlencoded'][0].split('?')[1].replace('welcome', 'notes')}"
reponse = session.get(url)
self.ecrirePageHTML(reponse.text)
# page des notes
# TODO
# récupération tableaux des notes
""" soup = self.maSoupe(reponse)
for attrs in soup.findAll("table"):
try:
texte = str(attrs).split("thead")[1][2:-2]
while " " in texte:
texte = texte.replace(" ", ' ')
return texte
except:
pass """
return "WIP"
if __name__ == "__main__":
argv = argv[1:]
if len(argv) == 3:
print(Universite(*argv).recuperationNotes())
else:
print("Merci de renseigner l'URL, le pseudo et le mot de passe (avec des \").")