Skip to content

Instantly share code, notes, and snippets.

@dentarg
Created July 15, 2010 02:05
Show Gist options
  • Save dentarg/476391 to your computer and use it in GitHub Desktop.
Save dentarg/476391 to your computer and use it in GitHub Desktop.
Spelschema för Arvikafestivalen
#!/usr/bin/env ruby
require 'rubygems'
require 'icalendar'
require 'date'
include Icalendar
# raw data looks like this
#
# \n
# location\n
# \n
# start - stop\n
# artist\n
# \n
# start - stop\n
#
# and so on...
class Concert <
Struct.new(:artist, :start_hour, :start_minute, :stop_hour, :stop_minute,
:start_day, :stop_day, :location)
def set_time(timestr)
# 01:00 - 02:00\n
self.start_hour = timestr[0..1].to_i
self.start_minute = timestr[3..4].to_i
self.stop_hour = timestr[8..9].to_i
self.stop_minute = timestr[11..12].to_i
if self.start_hour >= 0 && self.start_hour < 12
self.start_day = self.start_day + 1
end
if self.stop_hour >= 0 && self.stop_hour < 12
self.stop_day = self.stop_day + 1
end
end
def set_day(day)
days = { "torsdag" => 15, "fredag" => 16, "lordag" => 17}
self.start_day = days["#{day}"]
self.stop_day = days["#{day}"]
end
def to_s
"#{self.artist} (#{self.start}-#{self.stop}) [#{self.location}] #{self.day}"
end
def to_ical
event = Event.new
event.summary = self.artist
event.location = self.location
event.start = DateTime.civil(2010, 07, self.start_day, self.start_hour, self.start_minute)
event.end = DateTime.civil(2010, 07, self.stop_day, self.stop_hour, self.stop_minute)
return event
end
end
# Helpers
def match_artist(name)
name.match(/^[A-Za-z]/)
end
def match_time(timestr)
timestr.match(/^\d/)
end
# Main
def concerts(day)
lines = File.open("#{day}.txt", 'r').readlines
concerts = []
location = ""
# Find occurance of "\n", "<location>\n", "\n"
lines.each_with_index do |line,i|
if lines[i] == "\n" and lines[i+2] == "\n"
location = lines[i+1].strip
elsif match_time(lines[i]) && match_artist(lines[i+1])
concert = Concert.new
concert.set_day(day)
concert.set_time(lines[i])
concert.artist = lines[i+1].strip
concert.location = location
concerts << concert
end
end
return concerts
end
def make_calendar
tor = concerts("torsdag")
fre = concerts("fredag")
lor = concerts("lordag")
all = tor + fre + lor
cal = Calendar.new
cal.custom_property("X-WR-CALNAME;VALUE=TEXT", "Arvikafestivalen")
cal.custom_property("X-WR-CALDESC;VALUE=TEXT", "Spelschema")
for concert in all
cal.add_event(concert.to_ical)
end
puts cal.to_ical
end
make_calendar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment