cal.add('prodid', '-//VK Birthdays//vk.com//') cal.add('version', '2.0') for entry in api.friends.get(user_id=USER_ID, fields='bdate'): if not 'bdate' in entry: continue event = ic.Event() event['uid'] = str(entry['uid']) + '@vk.com' event.add('summary', 'Birthday of ' + entry['first_name'] + ' ' + entry['last_name']) bvalues = entry['bdate'].split('.') day = int(bvalues[0]) month = int(bvalues[1]) year = int(date.today().year) bdate = datetime(year, month, day, tzinfo=pytz.utc) event.add('dtstart', bdate) event.add('dtend', bdate) event.add('dtstamp', bdate) event['rrule'] = ic.vText('FREQ=YEARLY') event['sequence'] = ic.vInt(0) event['transp'] = ic.vText('TRANSPARENT') cal.add_component(event) f = open('bdate.ics', 'wb') f.write(cal.to_ical()) f.close()
def create_ical_entry (e, request): event = icalendar.Event() # TODO should we generate an UUID when creating the event? uid = u'*****@*****.**' % (str(e.id)) event['uid'] = icalendar.vText(uid) event['dtstamp'] = icalendar.vDatetime(datetime.utcnow()) # The sequence field must be incremented each time the event is modifed. # The trick here is to subtract the create TS from the modify TS and # use the difference as sequence. sequence = 0 if e.date_time_created and e.date_time_modified: createTimestamp = time.mktime(e.get_date_time_created_utc().timetuple()) modifyTimestamp = time.mktime(e.get_date_time_modified_utc().timetuple()) sequence = modifyTimestamp - createTimestamp event['sequence'] = icalendar.vInt(sequence) + 1 # created and last-modified if e.date_time_created: event['created'] = icalendar.vDatetime(e.get_date_time_created_utc()) if e.date_time_modified: event['last-modified'] = icalendar.vDatetime(e.get_date_time_modified_utc()) # TENTATIVE, CONFIRMED, CANCELLED if e.canceled: event['status'] = icalendar.vText(u'CANCELLED') else: event['status'] = icalendar.vText(u'CONFIRMED') relative_url = e.get_absolute_url() absolute_url = request.build_absolute_uri(relative_url) event['url'] = icalendar.vUri(absolute_url) if e.title: event['summary'] = icalendar.vText(e.title) description = u'' if e.description: description += e.description if e.url: if len(description) > 0: description += u'\n\n' description += u'Event Webseite: ' + e.url if len(description) > 0: description += u'\n\n' description += u'Event bei Techism: ' + absolute_url event['description'] = icalendar.vText(description) if e.date_time_begin: event['dtstart'] = icalendar.vDatetime(e.get_date_time_begin_utc()) if e.date_time_end: event['dtend'] = icalendar.vDatetime(e.get_date_time_end_utc()) # geo value isn't used by iCal readers :-( # maybe a trick is to add the geo coordinates to the location field using the following format: # $latitude, $longitude ($name, $street, $city) if e.location: location = u'%s, %s, %s' % (e.location.name, e.location.street, e.location.city) event['location'] = icalendar.vText(location) if e.location and e.location.latitude and e.location.longitude: event['geo'] = icalendar.vGeo((e.location.latitude, e.location.longitude)) return event
def ical(request): ninety_days = datetime.utcnow() + timedelta(days=90) event_list = service.get_event_query_set().filter(date_time_begin__lte=ninety_days).order_by('date_time_begin') cal = icalendar.Calendar() cal['prodid'] = icalendar.vText(u'-//Techism//Techism//DE') cal['version'] = icalendar.vText(u'2.0') cal['x-wr-calname'] = icalendar.vText(u'Techism') cal['x-wr-caldesc'] = icalendar.vText(u'Techism - IT-Events in München') for e in event_list: event = icalendar.Event() # TODO should we generate an UUID when creating the event? uid = u'*****@*****.**' % (str(e.id)) event['uid'] = icalendar.vText(uid) event['dtstamp'] = icalendar.vDatetime(datetime.utcnow()) # The sequence field must be incremented each time the event is modifed. # The trick here is to subtract the create TS from the modify TS and # use the difference as sequence. sequence = 0 if e.date_time_created and e.date_time_modified: createTimestamp = time.mktime(e.get_date_time_created_utc().timetuple()) modifyTimestamp = time.mktime(e.get_date_time_modified_utc().timetuple()) sequence = modifyTimestamp - createTimestamp event['sequence'] = icalendar.vInt(sequence) # created and last-modified if e.date_time_created: event['created'] = icalendar.vDatetime(e.get_date_time_created_utc()) if e.date_time_modified: event['last-modified'] = icalendar.vDatetime(e.get_date_time_modified_utc()) # TENTATIVE, CONFIRMED, CANCELLED event['status'] = icalendar.vText(u'CONFIRMED') if e.title: event['summary'] = icalendar.vText(e.title) if e.description: event['description'] = icalendar.vText(e.description) if e.date_time_begin: event['dtstart'] = icalendar.vDatetime(e.get_date_time_begin_utc()) if e.date_time_end: event['dtend'] = icalendar.vDatetime(e.get_date_time_end_utc()) if e.url: relative_url = reverse('event-show', args=[e.id]) absolute_url = request.build_absolute_uri(relative_url) event['url'] = icalendar.vUri(absolute_url) # geo value isn't used by iCal readers :-( # maybe a trick is to add the geo coordinates to the location field using the following format: # $latitude, $longitude ($name, $street, $city) if e.location: location = u'%s, %s, %s' % (e.location.name, e.location.street, e.location.city) event['location'] = icalendar.vText(location) if e.location and e.location.latitude and e.location.longitude: event['geo'] = icalendar.vGeo((e.location.latitude, e.location.longitude)) cal.add_component(event) response = HttpResponse(cal.as_string()) response['Content-Type'] = 'text/calendar; charset=UTF-8' response['Cache-Control'] = 'no-cache, no-store, max-age=0, must-revalidate' response['Pragma'] = 'no-cache' response['Expires'] = 'Fri, 01 Jan 1990 00:00:00 GMT' return response