Parse Events from iCal URL or File (e.g. Google Calendar)


/ Published in: Groovy
Save to your folder(s)

Thanx to Guillaume Laforge, who provided this code snippet of Groovy code in a GSP. The snippet below is more or less the same, less the HTML. It will show all future evens and create a eventMap for each event.


Copy this code and paste it in your HTML
  1. import net.fortuna.ical4j.util.Calendars
  2. import net.fortuna.ical4j.model.component.VEvent
  3. import java.text.SimpleDateFormat
  4.  
  5.  
  6. def url = 'http://www.google.com/calendar/ical/jds6o8imt8qgtepchsmr72jhf4%40group.calendar.google.com/public/basic.ics'.toURL()
  7. def cal = Calendars.load(url)
  8.  
  9. def dayFormatter = new SimpleDateFormat("EEEEEEEE, MMMMMMMM dd'th' yyyy", Locale.US)
  10. def timeFormatter = new SimpleDateFormat('HH:mm', Locale.US)
  11.  
  12. def dates = []
  13. def now = new Date()
  14. log.debug("Total components in iCal file: ${cal.components.size()}" )
  15.  
  16. cal.components.findAll {it.startDate.date.time > now.time }.sort { it.startDate.date }.each
  17. {
  18. if (!it instanceof VEvent)
  19. {
  20. log.warn("Found event of class ${it.getClass()}, skipping!")
  21. }
  22.  
  23. def startDate = dayFormatter.format(it.startDate.date)
  24. def startTime = timeFormatter.format(it.startDate.date)
  25.  
  26. def eventMap = [
  27. startDate : startDate,
  28. startTime : startTime,
  29. location : (it.location.value) ? it.location.value : 'No Location',
  30. summary : it.summary.value,
  31. description : it.description.value
  32. ]
  33.  
  34. dates << eventMap

URL: http://grailspodcast.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.