]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blobdiff - bin/whatever-dl
Added independent (non-wget) file downloads via open-uri.
[dead/whatever-dl.git] / bin / whatever-dl
index c20b8bd7922e761c0bf553109381425f30830bee..cde9e4dbbaaac2e3eb25790a2767d1c4d59d3c2c 100755 (executable)
 # http://www.fsf.org/licensing/licenses/gpl.html
 #
 
+# We require the UriUtilities class to handle
+# the download of the video URL.
+require 'src/uri_utilities'
+
+
 # All of the website classes are located in one
 # directory, so we can 'require' them automatically.
 Dir.glob('src/websites/*.rb').each do |r|
@@ -26,6 +31,16 @@ Dir.glob('src/websites/*.rb').each do |r|
 end
 
 
+EXIT_SUCCESS = 0
+EXIT_NO_URL = 1
+EXIT_INVALID_URL = 2
+EXIT_COULDNT_GET_VIDEO_URL = 3
+EXIT_OUTPUT_FILE_ALREADY_EXISTS = 4
+EXIT_ERROR_READING_FROM_VIDEO_URL = 5
+EXIT_CONNECTION_REFUSED = 6
+EXIT_HTTP_ERROR = 7
+EXIT_ACCESS_DENIED = 8
+
 # Only actually do something if this script was called
 # directly (i.e. not from the tests).
 if (__FILE__ == $0) then
@@ -33,7 +48,7 @@ if (__FILE__ == $0) then
     # If the user didn't give us a URL, yell
     # at him or her.
     puts 'Usage: whatever-dl <url>'
-    Kernel.exit(1)
+    Kernel.exit(EXIT_NO_URL)
   end
 
   # Check the URL against each website's class.
@@ -51,16 +66,52 @@ if (__FILE__ == $0) then
 
   if site.nil?
     puts 'Invalid URL.'
-    exit(1)
+    exit(EXIT_INVALID_URL)
   end
   
   video_url = site.get_video_url(ARGV[0])
 
   if video_url.nil?
     puts 'Error retrieving video URL.'
-    exit(2)
+    exit(EXIT_COULDNT_GET_VIDEO_URL)
+  end
+
+  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."
+    Kernel.exit(EXIT_OUTPUT_FILE_ALREADY_EXISTS)
+  end
+
+
+  # Attempt to download the file, and rescue and report
+  # any (predictable) exceptions.
+  begin
+    uu.download_with_progress_bar(video_uri, outfile_name)
+  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)
+  rescue Errno:EACCES => e
+    puts "Access denied. Check that you have write permission to the output file/directory. Details: #{e.message}."
+  rescue OpenURI::HTTPError => e
+    puts "An HTTP error occurred while downloading the video file: #{e.message}."
+    Kernel.exit(EXIT_HTTP_ERROR)
   end
 
-  # *classy*
-  Kernel.exec("wget \"#{video_url}\"")
+  Kernel.exit(EXIT_SUCCESS)
 end