# encoding: utf-8 module RSeed class Rtorrent def initialize(host, path, port) @client = XMLRPC::Client.new(host, path, port) puts "#{host}, #{path}, #{port}" end def getAll begin response = @client.call("d.multicall", "main", "d.name=", "d.completed_bytes=", "d.size_bytes=", "d.ratio=", "d.up.rate=", "d.down.rate=", "d.hash=") rescue XMLRPC::FaultException => e puts "Error:" puts e.faultCode puts e.faultString end columns = ["name", "completed_bytes", "size_bytes", "ratio", "up", "down", "hash"] formated_response = response.map { |row| Hash[*columns.zip(row).flatten] } return formated_response end def getHash(partialHash) begin response = @client.call("d.multicall", "main", "d.hash=") rescue XMLRPC::FaultException => e puts "Error:" puts e.faultCode puts e.faultString end hash = "" response.each do |r| hash = r.find{ |i| i =~ /^#{partialHash}/ } || hash end return hash end def getName(hash) begin response = @client.call("d.name", "#{hash}") rescue XMLRPC::FaultException => e puts "Error:" puts e.faultCode puts e.faultString end return response end def getTorrentPath(hash) begin response = @client.call("d.base_path", "#{hash.capitalize}") rescue XMLRPC::FaultException => e puts "Error:" puts e.faultCode puts e.faultString end end def getFilesInfo(torrentHash) begin response = @client.call("f.multicall", "#{torrentHash.capitalize}", "", "f.get_path=", "f.frozen_path=", "f.get_size_bytes=") rescue XMLRPC::FaultException => e puts "Error:" puts e.faultCode puts e.faultString end columns = ["name", "path", "size_bytes"] formated_response = response.map { |row| Hash[*columns.zip(row).flatten] } formated_response.sort_by! { |hsh| hsh["name"] } return formated_response end def getUpDown torrents = self.getAll stats = { :up => 0, :down => 0} torrents.each do |t| stats[:up] += t["up"] stats[:down] += t["down"] end return stats end def diskInfo begin response = @client.call("get_directory") rescue XMLRPC::FaultException => e puts "Error:" puts e.faultCode puts e.faultString end stats = Sys::Filesystem.stat(response) return {:total => (stats.blocks * stats.block_size), :free => (stats.block_size * stats.blocks_available)} end def add(file) begin @client.call("load_start", file) rescue XMLRPC::FaultException => e puts "Error:" puts e.faultCode puts e.faultString end end def delete(hash) begin @client.call("d.stop", hash) FileUtils::remove_dir(@client.call("get_directory") + "/" + self.getName(hash)) @client.call("d.erase", hash) rescue XMLRPC::FaultException => e puts "Error:" puts e.faultCode puts e.faultString end end end end