mandarons / icloudpy

iCloud web service interface library in Python
Other
179 stars 17 forks source link

Fix calendar sync failing on months having 1st day as Monday #39

Closed ReekenX closed 5 months ago

ReekenX commented 5 months ago

Somebody created bug in the past https://github.com/mandarons/icloudpy/issues/26. It missed steps to reproduce or a fix.

This PR describes how to reproduce and has code fix.

The issue is that calendar sync will fail on months that have Monday as 1st day of the month (e.g. 2024 April 1 is Monday).

Buggy code is:

from calendar import monthrange
first_day, last_day = monthrange(today.year, today.month)

According to the docs:

The monthrange() method is used to get weekday of first day of the month and number of days in month

So in this code variable first_day can return 0 (for Monday) and this will crash datetime().

Before the fix

>>> from icloudpy import ICloudPyService
>>>
>>> api = ICloudPyService('username, 'password')
>>> api.validate_2fa_code(123456)
True
>>> api.calendar.events()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "~/icloudpy/icloudpy/services/calendar.py", line 71, in events
    self.refresh_client(from_dt, to_dt)
  File "~/icloudpy/icloudpy/services/calendar.py", line 51, in refresh_client
    from_dt = datetime(today.year, today.month, first_day)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: day is out of range for month

After the fix

>>> from icloudpy import ICloudPyService
>>> api = ICloudPyService('username', 'password')
>>> api.calendar.events()
[{'tz': 'Floating', 'icon': 0, 'recurrenceException': False, 'title': 'Test', 'duration': 0, 'allDay': False, [..]]
mandarons commented 5 months ago

@ReekenX Thank you for the PR. Appreciate it!

ReekenX commented 4 months ago

Follow up question to @mandarons

Could you bump version to icloudpy 0.5.1 and release to PyPi? Otherwise pip install icloudpy would still give this calendar sync issue.

https://pypi.org/project/icloudpy/