Created three new classes: Downloader, OpenUriDownloader, and WgetDownloader.
Created a configuration file.
Modified whatever-dl to use the configuration information.
--- /dev/null
+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
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
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)
end
# Write an empty line at the end for aesthetic reasons.
- puts ""
+ puts ''
Kernel.exit(EXIT_SUCCESS)
end
--- /dev/null
+#
+# 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'
--- /dev/null
+#
+# 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
--- /dev/null
+#
+# 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