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.



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

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.



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

def details
  @details
end

Instance Method Details

#contentObject

Downloads the detailed view for this torrent Returns an Nokogiri object



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

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)


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

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



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

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



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

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



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

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



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

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



221
222
223
224
225
226
227
# File 'lib/torrents/container.rb', line 221

def movie
  if imdb_id
    @_movie ||= MovieSearcher.find_movie_by_id(imdb_id)
  else 
    @_movie ||= MovieSearcher.find_by_release_name(title, :options => {:details => true}) 
  end
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



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

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



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

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

#tidObject

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



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

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



232
233
234
235
# File 'lib/torrents/container.rb', line 232

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



250
251
252
# File 'lib/torrents/container.rb', line 250

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



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

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)


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

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)


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

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)


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

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