This repository has been archived on 2023-01-13. You can view files and clone it, but cannot push or open issues or pull requests.
uwm/main.py
2022-01-14 15:16:57 +01:00

76 lines
3.1 KiB
Python

from dotenv import load_dotenv
from os import environ
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__":
nom = argv.pop(0)
if len(argv) == 3:
print(Universite(*argv).recuperationNotes())
else:
load_dotenv()
try:
print(Universite(environ["URL"], environ["LOGIN"], environ["PASSWORD"]).recuperationNotes())
except:
print(f"""Merci 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""")