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.



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

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



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

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)


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

def page
  @page
end

Class Method Details

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

Raises:

  • (Exception)


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

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



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

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

#category(cat) ⇒ Object



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

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

#contentObject



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

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

#cookies(args) ⇒ Object



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

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



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

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

#debugger(value) ⇒ Object



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

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

#errorsObject

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



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

def errors
  self.results; @errors.uniq
end

#exists?(tracker) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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

#inner_pageObject

Set the default page



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

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

#resultsObject



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
147
148
149
150
# File 'lib/torrents.rb', line 122

def results
  @torrents = [] if @step
  return @torrents if @torrents.any?
  
  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



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

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

#stepObject



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

def step
  @step = true; self
end

#urlObject



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

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