A Sourceful of Secrets

Andrew E. Bruno

Creating iCalendar Files in Ruby

with one comment

I recently came across a great ruby module called icalendar for working with iCalendar files. For a quick example here’s some code that generates the Buffalo Sabres 06/07 schedule in iCalendar format. The source data comes from the Excel spreadsheet available here which gets converted to CSV before running through the script.

require 'rubygems'
require 'icalendar'
require 'csv'

if ARGV.size != 1
    puts "usage: ruby sabres_ical [schedule.csv]"
    exit;
end

cal = Icalendar::Calendar.new

CSV.open(ARGV[0], 'r') do |row|
    event = cal.event
    event.timestamp = DateTime.now
    event.summary = row[0]
    event.description = row[0]
    event.location = row[16] 

    start_date = Date.parse(row[1])
    end_date = Date.parse(row[3])
    game_time = row[2]

    event.start = DateTime.parse(start_date.to_s + " " + game_time)

    # Games usually last about 3 hours
    (h,m,s) = game_time.split(":")
    endTime = [h.to_i + 3,m,s].join(":")
    event.end = DateTime.parse(end_date.to_s + " " + endTime)
end

puts cal.to_ical

Here’s an example of the output which is sent to stdout:

BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
PRODID:iCalendar-Ruby
BEGIN:VEVENT
DTEND:20061006T223000
UID:2007-01-16T17:59:38-0800_408345411@sugaree
DESCRIPTION:Sabres vs. Montreal
SUMMARY:Sabres vs. Montreal
DTSTART:20061006T193000
DTSTAMP:20070116T175938
SEQ:0
LOCATION:HOME
END:VEVENT
BEGIN:VEVENT
DTEND:20061014T220000
UID:2007-01-16T17:59:38-0800_606587692@sugaree
DESCRIPTION:Sabres vs. NY Rangers
SUMMARY:Sabres vs. NY Rangers
DTSTART:20061014T190000
DTSTAMP:20070116T175938
SEQ:0
LOCATION:HOME
END:VEVENT

If you’re a Sabres fan and not able to run ruby scripts you can download the iCal version of the Sabres 06/07 schedule here for importing into your favorite calendaring program. For some reason there are some games missing in the source Excel spreadsheet so unfortunately they will also be missing in the iCalendar version. I don’t make any guarantees to it’s correctness so if you find an error or miss a game you’ve been warned.

About these ads

Written by Andrew

2007/02/06 at 22:24

Posted in Ruby

One Response

Subscribe to comments with RSS.

  1. Thank you for sharing!

    Wahoo

    2007/10/05 at 23:36


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: