class DownloadTV::Torrent

Class in charge of managing the link grabbers
#

def change_grabbers

def change_grabbers
  # Rotates the instantiated grabbers
  @g_instances.rotate!
  check_grabber_online
end

def check_grabber_online

def check_grabber_online
  return if @g_instances.first.online?
  # We won't be using this grabber
  warn "Problem accessing #{@g_instances.first.class.name}"
  @tries -= 1
  @g_instances.drop 1
end

def get_links(show)

def get_links(show)
  links = @g_instances.first.get_links(show)
  reset_grabbers_order
  reset_tries
  links
rescue NoTorrentsError
  puts "No torrents found for #{show} using #{@g_instances.first.class.name}"
  # Use next grabber
  if @tries.positive?
    @tries -= 1
    change_grabbers
    retry
  else
    reset_grabbers_order
    reset_tries
    # Handle show not found here!!
    return []
  end
end

def grabbers

def grabbers
  %w[TorrentAPI ThePirateBay Eztv KAT]
end

def initialize(default_grabber = nil)

def initialize(default_grabber = nil)
  g_names = grabbers
  # Silently ignores bad names
  found_default = g_names.find_index(default_grabber)
  g_names.rotate! found_default if found_default
  @g_instances = g_names.map { |g| (DownloadTV.const_get g).new }
  reset_tries
  check_grabber_online
end

def reset_grabbers_order

def reset_grabbers_order
  @g_instances.rotate!(@tries + 1)
end

def reset_tries

def reset_tries
  @tries = @g_instances.size - 1
end