Login

iCal for Google Calendar

Author:
TrevorP
Posted:
January 28, 2012
Language:
Python
Version:
1.2
Score:
0 (after 0 ratings)

This code publishes an iCal file that can be subscribed to in Google Calendar. They change the way they interpret iCal data occasionally, so this may break, I'll try to keep it up to date.

There is some crazy string replace stuff going on there, I haven't yet convinced vObject to format things properly.

Feedback welcome.

*Note: this works for my existing feeds, but if I add a new feed to GCal, the timezones are incorrect, I'm working on that.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    cal = vobject.iCalendar()
    cal.add('prodid').value='-//'+CALENDAR_NAME+'//PYVOBJ_'+CALENDAR_SHORT_NAME+'-Cal//EN'
    cal.add('VTIMEZONE').tzinfo=dateutil.tz.tzlocal()
    #cal.add('CALSCALE').value='GREGORIAN'
    #cal.add('METHOD').value='PUBLISH'
    #cal.add('X-WR-CALNAME').value=request.build_absolute_uri()
    #cal.add('X-WR-TIMEZONE').value='Australia/Sydney'
    for event in events:
        ev = cal.add('vevent')
        ev.add('uid').value = str(event.id)+'@'+CALENDAR_SHORT_NAME
        dts=ev.add('dtstart')
        dts.value = <datetime>
        dts.params['tzid']='E'
        dte=ev.add('dtend')
        dte.value = <datetime>
        dte.params['tzid']='E'
        ev.add('summary').value = <str>
        ev.add('description').value = <str>
        ev.add('location').value = <str>
        ev.add('status').value = <str:CONFIRMED|TENTATIVE|CANCELLED>
        alarm=ev.add('valarm')
        alarm.add('value').value='DURATION'
        alarm.add('trigger').value=datetime.timedelta(minutes=-30)
    text=cal.serialize()
    text=text.replace('tzid=E', 'TZID=EST')
    if False: text=text.replace('BEGIN:VTIMEZONE', """CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-CALNAME:%s
X-WR-TIMEZONE:Australia/Sydney
BEGIN:VTIMEZONE""" % (request.build_absolute_uri()))
    return HttpResponse(text, mimetype='text/calendar')

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

ayakovlev (on April 6, 2012):

Or use django-cal for iCalendar files generation a la django.contrib.syndication.

#

Please login first before commenting.