Calendar¶
← Back to the documentation index
The calendar webservice supports fetching, creating, and removing calendars and events, with support for alarms, and invitees.
Calendars¶
The calendar functionality is based around the CalendarObject dataclass. Every variable has a default value named according to the http payload parameters from the icloud API. The guid is a uuid4 identifier unique to each calendar. The class will create one automatically if it is left blank when the CalendarObject is instanced. the guid parameter should only be set when you know the guid of an existing calendar. The color is an rgb hex value and will be a random color if not set.
Functions¶
get_calendars(as_objs:bool=False) -> list
returns a list of the user's calendars
if as_objs is set to True, the returned list will be of CalendarObjects; else it will be of dictionaries.
add_calendar(calendar:CalendarObject) -> dict
adds a calendar to the users apple calendar and returns the response JSON
remove_calendar(cal_guid:str) -> dict
Removes a Calendar from the apple calendar given the provided guid and returns the response JSON
Examples¶
Create and add a new calendar:
from pyicloud import PyiCloudService
from pyicloud.services.calendar import CalendarObject
api = PyiCloudService("username", "password")
calendar_service = api.calendar
cal = CalendarObject(title="My Calendar", share_type="published")
cal.color = "#FF0000"
calendar_service.add_calendar(cal)
Remove an existing calendar:
cal = calendar_service.get_calendars(as_objs=True)[1]
calendar_service.remove_calendar(cal.guid)
Events¶
The events functionality is based around the EventObject dataclass with support for alarms and invitees. guid is the unique identifier of each event, while pguid is the identifier of the calendar to which this event belongs. pguid is the only required parameter. The EventObject includes automatic validation, dynamic timezone detection, and multiple methods for event management.
Key Features¶
- Automatic Validation: Events validate required fields, date ranges, and calendar GUIDs
- Dynamic Timezone Detection: Automatically detects and uses the user's local timezone
- Alarm Support: Add alarms at event time or before the event with flexible timing
- Invitee Management: Add multiple invitees who will receive email notifications
Functions¶
get_events(from_dt:datetime=None, to_dt:datetime=None, period:str="month", as_objs:bool=False)
Returns a list of events from from_dt to to_dt. If period is provided, it will return the events in that period referencing from_dt if it was provided; else using today's date. IE if period is "month", the events for the entire month that from_dt falls within will be returned.
get_event_detail(pguid, guid, as_obj:bool=False)
Returns a specific event given that event's guid and pguid
add_event(event:EventObject) -> None
Adds an Event to a calendar specified by the event's pguid.
remove_event(event:EventObject) -> None
Removes an Event from a calendar specified by the event's pguid.
EventObject Methods¶
add_invitees(emails: list) -> None
Adds a list of email addresses as invitees to the event. They will receive email notifications when the event is created.
add_alarm_at_time() -> str
Adds an alarm that triggers at the exact time of the event. Returns the alarm GUID for reference.
add_alarm_before(minutes=0, hours=0, days=0, weeks=0) -> str
Adds an alarm that triggers before the event starts. You can specify any combination of time units. Returns the alarm GUID for reference.
Examples¶
Create an event with invitees and alarms:
from datetime import datetime, timedelta
from pyicloud import PyiCloudService
from pyicloud.services.calendar import EventObject
api = PyiCloudService("username", "password")
calendar_service = api.calendar
# Get a calendar to use
calendars = calendar_service.get_calendars(as_objs=True)
calendar_guid = calendars[0].guid
# Create an event with proper validation
event = EventObject(
pguid=calendar_guid,
title="Team Meeting",
start_date=datetime.now() + timedelta(hours=2),
end_date=datetime.now() + timedelta(hours=3),
location="Conference Room A",
all_day=False,
)
# Add invitees (they'll receive email notifications)
event.add_invitees(["colleague1@company.com", "colleague2@company.com"])
# Add alarms
event.add_alarm_before(minutes=15) # 15 minutes before
event.add_alarm_before(days=1) # 1 day before
# Add the event to the calendar
calendar_service.add_event(event)
Create a simple event:
# Basic event creation
event = EventObject(
pguid=calendar_guid,
title="Doctor Appointment",
start_date=datetime(2024, 1, 15, 14, 0),
end_date=datetime(2024, 1, 15, 15, 0),
)
# Add a 30-minute warning alarm
event.add_alarm_before(minutes=30)
calendar_service.add_event(event)
Get events in a specific date range:
from_dt = datetime(2024, 1, 1)
to_dt = datetime(2024, 1, 31)
events = calendar_service.get_events(from_dt, to_dt, as_objs=True)
for event in events:
print(f"Event: {event.title} at {event.start_date}")
Get next week's events:
next_week_events = calendar_service.get_events(
from_dt=datetime.today() + timedelta(days=7), period="week", as_objs=True
)
Remove an event:
calendar_service.remove_event(event)