Python library to access a CalDAV server

…衆ロ難τιáo~ 提交于 2019-12-11 04:03:17

问题


I run ownCloud on my webspace for a shared calendar. Now I'm looking for a suitable python library to get read only access to the calendar. I want to put some information of the calendar on an intranet website.

I have tried http://trac.calendarserver.org/wiki/CalDAVClientLibrary but it always returns a NotImplementedError with the query command, so my guess is that the query command doesn't work well with the given library.

What library could I use instead?


回答1:


I recommend the library, caldav.

Read-only is working really well with this library and looks straight-forward to me. It will do the whole job of getting calendars and reading events, returning them in the iCalendar format. More information about the caldav library can also be obtained in the documentation.

import caldav

client = caldav.DAVClient(<caldav-url>, username=<username>,
                          password=<password>)
principal = client.principal()
for calendar in principal.calendars():
    for event in calendar.events():
        ical_text = event.data

From this on you can use the icalendar library to read specific fields such as the type (e. g. event, todo, alarm), name, times, etc. - a good starting point may be this question.




回答2:


pyOwnCloud could be the right thing for you. I haven't tried it, but it should provide a CMDline/API for reading the calendars.




回答3:


You probably want to provide more details about how you are actually making use of the API but in case the query command is indeed not implemented, there is a list of other Python libraries at the CalConnect website (archvied version, original link is dead now).




回答4:


I wrote this code few months ago to fetch data from CalDAV to present them on my website. I have changed the data into JSON format, but you can do whatever you want with the data.

I have added some print for you to see the output which you can remove them in production.

    from datetime import datetime
import json
from pytz import UTC # timezone
import caldav
from icalendar import Calendar, Event

# CalDAV info
url = "YOUR CALDAV URL"
userN = "YOUR CALDAV USERNAME"
passW = "YOUR CALDAV PASSWORD"

client = caldav.DAVClient(url=url, username=userN, password=passW)
principal = client.principal()
calendars = principal.calendars()

if len(calendars) > 0:
    calendar = calendars[0]
    print ("Using calendar", calendar)
    results = calendar.events()
    eventSummary = []
    eventDescription = []
    eventDateStart = []
    eventdateEnd = []
    eventTimeStart = []
    eventTimeEnd = []

    for eventraw in results:

        event = Calendar.from_ical(eventraw._data)
        for component in event.walk():
            if component.name == "VEVENT":
                print (component.get('summary'))
                eventSummary.append(component.get('summary'))
                print (component.get('description'))
                eventDescription.append(component.get('description'))
                startDate = component.get('dtstart')
                print (startDate.dt.strftime('%m/%d/%Y %H:%M'))
                eventDateStart.append(startDate.dt.strftime('%m/%d/%Y'))
                eventTimeStart.append(startDate.dt.strftime('%H:%M'))
                endDate = component.get('dtend')
                print (endDate.dt.strftime('%m/%d/%Y %H:%M'))
                eventdateEnd.append(endDate.dt.strftime('%m/%d/%Y'))
                eventTimeEnd.append(endDate.dt.strftime('%H:%M'))
                dateStamp = component.get('dtstamp')
                print (dateStamp.dt.strftime('%m/%d/%Y %H:%M'))
                print ('')

    # Modify or change these values based on your CalDAV
    # Converting to JSON
    data = [{ 'Events Summary':eventSummary[0], 'Event Description':eventDescription[0],'Event Start date':eventDateStart[0], 'Event End date':eventdateEnd[0], 'At:':eventTimeStart[0], 'Until':eventTimeEnd[0]}]
    data_string = json.dumps(data)
    print ('JSON:', data_string)


来源:https://stackoverflow.com/questions/17634052/python-library-to-access-a-caldav-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!