Created
December 27, 2009 11:22
-
-
Save badboy/264245 to your computer and use it in GitHub Desktop.
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 ruby18 | |
require 'rubygems' | |
require 'hpricot' | |
require 'open-uri' | |
require 'cgi' | |
require 'pp' | |
require 'ostruct' | |
require 'optparse' | |
SCHEDULE_URI = 'http://events.ccc.de/congress/2009/Fahrplan/schedule.en.xml' | |
class Event | |
attr_accessor :start, :duration, :title, :room | |
def initialize | |
yield self if block_given? | |
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 | |
begin | |
ARGV.parse! | |
rescue => exc | |
STDERR.puts "E: #{exc.message}" | |
STDERR.puts ARGV.options.to_s | |
exit 1 | |
end | |
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