Skip to content

Commit

Permalink
Youtube improvements and refactoring
Browse files Browse the repository at this point in the history
Moved spec files

Youtube improvements and refactoring
  • Loading branch information
Kalle Lindström committed Feb 25, 2014
1 parent 0753b8e commit cabd4b6
Show file tree
Hide file tree
Showing 15 changed files with 624 additions and 332 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ Viddl-rb supports the following command line options:
-f, --filter REGEX Filters a video playlist according to the regex (Youtube only right now)
-s, --save-dir DIRECTORY Specifies the directory where videos should be saved
-d, --downloader TOOL Specifies the tool to download with. Supports 'wget', 'curl' and 'net-http'
-q, --quality QUALITY Specifies the video format and resolution in the following way => resolution:extension (e.g. 720:mp4). Currently only supported by the Youtube plugin.
-q, --quality QUALITY Specifies the video format and resolution in the following way: width:height:res (e.g. 1280:720:mp4)
The width, height and resolution may be omitted with a *.
For example, to match any quality with a width of 720 pixels in any format specify --quality *:720:*
-h, --help Displays the help screen
```

Expand Down
26 changes: 18 additions & 8 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,31 @@ require 'rubygems'
require 'bundler/setup'
require 'rake/testtask'

task :default => [:test]
ALL_INTEGRATION = FileList["spec/integration/*.rb"]
ALL_UNIT = FileList["spec/unit/*/*.rb"]

Rake::TestTask.new(:test) do |t|
#t.pattern = "spec/*_spec.rb"
t.test_files = ["spec/lib_spec.rb", "spec/url_extraction_spec.rb", "spec/integration_spec.rb"]
task :default => [:all]

Rake::TestTask.new(:all) do |t|
t.test_files = ALL_INTEGRATION + ALL_UNIT
end

Rake::TestTask.new(:test_unit) do |t|
t.test_files = ALL_UNIT
end

Rake::TestTask.new(:test_integration) do |t|
t.test_files = ALL_INTEGRATION
end

Rake::TestTask.new(:test_lib) do |t|
t.test_files = FileList["spec/lib_spec.rb"]
t.test_files = FileList["spec/integration/lib_spec.rb"]
end

Rake::TestTask.new(:test_extract) do |t|
t.test_files = FileList["spec/url_extraction_spec.rb"]
t.test_files = FileList["spec/integration/url_extraction_spec.rb"]
end

Rake::TestTask.new(:test_integration) do |t|
t.test_files = FileList["spec/integration_spec.rb"]
Rake::TestTask.new(:test_download) do |t|
t.test_files = FileList["spec/integration/download_spec.rb"]
end
2 changes: 1 addition & 1 deletion bin/helper/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_download_queue
plugin.get_urls_and_filenames(url, @params)

rescue ViddlRb::PluginBase::CouldNotDownloadVideoError => e
raise "ERROR: The video could not be downloaded.\n" +
raise "CouldNotDownloadVideoError.\n" +
"Reason: #{e.message}"
rescue StandardError => e
raise "Error while running the #{plugin.name.inspect} plugin. Maybe it has to be updated?\n" +
Expand Down
51 changes: 35 additions & 16 deletions bin/helper/parameter-parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def self.parse_app_parameters(args)
optparse = OptionParser.new do |opts|
opts.banner = "Usage: viddl-rb URL [options]"

opts.on('-h', '--help', 'Display this screen') do
print_help_and_exit(opts)
end

opts.on("-e", "--extract-audio", "Save video audio to file") do
if ViddlRb::UtilityHelper.os_has?("ffmpeg")
options[:extract_audio] = true
Expand Down Expand Up @@ -75,21 +79,18 @@ def self.parse_app_parameters(args)
end

opts.on("-q", "--quality QUALITY",
"Specifies the video format and resolution in the following way => resolution:extension (e.g. 720:mp4)") do |quality|
if match = quality.match(/(\d+):(.*)/)
res = match[1]
ext = match[2]
elsif match = quality.match(/\d+/)
res = match[0]
ext = nil
else
raise OptionParse.InvalidArgument.new("#{quality} is not a valid argument.")
end
options[:quality] = {:extension => ext, :resolution => res}
end

opts.on_tail('-h', '--help', 'Display this screen') do
print_help_and_exit(opts)
"Specifies the video format and resolution in the following way: width:height:res (e.g. 1280:720:mp4). " +
"The width, height and resolution may be omitted with a *. For example, to match any quality with a " +
"width of 720 pixels in any format specify --quality *:720:*") do |quality|

tokens = quality.split(":")
raise OptionParser::InvalidArgument.new("#{quality} is not a valid argument.") unless tokens.size == 3
width, height, ext = tokens
validate_quality_options!(width, height, ext, quality)

options[:quality] = {width: width == "*" ? nil : width.to_i,
height: height == "*" ? nil : height.to_i,
extension: ext == "*" ? nil : ext}
end
end

Expand All @@ -109,7 +110,25 @@ def self.print_help_and_exit(opts)
def self.validate_url!(url)
unless url =~ /^http/
raise OptionParser::InvalidArgument.new(
"please include 'http' with your URL e.g. http://www.youtube.com/watch?v=QH2-TGUlwu4")
"please include 'http' with your URL e.g. http://www.youtube.com/watch?v=QH2-TGUlwu4")
end
end

def self.validate_quality_options!(width, height, extension, quality)
if !width =~ /(\*|\d+)/ || !height =~ /(\*|\d+)/ || !extension =~ /(\*|\w+)/
raise OptionParser::InvalidArgument.new("#{quality} is not a valid argument.")
end
end
end












2 changes: 0 additions & 2 deletions helper/plugin-helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,4 @@ def self.printf(string, *objects)
nil
end
end

end

26 changes: 22 additions & 4 deletions helper/utility-helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,29 @@
module ViddlRb

class UtilityHelper
#loads all plugins in the plugin directory.
#the plugin classes are dynamically added to the ViddlRb module.

# Loads all plugins in the plugin directory.
# The plugin classes are dynamically added to the ViddlRb module.
# A plugin can have helper classes. These classes must exist in a in directory under the
# plugins directory that has the same name as the plugin filename wihouth the .rb extension.
# All classes found in such a directory will dynamically added as inner classes of the
# plugin class.
def self.load_plugins
Dir[File.join(File.dirname(__FILE__), "../plugins/*.rb")].each do |plugin|
ViddlRb.class_eval(File.read(plugin))
plugins_dir = File.join(File.dirname(__FILE__), "../plugins")
plugin_paths = Dir[File.join(plugins_dir, "*.rb")]

plugin_paths.each do |path|
filename = File.basename(path, File.extname(path))
plugin_code = File.read(path)
class_name = plugin_code[/class (\w+) < PluginBase/, 1]
components = Dir[File.join(plugins_dir, filename, "*.rb")]

ViddlRb.class_eval(plugin_code)

components.each do |component|
code = File.read(component)
ViddlRb.const_get(class_name).class_eval(code)
end
end
end

Expand Down
Loading

0 comments on commit cabd4b6

Please sign in to comment.