Skip to content

Conversation

@ivanhrabcak
Copy link
Collaborator

Hopefully fixes #97.

Currently there is support for "statistics" (how many late/excused/unexcused/absent lessons?) and arrivals (the time of arrival and departure from school).

Example usage

Here is a short example, that prints how many missed lessons there are for each day where there is attendance data, and checks if all the missed lessons are excused:

from edupage_api import Edupage

edupage = Edupage()
edupage.login("Username", "Password", "Your school's subdomain")

my_user_id = edupage.get_user_id()

available_dates = edupage.get_days_with_available_attendance(my_user_id)
for date in available_dates:
    stats = edupage.get_attendance_statistics(my_user_id, date)

    # You can find what data is available here: https://github.com/EdupageAPI/edupage-api/blob/0643e9c202e42f918222829e3610c99e079a3bfb/edupage_api/attendance.py#L21-L40
    print(
        f"On {date.strftime('%d-%m-%Y')} I missed {stats.total_lessons_absent.count} lessons."
    )

    if stats.total_lessons_absent.count == stats.total_lessons_absent.excused:
        print("Luckily, all of them are excused.")

And for the last new method, here is a short example that prints the arrival and departure time for each date where there is attendance data:

from edupage_api import Edupage

edupage = Edupage()
edupage.login("Username", "Password", "Your school's subdomain")

my_user_id = edupage.get_user_id()
for arrival in edupage.get_arrivals(my_user_id).values():
    if arrival.arrival is not None:
        print(
            f"On {arrival.date.strftime('%d-%m-%Y')} I arrived to school at {arrival.arrival.strftime('%H:%M:%S')}"
        )

    if arrival.departure is not None:
        print(
            f"On {arrival.date.strftime('%d-%m-%Y')} I left school at {arrival.departure.strftime('%H:%M:%S')}"
        )

@kapitan-iglu, could you please test if this solves your issue? While developing I noticed that only days with an excuse note show up in the My Attendance -> Arrivals to school column. Maybe I am looking at the wrong page? Is that the case for you?

You can install the version of edupage_api from this branch by running:

pip install -U git+https://github.com/EdupageAPI/edupage-api@feature/attendance

And to revert back:

pip uninstall edupage-api
pip install edupage-api

@ivanhrabcak ivanhrabcak requested a review from BelKed October 27, 2025 00:08
@kapitan-iglu
Copy link

Just tested both example codes but I'm getting exception in JSON response parsing.

  • Installed your branch using pip into my venv
  • Copied example code
  • Adjusted credentials
  • After execution I've got exception json.decoder.JSONDecodeError: Extra data: line 96 column 50 (char 5962) in attendance.py:69 while trying to parse json data:
{
"students":{

//... valid json data until line 96: 

"halves":{"1":"1. Half year","2":"2. Half year"}},[537103],true);});gi7699.dochadzka_c.style.height='';});
</script></div></div></div></body>			
			</html>

It lookls like the json data was incorrectly parsed from the html response. Obviously I'm getting different html response that you, but I don't know why.

@ivanhrabcak
Copy link
Collaborator Author

@kapitan-iglu
Thank you very much for taking your time to write feedback and for the snippet that you sent! To me, it seems that the returned data is actually correct.
Let me explain:
At the end of the returned string - the snippet you sent, there is a very important part:
...Half year"}},[537103],true);...
The second parameter in the string ([537103]), is always your account id number. The whole id (including your account type, as it is returned from edupage) is available via Edupage.get_user_id (Example return value: Student123456). The code just strips the Student or Ucitel prefix from it, and looks for this string (for my example ,[123456],true);) with your account number.

Does edupage.get_user_id().replace("Student", "").replace("Ucitel", "") give you the same number as in the response? If not, what do you get?

Maybe your id (returned by Edupage.get_user_id) contains a hyphen (-). I edited the code to also remove hyphens, can you please retest?

Uninstall the current version:

pip uninstall edupage-api

You can install the version of edupage_api from this branch

pip install -U git+https://github.com/EdupageAPI/edupage-api@feature/attendance

And to revert back:

pip uninstall edupage-api
pip install edupage-api

@kapitan-iglu
Copy link

Maybe your id (returned by Edupage.get_user_id) contains a hyphen (-). I edited the code to also remove hyphens, can you please retest?

Yes, my user ID contains hyphen, so removing it is good idea. But...

Does edupage.get_user_id().replace("Student", "").replace("Ucitel", "") give you the same number as in the response? If not, what do you get?

  1. ...there is neither Student nor Ucitel prefix, but Rodic :-D
  2. ...numeric ID after hyphen is different from the expected one in brackets, it has only four completely different digits :-/

First suggestion is to use ],true);}); as delimiter string (as it is not valid json combination IMO) and then strip trailing text after last ,[.

Example code:

--- attendance.py
+++ attendance_edited.py
@@ -60,21 +60,21 @@
         response = self.edupage.session.get(request_url, params=params)
         response_html = response.text
 
-        user_id_number = Attendance.__get_user_id_number(user_id)
         try:
             data = response_html.split(
                 'ASC.requireAsync("/dashboard/dochadzka.js#initZiak").then'
-            )[1][37:].split(f",[{user_id_number}],true);")[0]
+            )[1][37:].split("],true);});")[0]
+            data = data[0:data.rindex(",[")]
 
             return json.loads(data)
-        except IndexError:
+        except (IndexError, ValueError):
             raise MissingDataException(
                 'Unexpected response from attendance endpoint! (expected string `ASC.requireAsync("/dashboard/dochadzka.js#initZiak").then` to be in the response)'
             )
 
     @staticmethod
     def __get_user_id_number(user_id: str):
-        return user_id.replace("Student", "").replace("Ucitel", "").replace("-", "")
+        return user_id.replace("Student", "").replace("Ucitel", "").replace("Rodic", "").replace("-", "")
 
     def get_days_with_available_attendance(self, user_id: str) -> list[date]:
         user_id_number = Attendance.__get_user_id_number(

With this code I was able to get valid JSON data from __get_attendance_data(). Processing then fails in get_arrivals() as my user ID is not valid key in attendance_data["students"] because it contains only 537103...

So, I suppose that get_user_id() returns my (parent's) user id, but I need to use my child's user id (somehow) when requesting get_arrivals()...

Any ideas?


Second suggestion is to catch also ValueError exception which includes json.decoder.JSONDecodeError (and rindex() too) in case that [{user_id_number}],true); is not found (included in diff).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How to read all ARRIVAL_TO_SCHOOL events?

3 participants