class Rget

A download manager.

Public Class Methods

new() click to toggle source

reads commandline-options, organizes, reads or updates the download-queue, cuts and washes the vegetables, irons my shirts and starts the download.

# File lib/rget.rb, line 42
def initialize()       
        @log = @@log
        options = ArgParser.parse(ARGV)
        @log.level = $log_level
        @log.debug('options are ' << options.to_s)

        # create the history only after the options have been read.
        $HISTORY = DownloadHistory.instance
        if(options.list_only)
                show_queue
                exit true
        end

        if(options.queued_item)
                @log.debug('queued item is ' << options.queued_item)
                dw = $HISTORY[options.queued_item]
                @log.debug('from queue: ' << dw.to_s)
                @url = $HISTORY[options.queued_item].url
                @file = $HISTORY[options.queued_item].local_file
        end

        @url ||= ARGV.first
        @file ||= ARGV.last if ARGV.length > 1
        if !@url || @url.strip.empty?
                @log.warn('No URL to download!')
                @log.info('Call with option "-h" to see some usage information')
                exit false
        else
                @log.debug('URL to download: ' << @url) 
                if(@file)
                        @log.debug('Requested file to write: ' << @file) 
                else
                        @file = File.basename(@url)
                        @log.debug('Derived file to write: ' << @file) 
                end
                verify_file
        end
        @continue = options.continue
        
        #do it
        download
end

Private Instance Methods

check_history(element) click to toggle source

See if there is already an entry for the url in the history. Return the corresponding Download-object, if the user desires to continue downloading to the existing, local file, else nil.

# File lib/rget.rb, line 143
def check_history(element)
        @previous_download = $HISTORY[element]
        if(@previous_download && @previous_download.local_file && File::exist?(@previous_download.local_file) )
                puts "\nQueued item found: " << @previous_download.url
                puts "A previous attempt to download this file has been interrupted."
                puts @previous_download.date.to_s << " local file: " << @previous_download.local_file << " current size: " << byte_units(File.size(@previous_download.local_file)) << ' bytes'
                puts "Do you want to continue downloading to " << @previous_download.local_file << " (Y/n)?"
                resp = wait_for_user
                if("\u0003" == resp)
                        puts ("\n\tProgram interrupted by user (CTRL+C)")
                        exit 0
                end
                unless ('y' == resp.chr.downcase)
                        puts "\n\tIgnoring previous download. Will fetch the complete file."
                        @previous_download = nil
                end
        end
end
download() click to toggle source

does it.

# File lib/rget.rb, line 119
def download()
        local = File.absolute_path(@file)

        network_opts = {}
        if(@url)
                check_history(@url) 
                if(@previous_download && @previous_download.local_file && File::exist?(@previous_download.local_file) )
                        local = @previous_download.local_file
                        network_opts[:previous_size] = @previous_download.size
                else
                        $HISTORY.update(@url, :local=>local )
                end
                puts 'Will download ' << @url
                network_opts[:continue] = @continue

                Network::http_to_file(local, @url, network_opts)
        end
end
queued_url(element) click to toggle source
# File lib/rget.rb, line 162
def queued_url(element)
        $HISTORY[element].url
end
show_queue() click to toggle source

Print out the current list of unfinished downloads.

# File lib/rget.rb, line 170
def show_queue()
        if($HISTORY.empty?)
                puts "There are no queued downloads."
        else
                puts "Queued downloads (%i):" %$HISTORY.queued_URLs.size
                $HISTORY.queued_URLs.each_with_index do |url, i|
                        download = $HISTORY.previous_download(url)
                        puts "\t%i) %s" %[i+1, url]
                        puts "\t\t        since: %s" %download.date.strftime('%d.%m.%Y')
                        puts "\t\t last attempt: %s" %download.updated.strftime('%d.%m.%Y %kh%M')
                        puts "\t\texpected size: %s" %byte_units(download.size)
                        puts "\t\t current size: %s" %byte_units(download.position)
                        puts "\t\t   local file: %s" %download.local_file
                        puts "\t---------------------"
                end
        end
end
verify_file() click to toggle source
# File lib/rget.rb, line 87
def verify_file
        if(@file)
                if( File.exist?(@file))
                        if(!File.writable?(@file)) 
                                @log.error('The local file ' << @file.dup << ' exists and cannot be overwritten! Aborting!')
                                exit false
                        else
                                puts('The local file ' << @file.dup << ' exists! Do you want to replace it or append data to this file (Y/n)?')    
                                resp = wait_for_user       
                                if(resp.chr.downcase != 'y')
                                        @log.info('Okay doing nothing')
                                        exit true
                                else
                                        @log.debug('Local file will be modified!')
                                end
                        end
                elsif(File.exist?(File.dirname(@file)))
                        if(!File.directory?(File.dirname(@file)) || !File.writable?(File.dirname(@file)))
                                @log.error('Cannot create the file ' << @file.dup << ' in the directory ' << File.dirname(@file))
                                @log.error('Aborting')
                                exit false
                        else
                                @log.info('Will download to ' << @file)    
                        end
                end
        else  # this should never happen.
                @log.error('No local file-name given. Aborting.')
                exit false
        end
end