Created
July 27, 2010 00:04
-
-
Save dentarg/491488 to your computer and use it in GitHub Desktop.
Spelschema för Emmabodafestivalen
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 "bundler/setup" | |
require 'tzinfo' | |
require 'ri_cal' | |
DEBUG = false | |
YEAR = 2011 | |
class Concert < | |
Struct.new(:artist, :start_hour, :start_minute, :stop_hour, :stop_minute, | |
:start_day, :stop_day, :start_month, :stop_month, :location) | |
def set_time(timestr) | |
# "13:15 \tBye Bye Bicycle\n" | |
self.start_hour = timestr[0..1].to_i | |
self.start_minute = timestr[3..4].to_i | |
self.stop_hour = self.start_hour + 1 | |
self.stop_minute = self.start_minute | |
self.start_month = 7 | |
self.stop_month = 7 | |
if self.stop_hour == 24 | |
self.stop_hour = 0 | |
end | |
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 | |
if self.start_day == 32 | |
self.start_day = 1 | |
self.start_month = self.start_month + 1 | |
end | |
if self.stop_day == 32 | |
self.stop_day = 1 | |
self.stop_month = self.stop_month + 1 | |
end | |
end | |
def start | |
if start_minute == 0 | |
"#{start_hour}:#{start_minute}0" | |
else | |
"#{start_hour}:#{start_minute}" | |
end | |
end | |
def stop | |
"#{stop_hour}:#{stop_minute}" | |
end | |
def day | |
"#{start_day}/#{start_month}" | |
end | |
def set_artist(str) | |
# 2010: "13:15 \tBye Bye Bicycle\n" | |
# 2011: "13:15\tBye Bye Bicycle\n" | |
case YEAR | |
when 2010 | |
self.artist = str.match(/\d\d:\d\d\s\t(.+)/).to_a[1] | |
when 2011 | |
self.artist = str.match(/\d\d:\d\d\t(.+)/).to_a[1] | |
else | |
"This year is not implemented yet." | |
end | |
end | |
def set_day(day) | |
case YEAR | |
when 2010 | |
days = { "Onsdag" => 28, "Torsdag" => 29, "Fredag" => 30, "Lördag" => 31} | |
when 2011 | |
days = { "Onsdag" => 27, "Torsdag" => 28, "Fredag" => 29, "Lördag" => 30} | |
else | |
"This year is not implemented yet." | |
end | |
self.start_day = days["#{day}"] | |
self.stop_day = days["#{day}"] | |
end | |
def to_s | |
"#{self.day} #{self.artist} (#{self.start}-#{self.stop}) [#{self.location}]" | |
end | |
def to_ical(event) | |
event.summary = self.artist | |
event.location = self.location | |
event.dtstart = Time.local(YEAR, self.start_month, self.start_day, self.start_hour, self.start_minute).getutc | |
event.dtend = Time.local(YEAR, self.stop_month, self.stop_day, self.stop_hour, self.stop_minute).getutc | |
return event | |
end | |
end | |
# Helpers | |
def match_day(name) | |
name.match(/^\w/) | |
end | |
def match_location(str) | |
str.match(/.+/) and !str.match(/^\d/) | |
end | |
def match_time_and_artist(str) | |
case YEAR | |
when 2010 | |
str.match(/^\d\d:\d\d\s\t.+/) | |
when 2011 | |
str.match(/^\d\d:\d\d\t.+/) | |
else | |
"This year is not implemented yet." | |
end | |
end | |
# raw data looks like this | |
# | |
# \n | |
# \n | |
# day\n | |
# location\n | |
# hh:mm \t artist\n | |
# location\n | |
# hh:mm \t artist\n | |
# \n | |
# \n | |
# | |
# and so on... | |
# Main | |
def make_concerts(day) | |
lines = File.open(day, 'r').readlines | |
concerts = [] | |
day = "" | |
location = "" | |
lines.each_with_index do |line,i| | |
#p line | |
# Special treatment for the last one | |
if i > lines.length-2 | |
concert = Concert.new | |
concert.set_day(day) | |
concert.set_artist(lines[i]) | |
concert.set_time(lines[i]) | |
concert.location = location | |
concerts << concert | |
# Find occurance of "\n", "\n", "<day>\n <location>\n" | |
elsif lines[i] == "\n" and lines[i+1] == "\n" and match_day(lines[i+2]) and match_location(lines[i+3]) | |
day = lines[i+2].strip | |
location = lines[i+3].strip | |
p "FOUND DAY AND LOCATION: #{day} | #{location}" if DEBUG | |
# 2010: "<time> \t<artist>\n", <location>\n, "<time> \t<artist>\n" | |
elsif match_time_and_artist(lines[i]) and match_location(lines[i+1]) and match_time_and_artist(lines[i+2]) | |
location = lines[i+1].strip | |
p "FOUND NEW LOCATION: #{location}" if DEBUG | |
# 2011: "<time>\t<artist>\n", <location>\n, "<time>\t<artist>\n" | |
elsif match_time_and_artist(lines[i]) and match_location(lines[i+1]) and match_time_and_artist(lines[i+2]) | |
location = lines[i+1].strip | |
p "FOUND NEW LOCATION: #{location}" if DEBUG | |
elsif match_time_and_artist(lines[i]) | |
concert = Concert.new | |
concert.set_day(day) | |
concert.set_artist(lines[i]) | |
concert.set_time(lines[i]) | |
concert.location = location | |
concerts << concert | |
end | |
end | |
return concerts | |
end | |
def make_calendar(data) | |
concerts = make_concerts(data) | |
#cal.custom_property("X-WR-CALNAME;VALUE=TEXT", "Emmabodafestivalen") | |
#cal.custom_property("X-WR-CALDESC;VALUE=TEXT", "Spelschema") | |
cal = RiCal.Calendar do |cal| | |
for concert in concerts | |
cal.event do |event| | |
event = concert.to_ical(event) | |
end | |
puts concert if DEBUG | |
end | |
end | |
puts cal.export if !DEBUG | |
end | |
make_calendar(ARGV[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment