Class: Container::Torrent

Inherits:
Shared
  • Object
show all
Defined in:
lib/torrents/container.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Shared

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

Constructor Details

#initialize(args) ⇒ Torrent

Returns a new instance of Torrent.



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

def initialize(args)
  args.keys.each { |name| instance_variable_set "@" + name.to_s, args[name] }
  @errors = [] unless @errors
  @debug = false unless @debug
end

Instance Attribute Details

#detailsObject

Returns the value of attribute details.



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

def details
  @details
end

Instance Method Details

#contentObject

Downloads the detailed view for this torrent Returns an Nokogiri object



166
167
168
# File 'lib/torrents/container.rb', line 166

def content
  @content ||= Nokogiri::HTML self.download(@details)
end

#dead?Boolean

Is the torrent dead? The definition of dead is; no seeders Returns a boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/torrents/container.rb', line 127

def dead?
  self.seeders <= 0
end

#domainObject

Returns the domain for the torrent, without http or www If the domain for some reason isn’t found, it will use an empty string



188
189
190
# File 'lib/torrents/container.rb', line 188

def domain
  @domain ||= self.details.match(/(ftp|http|https):\/\/([w]+\.)?(.+?\.[a-z]{2,3})/i).to_a[3] || ""
end

#idObject

Generates an id using the details url



182
183
184
# File 'lib/torrents/container.rb', line 182

def id
  @id ||= self.inner_call(:id, self.details).to_i
end

#imdbObject

Returns the full url to the related imdb page The link is parsed from the details view Example: www.imdb.com/title/tt0066026 Return type: String or nil



206
207
208
# File 'lib/torrents/container.rb', line 206

def imdb
  @imdb ||= self.content.to_s.match(/((http:\/\/)?([w]{3}\.)?imdb.com\/title\/tt\d+)/i).to_a[1]
end

#imdb_idObject

Returns the imdb id for the torrent, including the tt at the beginning Example: tt0066026 Return type: String or nil



213
214
215
# File 'lib/torrents/container.rb', line 213

def imdb_id
  @imdb_id ||= self.imdb.to_s.match(/(tt\d+)/).to_a[1]
end

#movieObject

Returns an movie_searcher object based on the imdb_id, if it exists, otherwise the torrent title Read more about it here: github.com/oleander/MovieSearcher Return type: A MovieSearcher object or nil



220
221
222
# File 'lib/torrents/container.rb', line 220

def movie
  self.imdb_id.nil? ? MovieSearcher.find_by_release_name(self.title, :options => {:details => true}) : MovieSearcher.find_movie_by_id(self.imdb_id)
end

#seedersObject

Returns the amount of seeders for the current torrent If the seeder-tag isn’t found, the value one (1) will be returned. Returns an integer from 0 to inf



134
135
136
# File 'lib/torrents/container.rb', line 134

def seeders
  @seeders ||= self.inner_call(:seeders, self.content).to_i
end

#subtitle(option = :english) ⇒ Object

Returns a Undertexter object, if we found a imdb_id, otherwise nil Read more about it here: github.com/oleander/Undertexter Return type: A single Undertexter object or nil



235
236
237
238
# File 'lib/torrents/container.rb', line 235

def subtitle(option = :english)
  @subtitle = {} unless @subtitle
  @subtitle[option] ||= Undertexter.find(self.imdb_id, language: option).based_on(self.title)
end

#tidObject

Returns a unique id for the torrent based on the domain and the id of the torrent



193
194
195
# File 'lib/torrents/container.rb', line 193

def tid
  @tid ||= Digest::MD5.hexdigest("#{domain}#{id}")
end

#titleObject

Returns the title for the torrent If the title has’t been set from the Torrents class, we will download the details page try to find it there. Return type: String or nil



227
228
229
230
# File 'lib/torrents/container.rb', line 227

def title
  @title ||= self.inner_call(:details_title, self.content)
  @title = @title.strip unless @title.nil?
end

#torrentObject

Returns the torrent for the torrent If the torrent has’t been set from the Torrents class, we will download the details page try to find it there. Return type: String or nil



243
244
245
# File 'lib/torrents/container.rb', line 243

def torrent
  @torrent ||= self.inner_call(:details_torrent, self.content)
end

#torrent_idObject

Just a mirror method for #tid, just in case someone don’t like the method name tid



198
199
200
# File 'lib/torrents/container.rb', line 198

def torrent_id
  @torrent_id ||= self.tid
end

#valid?Boolean

Is the torrent valid? The definition of valid:

Non of the accessors
=> is nil
=> contains htmltags
=> starts or ends with whitespace

It must also stand up to the following requirements

=> The details and torrent url must be valid
=> The id for the torrent must only contain integers.

Returns true or false

Returns:

  • (Boolean)


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/torrents/container.rb', line 148

def valid?
  [:details, :torrent, :title, :id].each do |method|
    data = self.send(method)
    return false if self.send(method).nil? or 
      data.to_s.empty? or 
      data.to_s.match(/<\/?[^>]*>/) or 
      data.to_s.strip != data.to_s
  end
  
  return [
    !! self.valid_url?(self.details),
    !! self.valid_torrent?(self.torrent),
    !! self.inner_call(:id, self.details).to_s.match(/^\d+$/)
  ].all?
end

#valid_torrent?(torrent) ⇒ Boolean

Check to see if the ingoing param is a valid torrent url or not The url has to be a valid url and has to end with .torrent

Returns:

  • (Boolean)


177
178
179
# File 'lib/torrents/container.rb', line 177

def valid_torrent?(torrent)
  torrent.match(/\.torrent$/) and self.valid_url?(torrent)
end

#valid_url?(url) ⇒ Boolean

Check to see if the ingoing param is a valid url or not

Returns:

  • (Boolean)


171
172
173
# File 'lib/torrents/container.rb', line 171

def valid_url?(url)
  !! url.match(/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i)
end