X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=bin%2Fwhatever-dl;h=146dbeca69cc48087eefb2914f0e065c7fe8322f;hb=8e886df259246365023322b78f58e4037cb536a4;hp=fbc84e909bde09dbfc14f525404300c56e00394a;hpb=e3eeb8df887fc68959ab7d7ebae5c0206b79c230;p=dead%2Fwhatever-dl.git diff --git a/bin/whatever-dl b/bin/whatever-dl index fbc84e9..146dbec 100755 --- a/bin/whatever-dl +++ b/bin/whatever-dl @@ -24,12 +24,15 @@ # a symlink which points to it. require 'pathname' +# 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 -$: << File.dirname(executable) + '/../' +$LOAD_PATH << File.dirname(executable) + '/../' # Load our config file. require 'bin/configuration' @@ -53,19 +56,56 @@ EXIT_SUCCESS = 0 EXIT_NO_URL = 1 EXIT_INVALID_URL = 2 EXIT_COULDNT_GET_VIDEO_URL = 3 -EXIT_OUTPUT_FILE_ALREADY_EXISTS = 4 +EXIT_IO_ERROR = 4 EXIT_ERROR_READING_FROM_VIDEO_URL = 5 EXIT_CONNECTION_REFUSED = 6 EXIT_HTTP_ERROR = 7 EXIT_ACCESS_DENIED = 8 +def usage() + puts < + +Options: + -c, --continue Continue downloading a previously-attempted file. + +EOF + +end + # Only actually do something if this script was called # directly (i.e. not from the tests). if (__FILE__ == $0) then + # Default options. + options = { :continue => 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 + + # 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. - puts 'Usage: whatever-dl ' + usage() Kernel.exit(EXIT_NO_URL) end @@ -80,14 +120,9 @@ if (__FILE__ == $0) then video_url = site.get_video_url() if video_url.nil? - puts 'Error retrieving video URL.' - exit(EXIT_COULDNT_GET_VIDEO_URL) - end - - if File.exists?(site.get_video_filename()) - puts "Error: output file already exists. Please remove #{site.get_video_filename()}, and try again." - Kernel.exit(EXIT_OUTPUT_FILE_ALREADY_EXISTS) - end + 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 # The Downloader class is a factory; it should decide # which subclass we get. @@ -98,7 +133,10 @@ if (__FILE__ == $0) then # naturally not report any of these, since it will die in # its own process. begin - downloader.download(video_url, site.get_video_filename()) + downloader.download(video_url, + 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.' Kernel.exit(EXIT_CONNECTION_REFUSED) @@ -107,6 +145,9 @@ if (__FILE__ == $0) then rescue OpenURI::HTTPError => e puts "An HTTP error occurred while downloading the video file: #{e.message}." 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.