class NowNext

Lists what is airing now and next accross BBC Radio and Television stations

Constants

Programme

Public Class Methods

new(io = STDOUT) click to toggle source
# File lib/bbc/now_next.rb, line 8
def initialize(io = STDOUT)
  @io = io
end

Public Instance Methods

radio_next() click to toggle source
# File lib/bbc/now_next.rb, line 33
def radio_next
  @fmt = '%-32s %s'
  load 'radio'

  on_next
end
radio_now() click to toggle source
# File lib/bbc/now_next.rb, line 26
def radio_now
  @fmt = '%-32s %s'
  load 'radio'

  on_now
end
tv_next() click to toggle source
# File lib/bbc/now_next.rb, line 19
def tv_next
  @fmt = '%-18s %s'
  load 'tv'

  on_next
end
tv_now() click to toggle source
# File lib/bbc/now_next.rb, line 12
def tv_now
  @fmt = '%-18s %s'
  load 'tv'

  on_now
end

Private Instance Methods

format_channel(name) click to toggle source
# File lib/bbc/now_next.rb, line 89
def format_channel(name)
  name.gsub(/^BBC | (London|England|Channel)/, '')
end
load(medium) click to toggle source
# File lib/bbc/now_next.rb, line 44
def load(medium)
  begin
    raw = open("http://www.bbc.co.uk/iplayer/ion/multinownext/service_type/#{medium}/simulcast_only/1/format/json", 'UserAgent' => AUNTIE::USER_AGENT).read
    @data = JSON.parse(raw)
  rescue
    raise "Unable to download #{medium} schedules"
  end
end
on_next() click to toggle source
# File lib/bbc/now_next.rb, line 62
def on_next
  programmes = []

  second = 0

  @data['blocklist'].each do |e|
    p = Programme.new
    p.channel = format_channel e['title']

    p.title = e['next'][0]['episode']['passionsite_title'] rescue ''
    p.starts = Time.parse(e['next'][0]['start_time_iso']) rescue ''

    # next_start = starts.strftime("%H:%M")
    p.starts_in = how_long_between(time_now, p.starts)

    second = p.channel.length + 3 if p.channel.length > second

    programmes << p
  end

  programmes.sort_by! { |p| p.starts }

  programmes.each do |p|
    @io.puts sprintf("%-9s %-#{second}s %s", p.starts_in, p.channel, p.title)
  end
end
on_now() click to toggle source
# File lib/bbc/now_next.rb, line 53
def on_now
  @data['blocklist'].each do |e|
    channel = format_channel e['title']
    programme = e['now'][0]['episode']['passionsite_title'] rescue next

    @io.puts sprintf @fmt, channel, programme
  end
end