Class: TorrentReactor::Search

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(search_string) ⇒ Search

Returns a new instance of Search.



5
6
7
8
9
10
11
# File 'lib/torrent_reactor/base.rb', line 5

def initialize(search_string)
  self.search_string = URI.encode(search_string)
#      self.category_id = TorrentReactor::Categories::IDS[category.upcase.strip.gsub(/S$/, "").to_sym] unless category == 0
  self.page = -1

  @results = TorrentReactor::ResultSet.new(self)
end

Instance Attribute Details

#cachingObject

Returns the value of attribute caching.



3
4
5
# File 'lib/torrent_reactor/base.rb', line 3

def caching
  @caching
end

#category_idObject

Returns the value of attribute category_id.



3
4
5
# File 'lib/torrent_reactor/base.rb', line 3

def category_id
  @category_id
end

#pageObject

Returns the value of attribute page.



3
4
5
# File 'lib/torrent_reactor/base.rb', line 3

def page
  @page
end

#resultsObject

Returns the value of attribute results.



3
4
5
# File 'lib/torrent_reactor/base.rb', line 3

def results
  @results
end

#search_stringObject

Returns the value of attribute search_string.



3
4
5
# File 'lib/torrent_reactor/base.rb', line 3

def search_string
  @search_string
end

Instance Method Details

#cached_filenameObject



39
40
41
# File 'lib/torrent_reactor/base.rb', line 39

def cached_filename
  File.join("tmp", "searches", "#{search_string}_#{category_id}_#{page}.html")
end

#executeObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/torrent_reactor/base.rb', line 27

def execute
  return nil if search_string.nil?
  self.page += 1

  if (@results.size < @results.total_results)
    doc = get_search_results
  end

  next_page(doc)

end

#fetch_search_resultsObject



44
45
46
47
48
49
50
51
# File 'lib/torrent_reactor/base.rb', line 44

def fetch_search_results

  base_search_url = 'http://www.torrentreactor.net/search.php?search=&words='
  sortstring = "&sid=&type=1&exclude=&orderby=a.seeds&cid=5&asc=0&x=47&y=6"
  searchurl = base_search_url << search_string.split.join("+") << sortstring

  Hpricot(URI.parse(searchurl).read)
end

#get_search_resultsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/torrent_reactor/base.rb', line 13

def get_search_results
  if caching && File.exists?(cached_filename)
    content = File.read(cached_filename)
  else
    content = fetch_search_results

    FileUtils.mkdir_p("tmp/searches")
    File.open(cached_filename, "w") do |f|
      f.write(content)
    end
  end
  content
end