diff --git a/main.py b/main.py index 249347f..eb724f5 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,9 @@ +from difflib import restore from dotenv import load_dotenv from os import environ from sys import argv -from requests_html import BaseSession +from requests_html import HTMLSession class Universite: def __init__(self, url: str, pseudo: str, motDePasse: str): @@ -14,14 +15,14 @@ class Universite: "submit": "SE CONNECTER" } - def ecrirePageHTML(self, texte: str): + def ecrirePageHTML(self, nom: str, texte: str): """Affiche la page HTML pour le debug.""" - with open("page.html", 'w') as f: + with open(f"{nom}.html", 'w') as f: f.write(texte) def recuperationNotes(self) -> dict: """Récupère les notes sous forme d'un dictionnaire.""" - with BaseSession() as session: + with HTMLSession() as session: reponse = session.get(self.url) # login @@ -41,28 +42,28 @@ class Universite: # 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) + anneesTemp = [element for element in reponse.html.find('a') if "href" in element.attrs if element.attrs["href"] == '#'][6:] + # on retire un item sur deux car : ['L2MINF/210', 'L2 Informatique', 'L1MINF/210', 'L1 Informatique'] il y a des doublons + annees = [] + for i in range(0, len(anneesTemp)): + if i % 2: + annees.append(anneesTemp[i]) - # page des notes - # TODO + # récupération notes + resultat = {} + for annee in annees: + reponse = session.post(url) + script = annee.attrs['onclick'][7:] + reponse.html.render(script=script) + resultat[annee.text] = None - return self.stringVersDictNotes(reponse.text) + url = url.replace("notes", "detailnotes") + reponse = session.get(url) + url = url.replace("detailnotes", "notes") + self.ecrirePageHTML(annee.text, reponse.text) - def stringVersDictNotes(self, html: str) -> dict: - """Récupère la page HTML contenant les notes et renvoie un dictionnaire.""" - self.ecrirePageHTML(html) - # 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": None} + return resultat def affichageNotes(self, notes: dict) -> str: """Renvoie un jolie tableau avec les notes"""