20110817ΒΆ

Today I continued with the CalDAV functionality (docs/tickets/47). As a first step I got the introduction script from python-caldav running:

 1import sys
 2from datetime import datetime, timedelta
 3import caldav
 4from caldav.elements import dav, cdav
 5
 6# Principal url
 7#~ url = "https://user:pass@hostname/user/Calendar"
 8username = 'luc.saffre@gmail.com'
 9pwd = raw_input("Please enter password for %s: " % username)
10
11loc = '/calendar/dav/%s/' % username
12url = "https://%s:%s@www.google.com%s" % (username,pwd,loc)
13
14vcal = """BEGIN:VCALENDAR
15VERSION:2.0
16PRODID:-//Example Corp.//CalDAV Client//EN
17BEGIN:VEVENT
18UID:1234567890
19DTSTAMP:20100510T182145Z
20DTSTART:20100512T170000Z
21DTEND:20100512T180000Z
22SUMMARY:This is an event
23END:VEVENT
24END:VCALENDAR
25"""
26
27# the above raw data can be done using vobject:
28
29import vobject
30mycal = vobject.iCalendar()
31mycal.add('vevent')
32mycal.vevent.add('uid').value = "1234567890"
33mycal.vevent.add('dtstamp').value = datetime(2010,5,10,18,21,45)
34#~ mycal.vevent.add('dtstamp').value = datetime.now()
35mycal.vevent.add('dtstart').value = datetime(2010,5,12,17,0,0)
36mycal.vevent.add('dtend').value = datetime(2010,5,12,18,0,0)
37mycal.vevent.add('summary').value = "This is still my event"
38vcal = mycal.serialize()
39
40
41client = caldav.DAVClient(url)
42principal = caldav.Principal(client, url)
43#~ print "url.username:", principal.url.username
44#~ print "url.hostname:", principal.url.hostname
45
46calendars = principal.calendars()
47if len(calendars) == 0:
48    print "Sorry, no calendar"
49    print principal.children()
50    sys.exit()
51    
52if len(calendars) > 1:
53    print "WARNING: more than 1 calendar"
54    
55calendar = calendars[0]
56print "Using calendar", calendar
57
58print "Renaming (works, but is deactivated)"
59if False:
60    calendar.set_properties([dav.DisplayName("Test calendar"),])
61print calendar.get_properties([dav.DisplayName(),])
62
63#~ event = caldav.Event(client, data = vcal, parent = calendar).save()
64#~ print "Event", event, "created"
65
66d = datetime(2010, 5, 12)
67print "Looking for events on ", d
68results = calendar.date_search(d,d+timedelta(days=1))
69for event in results:
70    print "Found", event.instance.prettyPrint()
71
72d = datetime(2011, 8, 20)
73print "Looking for events after", d
74results = calendar.date_search(d,d+timedelta(days=4))
75for event in results:
76    print "Found", event.instance.prettyPrint()
77