Class: Torrents

Inherits:
Container::Shared show all
Defined in:
lib/torrents.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Container::Shared

#default_values, #download, #error, #inner_call, #load, #url_cleaner, #valid_option?

Constructor Details

#initializeTorrents

Returns a new instance of Torrents.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/torrents.rb', line 11

def initialize
  @torrents = []
  @errors   = []
  @url = {
    callback: lambda { |obj|
      obj.send(:inner_recent_url)
    },
    search: {
      value: ""
    }
  }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

If the user is trying to do some funky stuff to the data



81
82
83
84
# File 'lib/torrents.rb', line 81

def method_missing(method, *args, &block)
  return self.inner_call($1.to_sym, args.first) if method =~ /^inner_(.+)$/
  super(method, args, block)
end

Instance Attribute Details

#page(value) ⇒ Object

Returns the value of attribute page.

Raises:

  • (ArgumentError)


9
10
11
# File 'lib/torrents.rb', line 9

def page
  @page
end

Class Method Details

.method_missing(method, *args, &block) ⇒ Object

Raises:

  • (Exception)


86
87
88
89
90
91
92
93
# File 'lib/torrents.rb', line 86

def self.method_missing(method, *args, &block)
  this = Torrents.new
  # Raises an exception if the site isn't in the trackers.yaml file
  raise Exception.new("The site #{method} does not exist") unless this.exists?(method)
  
  # Yes, I like return :)
  return this.add(method)
end

Instance Method Details

#add(tracker) ⇒ Object

Makes this the tracker tracker



49
50
51
# File 'lib/torrents.rb', line 49

def add(tracker)
  @tracker = tracker.to_s; self
end

#category(cat) ⇒ Object



70
71
72
73
74
# File 'lib/torrents.rb', line 70

def category(cat)
  @url.merge!(:callback => lambda { |obj|
    obj.send(:inner_category_url, cat)
  }); self
end

#contentObject



28
29
30
31
# File 'lib/torrents.rb', line 28

def content
  @content = {} unless @content
  @content[self.inner_page] ||= Nokogiri::HTML(self.download(self.url))
end

#cookies(args) ⇒ Object



76
77
78
# File 'lib/torrents.rb', line 76

def cookies(args)
  @cookies = args; self
end

#create_torrent(arguments) ⇒ Object

Creates a torrent based on the ingoing arguments Is used by #find_by_details and the #results method Returns a Container::Torrent object arguments (Hash) The params to the Torrent constructor The debugger and cookie param is passed by default



109
110
111
112
113
# File 'lib/torrents.rb', line 109

def create_torrent(arguments)
  arguments.merge!(:debug => @debug) if @debug
  arguments.merge!(:cookies => @cookies) if @cookies
  Container::Torrent.new(arguments)
end

#debugger(value) ⇒ Object



59
60
61
# File 'lib/torrents.rb', line 59

def debugger(value)
  @debug = value; self
end

#errorsObject

Returns errors from the application. Return type: A list of strings



117
118
119
# File 'lib/torrents.rb', line 117

def errors
  self.results; @errors.uniq
end

#exists?(tracker) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/torrents.rb', line 24

def exists?(tracker)
  File.exists?(File.dirname(File.expand_path( __FILE__)) + "/torrents/trackers/" + tracker.to_s + ".rb")
end

#find_by_details(details) ⇒ Object

Returns a Container::Torrent object details (String) The details url for the torrent



97
98
99
100
101
102
# File 'lib/torrents.rb', line 97

def find_by_details(details)
  self.create_torrent({
    details: details,
    tracker: @tracker
  })
end

#inner_pageObject

Set the default page



34
35
36
# File 'lib/torrents.rb', line 34

def inner_page
  ((@page ||= 1) - 1 + self.inner_start_page_index).to_s
end

#resultsObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/torrents.rb', line 121

def results
  return @torrents if @torrents.any? and not @step
  counter  = 0
  rejected = 0
  self.inner_torrents(self.content).each do |tr|
    counter += 1
    
    torrent = self.create_torrent({
      details: self.inner_details(tr),
      torrent: self.inner_torrent(tr),
      title: self.inner_title(tr).to_s.strip,
      tracker: @tracker
    })
    
    if torrent.valid?
      @torrents << torrent
    else
      rejected += 1
    end 
  end
  
  @errors << "#{counter} torrents where found, #{rejected} where not valid" unless rejected.zero?
  @page += 1 if @step

  return @torrents
end

#search(value) ⇒ Object

Set the search value



64
65
66
67
68
# File 'lib/torrents.rb', line 64

def search(value)
  @url.merge!(:callback => lambda { |obj|
    obj.send(:inner_search_url)
  }, :search => {:value => value}); self
end

#stepObject



44
45
46
# File 'lib/torrents.rb', line 44

def step
  @step = true; self
end

#urlObject



38
39
40
41
42
# File 'lib/torrents.rb', line 38

def url
  @url[:callback].call(self).
    gsub('<SEARCH>', @url[:search][:value]).
    gsub('<PAGE>', self.inner_page)
end