+ 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)