From 097749b9e9d7ab29bb78f681f01865741aea9342 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sun, 10 Jun 2012 21:24:13 -0400 Subject: [PATCH] Update bin/whatever-dl and the configuration class for the other changes. --- bin/whatever-dl | 49 +++++++++++--------------------- lib/whatever-dl/configuration.rb | 48 ++++++++++++++++++++++++++++--- 2 files changed, 61 insertions(+), 36 deletions(-) diff --git a/bin/whatever-dl b/bin/whatever-dl index 146dbec..7826249 100755 --- a/bin/whatever-dl +++ b/bin/whatever-dl @@ -19,39 +19,12 @@ # http://www.fsf.org/licensing/licenses/gpl.html # -# We need Pathname to get the real filesystem path -# of this script (and not, for example, the path of -# a symlink which points to it. -require 'pathname' +# This should load everything we need for us. +require 'whatever-dl' # And getoptlong to check for our one option, --continue. require 'getoptlong' -# This bit of magic adds the parent directory (the -# project root) to the list of ruby load paths. -# Thus, our require statements will work regardless of -# how or from where the script was run. -executable = Pathname.new(__FILE__).realpath.to_s -$LOAD_PATH << File.dirname(executable) + '/../' - -# Load our config file. -require 'bin/configuration' - -# And the downloaders... -require 'src/downloader' - -# The Dir.glob that's coming up doesn't use the -# Ruby library path so we need to tell it where to -# look explicitly. -websites_pattern = File.dirname(executable) + '/../src/websites/*.rb' - -# All of the website classes are located in one -# directory, so we can 'require' them automatically. -Dir.glob(websites_pattern).each do |r| - require r -end - - EXIT_SUCCESS = 0 EXIT_NO_URL = 1 EXIT_INVALID_URL = 2 @@ -137,17 +110,29 @@ if (__FILE__ == $0) then site.get_video_filename(), site.headers(), continue=options[:continue]) + rescue Errno::ECONNREFUSED => e - puts 'The connection to the server (to download the video file) was refused. Check your connection, and try again later.' + msg = 'The connection to the server (to download the video file) ' + msg += 'was refused. Check your connection, and try again later.' + puts msg Kernel.exit(EXIT_CONNECTION_REFUSED) + rescue Errno::EACCES => e - puts "Access denied. Check that you have write permission to the output file/directory. Details: #{e.message}." + msg = 'Access denied. Check that you have write permission ' + msg += "to the output file/directory. Details: #{e.message}." + puts msg + Kernel.exit(EXIT_ACCESS_DENIED) + rescue OpenURI::HTTPError => e - puts "An HTTP error occurred while downloading the video file: #{e.message}." + msg = 'An HTTP error occurred while downloading ' + msg += " the video file: #{e.message}." + puts msg Kernel.exit(EXIT_HTTP_ERROR) + rescue IOError => e puts "Input/Output Error: #{e.message}" Kernel.exit(EXIT_IO_ERROR) + end # Write an empty line at the end for aesthetic reasons. diff --git a/lib/whatever-dl/configuration.rb b/lib/whatever-dl/configuration.rb index 60066cc..643479e 100644 --- a/lib/whatever-dl/configuration.rb +++ b/lib/whatever-dl/configuration.rb @@ -1,14 +1,54 @@ +require 'yaml' + module Configuration # Configurable Options - + # # We support a couple of different download methods. The :openuri # method utilizes the ruby open-uri library, and provides its own # progress bar with no external dependencies. The :wget method, on the # other hand, requires GNU Wget (http://www.gnu.org/software/wget/), # but will support auto-resume of partially-downloaded files for # example. - DOWNLOAD_METHOD = :openuri - #DOWNLOAD_METHOD = :wget - USER_AGENT = 'whatever-dl' + USERCONF_PATH = ENV['HOME'] + '/.whatever-dl.conf.yml' + @dict = {} + + def initialize() + cfg = default_configuration() + + # Now, load the user configuration which will override the + # variables defined above. + begin + user_config = YAML.load(File.open(USERCONF_PATH)) + + # We require ruby-1.9.x now so this should exist. + cfg.merge!(user_config) + + rescue Errno::ENOENT + # If the user config file doesn't exist, whatever. + end + + # Convert all of the keys to symbols. + cfg = cfg.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} + + @dict = cfg + end + + def method_missing(sym, *args) + # Replace all missing method calls with dictionary lookups. + return @dict[sym] + end + + + private; + + def default_configuration() + d = {} + + d['download_method'] = 'openuri' + d['user_agent'] = 'whatever-dl' + + return d + end + end -- 2.44.2