Python Caldav, all calendars have None name

1.3k views Asked by At

I'm trying to access to all events of my calendar, hosted on Nextcloud, with python and the caldav library. With this code:

client = caldav.DAVClient(url) #like "https://..../nextcloud/remote.php/dav/calendars
principal = client.principal()
calendars = principal.calendars()

I can access to all my calendars and iterate over it.

How I can read only a specific calendar, with the name "calendar_name"? In this case I get all calendars, even if I specify the calendar name:

client = caldav.DAVClient(url) #like "https://..../nextcloud/remote.php/dav/calendars/user/calendar_name
principal = client.principal()
calendars = principal.calendars()

If I change the last line of code with calendar_name, I get an empty array.

calendar = principal.calendar('calendar_name')

Note: I can access all calendars and events with the first code posted, but all names are "None", even if the Url is right.

1

There are 1 answers

0
hnh On

The second snippet still gives you all calendars because you first grab the account (.principal()) and then you list all calendars the account has (principal.calendars()).

The third snippet probably doesn't work because the name (the display name property, not the URL path component) of the calendar quite likely isn't calendar_name but something like Calendar. Theoretically it may even be empty.

To access a single calendar using its URL this may work, didn't try:

client   = caldav.DAVClient(url)
calendar = caldav.Calendar(client=client, 
             url="/nextcloud/remote.php/dav/calendars/user/calendar_name")

Though it may be better to do something like this for various reasons:

client    = caldav.DAVClient(url)
principal = client.principal()
calendars = principal.calendars()
calendar  = any(c for c in calendars if c.url == your url)

To address your actual question, you need to add more information. If you want the (relative or absolute) URL of the calendar, use something like this:

print calendar.url

If you want to explicitly retrieve the calendar display name, this may work:

print calendar.get_properties([dav.DisplayName()])

Hope this helps.