python里cal_用于访问CalDAV服务器的Python库

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?

解决方案

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(, username=,

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
钉钉的CalDAV服务器是一个标准的CalDAV服务器,因此你可以使用任何支持CalDAV协议的第三方库来访问它。下面是一个使用Android系统自带的SyncAdapter框架实现的CalDAV同步Demo。 1. 添加依赖 在build.gradle文件中添加以下依赖: ``` dependencies { implementation "com.github.aflx:sardine-android:5.7.0" } ``` 这使用了Sardine-Android库,它是一个支持WebDAV和CalDAV协议的Android库,可以方便地与钉钉的CalDAV服务器进行交互。 2. 创建SyncAdapter 创建一个继承自AbstractThreadedSyncAdapter的SyncAdapter类,并实现其中的onPerformSync()方法,用于执行CalDAV同步任务。 ```java public class CalDAVSyncAdapter extends AbstractThreadedSyncAdapter { private static final String TAG = "CalDAVSyncAdapter"; private final Sardine mSardine; public CalDAVSyncAdapter(Context context, boolean autoInitialize) { super(context, autoInitialize); mSardine = new OkHttpSardine(); } @Override public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) { try { // TODO: 执行CalDAV同步任务 } catch (Exception e) { Log.e(TAG, "CalDAV sync failed", e); syncResult.stats.numIoExceptions++; } } } ``` 3. 注册SyncAdapter 在AndroidManifest.xml文件中注册SyncAdapter,并指定对应的账户类型和CalDAV服务器地址。 ```xml <application> <provider android:name="android.content.ContentProvider" android:authorities="com.android.calendar" android:exported="false" android:syncable="true" /> <service android:name=".CalDAVSyncAdapterService" android:exported="true"> <intent-filter> <action android:name="android.content.SyncAdapter" /> </intent-filter> <meta-data android:name="android.content.SyncAdapter" android:resource="@xml/caldav_sync_adapter" /> <meta-data android:name="android.provider.CONTACTS_STRUCTURE" android:resource="@xml/contacts" /> </service> </application> <uses-permission android:name="android.permission.READ_CALENDAR" /> <uses-permission android:name="android.permission.WRITE_CALENDAR" /> <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" /> <account-authenticator xmlns:android="http://schemas.android.com/apk/res/android" android:accountType="com.android.exchange" android:icon="@drawable/icon_exchange" android:smallIcon="@drawable/icon_exchange" android:label="@string/app_name" /> <sync-adapter xmlns:android="http://schemas.android.com/apk/res/android" android:contentAuthority="com.android.calendar" android:accountType="com.android.exchange" android:userVisible="false" android:supportsUploading="true" android:allowParallelSyncs="false" android:isAlwaysSyncable="true" /> ``` 在res/xml目录下创建caldav_sync_adapter.xml文件,指定SyncAdapter的参数。 ```xml <sync-adapter xmlns:android="http://schemas.android.com/apk/res/android" android:contentAuthority="com.android.calendar" android:accountType="com.android.exchange" android:userVisible="false" android:supportsUploading="true" android:allowParallelSyncs="false" android:isAlwaysSyncable="true" /> ``` 4. 执行CalDAV同步任务 在SyncAdapter的onPerformSync()方法中,使用Sardine库实现CalDAV同步任务。以下是一个简单的例子,可以获取钉钉CalDAV服务器上的所有日历事件。 ```java @Override public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) { try { // 创建Sardine对象 mSardine.setCredentials("username", "password"); // 获取所有日历事件 List<DavResource> resources = mSardine.getResources("https://calendar.dingtalk.com/caldav/username/events/"); // 解析日历事件 for (DavResource resource : resources) { CalendarBuilder builder = new CalendarBuilder(); Calendar calendar = builder.build(resource.getInputStream()); Log.d(TAG, "Event: " + calendar.toString()); } } catch (Exception e) { Log.e(TAG, "CalDAV sync failed", e); syncResult.stats.numIoExceptions++; } } ``` 注意:在使用Sardine库访问CalDAV服务器时,需要使用完整的CalDAV资源地址,例如"https://calendar.dingtalk.com/caldav/username/events/",其中username为钉钉账号的用户名。另外,钉钉的CalDAV服务器使用的是HTTPS协议,需要添加相应的证书验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值