123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- # 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
|