class Transmission::Model::Torrent

Attributes

attributes[RW]
connector[RW]
deleted[RW]
ids[RW]
torrents[RW]

Public Class Methods

add(options = {}) click to toggle source
# File lib/transmission/model/torrent.rb, line 124
def add(options = {})
  rpc = options[:connector] || connector
  body = rpc.add_torrent options[:arguments]
  raise DuplicateTorrentError if body['torrent-duplicate']
  find body['torrent-added']['id'], options
end
all(options = {}) click to toggle source
# File lib/transmission/model/torrent.rb, line 110
def all(options = {})
  rpc = options[:connector] || connector
  body = rpc.get_torrent nil, options[:fields]
  Torrent.new body['torrents'], rpc
end
connector() click to toggle source
# File lib/transmission/model/torrent.rb, line 141
def connector
  Transmission::Config.get_connector
end
find(id, options = {}) click to toggle source
# File lib/transmission/model/torrent.rb, line 116
def find(id, options = {})
  rpc = options[:connector] || connector
  ids = id.is_a?(Array) ? id : [id]
  body = rpc.get_torrent ids, options[:fields]
  raise TorrentNotFoundError if body['torrents'].size.zero?
  Torrent.new body['torrents'], rpc
end
new(torrent_object, connector) click to toggle source
# File lib/transmission/model/torrent.rb, line 11
def initialize(torrent_object, connector)
  if torrent_object.is_a? Array
    is_single = torrent_object.size == 1
    @attributes = is_single ? torrent_object.first : {}
    @ids = is_single ? [@attributes['id'].to_i] : []
    @torrents = torrent_object.inject([]) do |torrents, torrent|
      @ids << torrent['id'].to_i
      torrents << Torrent.new([torrent], connector)
    end unless is_single
  end
  @connector = connector
end
start_all!(options = {}) click to toggle source
# File lib/transmission/model/torrent.rb, line 131
def start_all!(options = {})
  rpc = options[:connector] || connector
  rpc.start_torrent nil
end
stop_all!(options = {}) click to toggle source
# File lib/transmission/model/torrent.rb, line 136
def stop_all!(options = {})
  rpc = options[:connector] || connector
  rpc.stop_torrent nil
end

Public Instance Methods

biggest() click to toggle source

Returns torrent biggest file and size.

# File lib/transmission/extensions/torrent_attributes.rb, line 96
def biggest
  b = attributes['files'].max_by { |f| f['length'] }
  [File.basename(b['name']), b['length']]
end
completed?() click to toggle source

Returns true if torrent is completed.

# File lib/transmission/extensions/torrent_status.rb, line 35
def completed?
  attributes['percentDone'] >= 1
end
content() click to toggle source

Returns torrent files names.

# File lib/transmission/extensions/torrent_attributes.rb, line 91
def content
  files.map { |f| File.basename(f) }
end
delete!(remove_local_data = false) click to toggle source
# File lib/transmission/model/torrent.rb, line 24
def delete!(remove_local_data = false)
  connector.remove_torrent @ids, remove_local_data
  @deleted = true
end
downloading?() click to toggle source

Returns true if torrent is downloading.

# File lib/transmission/extensions/torrent_status.rb, line 15
def downloading?
  attributes['status'] == 4
end
error() click to toggle source

Returns torrent error string.

# File lib/transmission/extensions/torrent_attributes.rb, line 123
def error
  attributes['errorString'] if error?
end
error?() click to toggle source

Returns true if torrent has errors.

# File lib/transmission/extensions/torrent_status.rb, line 45
def error?
  attributes['error'] != 0
end
eta() click to toggle source

Returns torrent eta (in seconds).

# File lib/transmission/extensions/torrent_attributes.rb, line 68
def eta
  attributes['eta']
end
file?() click to toggle source

Returns true if torrent is a single file.

# File lib/transmission/extensions/torrent_attributes.rb, line 73
def file?
  name = attributes['name']
  files = attributes['files']
  files.size == 1 && files.first['name'] == name
end
files() click to toggle source

Returns all torrent files full path.

# File lib/transmission/extensions/torrent_attributes.rb, line 85
def files
  dir = File.absolute_path(attributes['downloadDir'])
  attributes['files'].map { |f| File.join(dir, f['name']) }
end
finished?() click to toggle source

Returns true if torrent is finished.

# File lib/transmission/extensions/torrent_status.rb, line 30
def finished?
  attributes['isFinished']
end
folder?() click to toggle source

Returns true if torrent is a folder.

# File lib/transmission/extensions/torrent_attributes.rb, line 80
def folder?
  !file?
end
hash() click to toggle source

Returns torrent hash.

# File lib/transmission/extensions/torrent_attributes.rb, line 21
def hash
  attributes['hashString']
end
id() click to toggle source

Returns torrent id.

# File lib/transmission/extensions/torrent_attributes.rb, line 16
def id
  attributes['id']
end
incomplete?() click to toggle source

Returns true if torrent is not completed.

# File lib/transmission/extensions/torrent_status.rb, line 40
def incomplete?
  attributes['percentDone'] < 1
end
isolated?() click to toggle source

Returns true if torrent is isolated.

# File lib/transmission/extensions/torrent_status.rb, line 25
def isolated?
  attributes['status'] == 7
end
length() click to toggle source

Returns torrent size (in bytes).

# File lib/transmission/extensions/torrent_attributes.rb, line 26
def length
  attributes['totalSize']
end
magnet() click to toggle source

Returns torrent magnet link.

# File lib/transmission/extensions/torrent_attributes.rb, line 128
def magnet
  attributes['magnetLink']
end
method_missing(symbol, *args) click to toggle source
Calls superclass method
# File lib/transmission/model/torrent.rb, line 96
def method_missing(symbol, *args)
  string = symbol.to_s
  if string[-1] == '='
    string = string[0..-2]
    key = Transmission::Arguments::TorrentSet.real_key string
    return @attributes[key] = args.first unless key.nil?
  else
    key = Transmission::Fields::TorrentGet.real_key string
    return @attributes[key] unless key.nil?
  end
  super
end
move_bottom!() click to toggle source
# File lib/transmission/model/torrent.rb, line 46
def move_bottom!
  connector.move_bottom_torrent @ids
end
move_down!() click to toggle source
# File lib/transmission/model/torrent.rb, line 38
def move_down!
  connector.move_down_torrent @ids
end
move_top!() click to toggle source
# File lib/transmission/model/torrent.rb, line 42
def move_top!
  connector.move_top_torrent @ids
end
move_up!() click to toggle source
# File lib/transmission/model/torrent.rb, line 34
def move_up!
  connector.move_up_torrent @ids
end
multi?() click to toggle source
# File lib/transmission/model/torrent.rb, line 74
def multi?
  @ids.size > 1
end
name() click to toggle source

Returns torrent name.

# File lib/transmission/extensions/torrent_attributes.rb, line 5
def name
  attributes['name']
end
path() click to toggle source

Returns torrent full download path.

# File lib/transmission/extensions/torrent_attributes.rb, line 10
def path
  dir = File.expand_path(attributes['downloadDir'])
  File.join(dir, name)
end
paused?() click to toggle source

Returns true if torrent is paused.

# File lib/transmission/extensions/torrent_status.rb, line 5
def paused?
  attributes['status'] == 0
end
peers() click to toggle source

Returns torrent peers.

# File lib/transmission/extensions/torrent_attributes.rb, line 108
def peers
  attributes['peersConnected'].size
end
peers_leeching() click to toggle source

Returns torrent leeching peers.

# File lib/transmission/extensions/torrent_attributes.rb, line 118
def peers_leeching
  attributes['peersGettingFromUs']
end
peers_seeding() click to toggle source

Returns torrent seeding peers.

# File lib/transmission/extensions/torrent_attributes.rb, line 113
def peers_seeding
  attributes['peersSendingToUs']
end
percent_downloaded() click to toggle source

Returns torrent downloaded files percent.

# File lib/transmission/extensions/torrent_attributes.rb, line 36
def percent_downloaded
  (attributes['haveValid'] * 1.0 / attributes['totalSize']).round(2)
end
percent_ratio() click to toggle source

Returns torrent ratio percent if session ratio is set.

# File lib/transmission/extensions/torrent_attributes.rb, line 46
def percent_ratio
  session = Transmission::Model::Session.get
  return unless session.seed_ratio_limited
  (attributes['uploadRatio'] * 1.0 / session.seed_ratio_limit).round(2)
end
percent_uploaded() click to toggle source

Returns torrent uploaded files percent.

# File lib/transmission/extensions/torrent_attributes.rb, line 41
def percent_uploaded
  (attributes['uploadedEver'] * 1.0 / attributes['totalSize']).round(2)
end
pretty_eta() click to toggle source

Returns prettified eta.

# File lib/transmission/extensions/torrent_prettify.rb, line 15
def pretty_eta
  pretty_time(attributes['eta']) if attributes['eta'] > 0
end
pretty_length() click to toggle source

Returns prettified torrent size.

# File lib/transmission/extensions/torrent_prettify.rb, line 5
def pretty_length
  pretty_size(length)
end
pretty_valid() click to toggle source

Returns prettified downloaded files size.

# File lib/transmission/extensions/torrent_prettify.rb, line 10
def pretty_valid
  pretty_size(valid)
end
queued?() click to toggle source

Returns true if torrent is queued.

# File lib/transmission/extensions/torrent_status.rb, line 10
def queued?
  attributes['status'] == 3
end
ratio() click to toggle source

Returns torrent ratio.

# File lib/transmission/extensions/torrent_attributes.rb, line 63
def ratio
  (attributes['uploadRatio'] * 1.0).round(2)
end
re_announce!() click to toggle source
# File lib/transmission/model/torrent.rb, line 66
def re_announce!
  connector.re_announce_torrent @ids
end
reload!() click to toggle source
# File lib/transmission/model/torrent.rb, line 78
def reload!
  torrents = Torrent.find @ids, connector: @connector
  @ids = torrents.ids
  @attributes = torrents.attributes
  @torrents = torrents.torrents
  self
end
remove!() click to toggle source

Removes torrent.

# File lib/transmission/extensions/torrent_actions.rb, line 5
def remove!
  connector.remove_torrent @ids
  @deleted = true
end
remove_data!() click to toggle source

Removes torrent and trashes data files.

# File lib/transmission/extensions/torrent_actions.rb, line 11
def remove_data!
  connector.remove_torrent @ids, true
  @deleted = true
end
save!() click to toggle source
# File lib/transmission/model/torrent.rb, line 29
def save!
  filtered = Transmission::Arguments::TorrentSet.filter @attributes
  connector.set_torrent @ids, filtered
end
seeding?() click to toggle source

Returns true if torrent is seeding.

# File lib/transmission/extensions/torrent_status.rb, line 20
def seeding?
  attributes['status'] == 6
end
set_location(new_location, move = false) click to toggle source
# File lib/transmission/model/torrent.rb, line 70
def set_location(new_location, move = false)
  connector.torrent_set_location @ids,  location: new_location, move: move
end
smallest() click to toggle source

Returns torrent smallest file and size.

# File lib/transmission/extensions/torrent_attributes.rb, line 102
def smallest
  b = attributes['files'].min_by { |f| f['length'] }
  [File.basename(b['name']), b['length']]
end
speed_download() click to toggle source

Returns torrent download speed (in bytes).

# File lib/transmission/extensions/torrent_attributes.rb, line 53
def speed_download
  attributes['rateDownload']
end
speed_upload() click to toggle source

Returns torrent upload speed (in bytes).

# File lib/transmission/extensions/torrent_attributes.rb, line 58
def speed_upload
  attributes['rateUpload']
end
start!() click to toggle source
# File lib/transmission/model/torrent.rb, line 50
def start!
  connector.start_torrent @ids
end
start_now!() click to toggle source
# File lib/transmission/model/torrent.rb, line 54
def start_now!
  connector.start_torrent_now @ids
end
stop!() click to toggle source
# File lib/transmission/model/torrent.rb, line 58
def stop!
  connector.stop_torrent @ids
end
to_json() click to toggle source
# File lib/transmission/model/torrent.rb, line 86
def to_json
  if multi?
    @torrents.inject([]) do |torrents, torrent|
      torrents << torrent.to_json
    end
  else
    @attributes
  end
end
trackers() click to toggle source

Returns torrent trackers.

# File lib/transmission/extensions/torrent_attributes.rb, line 133
def trackers
  url_regex = /([-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b)(?:[-a-zA-Z0-9@:%_\+.~#?&\/=]*)/i
  attributes['trackers'].map do |r|
    if (a = r['announce'].match(url_regex))
      a[1]
    end
  end.compact
end
valid() click to toggle source

Returns torrent downloaded files size (in bytes).

# File lib/transmission/extensions/torrent_attributes.rb, line 31
def valid
  attributes['haveValid']
end
verify!() click to toggle source
# File lib/transmission/model/torrent.rb, line 62
def verify!
  connector.verify_torrent @ids
end

Private Instance Methods

pretty_size(size) click to toggle source

Converts filesize to proper units.

# File lib/transmission/extensions/torrent_prettify.rb, line 22
def pretty_size(size)
  {
    'B'  => 1000**1,
    'KB' => 1000**2,
    'MB' => 1000**3,
    'GB' => 1000**4,
    'TB' => 1000**5
  }.each_pair do |e, s|
    return "#{(size.to_f / (s / 1000)).round(2)}#{e}" if size < s
  end
end
pretty_time(seconds) click to toggle source

Convert seconds in seconds/minutes/hours.

# File lib/transmission/extensions/torrent_prettify.rb, line 35
def pretty_time(seconds)
  mm, ss = seconds.divmod(60)
  hh, mm = mm.divmod(60)
  dd, hh = hh.divmod(24)
  eta = {
    days: dd, hours: hh, minutes: mm, seconds: ss
  }.delete_if { |_k, v| v.zero? }
  eta.to_a.map { |e| e.reverse.join(' ') }.join(', ')
end
status_name() click to toggle source

Converts status code in status name.

# File lib/transmission/extensions/torrent_prettify.rb, line 46
def status_name
  ['paused', 'check wait', 'check', 'download wait', 'download',
   'seed wait', 'seed', 'isolated'].at(status)
end
status_symbol() click to toggle source

Converts status code in status symbol.

# File lib/transmission/extensions/torrent_prettify.rb, line 52
def status_symbol
  ['❚❚', '~', '⟳', '⥥', '⬇', '⥣', '⬆', '✘'].at(status)
end