From 778e8f555a929540819c671f1415491c2bffa1d1 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Tue, 12 Jun 2012 21:44:21 -0400 Subject: [PATCH] Use the logger class to log warnings and errors. Run the main script whether or not it was invoked directly. Move the exit codes into their own module. --- bin/whatever-dl | 172 ++++++++++++++++------------------ lib/whatever-dl.rb | 2 + lib/whatever-dl/exit_codes.rb | 13 +++ 3 files changed, 97 insertions(+), 90 deletions(-) create mode 100644 lib/whatever-dl/exit_codes.rb diff --git a/bin/whatever-dl b/bin/whatever-dl index 7826249..b9ea02c 100755 --- a/bin/whatever-dl +++ b/bin/whatever-dl @@ -25,15 +25,8 @@ require 'whatever-dl' # And getoptlong to check for our one option, --continue. require 'getoptlong' -EXIT_SUCCESS = 0 -EXIT_NO_URL = 1 -EXIT_INVALID_URL = 2 -EXIT_COULDNT_GET_VIDEO_URL = 3 -EXIT_IO_ERROR = 4 -EXIT_ERROR_READING_FROM_VIDEO_URL = 5 -EXIT_CONNECTION_REFUSED = 6 -EXIT_HTTP_ERROR = 7 -EXIT_ACCESS_DENIED = 8 +log = Logger.new(STDOUT) +log.level = Logger::WARN def usage() puts < false } - - # Parse the command-line options into the options hash. - opts = GetoptLong.new(["--continue", "-c", GetoptLong::NO_ARGUMENT], - ["--help", "-h", GetoptLong::NO_ARGUMENT]) - - opts.each do |opt, arg| - case opt - when '--help' - usage() - Kernel.exit(EXIT_SUCCESS) - when '--continue' - options[:continue] = true - end - end - # Warn about nonsensical options. - if options[:continue] and not Configuration::DOWNLOAD_METHOD == :wget - puts 'WARNING: The --continue flag does nothing unless DOWNLOAD_METHOD is :wget.' - end +# Default options. +options = { :continue => false } - # Note that GetoptLong steals its arguments from ARGV, so we don't need - # to take optional arguments into account when figuring out whether or not - # we were passed a URL. - if (ARGV.length < 1) then - # If the user didn't give us a URL, yell - # at him or her. +# Parse the command-line options into the options hash. +opts = GetoptLong.new(["--continue", "-c", GetoptLong::NO_ARGUMENT], + ["--help", "-h", GetoptLong::NO_ARGUMENT]) + +opts.each do |opt, arg| + case opt + when '--help' usage() - Kernel.exit(EXIT_NO_URL) + Kernel.exit(ExitCodes::EXIT_SUCCESS) + when '--continue' + options[:continue] = true end +end - # Factory method. - site = Website.create(ARGV[0]) +# Warn about nonsensical options. +if options[:continue] and not Configuration::DOWNLOAD_METHOD == :wget + log.warn('The --continue flag does nothing unless DOWNLOAD_METHOD is :wget.') +end - if site.nil? - puts 'Invalid URL.' - exit(EXIT_INVALID_URL) - end - - video_url = site.get_video_url() +# Note that GetoptLong steals its arguments from ARGV, so we don't need +# to take optional arguments into account when figuring out whether or not +# we were passed a URL. +if (ARGV.length < 1) then + # If the user didn't give us a URL, yell + # at him or her. + usage() + Kernel.exit(ExitCodes::EXIT_NO_URL) +end - if video_url.nil? - puts 'Error retrieving video URL:' - puts "Site not supported, and the generic parser couldn't find any videos." - exit(EXIT_COULDNT_GET_VIDEO_URL) end +# Factory method. +site = Website.create(ARGV[0]) - # The Downloader class is a factory; it should decide - # which subclass we get. - downloader = Downloader.create(Configuration::DOWNLOAD_METHOD) +if site.nil? + log.error('Invalid URL.') + exit(ExitCodes::EXIT_INVALID_URL) +end - # Attempt to download the file, and rescue and report - # any (predictable) exceptions. The wget downloader will - # naturally not report any of these, since it will die in - # its own process. - begin - downloader.download(video_url, - site.get_video_filename(), - site.headers(), - continue=options[:continue]) - - rescue Errno::ECONNREFUSED => e - 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 - 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 - 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) +video_url = site.get_video_url() - end +if video_url.nil? + msg = 'Error retrieving video URL: ' + msg += "Site not supported, and the generic parser couldn't find any videos." + log.error(msg) + exit(ExitCodes::EXIT_COULDNT_GET_VIDEO_URL) +end + +# The Downloader class is a factory; it should decide +# which subclass we get. +downloader = Downloader.create(Configuration::DOWNLOAD_METHOD) + +# Attempt to download the file, and rescue and report +# any (predictable) exceptions. The wget downloader will +# naturally not report any of these, since it will die in +# its own process. +begin + downloader.download(video_url, + site.get_video_filename(), + site.headers(), + continue=options[:continue]) + +rescue Errno::ECONNREFUSED => e + msg = 'The connection to the server (to download the video file) ' + msg += 'was refused. Check your connection, and try again later.' + log.error(msg) + Kernel.exit(ExitCodes::EXIT_CONNECTION_REFUSED) + +rescue Errno::EACCES => e + msg = 'Access denied. Check that you have write permission ' + msg += "to the output file/directory. Details: #{e.message}." + log.error(msg) + Kernel.exit(ExitCodes::EXIT_ACCESS_DENIED) + +rescue OpenURI::HTTPError => e + msg = 'An HTTP error occurred while downloading ' + msg += " the video file: #{e.message}." + log.error(msg) + Kernel.exit(ExitCodes::EXIT_HTTP_ERROR) + +rescue IOError => e + log.error("Input/Output Error: #{e.message}") + Kernel.exit(ExitCodes::EXIT_IO_ERROR) - # Write an empty line at the end for aesthetic reasons. - puts '' - - Kernel.exit(EXIT_SUCCESS) end + +# Write an empty line at the end for aesthetic reasons. +puts '' + +Kernel.exit(ExitCodes::EXIT_SUCCESS) diff --git a/lib/whatever-dl.rb b/lib/whatever-dl.rb index 0f39152..b32cefb 100644 --- a/lib/whatever-dl.rb +++ b/lib/whatever-dl.rb @@ -20,6 +20,8 @@ # Load everything the main script and tests will require. # +require 'logger' +require 'whatever-dl/exit_codes' require 'whatever-dl/configuration' require 'whatever-dl/downloader' diff --git a/lib/whatever-dl/exit_codes.rb b/lib/whatever-dl/exit_codes.rb new file mode 100644 index 0000000..42999a5 --- /dev/null +++ b/lib/whatever-dl/exit_codes.rb @@ -0,0 +1,13 @@ +module ExitCodes + + EXIT_SUCCESS = 0 + EXIT_NO_URL = 1 + EXIT_INVALID_URL = 2 + EXIT_COULDNT_GET_VIDEO_URL = 3 + EXIT_IO_ERROR = 4 + EXIT_ERROR_READING_FROM_VIDEO_URL = 5 + EXIT_CONNECTION_REFUSED = 6 + EXIT_HTTP_ERROR = 7 + EXIT_ACCESS_DENIED = 8 + +end -- 2.44.2