69 lines
2.5 KiB
Python
69 lines
2.5 KiB
Python
from sys import argv
|
|
from requests import Session
|
|
from bs4 import BeautifulSoup
|
|
|
|
class Universite:
|
|
def __init__(self, url, pseudo, motDePasse):
|
|
self.url = url
|
|
self.loginData = {
|
|
"username": pseudo,
|
|
"password": motDePasse,
|
|
"_eventId": "submit",
|
|
"submit": "SE CONNECTER"
|
|
}
|
|
|
|
def maSoupe(self, page):
|
|
"""Macro qui utilise le parser HTML de bs4."""
|
|
return BeautifulSoup(page.content, "html.parser")
|
|
|
|
def ecrirePageHTML(self, texte):
|
|
"""Affiche la page HTML pour le debug."""
|
|
with open("page.html", 'w') as f:
|
|
f.write(texte)
|
|
|
|
def recuperationNotes(self):
|
|
"""Récupère les notes."""
|
|
with Session() as session:
|
|
# login
|
|
reponse = session.get(self.url)
|
|
soup = self.maSoupe(reponse)
|
|
self.loginData["lt"] = soup.find("input", attrs = {"name": "lt"})["value"]
|
|
self.loginData["execution"] = soup.find("input", attrs = {"name": "execution"})["value"]
|
|
reponse = session.post(self.url, data = self.loginData)
|
|
|
|
# page des résultats intermédiaire
|
|
soup = self.maSoupe(reponse)
|
|
try:
|
|
url = soup.find("a", attrs = {"id": "service-407"})["href"]
|
|
except:
|
|
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
|
|
soup = self.maSoupe(reponse)
|
|
url = f"{url}?{soup.find('form', attrs = {'enctype': 'application/x-www-form-urlencoded'})['action'].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
|
|
|
|
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 \").")
|