From 3f11d7342f0bab300c05d85b4da2dcc20ff47ab5 Mon Sep 17 00:00:00 2001 From: ShadowMikado <89030950+ShadowMikado@users.noreply.github.com> Date: Tue, 3 Oct 2023 21:00:54 +0200 Subject: [PATCH] Various features (#273) * Update clients.py * Update clients.py * Update clients.py * Update test_pronotepy.py * Update clients.py (add get_last_connection) * Update test_pronotepy.py * Update clients.py (simplify calendar URL) * Update clients.py * Update dataClasses.py (moved last_connection in ClientInfo) * Update test_pronotepy.py * rename get_calendar and add parameters * move last_connection to Client * use typing.Tuple (python 3.7 support) --------- Co-authored-by: bain --- pronotepy/clients.py | 68 ++++++++++++++++++++++++++++++++++++- pronotepy/test_pronotepy.py | 6 ++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/pronotepy/clients.py b/pronotepy/clients.py index 526a34a..16fef7f 100644 --- a/pronotepy/clients.py +++ b/pronotepy/clients.py @@ -1,11 +1,12 @@ import datetime import logging from time import time -from typing import List, Callable, Optional, Union, TypeVar, Type, TYPE_CHECKING +from typing import List, Callable, Optional, Union, TypeVar, Type, TYPE_CHECKING, Tuple from Crypto.Hash import SHA256 from uuid import uuid4 import re +import urllib from . import dataClasses from .exceptions import * @@ -44,6 +45,7 @@ class ClientBase: password (str) pronote_url (str) info (ClientInfo): Provides information about the current client. Name etc... + last_connection (datetime.datetime) """ def __init__( @@ -104,6 +106,8 @@ def __init__( self.logged_in = self._login() self._expired = False + self.last_connection: Optional[datetime.datetime] + @classmethod def qrcode_login(cls: Type[T], qr_code: dict, pin: str, uuid: str) -> T: """Login with QR code @@ -230,6 +234,11 @@ def _login(self) -> bool: self.encryption.aes_key = e.aes_key log.info(f"successfully logged in as {self.username}") + last_conn = auth_response["donneesSec"]["donnees"].get("derniereConnexion") + self.last_connection = ( + dataClasses.Util.datetime_parse(last_conn["V"]) if last_conn else None + ) + if self.login_mode in ("qr_code", "token") and auth_response["donneesSec"][ "donnees" ].get("jetonConnexionAppliMobile"): @@ -508,6 +517,63 @@ def homework( out.append(hw) return out + def generate_timetable_pdf( + self, + day: Optional[datetime.date] = None, + portrait: bool = False, + overflow: int = 0, + font_size: Tuple[int, int] = (8, 3), + ) -> str: + """Generate a PDF timetable. + + Args: + day (Optional[datetime.date]): the day for which to create the timetable, the whole week is always generated + portrait (bool): switches the timetable to portrait mode + overflow (int): Controls overflow / formatting of lesson names. + + * ``0``: no overflow + * ``1``: overflow to the same page, long names printed on the bottom + * ``2``: overflow to a separate page + + font_size (Tuple[int, int]): font size restrictions, first element is the *preferred* font size, and second is the *minimum* + """ + user = { + "G": 4, + "N": self.parametres_utilisateur["donneesSec"]["donnees"]["ressource"]["N"], + } + + data = { + "options": { + "portrait": portrait, + "taillePolice": font_size[0], + "taillePoliceMin": font_size[1], + "couleur": 1, + "renvoi": overflow, + "uneGrilleParSemaine": False, + "inversionGrille": False, + "ignorerLesPlagesSansCours": False, + "estEDTAnnuel": day is None, + }, + "genreGenerationPDF": 0, + "estPlanning": False, + "estPlanningParRessource": False, + "estPlanningOngletParJour": False, + "estPlanningParJour": False, + "indiceJour": 0, + "ressource": user, + "ressources": [user], + "domaine": {"_T": 8, "V": f"[{self.get_week(day) if day else 0}]"}, + "avecCoursAnnules": True, + "grilleInverse": False, + } + + response = self.post("GenerationPDF", 16, data) + return ( + self.communication.root_site + + "/" + + response["donneesSec"]["donnees"]["url"]["V"] + ) + def get_recipients(self) -> List[dataClasses.Recipient]: """Get recipients for new discussion diff --git a/pronotepy/test_pronotepy.py b/pronotepy/test_pronotepy.py index cc7c527..a969fff 100644 --- a/pronotepy/test_pronotepy.py +++ b/pronotepy/test_pronotepy.py @@ -87,6 +87,12 @@ def test_refresh(self) -> None: def test_get_teaching_staff(self) -> None: self.assertGreater(len(client.get_teaching_staff()), 0) + def test_get_calendar(self) -> None: + import requests + + resp = requests.get(client.generate_timetable_pdf()) + self.assertEqual(resp.status_code, 200) + class TestPeriod(unittest.TestCase): period: pronotepy.Period