summaryrefslogtreecommitdiffstats
path: root/aria2mon
blob: 9082e96183e60b9eb2c96cd2820243331d261e39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env ruby
# The MIT License
#
# Copyright (c) 2009 Tatsuhiro Tsujikawa
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
require 'xmlrpc/client'
require 'optparse'

program_name=File.basename($0)
options={}
args=nil
OptionParser.new do |opt|
  opt.on("--server SERVER", "hostname of XML-RPC server. Default: localhost"){|val| options["server"]=val }
  opt.on("--port PORT", "port of XML-RPC server. Default: 6800"){|val| options["port"]=val }

  opt.on("--user USERNAME", "XML-RPC username"){|val| options["user"]=val }
  opt.on("--passwd PASSWORD", "XML-RPC password"){|val| options["passwd"]=val }

  opt.banner=<<EOS
Usage: #{program_name} [options]
EOS
  
  args=opt.parse(ARGV)
end

def compute_eta speed,rem_length
  return "n/a" if speed == 0
  remsec=rem_length/speed
  hr=remsec/3600
  remsec=remsec%3600
  min=remsec/60
  remsec=remsec%60
  result=""
  result += "#{hr}h" if hr > 0
  result += "#{min}m" if min > 0
  result += "#{remsec}s"
end

def abbrev value
  n=value/1024.0
  if n < 1 then
    return "#{value}"
  end
  value=n
  n=value/1024.0
  if n < 1 then
    return sprintf("%.1fKi", value)
  else
    return sprintf("%.1fMi", n)
  end
end

auth=""
if options.has_key?("user") then
  auth=options["user"]+":"+options["passwd"]+"@"
end
if not options.has_key?("server") then
  options["server"]="localhost"
end
if not options.has_key?("port") then
  options["port"]="6800"
end

client=XMLRPC::Client.new3({:host => options["server"],
                             :port => options["port"],
                             :path => "/rpc",
                             :user => options["user"],
                             :password => options["passwd"]})

options.delete("server")
options.delete("port")
options.delete("user")
options.delete("passwd")

result=client.call("aria2.tellActive")

print "-- Download Progress --\n"
result.each { |entry|
  gid=entry['gid']
  total_length=entry['totalLength'].to_i
  completed_length=entry['completedLength'].to_i
  upload_length=entry['uploadLength'].to_i
  download_speed=entry['downloadSpeed'].to_i
  print "GID##{gid}"
  if total_length == completed_length then
    if entry.key? 'infoHash' then
      # for BitTorrent print seed status
      print " SEEDING"
      if completed_length > 0 then
        print "(#{upload_length*100/completed_length}%)"
      end
    end
  else
    print " SIZE:#{abbrev completed_length}B/#{abbrev total_length}B"
    if total_length > 0 then
      print "(#{completed_length*100/total_length}%)"
    end
  end
  print " CN:#{entry['connections']}"
  if entry.key? 'numSeeders' then
    print " SEED:#{entry['numSeeders']}"
  end
  print " SPD:#{abbrev download_speed}B/s"
  if entry.key? 'infoHash'
    printf " UP:#{abbrev entry['uploadSpeed'].to_i}B/s(#{abbrev upload_length}B)"
  end
  print " ETA:#{compute_eta(download_speed, total_length-completed_length)}"
  print "\n"

  if entry.key? 'infoHash'
    print " InfoHash:#{entry['infoHash']}"
  end
  print "\n"

  files=client.call("aria2.getFiles",entry['gid'])
  if files.length > 0 then
    first_file=files.find{|file| file["selected"]=="true"}
    if first_file != nil then
      print " File:#{first_file['path']}"
      count=0
      files.each {|file| count += 1 if file["selected"]=="true"}
      if count > 1 then
        print "(#{count-1}more)"
      end
      print "\n"
    end
  end
  print "--------------------------------------------------------------------------------\n"
}