Class: Pirata::Search

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, sort_type = Pirata::Sort::RELEVANCE, categories = ["0"]) ⇒ Search

Returns a new instance of Search.



15
16
17
18
19
20
21
# File 'lib/pirata/search.rb', line 15

def initialize(query, sort_type = Pirata::Sort::RELEVANCE, categories = ["0"])
  @sort_type = sort_type
  @category = categories.join(',')
  @pages = 0
  @query = query
  @results = search()
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



13
14
15
# File 'lib/pirata/search.rb', line 13

def category
  @category
end

#pagesObject

Returns the value of attribute pages.



12
13
14
# File 'lib/pirata/search.rb', line 12

def pages
  @pages
end

#queryObject (readonly)

Returns the value of attribute query.



13
14
15
# File 'lib/pirata/search.rb', line 13

def query
  @query
end

#resultsObject

Returns the value of attribute results.



12
13
14
# File 'lib/pirata/search.rb', line 12

def results
  @results
end

#sort_typeObject (readonly)

Returns the value of attribute sort_type.



13
14
15
# File 'lib/pirata/search.rb', line 13

def sort_type
  @sort_type
end

Class Method Details

.parse_html(url) ⇒ Object



63
64
65
66
# File 'lib/pirata/search.rb', line 63

def parse_html(url)
  response = open(url,'User-Agent' => 'ruby')
  Nokogiri::HTML(response)
end

.recentObject

Return an array of the 30 most recent Torrents



49
50
51
52
53
# File 'lib/pirata/search.rb', line 49

def self.recent
  url = Pirata.config[:base_url] + '/recent'
  html = self.parse_html(url)
  Pirata::Search::parse_search_page(html)
end

.top(category = "all") ⇒ Object

Return the n most recent torrents from a category Searches all categories if none supplied



42
43
44
45
46
# File 'lib/pirata/search.rb', line 42

def self.top(category = "all")
  url = Pirata.config[:base_url] + '/top/' + URI.escape(category)
  html = self.parse_html(url)
  Pirata::Search::parse_search_page(html)
end

Instance Method Details

#multipage?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/pirata/search.rb', line 55

def multipage?
  @pages > 0
end

#search(page = 0) ⇒ Object

Perform a search and return an array of Torrent objects



24
25
26
27
28
29
# File 'lib/pirata/search.rb', line 24

def search(page = 0)
  #build URL ex: http://thepiratebay.se/search/cats/0/99/0
  url = Pirata.config[:base_url] + "/search/#{URI.escape(@query)}" + "/#{page.to_s}" + "/#{@sort_type}" + "/#{@category}"
  html = Pirata::Search.parse_html(url)
  Pirata::Search::parse_search_page(html, self)
end

#search_page(page) ⇒ Object

Return the n page results of a search, assuming it is multipage



32
33
34
35
36
37
38
# File 'lib/pirata/search.rb', line 32

def search_page(page)
  raise "Search must be multipage to search pages" if !multipage?
  raise "Page must be a valid, positive integer" if page.class != Fixnum || page < 0
  raise "Invalid page range" if page > @pages

  self.search(page)
end