class TorrentFinder::Command

Public Class Methods

new(argv) click to toggle source
Calls superclass method
# File lib/torrent-finder/command.rb, line 21
def initialize(argv)
  @use_peerflix = argv.flag?('peerflix', false)
  @list = argv.flag?('list', false)
  @site = argv.option('site', "popgo")
  @keywords = argv.shift_argument

  super
end
options() click to toggle source
Calls superclass method
# File lib/torrent-finder/command.rb, line 13
def self.options
  [
    ['--peerflix', 'launch peerflix with first matched result'],
    ['--site=site', 'use site, default popgo'],
    ['--list', 'list all available site']
  ].concat(super)
end

Public Instance Methods

run() click to toggle source
# File lib/torrent-finder/command.rb, line 30
def run
  if @list
    puts "Available Sites: " + TorrentFinder::Adapters::Registry.adapters.collect {|a| a.name }.join(", ")
    return
  end

  begin
    require "torrent-finder/adapters/#{@site}_adapter"
  rescue
    # ignore any error here
  end

  adapter_clazz = TorrentFinder::Adapters::Registry.adapters.find{|adapter| adapter.name == @site }
  unless adapter_clazz
    puts "Not supported: #{@site}"  
    return     
  end

  adapter = adapter_clazz.new
  if @keywords
    torrents = adapter.search(@keywords)
  else
    torrents = adapter.list
  end

  if @use_peerflix
    torrent = torrents.find {|torrent| torrent.name.include?(@keywords) } || torrents.first
    exec %{peerflix "#{torrent.url}" --vlc -r}
  else
    torrents.each do |torrent|
      puts "#{torrent.name},#{torrent.url}"
    end
  end
end