class RTorrent::XMLRPC

Attributes

connection_options[R]
host[RW]
password[RW]
path[RW]
port[RW]
server[R]
torrents[R]
use_ssl[R]
user[RW]

Public Class Methods

new(host: 'localhost', port: nil, user: nil, password: nil, path: '/xmlrpc', use_ssl: false) click to toggle source
# File lib/rtorrent_xmlrpc/xmlrpc.rb, line 11
def initialize(host: 'localhost', port: nil, user: nil, password: nil, path: '/xmlrpc', use_ssl: false)
  unless port
    if use_ssl
      port = 443
    else
      port = 80
    end
  end
  self.host = host
  self.port = port
  self.user = user
  self.password = password
  self.path = path
  self.use_ssl = use_ssl
  @torrents = Torrents.new
  @status = :initialized
  connect
  fetch_torrents
end
new_from_hash(hash = {}) click to toggle source
# File lib/rtorrent_xmlrpc/xmlrpc.rb, line 31
def self.new_from_hash(hash = {})
  h = {}
  hash.each { |k,v| h[k.to_s.downcase.to_sym] = v }
  self.new(host: h[:host], port: h[:port], user: h[:user], password: h[:password], path: h[:path], use_ssl: h[:use_ssl])
end

Public Instance Methods

add_labels(hash, labels) click to toggle source

Add labels to torrent

# File lib/rtorrent_xmlrpc/xmlrpc.rb, line 120
def add_labels(hash, labels)
  self.torrents[hash].add_labels(labels)
  @server.call('d.set_custom1', hash, self.torrents[hash].labels_str)
end
connect() click to toggle source

Connect to rtorrent xmlrpc service

# File lib/rtorrent_xmlrpc/xmlrpc.rb, line 43
def connect
  return true if @status == :connected
  connection_options = {
    host: @host,
    port: @port,
    user: @user,
    password: @password,
    path: @path,
    use_ssl: @use_ssl,
  }
  @server = ::XMLRPC::Client.new3(connection_options)
  @status = :connected
end
disconnect() click to toggle source

Disconnect from xmlrpc service

# File lib/rtorrent_xmlrpc/xmlrpc.rb, line 58
def disconnect
  @server = nil
  @status = :disconnected
end
fetch_torrents() click to toggle source

Grab list of torrents from server

# File lib/rtorrent_xmlrpc/xmlrpc.rb, line 64
def fetch_torrents
  self.connect unless @status == :connected
  @torrents = Torrents.new
  args = [
    'd.multicall',
    'main',
    'd.hash=',
    'd.name=',
    'd.custom1=',
    'd.complete=',
    'd.base_filename=',
    'd.base_path=',
    'd.is_multi_file=',
    'd.tied_to_file=',
    'd.get_size_bytes=',
    'd.get_down_total=',
    'd.get_up_total=',
    'd.get_ratio=',
    'd.get_priority=',
  ]
  @server.call(*args).each do |stats|
    torrent = RTorrent::Torrent.new
    torrent.hash = stats[0]
    torrent.name = stats[1]
    torrent.labels = stats[2]
    torrent.completed = stats[3] == 1 ? true : false
    torrent.base_filename = stats[4]
    torrent.base_path = stats[5]
    torrent.is_multi_file = stats[6] == 1 ? true: false
    torrent.tied_to_file = stats[7]
    torrent.size = stats[8]
    torrent.down_total = stats[9]
    torrent.up_total = stats[10]
    torrent.ratio = stats[11].to_f / 1000
    torrent.priority = stats[12]
    torrent.files = @server.call('f.multicall', torrent.hash, '', 'f.get_path=').flatten
    @torrents[torrent.hash] = torrent
  end
end
pause(hash) click to toggle source

Pause a torrent

# File lib/rtorrent_xmlrpc/xmlrpc.rb, line 115
def pause(hash)
  @server.call('d.stop', hash)
end
remove_labels(hash, labels) click to toggle source

Remove labels from torrent

# File lib/rtorrent_xmlrpc/xmlrpc.rb, line 126
def remove_labels(hash, labels)
  self.torrents[hash].remove_labels(labels)
  @server.call('d.set_custom1', hash, self.torrents[hash].labels_str)
end
set_labels(hash, labels) click to toggle source

Set the custom1 (label) field for a torrent

# File lib/rtorrent_xmlrpc/xmlrpc.rb, line 132
def set_labels(hash, labels)
  labels = [labels] unless labels.is_a? Array
  @server.call('d.set_custom1', hash, labels.join(', '))
  self.torrents[hash].labels = labels
end
start(hash) click to toggle source

Start a torrent

# File lib/rtorrent_xmlrpc/xmlrpc.rb, line 105
def start(hash)
  @server.call('d.start', hash)
end
stop(hash) click to toggle source

Stop a torrent

# File lib/rtorrent_xmlrpc/xmlrpc.rb, line 110
def stop(hash)
  @server.call('d.close', hash)
end
use_ssl=(use_ssl) click to toggle source
# File lib/rtorrent_xmlrpc/xmlrpc.rb, line 37
def use_ssl=(use_ssl)
  fail "use_ssl must be a boolean value" unless use_ssl.is_a?(TrueClass) || use_ssl.is_a?(FalseClass)
  @use_ssl = use_ssl
end