Skip to content

Instantly share code, notes, and snippets.

@badboy
Created December 28, 2008 21:28
Show Gist options
  • Save badboy/41055 to your computer and use it in GitHub Desktop.
Save badboy/41055 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'hpricot'
require 'open-uri'
require 'cgi'
require 'pp'
require 'ostruct'
require 'optparse'
SCHEDULE_URI = 'http://events.ccc.de/congress/2008/Fahrplan/schedule.en.xml'
class Event
attr_accessor :start, :duration, :title, :room
def initialize
if block_given?
yield self
end
end
def to_s
"(#{@room}) #{@start}: #{title} [Duration: #{@duration}h]"
end
end
class Day
attr_accessor :index, :date, :events
def initialize(index, date, events)
@index = index
@date = date
@events = events
end
def inspect
"#<Day> @date=#{@date} @events=(#{@events.size})"
end
end
def getdays
doc = Hpricot.XML(open(SCHEDULE_URI))
days = (doc/"day").map do |day|
events = (day/"//event").map do |event|
Event.new do |ev|
ev.start = (event/'start').inner_html
ev.duration = (event/'duration').inner_html
ev.title = CGI.unescapeHTML((event/'title').inner_html)
ev.room = (event/'room').inner_html
end
end
Day.new(day.attributes['index'], day.attributes['date'], events)
end
end
$options = OpenStruct.new
ARGV.options do |o|
o.banner = 'usage: schedule.rb [option]'
o.separator('')
o.on('-t', '--time TIME', 'find by time (regular expression)') do |t|
$options.time = t
end
o.on('-d', '--day DAY', 'only day (1..4)') do |d|
if (1..4).to_a.include? d.to_i
$options.day = d
end
end
o.on('-n', '--title TITLE', 'find by title (regular expression)') do |t|
$options.title = t
end
end
ARGV.parse!
days = getdays
searchdays = $options.day ? [days[$options.day.to_i-1]] : days
searchdays.each do |d|
puts "Tag #{d.index} (#{d.date})"
events = d.events.sort_by{|e| e.start }
if $options.time
events = events.select{|ev| ev.start =~ /^#{$options.time}/i }
end
if $options.title
events = events.select{|ev| ev.title =~ /#{$options.title}/i }
end
puts events
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment