aria2mon 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. result=client.call("aria2.tellActive")
  79. print "-- Download Progress --\n"
  80. result.each { |entry|
  81. gid=entry['gid']
  82. total_length=entry['totalLength'].to_i
  83. completed_length=entry['completedLength'].to_i
  84. upload_length=entry['uploadLength'].to_i
  85. download_speed=entry['downloadSpeed'].to_i
  86. print "GID##{gid}"
  87. if total_length == completed_length then
  88. if entry.key? 'infoHash' then
  89. # for BitTorrent print seed status
  90. print " SEEDING"
  91. if completed_length > 0 then
  92. print "(#{upload_length*100/completed_length}%)"
  93. end
  94. end
  95. else
  96. print " SIZE:#{abbrev completed_length}B/#{abbrev total_length}B"
  97. if total_length > 0 then
  98. print "(#{completed_length*100/total_length}%)"
  99. end
  100. end
  101. print " CN:#{entry['connections']}"
  102. if entry.key? 'numSeeders' then
  103. print " SEED:#{entry['numSeeders']}"
  104. end
  105. print " SPD:#{abbrev download_speed}B/s"
  106. if entry.key? 'infoHash'
  107. printf " UP:#{abbrev entry['uploadSpeed'].to_i}B/s(#{abbrev upload_length}B)"
  108. end
  109. print " ETA:#{compute_eta(download_speed, total_length-completed_length)}"
  110. print "\n"
  111. if entry.key? 'infoHash'
  112. print " InfoHash:#{entry['infoHash']}"
  113. end
  114. print "\n"
  115. files=client.call("aria2.getFiles",entry['gid'])
  116. if files.length > 0 then
  117. first_file=files.find{|file| file["selected"]=="true"}
  118. if first_file != nil then
  119. print " File:#{first_file['path']}"
  120. count=0
  121. files.each {|file| count += 1 if file["selected"]=="true"}
  122. if count > 1 then
  123. print "(#{count-1}more)"
  124. end
  125. print "\n"
  126. end
  127. end
  128. print "--------------------------------------------------------------------------------"
  129. }