From c3e7a02961ef589c26c0acd26050ceb7524cd839 Mon Sep 17 00:00:00 2001 From: bain Date: Tue, 20 Aug 2024 00:05:05 +0200 Subject: [PATCH] keep fake User-Agent header for all requests Looks like some PRONOTE instances actively block clients that have a weird User-Agent header. It should be fine to send it on all requests. The header can be changed using: import pronotepy.pronoteAPI pronotepy.pronoteAPI.HEADERS = { "User-agent": "...", ... } --- pronotepy/pronoteAPI.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/pronotepy/pronoteAPI.py b/pronotepy/pronoteAPI.py index 2477b87..58a1562 100644 --- a/pronotepy/pronoteAPI.py +++ b/pronotepy/pronoteAPI.py @@ -33,6 +33,10 @@ 25: "[ERROR 25] Exceeded max authorization requests. Please wait before retrying...", } +HEADERS = { + "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:73.0) Gecko/20100101 Firefox/73.0", +} + class _Communication(object): def __init__( @@ -40,7 +44,10 @@ def __init__( ) -> None: """Handles all communication with the PRONOTE servers""" self.root_site, self.html_page = self.get_root_address(site) + self.session = requests.Session() + self.session.headers.update(HEADERS) + self.encryption = _Encryption() self.attributes: dict = {} self.request_number = 1 @@ -57,11 +64,6 @@ def initialise(self) -> Tuple[Any, Any]: Initialisation of the communication. Sets up the encryption and sends the IV for AES to PRONOTE. From this point, everything is encrypted with the communicated IV. """ - # some headers to be real - headers = { - "connection": "keep-alive", - "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:73.0) Gecko/20100101 Firefox/73.0", - } # get rsa keys and session id, retry 3 times for _ in range(3): @@ -70,7 +72,6 @@ def initialise(self) -> Tuple[Any, Any]: get_response = self.session.request( "GET", f"{self.root_site}/{self.html_page}", - headers=headers, cookies=self.cookies, ) self.attributes = self._parse_html(get_response.content)