aria2mon 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env ruby
  2. # The MIT License
  3. #
  4. # Copyright (c) 2009 Tatsuhiro Tsujikawa
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. # THE SOFTWARE.
  23. require 'xmlrpc/client'
  24. require 'optparse'
  25. program_name=File.basename($0)
  26. options={}
  27. args=nil
  28. OptionParser.new do |opt|
  29. opt.on("--server SERVER", "hostname of XML-RPC server. Default: localhost"){|val| options["server"]=val }
  30. opt.on("--port PORT", "port of XML-RPC server. Default: 6800"){|val| options["port"]=val }
  31. opt.on("--user USERNAME", "XML-RPC username"){|val| options["user"]=val }
  32. opt.on("--passwd PASSWORD", "XML-RPC password"){|val| options["passwd"]=val }
  33. opt.banner=<<EOS
  34. Usage: #{program_name} [options]
  35. EOS
  36. args=opt.parse(ARGV)
  37. end
  38. def compute_eta speed,rem_length
  39. return "n/a" if speed == 0
  40. remsec=rem_length/speed
  41. hr=remsec/3600
  42. remsec=remsec%3600
  43. min=remsec/60
  44. remsec=remsec%60
  45. result=""
  46. result += "#{hr}h" if hr > 0
  47. result += "#{min}m" if min > 0
  48. result += "#{remsec}s"
  49. end
  50. def abbrev value
  51. n=value/1024.0
  52. if n < 1 then
  53. return "#{value}"
  54. end
  55. value=n
  56. n=value/1024.0
  57. if n < 1 then
  58. return sprintf("%.1fKi", value)
  59. else
  60. return sprintf("%.1fMi", n)
  61. end
  62. end
  63. auth=""
  64. if options.has_key?("user") then
  65. auth=options["user"]+":"+options["passwd"]+"@"
  66. end
  67. if not options.has_key?("server") then
  68. options["server"]="localhost"
  69. end
  70. if not options.has_key?("port") then
  71. options["port"]="6800"
  72. end
  73. client=XMLRPC::Client.new3({:host => options["server"],
  74. :port => options["port"],
  75. :path => "/rpc",
  76. :user => options["user"],
  77. :password => options["passwd"]})
  78. options.delete("server")
  79. options.delete("port")
  80. options.delete("user")
  81. options.delete("passwd")
  82. result=client.call("aria2.tellActive")
  83. print "-- Download Progress --\n"
  84. result.each { |entry|
  85. gid=entry['gid']
  86. total_length=entry['totalLength'].to_i
  87. completed_length=entry['completedLength'].to_i
  88. upload_length=entry['uploadLength'].to_i
  89. download_speed=entry['downloadSpeed'].to_i
  90. print "GID##{gid}"
  91. if total_length == completed_length then
  92. if entry.key? 'infoHash' then
  93. # for BitTorrent print seed status
  94. print " SEEDING"
  95. if completed_length > 0 then
  96. print "(#{upload_length*100/completed_length}%)"
  97. end
  98. end
  99. else
  100. print " SIZE:#{abbrev completed_length}B/#{abbrev total_length}B"
  101. if total_length > 0 then
  102. print "(#{completed_length*100/total_length}%)"
  103. end
  104. end
  105. print " CN:#{entry['connections']}"
  106. if entry.key? 'numSeeders' then
  107. print " SEED:#{entry['numSeeders']}"
  108. end
  109. print " SPD:#{abbrev download_speed}B/s"
  110. if entry.key? 'infoHash'
  111. printf " UP:#{abbrev entry['uploadSpeed'].to_i}B/s(#{abbrev upload_length}B)"
  112. end
  113. print " ETA:#{compute_eta(download_speed, total_length-completed_length)}"
  114. print "\n"
  115. if entry.key? 'infoHash'
  116. print " InfoHash:#{entry['infoHash']}"
  117. end
  118. print "\n"
  119. files=client.call("aria2.getFiles",entry['gid'])
  120. if files.length > 0 then
  121. first_file=files.find{|file| file["selected"]=="true"}
  122. if first_file != nil then
  123. print " File:#{first_file['path']}"
  124. count=0
  125. files.each {|file| count += 1 if file["selected"]=="true"}
  126. if count > 1 then
  127. print "(#{count-1}more)"
  128. end
  129. print "\n"
  130. end
  131. end
  132. print "--------------------------------------------------------------------------------\n"
  133. }