bain3 / pronotepy

A python API wrapper for pronote
MIT License
189 stars 55 forks source link

Query lessons over longer period, missing last available end-date ? #248

Closed vingerha closed 1 year ago

vingerha commented 1 year ago

Hello, I am trying to write a logic to extract lessons for the next (say) 15 days. This I do via client.lessons (start, end) However, in certain cases the end date results into errors as (I guess) there are no lessons in the system, see below. The solution I now apply is to start from (say) today+15 and then step-wise reduce until data pops-up. The max number of iterations is thus 15 and this may lead to blocking from pronote. Is there another way of doing this, e.g. can I get the last available date with lessons with 1 'call'?


Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 320, in _async_setup_platform
    await asyncio.shield(task)
  File "/config/custom_components/pronote/sensor.py", line 117, in async_setup_entry
    lessons_period = await hass.async_add_executor_job(client.lessons, date.today(), date.today() + timedelta(days=LESSON_MAX_DAYS))
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pronotepy/clients.py", line 401, in lessons
    l_list = response["donneesSec"]["donnees"]["ListeCours"]
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
KeyError: 'ListeCours'
tarneaux commented 1 year ago

Hi, the following code definitely works with the test instance:

import pronotepy
import datetime

client = pronotepy.Client('https://demo.index-education.net/pronote/eleve.html',
                          username='demonstration',
                          password='pronotevs')

assert client.logged_in

# Get lessons for the entire year
lessons = client.lessons(datetime.date.today() - datetime.timedelta(days=365), 
                         datetime.date.today() + datetime.timedelta(days=365))

for lesson in lessons:
    # Testing what you've said doesn't work
    assert type(lesson.end) is datetime.datetime
    assert type(lesson.start) is datetime.datetime

Could you give us a working example in raw python that doesn't use another tool like HomeAssistant?

Another possible reason would be that the version of Pronote your school uses could be too old for Pronotepy to work. Did you see the UI redesign at the start of the year? If not, it may indicate that your school has bigger problems like not updating their software...

Cheers

vingerha commented 1 year ago

Thanks a lot, checked with my 2 schools and no error. I will ask the user for access to his account and see what that brings

vingerha commented 1 year ago

Could you give us a working example in raw python that doesn't use another tool like HomeAssistant?

Although the output is used in HA, this has nothing to do with that, I run my scripts on ubuntu with pronotepy lib. But it is indeed something I did not think of when possibly running this from HAOS (which I do not)

vingerha commented 1 year ago

I finally got some feedback... This is what I used. The connection works ith parent user/pwd (via the browser) but the script crashes, could be related to 'parent' and asking the user to provide user/pwd for eleve as well.

#Connection à Pronote 
client = pronotepy.Client('https://xxxxxxxx.index-education.net/pronote/parent.html', username, password, ent)

assert client.logged_in

print("pass 1")

# Get lessons for the entire year
lessons = client.lessons(datetime.date.today() - datetime.timedelta(days=365), 
                         datetime.date.today() + datetime.timedelta(days=365))

for lesson in lessons:
    # Testing what you've said doesn't work
    assert type(lesson.end) is datetime.datetime
    assert type(lesson.start) is datetime.datetime                      

print("pass 2")   

ERROR


pass 1
DEBUG:pronotepy.ent.generic_func:[ENT https://www.atrium-sud.fr/connexion/login] Logging in with 09CD7665421D
Traceback (most recent call last):
  File "/mnt/3692670A6ACD9016/docker/homeassistant1/python_scripts/pronote_old_attempts/pronotepy/clients.py", line 313, in post
    return self.communication.post(function_name, post_data)
  File "/mnt/3692670A6ACD9016/docker/homeassistant1/python_scripts/pronote_old_attempts/pronotepy/pronoteAPI.py", line 184, in post
    raise PronoteAPIError(
pronotepy.exceptions.PronoteAPIError: Unknown error from pronote: 3 | Accès refusé

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/mnt/3692670A6ACD9016/docker/homeassistant1/python_scripts/pronote_old_attempts/test4_sigalou.py", line 25, in <module>
    lessons = client.lessons(datetime.date.today() - datetime.timedelta(days=365),
  File "/mnt/3692670A6ACD9016/docker/homeassistant1/python_scripts/pronote_old_attempts/pronotepy/clients.py", line 400, in lessons
    response = self.post("PageEmploiDuTemps", 16, data)
  File "/mnt/3692670A6ACD9016/docker/homeassistant1/python_scripts/pronote_old_attempts/pronotepy/clients.py", line 327, in post
    self.refresh()
  File "/mnt/3692670A6ACD9016/docker/homeassistant1/python_scripts/pronote_old_attempts/pronotepy/clients.py", line 266, in refresh
    cookies = self.ent(self.username, self.password)
  File "/mnt/3692670A6ACD9016/docker/homeassistant1/python_scripts/pronote_old_attempts/pronotepy/ent/generic_func.py", line 464, in _simple_auth
    raise ENTLoginError(
pronotepy.exceptions.ENTLoginError: Fail to connect with https://www.atrium-sud.fr/connexion/login : probably wrong login information
vingerha commented 1 year ago

I now also received usr/pwd for eleve. of above script I only changed the url to eleve.html This error below. @tarneaux I have no clue if their version is old, is also not my child so will not be able to chase it. Do you have any other idea on how to get a last lesson date?


pass 1
Traceback (most recent call last):
  File "/mnt/3692670A6ACD9016/docker/homeassistant1/python_scripts/pronote_old_attempts/test4_sigalou_eleve.py", line 25, in <module>
    lessons = client.lessons(datetime.date.today() - datetime.timedelta(days=365),
  File "/mnt/3692670A6ACD9016/docker/homeassistant1/python_scripts/pronote_old_attempts/pronotepy/clients.py", line 401, in lessons
    l_list = response["donneesSec"]["donnees"]["ListeCours"]
KeyError: 'ListeCours'
tarneaux commented 1 year ago

When connecting as a parent, the cause of the error seems pretty clear to me: because a parent may have multiple children in the same school, we would need to first select one of the children, how can it know which it is otherwise? Even if the parent has only one child at that school, the API would still be the same, needing the client to select a child. What I still don't know is why it returns "Accès refusé" instead of something like "Adresse introuvable", because logically there shouldn't be a PageEmploiDuTemps in the root.

For the other test you did, it does look like an outdated version to me, though my knowledge of pronotepy falls short here, and I think @bain3 will have to take a look.

vingerha commented 1 year ago

Thanks and makes sense... although having 2 children, I forgot that they could be in the same school :) and I have never used the parent access myself, not via web not via api. Will wait for input from bain then

bain3 commented 1 year ago

Hi, to connect to a parent account you must use a ParentClient. It supports switching between children. I'm not sure how the KeyError was raised though...

Do you have any other idea on how to get a last lesson date?

There is something in the FonctionParametres response called DerniereDate, which might be what you're looking for.

client.func_options["donneesSec"]["donnees"]["General"]["DerniereDate"]["V"]  # '27/08/2023'
# use datetime.datetime.strptime() or pronotepy.Util.date_parse() to parse the date
vingerha commented 1 year ago

Thanks a lot !!

bain3 commented 1 year ago

Are you still experiencing this issue? I can't seem to reproduce it...

vingerha commented 1 year ago

Very sorry, I thought to have closed it....so no ...no lomger needed with your hint above :)