Class: TorrentFinder::Command

Inherits:
CLAide::Command
  • Object
show all
Defined in:
lib/torrent-finder/command.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Command

Returns a new instance of Command.



21
22
23
24
25
26
27
28
# 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

Class Method Details

.optionsObject



13
14
15
16
17
18
19
# 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

Instance Method Details

#runObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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

  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