class Rbitter::DLThread

Constants

MAX_THREADS

Public Class Methods

new(dlfolder, large_flag) click to toggle source
# File lib/rbitter/dlthread.rb, line 10
def initialize(dlfolder, large_flag)
  @dest = dlfolder
  if not File.directory?(dlfolder)
    warn "[dlthread] Given download location is not available for downloading."
    warn "[dlthread] Fallback to current directory."
    @dest = "./"
  end

  if large_flag.nil?
    @large_image = false
  else
    @large_image = large_flag
  end

  @pool = Array.new
end

Public Instance Methods

<<(url_array) click to toggle source
# File lib/rbitter/dlthread.rb, line 27
def <<(url_array)
  if @pool.length >= MAX_THREADS
    job_cleanup
  end

  download_task = Thread.new do
    url_array.each { |url|
      uri = URI.parse(@large_image ? url + ":large" : url) 

      begin
        Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme.downcase == 'https') { |h|
          req = Net::HTTP::Get.new uri.request_uri
          h.request(req) { |res|
            case res
            when Net::HTTPOK
              fname = File.basename(url)

              puts "[fetch] remote: #{uri.path} => local: #{fname}"
              open(File.join(@dest, fname), "wb") { |file|
                res.read_body { |chunk| file.write(chunk) }
              }
            end
          }
        }
      rescue => e
        puts "[dlthread] exception from thread -> #{e.to_s}"
      end
    }
  end

  @pool.push download_task
end
job_cleanup() click to toggle source
# File lib/rbitter/dlthread.rb, line 60
def job_cleanup
  until @pool.empty?
    dlthrd = @pool.shift

    if dlthrd.join(5).nil?
      puts "[dlthread] #{dlthrd.to_s} is still running. (timeout: 5sec)"
    end
  end
end