2022-01-14 16:36:37 +01:00
|
|
|
from difflib import restore
|
2022-01-14 15:16:57 +01:00
|
|
|
from dotenv import load_dotenv
|
|
|
|
from os import environ
|
2021-05-24 22:22:03 +02:00
|
|
|
from sys import argv
|
2022-01-14 15:16:57 +01:00
|
|
|
|
2022-01-14 16:36:37 +01:00
|
|
|
from requests_html import HTMLSession
|
2021-05-24 22:22:03 +02:00
|
|
|
|
|
|
|
class Universite:
|
2022-01-13 23:07:30 +01:00
|
|
|
def __init__(self, url: str, pseudo: str, motDePasse: str):
|
2021-05-24 22:22:03 +02:00
|
|
|
self.url = url
|
|
|
|
self.loginData = {
|
|
|
|
"username": pseudo,
|
|
|
|
"password": motDePasse,
|
|
|
|
"_eventId": "submit",
|
|
|
|
"submit": "SE CONNECTER"
|
|
|
|
}
|
2022-01-13 21:23:22 +01:00
|
|
|
|
2022-01-14 16:36:37 +01:00
|
|
|
def ecrirePageHTML(self, nom: str, texte: str):
|
2022-01-13 22:52:32 +01:00
|
|
|
"""Affiche la page HTML pour le debug."""
|
2022-01-14 16:36:37 +01:00
|
|
|
with open(f"{nom}.html", 'w') as f:
|
2022-01-13 22:52:32 +01:00
|
|
|
f.write(texte)
|
2021-05-25 00:25:59 +02:00
|
|
|
|
2022-01-14 15:23:27 +01:00
|
|
|
def recuperationNotes(self) -> dict:
|
|
|
|
"""Récupère les notes sous forme d'un dictionnaire."""
|
2022-01-14 16:36:37 +01:00
|
|
|
with HTMLSession() as session:
|
2021-05-24 22:22:03 +02:00
|
|
|
reponse = session.get(self.url)
|
2022-01-14 14:58:11 +01:00
|
|
|
|
|
|
|
# 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]
|
2021-05-24 22:22:03 +02:00
|
|
|
reponse = session.post(self.url, data = self.loginData)
|
|
|
|
|
|
|
|
# page des résultats intermédiaire
|
2021-05-25 00:25:59 +02:00
|
|
|
try:
|
2022-01-14 14:58:11 +01:00
|
|
|
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"
|
2021-05-25 00:25:59 +02:00
|
|
|
raise TimeoutError("Le site a prit trop de temps pour répondre, veuillez réessayez plus tard.")
|
2021-05-24 22:22:03 +02:00
|
|
|
reponse = session.get(url, allow_redirects = False)
|
|
|
|
url = reponse.headers["Location"]
|
|
|
|
reponse = session.get(url)
|
|
|
|
|
2022-01-13 22:52:32 +01:00
|
|
|
# choix des années
|
2022-01-14 14:58:11 +01:00
|
|
|
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')}"
|
2021-05-24 22:22:03 +02:00
|
|
|
reponse = session.get(url)
|
2022-01-14 16:36:37 +01:00
|
|
|
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])
|
2022-01-13 22:52:32 +01:00
|
|
|
|
2022-01-14 16:36:37 +01:00
|
|
|
# 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
|
2021-05-24 22:22:03 +02:00
|
|
|
|
2022-01-14 16:36:37 +01:00
|
|
|
url = url.replace("notes", "detailnotes")
|
|
|
|
reponse = session.get(url)
|
|
|
|
url = url.replace("detailnotes", "notes")
|
|
|
|
self.ecrirePageHTML(annee.text, reponse.text)
|
2022-01-14 15:23:27 +01:00
|
|
|
|
|
|
|
|
2022-01-14 16:36:37 +01:00
|
|
|
return resultat
|
2022-01-14 15:23:27 +01:00
|
|
|
|
|
|
|
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()))
|
2021-05-24 22:22:03 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-01-14 15:16:57 +01:00
|
|
|
nom = argv.pop(0)
|
2021-05-24 22:22:03 +02:00
|
|
|
if len(argv) == 3:
|
2022-01-14 15:23:27 +01:00
|
|
|
Universite(*argv).notes()
|
2021-05-24 22:22:03 +02:00
|
|
|
else:
|
2022-01-14 15:16:57 +01:00
|
|
|
load_dotenv()
|
|
|
|
try:
|
2022-01-14 15:23:27 +01:00
|
|
|
Universite(environ["URL"], environ["LOGIN"], environ["PASSWORD"]).notes()
|
2022-01-14 15:27:10 +01:00
|
|
|
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""")
|