From e3eeb8df887fc68959ab7d7ebae5c0206b79c230 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sun, 27 Sep 2009 00:52:46 -0400 Subject: [PATCH] Added configurable downloaders. Created three new classes: Downloader, OpenUriDownloader, and WgetDownloader. Created a configuration file. Modified whatever-dl to use the configuration information. --- bin/configuration.rb | 12 +++++++++++ bin/whatever-dl | 28 ++++++++++++------------- src/downloader.rb | 42 ++++++++++++++++++++++++++++++++++++++ src/open_uri_downloader.rb | 33 ++++++++++++++++++++++++++++++ src/wget_downloader.rb | 26 +++++++++++++++++++++++ 5 files changed, 127 insertions(+), 14 deletions(-) create mode 100644 bin/configuration.rb create mode 100644 src/downloader.rb create mode 100644 src/open_uri_downloader.rb create mode 100644 src/wget_downloader.rb diff --git a/bin/configuration.rb b/bin/configuration.rb new file mode 100644 index 0000000..ad942c7 --- /dev/null +++ b/bin/configuration.rb @@ -0,0 +1,12 @@ +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 +end diff --git a/bin/whatever-dl b/bin/whatever-dl index 9a6b8c3..fbc84e9 100755 --- a/bin/whatever-dl +++ b/bin/whatever-dl @@ -31,10 +31,11 @@ require 'pathname' executable = Pathname.new(__FILE__).realpath.to_s $: << File.dirname(executable) + '/../' -# We require the UriUtilities class to handle -# the download of the video URL. -require 'src/uri_utilities' +# 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 @@ -82,23 +83,22 @@ if (__FILE__ == $0) then puts 'Error retrieving video URL.' exit(EXIT_COULDNT_GET_VIDEO_URL) end - - video_uri = URI.parse(video_url) - uu = UriUtilities.new() - + 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 - + # 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. + # any (predictable) exceptions. The wget downloader will + # naturally not report any of these, since it will die in + # its own process. begin - puts "Fetching #{video_url}" - puts "Saving as #{site.get_video_filename()}." - puts "" - uu.download_with_progress_bar(video_uri, site.get_video_filename()) + downloader.download(video_url, site.get_video_filename()) 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) @@ -110,7 +110,7 @@ if (__FILE__ == $0) then end # Write an empty line at the end for aesthetic reasons. - puts "" + puts '' Kernel.exit(EXIT_SUCCESS) end diff --git a/src/downloader.rb b/src/downloader.rb new file mode 100644 index 0000000..eac7c18 --- /dev/null +++ b/src/downloader.rb @@ -0,0 +1,42 @@ +# +# Copyright Michael Orlitzky +# +# http://michael.orlitzky.com/ +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU 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. +# +# http://www.fsf.org/licensing/licenses/gpl.html +# + +class Downloader + + def self.create(download_method) + # Return the subclass corresponding to download_method. + case download_method + when :openuri + return OpenUriDownloader.new() + when :wget + return WgetDownloader.new() + end + end + + + # Abstract + def download(url, outfile) + raise NotImplementedError + end + +end + + +# This is wacky, but seems to work. +require 'src/wget_downloader' +require 'src/open_uri_downloader' diff --git a/src/open_uri_downloader.rb b/src/open_uri_downloader.rb new file mode 100644 index 0000000..44ea037 --- /dev/null +++ b/src/open_uri_downloader.rb @@ -0,0 +1,33 @@ +# +# Copyright Michael Orlitzky +# +# http://michael.orlitzky.com/ +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU 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. +# +# http://www.fsf.org/licensing/licenses/gpl.html +# + +require 'src/uri_utilities' + +class OpenUriDownloader < Downloader + + def download(url, outfile) + uri = URI.parse(url) + + uu = UriUtilities.new() + puts "Fetching #{url}" + puts "Saving as #{outfile}." + puts '' + uu.download_with_progress_bar(uri, outfile) + end + +end diff --git a/src/wget_downloader.rb b/src/wget_downloader.rb new file mode 100644 index 0000000..2cbdd45 --- /dev/null +++ b/src/wget_downloader.rb @@ -0,0 +1,26 @@ +# +# Copyright Michael Orlitzky +# +# http://michael.orlitzky.com/ +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU 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. +# +# http://www.fsf.org/licensing/licenses/gpl.html +# + +class WgetDownloader < Downloader + + def download(url, outfile) + # This one's easy. + Kernel.exec("wget -O \"#{outfile}\" \"#{url}\"") + end + +end -- 2.44.2