Class: Torgo

Inherits:
Object
  • Object
show all
Defined in:
lib/torgo.rb

Overview

Torgo takes care of the place while The Master is away

Constant Summary collapse

VERSION =
SET UP ================#
'0.0.1'
CONFIG_DIR =
"#{ENV['HOME']}/.torgo/"
TOR_DIR =
"#{CONFIG_DIR}torrents/"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTorgo

Creates any directories needed for config/downloads



25
26
27
28
# File 'lib/torgo.rb', line 25

def initialize
  Dir.mkdir(CONFIG_DIR) unless File.exists? CONFIG_DIR
  Dir.mkdir(TOR_DIR) unless File.exists? TOR_DIR
end

Class Method Details

.runObject



128
129
130
131
132
# File 'lib/torgo.rb', line 128

def self.run
  torgo = Torgo.new
  title = torgo.get_search_results ARGV.join(' ')
  torgo.run_utor(title)
end

Instance Method Details

#download_torrent(url) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/torgo.rb', line 46

def download_torrent(url)
  response = ''
  
  open(url, "User-Agent" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9) Gecko/2008052906 Firefox/3.0") do |f|
    puts "Fetched document: #{f.base_uri}"
    if f.content_type == 'application/x-bittorrent'
      # Save the response body
      response = f.read
    else
      response = false
    end
  end

  return response
end

#get_doc(url) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/torgo.rb', line 30

def get_doc(url)
  response = ''
  
  open(url, "User-Agent" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9) Gecko/2008052906 Firefox/3.0") do |f|
    puts "Fetched document: #{f.base_uri}"
    puts "\t Content Type: #{f.content_type}\n"
    puts "\t Charset: #{f.charset}\n"
    puts "\t Content-Encoding: #{f.content_encoding}\n"
    puts "\t Last Modified: #{f.last_modified}\n\n"
    
    response = f.read
  end

  return response
end

#get_search_results(query) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/torgo.rb', line 88

def get_search_results(query)
  query = CGI.escape(query)
  url = "http://www.torrentz.com/search?q=#{query}"
  doc = Hpricot get_doc(url)
  elements = doc/'/html/body/div[4]/dl'
  
  #TODO: Iterate
  first = elements.first
  
  title_zone = (first/'dt/a').first
  puts title_zone
  
  title = title_zone.inner_text.gsub(/( )+/, '_') + ".torrent"
  href = title_zone['href']
  
  age = (first/'dd/span.a').inner_text
  size = (first/'dd/span.s').inner_text
  seeds = (first/'dd/span.u').inner_text
  peers = (first/'dd/span.d').inner_text
  
  #TODO: Hueristics
  
  puts "Title: #{title}"
  puts "Age: #{age}"
  puts "Size: #{size}"
  puts "Seeds: #{seeds}"
  puts "Peers: #{peers}"
  
  puts "href='#{href}'"
  
  get_tor_from_page "http://www.torrentz.com#{href}", title
  return title
end

#get_tor_from_page(url, title) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/torgo.rb', line 62

def get_tor_from_page(url, title)
  doc = Hpricot get_doc(url)
  
  elements = doc/'/html/body/div[4]/dl/dt/a'
  
  elements.each do |element|
    href = element['href']
    
    case href
    when /mininova/
      puts href
      tor_url = href.gsub('tor', 'get')
      
      if torrent = download_torrent(tor_url)
        File.open("#{TOR_DIR}#{title}", 'wb') do |f|
          f << torrent
        end
        return true
      end
    end
    
  end
  
  return false
end

#run_utor(title) ⇒ Object



122
123
124
125
126
# File 'lib/torgo.rb', line 122

def run_utor(title)
  command = "#{UTOR_PATH} \"#{TOR_DIR}#{title}\""
  puts command
  `#{command}`
end