EdupageAPI / edupage-api

A python library for accessing your Edupage account
https://edupageapi.github.io/edupage-api/
GNU General Public License v3.0
66 stars 13 forks source link

[Bug] Outdated lunches code? #66

Closed michqo closed 1 year ago

michqo commented 1 year ago

Describe the bug I'm getting an error in the get_launch method in Lunches class. More specifically in this line:

lunch_data = json.loads(response.split("edupageData : ")[1].split(",\r\n")[0])

The problem is that it seems like the response from the https://subdomain.edupage.org/menu/?date=230418 url is not just an json object.

Your code

lunches = edupage.get_lunches(datetime.now() + timedelta(days=1))

Error message

File "venv/lib/python3.10/site-packages/edupage_api/lunches.py", line 110, in get_lunch
    lunch_data = json.loads(response.split("edupageData : ")[1].split(",\r\n")[0])
IndexError: list index out of range

Expected behavior lunches variable was supposed to be an instance of the Lunch class.

Version

michqo commented 1 year ago

If you find it useful then here is the code I have written to extract the edupageData object which contains lunch data from html with beautifulsoup.

class Lunches(Module):
    @ModuleHelper.logged_in
    def get_lunch(self, date: datetime):
        date_strftime = date.strftime("%Y%m%d")
        request_url = (
            f"https://{self.edupage.subdomain}.edupage.org/menu/?date={date_strftime}"
        )
        response = self.edupage.session.get(request_url).content.decode()

        soup = BeautifulSoup(response, "html.parser")
        script = soup.find("script", string=re.compile("edupageData"))
        if (
            isinstance(script, NavigableString)
            or script is None
            or script.string is None
        ):
            raise TypeError("edupageData object not found")

        lines = script.string.split("\n")
        edupage_data_str = lines[20].strip()
        edupage_data_str_object = edupage_data_str[13:-1]
        lunch_data = orjson.loads(edupage_data_str_object)
        lunches_data = lunch_data.get(self.edupage.subdomain)

        try:
            boarder_id = lunches_data.get("novyListok").get("addInfo").get("stravnikid")
       # rest is unchanged
BelKed commented 1 year ago

Thanks for letting us know!

I've fixed the issue in https://github.com/ivanhrabcak/edupage-api/commit/1359db50b76cfeb03d393fdfcaa7c489871fb1e8. EduPage just removed a space between edupageData and : 😄


You can install the version of edupage-api with the fix from the master branch by using this command:

pip install git+https://github.com/ivanhrabcak/edupage-api.git

However, don't forget to uninstall the current version of edupage-api first by using:

pip uninstall edupage-api
michqo commented 1 year ago

Wait, so there is no need to use beautiful soup to extract the edupageData object? I thought the edupage /menu endpoint changed, because when I tested making a request to /menu it just returned html. I don't understand how the get_lunch method works then. Anyways thanks for fixing it, everything works now.