Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 482 lines (394 sloc) 11.93 kb
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
# Download Manager
#
# Copyright (C) 2009 Stefan Urbanek
#
# Author:: Stefan Urbanek
# Date:: November 2009
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

require 'monitor'
require 'pathname'
require 'curb'
require 'etl/download_batch'
require 'typhoeus'

module DownloadManagerDelegate
def download_thread_did_start(download_manager, thread_id)
    # do nothing
end

def download_thread_did_finish(download_manager, thread_id)
    # do nothing
end

# Create a download batch. Batch should contain list of all URLs to be downloaded.
# Delegate should return _nil_ if no more files are to be downloaded.
#
# Note: this method is called in thread-safe way.
def create_download_batch(download_manager, batch_id)
    return nil
end

def download_batch_failed(download_manager, batch_id, exception)
    puts "ERROR: Download batch #{batch_id} failed: #{exception.message}"
    puts exception.backtrace.join("\n")
end

def download_batch_processing_failed(download_manager, batch_id, exception)
    puts "ERROR: Processing batch #{batch_id} failed: #{exception.message}"
    puts exception.backtrace.join("\n")
end

def process_download_batch(download_manager, batch_id)
    # do nothing by default
end

def download_thread_failed(download_manager, thread_id, exception)
    puts "ERROR: Download thread #{thread_id} failed: #{exception.message}"
    puts exception.backtrace.join("\n")
end
def download_did_finish(download_manager)
# do nothing
end
end

class DownloadManager

attr_accessor :download_directory
attr_reader :delegate
attr_accessor :download_meethod

def initialize
    @download_directory = Pathname.new(".")
end

def delegate=(object)
    @delegate = object
end

def download(options = {})
    # Prepare batch processing
    @processing_queue = Array.new
    @batch_id = 0

    # Prepare thread controll variables
    @mutex = Monitor.new
    @lock = @mutex.new_cond
    @download_finished = false

    # puts ">>> ROLLING THREADS"

    # Run the threads
    if options[:threads]
     thread_count = options[:threads]
    else
    thread_count = 1
    end
   
    spawn_download_threads(thread_count)
    
    process_thread = Thread.new do
        process_downloads
    end
    
    # Wait for downloads to finish
    @download_threads.each { |thread| thread.join }
    @download_finished = true
@delegate.download_did_finish(self)

    # puts "X== DOWNLOADS FINISHED (and not signaling anymore)"

    @mutex.synchronize do
        # puts "<-x LAST SIGNAL E:#{@processing_queue.empty?} F:#{@download_finished}"
        @lock.broadcast
        # puts "<-x LAST SIGNAL CAST"
    end
    # puts "-v- LAST JOIN"

    process_thread.join

    # puts "<<< DOWNLOAD END"

end

def process_downloads
    # Process downloads
    loop do
        batch = nil
        # puts "--> SYNC"
        @mutex.synchronize do
            break if @download_finished and @processing_queue.empty?
            # puts "==> WAITING FOR SIGNAL E:#{@processing_queue.empty?} F:#{@download_finished}"
            @lock.wait_while { @processing_queue.empty? && (! @download_finished)} # or not @download_finished }
            # puts "<== GOT SIGNAL E:#{@processing_queue.empty?} F:#{@download_finished}"
            if not @processing_queue.empty?
                batch = @processing_queue.shift
            end
        end
        break unless batch
        # puts "<-- process SIGNAL"
        begin
@delegate.process_download_batch(self, batch)
rescue => exception
@delegate.download_batch_processing_failed(self, batch, exception)
end
        # puts "<-- done processing SIGNAL"
    end
end

def stop_download
    @mutex.synchronize do
        @stop_download = true
    end
end

def spawn_download_threads(thread_count)
    @download_threads = Array.new

    for thread_id in 1..thread_count
        @download_threads << Thread.new(thread_id) do
            |tid|
            
            #FIXME: handle exception more intelligently
begin
                @delegate.download_thread_did_start(self, tid)
    
                loop do
                    # create new batch in thread-safe way
                    break if @stop_download
                    batch = create_next_batch
                    # puts "--- (#{tid}) GOT BATCH? #{not batch.nil?}"
                    break if not batch
    
     begin
                     download_batch(batch, tid)
                    rescue => exception
                        @delegate.download_batch_failed(self, batch, exception)
                    end

                    # signalize that we are finished, so processing thread can
                    # start processing the downloaded batch
                    # queue batch even the download failed (wget fails when
                    # last download fails)
                    @mutex.synchronize do
                         @processing_queue << batch
                         # puts "<-- SIGNAL"
                         @lock.broadcast
                    end
                end
                @delegate.download_thread_did_finish(self, tid)
            rescue => exception
                @delegate.download_thread_failed(self, tid, exception)
            end
            @mutex.synchronize do
                # puts "x-- SIGNAL END"
                @lock.broadcast
            end

        end
    end
end

def create_next_batch
    batch = nil
    @mutex.synchronize do
        @batch_id = @batch_id + 1
        batch = @delegate.create_download_batch(self, @batch_id)
        if not batch.nil?
            batch.id = @batch_id
        end
    end
    return batch
end
    
def download_batch(batch, tid = nil)

case @download_method
when "curlmulti" then
     download_batch_multi(batch, tid)
when "curleasy" then
download_batch_curb(batch, tid)
when "curl" then
     download_batch_curlcmd(batch, tid)
    when "hydra", nil then
     download_batch_hydra(batch, tid)
    else
# unknown download method
raise RuntimeError, "unknown download method #{@download_method}"
    end
end

def download_batch_curlcmd(batch, tid = nil)
downloads = Array.new

batch.urls.each { |url_info|
# This is default curl method of creating a filename, kept here for possible
# future change.
if url_info.class == String
url = url_info
filename = nil
elsif url_info.class == Hash
url = url_info[:url]
filename = url_info[:filename]
else
# FIXME: raise exception
end

if !filename || filename == ""
filename = url.split(/\?/).first.split(/\//).last
end

path = @download_directory + filename

result = system("curl", "-s", "-o", path, url)
if not result
puts "WARNING: curl result is #{$?}"
end
# exit
# puts "WAITED: #{Time.now - time}s (#{tid})"
hash = Hash.new
hash[:url] = url_info[:url]
hash[:user_info] = url_info[:user_info]
hash[:file] = path
# hash[:status_code] = curl.response_code
downloads << hash
}

batch.downloads = downloads
end

def process_download(url_info, path, response)
file = File.new(path, "wb")
file.puts response.body
file.close

hash = {
:url => url_info[:url],
:file => path,
:status_code => response.code,
:user_info => url_info[:user_info]
}

return hash
end

def download_batch_hydra(batch, tid = nil)
downloads = Array.new

hydra = Typhoeus::Hydra.new(:max_concurrency => 20)

batch.urls.each { |url_info|
if url_info.class == String
url = url_info
filename = nil
elsif url_info.class == Hash
url = url_info[:url]
filename = url_info[:filename]
else
# FIXME: raise exception
end

if !filename || filename == ""
filename = url.split(/\?/).first.split(/\//).last
end

path = @download_directory + filename

reqest = Typhoeus::Request.new(url)

reqest.on_complete do | response |
hash = process_download(url_info, path, response)
downloads << hash
end

hydra.queue(reqest)
}

hydra.run
batch.downloads = downloads
end

def download_batch_curb(batch, tid = nil)
downloads = Array.new

curl = Curl::Easy.new
curl.timeout = 2

batch.urls.each { |url_info|
# This is default curl method of creating a filename, kept here for possible
# future change.
if url_info.class == String
url = url_info
filename = nil
elsif url_info.class == Hash
url = url_info[:url]
filename = url_info[:filename]
else
# FIXME: raise exception
end

if !filename || filename == ""
filename = url.split(/\?/).first.split(/\//).last
end

path = @download_directory + filename

curl.url = url

curl.on_success { |c|
file = File.new(path, "wb")
# file << c.body_str
file.close
}
time = Time.now
begin
curl.perform
rescue
# fail
end

# puts "WAITED: #{Time.now - time}s (#{tid})"
hash = Hash.new
hash[:url] = url_info[:url]
hash[:user_info] = url_info[:user_info]
hash[:file] = path
hash[:status_code] = curl.response_code
downloads << hash
}

# puts "USED CONNECTIONS (#{tid}): #{curl.num_connects}"
# exit
batch.downloads = downloads
end

def download_batch_multi(batch)
curls = Array.new

multi = Curl::Multi.new

# FIXME: make this configurable
# multi.max_connects = 50
multi.pipeline = true

url_info_dict = Hash.new
url_paths = Hash.new
downloads = Array.new
puts "DOWNLOAD BATCH #{batch.user_info} "
pre_transfer_time = 0

batch.urls.each { |url_info|

if url_info.class == String
url = url_info
filename = nil
elsif url_info.class == Hash
url = url_info[:url]
filename = url_info[:filename]
else
# FIXME: raise exception
end

if !filename || filename == ""
filename = url.split(/\?/).first.split(/\//).last
end

url_paths[url] = @download_directory + filename
url_info_dict[url] = url_info

curl = Curl::Easy.new(url)
curl.on_success { |c|
file_name = url_paths[c.url]
if file_name
file = File.new(file_name, "wb")
file << c.body_str
file.close
end
}

curl.on_complete { |c|
info = url_info_dict[c.url]
hash = Hash.new
hash[:url] = info[:url]
file = url_paths[info[:url]]

hash[:file] = file
hash[:status_code] = c.response_code

hash[:user_info] = info[:user_info]
downloads << hash

pre_transfer_time = [pre_transfer_time, c.pre_transfer_time].max
}

curl.timeout = 10

# curl.on_debug { |type, data|
# if not [1, 2, 3].include?(type)
# puts "DEBUG: #{type} #{data}"
# end
#}
multi.add(curl)
}

# puts "DOWNLOAD START"
multi.perform
# puts "DOWNLOAD END #{downloads.count}"
# puts "DOWNLOAD PRETRANSFER TIME #{pre_transfer_time}s"
batch.downloads = downloads
end

# wget method of downloading
def download_batch_wget(batch)

    # FIXME: check permissions

    # create batch URL list file for wget
    list_path = @download_directory + "batch_url_list_#{batch.id}"

    file = File.open(list_path, "w")
    batch.urls.each { |url|
        file.puts url
    }
    file.close

    # create directory where files will be downloaded
    download_directory = @download_directory + "batch_files_#{batch.id}"
    
    download_directory.mkpath

    result = system("wget", "-qE", "-P", download_directory, "-i", list_path)

    batch.files = download_directory.children.select { | path |
                      path.file?
                  }
    if !result
        return false
    end
    return true
end

end
Something went wrong with that request. Please try again.