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.'
# Abstract
- def download(url, outfile, continue=false)
+ def download(url, outfile, headers = {}, continue = false)
raise NotImplementedError
end
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
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
# 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
},
: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.
raise(e)
end
end
-
+
end
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
options = ''
if continue == true
- options += '--continue'
+ options += '--continue '
+ end
+
+ headers.each_key do |key|
+ options += "--header '#{key}: #{headers[key]}' "
end
# This one's easy.