Class: Rbitter::DLThread
- Inherits:
-
Object
- Object
- Rbitter::DLThread
- Defined in:
- lib/rbitter/dlthread.rb
Instance Method Summary collapse
- #<<(url_array) ⇒ Object
-
#initialize(dlfolder, large_flag) ⇒ DLThread
constructor
A new instance of DLThread.
- #job_cleanup ⇒ Object
Constructor Details
#initialize(dlfolder, large_flag) ⇒ DLThread
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rbitter/dlthread.rb', line 8 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 |
Instance Method Details
#<<(url_array) ⇒ Object
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 |
# File 'lib/rbitter/dlthread.rb', line 25 def <<(url_array) download_task = Thread.new { url_array.each { |url| uri = URI.parse(@large_image ? url + ":large" : url) ssl = uri.scheme.downcase == 'https' Net::HTTP.start(uri.host, uri.port, :use_ssl => ssl) { |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 } } } } @pool.push download_task end |
#job_cleanup ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rbitter/dlthread.rb', line 51 def job_cleanup until @pool.empty? dlthrd = @pool.shift if dlthrd.alive? puts "[dlthread] Thread forceful cleaning up [remains: #{@pool.length}]" dlthrd.terminate dlthrd.join end end end |