]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/commitdiff
Allow the downloaders to take advantage of the websites' headers.
authorMichael Orlitzky <michael@orlitzky.com>
Tue, 25 Jan 2011 02:07:08 +0000 (21:07 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Tue, 25 Jan 2011 02:07:08 +0000 (21:07 -0500)
bin/whatever-dl
src/downloader.rb
src/open_uri_downloader.rb
src/uri_utilities.rb
src/wget_downloader.rb

index 448db85289b0dcd0009d4c448184c5945d155ba9..f042b21b80e55f1d76dd0b74a40c91d8733ed645 100755 (executable)
@@ -135,6 +135,7 @@ if (__FILE__ == $0) then
   begin
     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.'
index 742e7928d371d4f01299c0330f50503e50ab2a7a..b9921e8ead9a999412d2061098c473afc834ebfc 100644 (file)
@@ -30,7 +30,7 @@ class Downloader
 
   
   # Abstract
-  def download(url, outfile, continue=false)
+  def download(url, outfile, headers = {}, continue = false)
     raise NotImplementedError
   end
   
index 06c9b3d3b26d9b46ca5b070402bd3f7ce494bbd6..e55c0fa7d0e60635b29b1d27b2f843f27da2d873 100644 (file)
@@ -20,7 +20,7 @@ require 'src/uri_utilities'
 
 class OpenUriDownloader < Downloader
 
-  def download(url, outfile, continue=false)
+  def download(url, outfile, headers = {}, continue = false)
     if File.exists?(outfile)
       raise IOError.new("Output file already exists. Please remove #{outfile}, and try again.")
     end
@@ -31,7 +31,7 @@ class OpenUriDownloader < Downloader
     puts "Fetching #{url}"
     puts "Saving as #{outfile}."
     puts ''
-    uu.download_with_progress_bar(uri, outfile)
+    uu.download_with_progress_bar(uri, outfile, headers)
   end
   
 end
index df09619fce99cf8adaaaaed4f3322eb9cba8a151..dcff2fe11c4ed330cf8ada0eb5df5f8d4acad371 100644 (file)
@@ -28,14 +28,14 @@ class UriUtilities
   # Download the given URI object to <outfile_name>.
   # Should use the progress_proc parameter to show
   # a progress bar using the Ruby/ProgressBar library.
-  def download_with_progress_bar(uri, outfile_name)
+  def download_with_progress_bar(uri, outfile_name, headers = {})
     # We wrap the whole thing in a begin/rescue so that
     # we can clean up afterwards in case of an error.
     begin
       File.open(outfile_name, 'wb') do |outfile|
         pbar = nil
-        uri.open(:content_length_proc => lambda {|content_length|
-                   if content_length && (0 < content_length)                   
+        uri.open({:content_length_proc => lambda {|content_length|
+                   if content_length && (0 < content_length)
                      pbar = ProgressBar.new("Download", content_length)
                      pbar.instance_eval { @bar_mark = '=' }
                      pbar.file_transfer_mode
@@ -43,9 +43,10 @@ class UriUtilities
                  },
                  :progress_proc => lambda {|size|
                    pbar.set(size) if pbar
-                 }) do |video_file|
+                   }
+                 }.merge!(headers)) do |video_file|
           outfile.write(video_file.read)
-        end      
+        end
       end
 
       # Toss out an empty line to get rid of the progress bar.
@@ -63,5 +64,5 @@ class UriUtilities
       raise(e)
     end
   end
-  
+
 end
index 825ee12b9865f556733e4d05078036990d068fff..ce4c760b5f2784c67db4ee2fcf2fe1951bb252da 100644 (file)
@@ -18,7 +18,7 @@
 
 class WgetDownloader < Downloader
 
-  def download(url, outfile, continue=false)
+  def download(url, outfile, headers = {}, continue = false)
     if (continue == false and File.exists?(outfile))
       raise IOError.new("Output file already exists. Please remove #{outfile}, and try again. If this is a partially-downloaded file, you can use the --continue flag to pick up where it left off.")
     end
@@ -26,7 +26,11 @@ class WgetDownloader < Downloader
     options = ''
 
     if continue == true
-      options += '--continue'
+      options += '--continue '
+    end
+
+    headers.each_key do |key|
+      options += "--header '#{key}: #{headers[key]}' "
     end
 
     # This one's easy.