class Aria2::Downloader
Public Class Methods
check()
click to toggle source
# File lib/aria2.rb, line 19 def self.check begin self.rpc_call('getGlobalStat', []) true rescue false end end
download(url, path)
click to toggle source
# File lib/aria2.rb, line 28 def self.download(url, path) path = File.expand_path(path) self.rpc_call('addUri', [[url], { 'dir' => File.dirname(path), 'out' => File.basename(path), 'allow-overwrite' => 'true' }]) end
new(host = 'localhost', port = 6800)
click to toggle source
# File lib/aria2.rb, line 13 def self.new(host = 'localhost', port = 6800) @host = host @port = port self end
query_status(gid)
click to toggle source
# File lib/aria2.rb, line 37 def self.query_status(gid) status = self.rpc_call('tellStatus', [gid, [ 'status', 'totalLength', 'completedLength', 'downloadSpeed', 'errorCode' ]]) status['totalLength'] = status['totalLength'].to_i status['completedLength'] = status['completedLength'].to_i status['downloadSpeed'] = status['downloadSpeed'].to_i status['errorCode'] = status['errorCode'].to_i status['progress'] = status['totalLength'] == 0 ? 0 : status['completedLength'].to_f / status['totalLength'].to_f status['remainingTime'] = status['downloadSpeed'] == 0 ? 0 : (status['totalLength'] - status['completedLength']).to_f / status['downloadSpeed'] status end
remove(gid)
click to toggle source
# File lib/aria2.rb, line 62 def self.remove(gid) self.rpc_call('remove', [gid]) == gid end
Private Class Methods
get(url, params = {})
click to toggle source
# File lib/aria2.rb, line 68 def self.get(url, params = {}) uri = URI.parse(url) uri.query = URI.encode_www_form(params) http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Get.new(uri.request_uri) response = http.request(request) { 'code' => response.code.to_i, 'body' => response.body } end
rpc_call(method, params)
click to toggle source
# File lib/aria2.rb, line 88 def self.rpc_call(method, params) method = "aria2.#{method}" id = 'ruby-aria2' params_encoded = Base64.encode64(JSON.generate(params)) response = get("#{self.rpc_path}", {'method' => method, 'id' => id, 'params' => params_encoded}) answer = JSON.parse(response['body']) if response['code'] == 200 answer['result'] else raise "AriaDownloader error #{answer['error']['code'].to_i}: #{answer['error']['message']}" end end
rpc_path()
click to toggle source
# File lib/aria2.rb, line 84 def self.rpc_path "http://#{@host}:#{@port}/jsonrpc" end