92 lines
3.9 KiB
Python
92 lines
3.9 KiB
Python
from difflib import restore
|
|
from dotenv import load_dotenv
|
|
from os import environ
|
|
from sys import argv
|
|
|
|
from requests_html import HTMLSession
|
|
|
|
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, nom: str, texte: str):
|
|
"""Affiche la page HTML pour le debug."""
|
|
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 HTMLSession() 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)
|
|
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])
|
|
|
|
# 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
|
|
|
|
url = url.replace("notes", "detailnotes")
|
|
reponse = session.get(url)
|
|
url = url.replace("detailnotes", "notes")
|
|
self.ecrirePageHTML(annee.text, reponse.text)
|
|
|
|
|
|
return resultat
|
|
|
|
def affichageNotes(self, notes: dict) -> str:
|
|
"""Renvoie un jolie tableau avec les notes"""
|
|
return str(notes)
|
|
|
|
def notes(self):
|
|
"""Affiche les notes dans stdout."""
|
|
print(self.affichageNotes(self.recuperationNotes()))
|
|
|
|
if __name__ == "__main__":
|
|
nom = argv.pop(0)
|
|
if len(argv) == 3:
|
|
Universite(*argv).notes()
|
|
else:
|
|
load_dotenv()
|
|
try:
|
|
Universite(environ["URL"], environ["LOGIN"], environ["PASSWORD"]).notes()
|
|
except KeyError:
|
|
raise Exception(f"""
|
|
\nMerci de renseigner l'URL, le pseudo et le mot de passe (avec des \"). \
|
|
\n-> python3 {nom} "URL" "pseudo" "mot-de-passe" \
|
|
\n--- \
|
|
\nOu fichier .env contenant ses informations avec les noms (conseillé) : \
|
|
\n-> URL \
|
|
\n-> LOGIN \
|
|
\n-> PASSWORD""")
|