Sha256: 72b5be51ecc0f50c107788509a7205e392fec36ce7307d0111801600365b5eae

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

require "xmlrpc/client"

module Saber
  class Downloader
    def initialize
      @aria2 = XMLRPC::Client.new2(Rc.aria2.rpc)
    end

    def aria2_add(uris, o={}, &blk)
      @aria2.call("aria2.addUri", uris, o, &blk)
    end

    def add(*files)
      files.each { |file|
        uri = "#{Rc.server.ftp}/#{file}"
        gid = aria2_add([uri], :dir => Pa.dir2("#{Rc.p.download}/#{file}"))
        Saber.ui.say "DOWNLOAD #{gid}:: #{uri}"
      }
    end

    def add_names(*names)
      files = retrive_files(*names)
      add(*files)
    end

  private

    def retrive_files(*names)
      files = []
      Net::SSH.start(Rc.server.host, Rc.server.user) do |s|
        name = "'#{names.join("' '")}'"
        cmd = "cd #{Rc.server.download.p} && find #{name} -type f"

        rst = s.exec!(cmd)
        if rst =~ /^find: `|^cd:cd:/
          raise Error, rst 
        elsif rst.nil?
          raise Error, "remote `#{name}' is an empty directory."
        end

        files = rst.split("\n")
      end

      files
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
saber-0.0.3 lib/saber/downloader.rb