X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=bin%2Fwhatever-dl;h=9a6b8c3e23eabe2b02624fe5a38539b3de340d74;hb=c292aca3372339f92ea6fa8fa563ffed7f4fd19c;hp=47253f13d8bd00eace1ccca43e69592de5948411;hpb=2c835ed7a247ed5639277bc9674b848722ad998d;p=dead%2Fwhatever-dl.git diff --git a/bin/whatever-dl b/bin/whatever-dl index 47253f1..9a6b8c3 100755 --- a/bin/whatever-dl +++ b/bin/whatever-dl @@ -1,4 +1,4 @@ -#!/usr/bin/ruby -w +#!/usr/bin/ruby -wKU # # whatever-dl, a script to download online (web-based) videos. # @@ -19,14 +19,31 @@ # 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 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) + '/../' + # We require the UriUtilities class to handle # the download of the video URL. require 'src/uri_utilities' +# 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('src/websites/*.rb').each do |r| +Dir.glob(websites_pattern).each do |r| require r end @@ -51,25 +68,15 @@ if (__FILE__ == $0) then Kernel.exit(EXIT_NO_URL) end - # Check the URL against each website's class. - # The class will know whether or not the URL - # "belongs" to its website. - - site = nil - - Website.subclasses.each do |w| - if w.owns_url?(ARGV[0]) - site = w.new() - break - end - end + # Factory method. + site = Website.create(ARGV[0]) if site.nil? puts 'Invalid URL.' exit(EXIT_INVALID_URL) end - video_url = site.get_video_url(ARGV[0]) + video_url = site.get_video_url() if video_url.nil? puts 'Error retrieving video URL.' @@ -79,22 +86,8 @@ if (__FILE__ == $0) then video_uri = URI.parse(video_url) uu = UriUtilities.new() - - # Here, we start out with a default file name and - # extension. If UriUtilities can parse a sane filename - # out of the URL, we'll use that. Otherwise, we fall - # back to the default. - outfile_name = 'default.ext' - - if not uu.get_filename(video_uri).nil? - outfile_name = uu.get_filename(video_uri) - else - puts "We couldn't determine the video's filename. Falling back to the default, #{outfile_name}." - end - - - if File.exists?(outfile_name) - puts "Error: output file already exists. Please remove #{outfile_name}, and try again." + 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 @@ -103,7 +96,9 @@ if (__FILE__ == $0) then # any (predictable) exceptions. begin puts "Fetching #{video_url}" - uu.download_with_progress_bar(video_uri, outfile_name) + puts "Saving as #{site.get_video_filename()}." + puts "" + uu.download_with_progress_bar(video_uri, 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) @@ -114,5 +109,8 @@ if (__FILE__ == $0) then Kernel.exit(EXIT_HTTP_ERROR) end + # Write an empty line at the end for aesthetic reasons. + puts "" + Kernel.exit(EXIT_SUCCESS) end