Skip to content

Instantly share code, notes, and snippets.

@mkroman
Created July 23, 2010 11:32
Show Gist options
  • Save mkroman/487318 to your computer and use it in GitHub Desktop.
Save mkroman/487318 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# encoding: utf-8
#
# This library makes the user able to retrieve the tv schedules from a single
# or multiple channels via DR's DBService
# (http://www.dr.dk/tjenester/programoversigt/dbservice.ashx).
#
# Author:: Mikkel Kroman
# Copyright:: Copyright (c) 2010 Mikkel Kroman
# License:: Distributes under the GNU license
# This class is the one the client is going to use.
$VERBOSE = $VERBOSE.tap do
$VERBOSE = nil
require 'methodphitamine'
end
require 'dr/program'
require 'dr/channel'
require 'dr/client'
require 'date'
module DR
class TV
attr_reader :channels
def initialize *options
@client = DR::Client.new
@channels = @client.channels
update_schedules if options.include? :auto_update
end
# Retrieve the channel with <tt>name</tt> as it's name from the
# channel list.
def [] name
@channels.select{ |channel| channel.name == name }.first
end
# Update the schedule for the rest of the day (<tt>date</tt>)
#
def update channel, date = DateTime.now # TODO: Refactor this.
channel = self[channel]
channel.clear
schedule = @client.schedules [channel], date
schedule.values[0].each{ |p| channel.add p }
end
def update_schedules # TODO: Refactor this.
@channels.map(&:clear)
buffer = @channels.dup
until buffer.empty?
@client.schedules(buffer[0..4]).each do |channel, schedule|
schedule.each &channel.add(it)
end
buffer.slice! 0..4
end
end
def find options, &block # :yields: program
options = { title: options } if options.is_a? String
if block_given?
find(options).each{ |p| yield p }
else
@channels.map(&it.search(options)).flatten
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment