Skip to content

Instantly share code, notes, and snippets.

@mkroman
Created July 23, 2010 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkroman/487319 to your computer and use it in GitHub Desktop.
Save mkroman/487319 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# encoding: utf-8
require 'date'
module DR
class TV
attr_reader :channels
def initialize options = {}
@client = DR::Client.new
@channels = @client.channels
# get schedules for all channels
if options[:update_schedules]
update_schedules
end
end
def channel name
@channels.select{ |c| c.name == name }.first
end
# Add a schedule to a channel, e.g. tomorrow.
# EXAMPLE: add_schedule channel("DR1"), DateTime.now + 1
def add_schedule channel, date = nil
schedule = @client.schedule channel, date
schedule.each { |program| channel.add program }
end
# Update the schedule of a single channel.
def update channel, date = nil
channel.clear
schedule = @client.schedule channel, date
schedule.each { |program| channel.add program }
end
# update all channels
def update_schedules
@channels.each { |channel| channel.clear }
debug "Updating schedules…"
buffer = @channels.dup
until buffer.empty?
@client.schedules(buffer[0..4]).each do |channel, schedule|
schedule.each { |program| channel.add program }
end
buffer.slice! 0..4
end
debug "Updating complete…"
end
# Search all channels for the program(s) which matches /<program_title>/
def program_search program_title, program_description
@channels.map{ |c| c.search program_title, program_description }.flatten
end
# program_search as a block!!!!11111111111
def find program_title, program_description = nil, &block
program_search(program_title, program_description).each do |program|
yield program
end
end
private
# denne metode er kun midlertidig
def debug string
puts ">> #{string}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment