Skip to content

Commit

Permalink
Various features (#273)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
ShadowMikado and bain3 authored Oct 3, 2023
1 parent 0e271b4 commit 3f11d73
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
68 changes: 67 additions & 1 deletion pronotepy/clients.py
Original file line number Diff line number Diff line change
@@ -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 *
Expand Down Expand Up @@ -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__(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"):
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions pronotepy/test_pronotepy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3f11d73

Please sign in to comment.