Module: PirateBayRuby

Defined in:
lib/pirate_bay_ruby.rb,
lib/pirate_bay_ruby/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.search(name, number = 0) ⇒ Object

module_function



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pirate_bay_ruby.rb', line 9

def self.search(name, number=0)
  number = number.to_i

 doc = Nokogiri::HTML(open("http://thepiratebay.sx/search/#{URI.escape(name)}/0/7/0"))
  
  cnt = 1
 doc.xpath('//*[@id="searchResult"]').search('tr').each do |row|
    name = row.search('a.detLink')
    next if name.empty? # Skip empty names

    magnet_link = ""
    row.search('a').each do |a|
      if a["title"] == "Download this torrent using magnet"
        magnet_link = a["href"]
        break
      end
    end

    if number == 0
      # Print to screen
      puts "#{cnt}:\t#{name.text}"
    elsif cnt == number
      # Download
      puts "Downloading:   #{name.text}"
      `open #{magnet_link}`
      break
    end
      
    cnt = cnt + 1
 end

  if cnt == 1
    "Empty search results"
  end
end