rtorrent.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # encoding: utf-8
  2. module RSeed
  3. class Rtorrent
  4. def initialize(host, path, port)
  5. @client = XMLRPC::Client.new(host, path, port)
  6. puts "#{host}, #{path}, #{port}"
  7. end
  8. def getAll
  9. begin
  10. response = @client.call("d.multicall", "main", "d.name=", "d.completed_bytes=", "d.size_bytes=", "d.ratio=", "d.up.rate=", "d.down.rate=", "d.hash=")
  11. rescue XMLRPC::FaultException => e
  12. puts "Error:"
  13. puts e.faultCode
  14. puts e.faultString
  15. end
  16. columns = ["name", "completed_bytes", "size_bytes", "ratio", "up", "down", "hash"]
  17. formated_response = response.map { |row| Hash[*columns.zip(row).flatten] }
  18. return formated_response
  19. end
  20. def getHash(partialHash)
  21. begin
  22. response = @client.call("d.multicall", "main", "d.hash=")
  23. rescue XMLRPC::FaultException => e
  24. puts "Error:"
  25. puts e.faultCode
  26. puts e.faultString
  27. end
  28. hash = ""
  29. response.each do |r|
  30. hash = r.find{ |i| i =~ /^#{partialHash}/ } || hash
  31. end
  32. return hash
  33. end
  34. def getName(hash)
  35. begin
  36. response = @client.call("d.name", "#{hash}")
  37. rescue XMLRPC::FaultException => e
  38. puts "Error:"
  39. puts e.faultCode
  40. puts e.faultString
  41. end
  42. return response
  43. end
  44. def getTorrentPath(hash)
  45. begin
  46. response = @client.call("d.base_path", "#{hash.capitalize}")
  47. rescue XMLRPC::FaultException => e
  48. puts "Error:"
  49. puts e.faultCode
  50. puts e.faultString
  51. end
  52. end
  53. def getFilesInfo(torrentHash)
  54. begin
  55. response = @client.call("f.multicall", "#{torrentHash.capitalize}", "", "f.get_path=", "f.frozen_path=", "f.get_size_bytes=")
  56. rescue XMLRPC::FaultException => e
  57. puts "Error:"
  58. puts e.faultCode
  59. puts e.faultString
  60. end
  61. columns = ["name", "path", "size_bytes"]
  62. formated_response = response.map { |row| Hash[*columns.zip(row).flatten] }
  63. formated_response.sort_by! { |hsh| hsh["name"] }
  64. return formated_response
  65. end
  66. def getUpDown
  67. torrents = self.getAll
  68. stats = { :up => 0, :down => 0}
  69. torrents.each do |t|
  70. stats[:up] += t["up"]
  71. stats[:down] += t["down"]
  72. end
  73. return stats
  74. end
  75. def diskInfo
  76. begin
  77. response = @client.call("get_directory")
  78. rescue XMLRPC::FaultException => e
  79. puts "Error:"
  80. puts e.faultCode
  81. puts e.faultString
  82. end
  83. stats = Sys::Filesystem.stat(response)
  84. return {:total => (stats.blocks * stats.block_size), :free => (stats.block_size * stats.blocks_available)}
  85. end
  86. def add(file)
  87. begin
  88. @client.call("load_start", file)
  89. rescue XMLRPC::FaultException => e
  90. puts "Error:"
  91. puts e.faultCode
  92. puts e.faultString
  93. end
  94. end
  95. def delete(hash)
  96. begin
  97. @client.call("d.stop", hash)
  98. FileUtils::remove_dir(@client.call("get_directory") + "/" + self.getName(hash))
  99. @client.call("d.erase", hash)
  100. rescue XMLRPC::FaultException => e
  101. puts "Error:"
  102. puts e.faultCode
  103. puts e.faultString
  104. end
  105. end
  106. end
  107. end