Created
October 21, 2011 03:56
-
-
Save agilous/1303061 to your computer and use it in GitHub Desktop.
Ruby script to convert Cincinnati Amateur Hockey Association CSV schedule to iCalendar format.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'csv' | |
require 'date' | |
require 'icalendar' | |
if ARGV.last.nil? | |
puts "You must pass the path to the CSV file as an argument to this script." | |
exit | |
end | |
unless File.readable?(ARGV.last) | |
puts "Unable to read file: #{ARGV.last}" | |
exit | |
end | |
# Returns the number of minutes or nil if no minutes were given. | |
def alarm_offset | |
/\A(\d+)\z/ =~ ARGV[(ARGV.index("-a") + 1)] | |
Regexp.last_match(1) | |
end | |
# Given a date and time string return the civil date. | |
def parse_date_and_time(date, time) | |
month, day, year = date.split('/').map{ |_| _.to_i } | |
hour, minute, _ = time.split(':').map{ |_| _.to_i } | |
_, meridiem = time.split(' ') | |
hour = (meridiem == "PM" ? hour + 12 : hour) | |
DateTime.civil(year, month, day, hour, minute) | |
end | |
# Returns true if the "set alarm" switch is present, false otherwise. | |
def set_alarm? | |
ARGV.include?("-a") | |
end | |
cal = Icalendar::Calendar.new | |
# CMT3-GANZ,10/19/2011,6:10:00 PM,10/19/2011,7:00:00 PM,FALSE,FALSE,10/19/2011,5:10:00 PM,,,,,,CMT3-GANZ,Practice - ADM Session,Northland,,Normal,FALSE,Normal,4 | |
CSV.foreach(ARGV.last, quote_char: '"', headers: true, col_sep: ',', row_sep: :auto) do |row| | |
cal.event do | |
dtstart parse_date_and_time(row[1], row[2]) | |
dtend parse_date_and_time(row[3], row[4]) | |
summary "#{row[15]} (#{row[16]})" | |
if set_alarm? | |
alarm do | |
action "AUDIO" | |
trigger (alarm_offset ? "-PT#{alarm_offset}M" : "-PT90M") | |
add_attach "Basso", {"VALUE" => ["URI"]} | |
end | |
end | |
end | |
end | |
puts cal.to_ical |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment