Login

iCal for Google Calendar

Author:
TrevorP
Posted:
January 28, 2012
Language:
Python
Version:
1.2
Tags:
calendar timezone google publish ical subscribe
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. Link Models to Google Calendar by JosefK 7 years, 8 months ago
  2. Generate Google Calendar links from django-event-calendar by ElfSternberg 4 years, 5 months ago
  3. Django Jalali Calendar Widget by behrooznobakht 4 years, 9 months ago
  4. rss news by montao 5 years, 3 months ago
  5. Calendar template-tag by masida 8 years 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.