viddl-rb 0.72 → 0.73

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- __viddl-rb:__
1
+ __viddl-rb:__
2
2
  Initially created by Marc Seeger (@rb2k)
3
3
  Repo: http://github.com/rb2k/viddl-rb
4
4
  [![Build Status](https://secure.travis-ci.org/rb2k/viddl-rb.png)](http://travis-ci.org/rb2k/viddl-rb) [![Dependency Status](https://gemnasium.com/rb2k/viddl-rb.png)](https://gemnasium.com/rb2k/viddl-rb)
@@ -7,21 +7,31 @@ __Installation:__
7
7
 
8
8
  gem install viddl-rb
9
9
 
10
- __Usage:__
10
+ __Usage:__
11
11
 
12
12
  Download a video:
13
13
  viddl-rb http://www.youtube.com/watch?v=QH2-TGUlwu4
14
14
 
15
+ Viddl-rb supports the following command line options:
16
+ ```
17
+ -e, --extract-audio Save video audio to file
18
+ -u, --url-only Prints url without downloading
19
+ -t, --title-only Prints title without downloading
20
+ -f, --filter REGEX Filters a video playlist according to the regex (Youtube only right now)
21
+ -s, --save-dir DIRECTORY Specifies the directory where videos should be saved
22
+ -h, --help Displays the help screen
23
+ ```
24
+
15
25
  Download a video and extract the audio:
16
26
  viddl-rb http://www.youtube.com/watch?v=QH2-TGUlwu4 --extract-audio
17
27
 
18
28
  In both cases we'll name the output file according to the video title.
19
29
 
20
30
  Setting the video save directory:
21
- viddl-rb http://vimeo.com/38372260 --save-dir=C:/myvideos
31
+ viddl-rb http://vimeo.com/38372260 --save-dir C:/myvideos
22
32
 
23
33
  The --save-dir option works with both absolute and relative paths (relative based on the directory viddl-rb is run from).
24
- If you want to save to a folder with spaces in it, you have to quote the path like this: --save-dir="C:/my videos"
34
+ If you want to save to a folder with spaces in it, you have to quote the path like this: --save-dir "C:/my videos"
25
35
 
26
36
  __Youtube plugin specifics:__
27
37
 
@@ -32,10 +42,10 @@ Download all videos from a user:
32
42
  viddl-rb http://www.youtube.com/user/tedtalksdirector
33
43
 
34
44
  Filter videos to download from a user/playlist:
35
- viddl-rb http://www.youtube.com/user/tedtalksdirector --filter=internet/i
45
+ viddl-rb http://www.youtube.com/user/tedtalksdirector --filter /internet/i
36
46
 
37
47
  The --filter argument accepts a regular expression and will only download videos where the title matches the regex.
38
- The /i option does a case-insensitive search.
48
+ It uses the same syntax as Ruby regular expression literals do.
39
49
 
40
50
  __Library Usage:__
41
51
 
@@ -13,7 +13,7 @@ class Downloader
13
13
  raise DownloadFailedError, "Download for #{name} failed."
14
14
  else
15
15
  puts "Download for #{name} successful."
16
- AudioHelper.extract(name) if params[:extract_audio]
16
+ ViddlRb::AudioHelper.extract(name, params[:save_dir]) if params[:extract_audio]
17
17
  end
18
18
  end
19
19
  end
@@ -1,67 +1,84 @@
1
1
 
2
2
  # ParameterParser parses the program parameters.
3
3
  # If the parameters are not valid in some way an exception is raised.
4
+ # The exceptions raised by this class are handeled in the bin program.
5
+
4
6
  class ParameterParser
5
7
 
6
8
  DEFAULT_SAVE_DIR = "."
7
9
 
8
10
  #returns a hash with the parameters in it:
9
- # :url => the video url
10
- # :extract_audio => should attempt to extract audio? (true/false)
11
- # :url_only => do not download, only print the urls to stdout
12
- # :title_only => do not download, only print the titles to stdout
13
- # :youtube_filer => a regular expression used ot fileter youtube playlists
14
- # :save_dir => the directory where the videos are saved
15
- def self.parse_app_parameters
16
- check_valid_parameters!
11
+ # :url => the video url
12
+ # :extract_audio => should attempt to extract audio? (true/false)
13
+ # :url_only => do not download, only print the urls to stdout
14
+ # :title_only => do not download, only print the titles to stdout
15
+ # :playlist_filter => a regular expression used to filter playlists
16
+ # :save_dir => the directory where the videos are saved
17
+ def self.parse_app_parameters(args)
17
18
 
18
- params = {}
19
- params[:url] = ARGV.first
20
- params[:extract_audio] = ARGV.include?("--extract-audio")
21
- params[:url_only] = ARGV.include?("--url-only")
22
- params[:title_only] = ARGV.include?("--title-only")
23
- params[:playlist_filter] = get_youtube_filter
24
- params[:save_dir] = get_save_dir
25
- params
26
- end
19
+ # Default option values are set here
20
+ options = {
21
+ :extract_audio => false,
22
+ :url_only => false,
23
+ :title_only => false,
24
+ :playlist_filter => nil,
25
+ :save_dir => DEFAULT_SAVE_DIR
26
+ }
27
27
 
28
- #check if parameters are valid.
29
- #the exceptions raised by this method are caught by the viddl-rb bin utility.
30
- def self.check_valid_parameters!
31
- if ARGV.empty?
32
- raise "Usage: viddl-rb URL [--extract-audio]"
33
- elsif !ARGV.first.match(/^http/)
34
- raise "ERROR: Please include 'http' with your URL e.g. http://www.youtube.com/watch?v=QH2-TGUlwu4"
35
- elsif ARGV.include?("--extract-audio") && !DownloadHelper.os_has?("ffmpeg")
36
- raise "ERROR: To extract audio you need to have ffmpeg on your system"
37
- end
38
- end
28
+ optparse = OptionParser.new do |opts|
29
+ opts.banner = "Usage: viddl-rb URL [options]"
30
+
31
+ opts.on("-e", "--extract-audio", "Save video audio to file") do
32
+ if ViddlRb::UtilityHelper.os_has?("ffmpeg")
33
+ options[:extract_audio] = true
34
+ else
35
+ raise OptionParser::ParseError.new("to extract audio you need to have ffmpeg on your PATH")
36
+ end
37
+ end
38
+
39
+ opts.on("-u", "--url-only", "Prints url without downloading") do
40
+ options[:url_only] = true
41
+ end
39
42
 
40
- #gets the regular expression used to filter youtube playlists.
41
- def self.get_youtube_filter
42
- filter = ARGV.find { |arg| arg =~ /--filter=./ } # --filter= and at least one more character
43
- return nil unless filter
43
+ opts.on("-t", "--title-only", "Prints title without downloading") do
44
+ options[:title_only] = true
45
+ end
44
46
 
45
- ignore_case = filter.include?("/i")
46
- regex = filter[/--filter=(.*?)(?:\/|$)/, 1] # everything up to the first / (could be an empty string)
47
- raise "ERROR: '#{regex}' is not a valid regular expression" unless is_valid_regex?(regex)
48
- Regexp.new(regex, ignore_case)
47
+ opts.on("-f", "--filter REGEX", Regexp, "Filters a video playlist according to the regex") do |regex|
48
+ options[:filter] = regex
49
+ end
50
+
51
+ opts.on("-s", "--save-dir DIRECTORY", "Specifies the directory where videos should be saved") do |dir|
52
+ if File.directory?(dir)
53
+ options[:save_dir] = dir
54
+ else
55
+ raise OptionParser::InvalidArgument.new("'#{dir}' is not a valid directory")
56
+ end
57
+ end
58
+
59
+ opts.on_tail('-h', '--help', 'Display this screen') do
60
+ print_help_and_exit(opts)
61
+ end
62
+ end
63
+
64
+ optparse.parse!(args) # removes all options from args
65
+ print_help_and_exit(optparse) if args.empty? # exit if no video url
66
+ url = validate_url(args.first) # the url is the only element left
67
+ options[:url] = url
68
+ options
49
69
  end
50
70
 
51
- #checks if the string is a valid regex (for example "*****" is not)
52
- def self.is_valid_regex?(regex)
53
- Regexp.compile(regex)
54
- rescue RegexpError
55
- false
71
+ def self.print_help_and_exit(opts)
72
+ puts opts
73
+ exit(0)
56
74
  end
57
-
58
- #gets the directory used for saving videos in.
59
- def self.get_save_dir
60
- save_dir = ARGV.find { |arg| arg =~ /--save-dir=./ }
61
- return DEFAULT_SAVE_DIR unless save_dir
62
75
 
63
- dir = save_dir[/--save-dir=(.+)/, 1]
64
- raise "ERROR: '#{dir}' is not a valid directory or does not exist" unless File.directory?(dir)
65
- dir
76
+ def self.validate_url(url)
77
+ if url =~ /^http/
78
+ url
79
+ else
80
+ raise OptionParser::InvalidArgument.new(
81
+ "please include 'http' with your URL e.g. http://www.youtube.com/watch?v=QH2-TGUlwu4")
82
+ end
66
83
  end
67
84
  end
@@ -8,6 +8,7 @@ require "mechanize"
8
8
  require "cgi"
9
9
  require "open-uri"
10
10
  require "open3"
11
+ require "optparse"
11
12
 
12
13
  require "driver.rb"
13
14
  require "downloader.rb"
@@ -20,7 +21,7 @@ require "utility-helper.rb"
20
21
  begin
21
22
  # params is a hash with keys for each of the parameters passed in.
22
23
  # see helper/parameter-parser.rb for what those keys are.
23
- params = ParameterParser.parse_app_parameters
24
+ params = ParameterParser.parse_app_parameters(ARGV)
24
25
 
25
26
  puts "Loading Plugins"
26
27
  ViddlRb::UtilityHelper.load_plugins
@@ -32,8 +33,13 @@ begin
32
33
  app = Driver.new(params)
33
34
  app.start # starts the download process
34
35
 
36
+ rescue OptionParser::ParseError => e
37
+ puts "Error: #{e.message}"
38
+ exit(1)
39
+
35
40
  rescue StandardError => e
36
- puts "#{e.message}"
41
+ puts "Error: #{e.message}"
42
+ puts "\nBacktrace:"
43
+ puts e.backtrace
37
44
  exit(1)
38
45
  end
39
-
@@ -3,7 +3,7 @@ module ViddlRb
3
3
  # This class is responsible for extracting audio from video files using ffmpeg.
4
4
  class AudioHelper
5
5
 
6
- def self.extract(file_path)
6
+ def self.extract(file_path, save_dir)
7
7
  no_ext_filename = file_path.split('.')[0..-1][0]
8
8
  #capture stderr because ffmpeg expects an output param and will error out
9
9
  puts "Gathering information about the downloaded file."
@@ -32,7 +32,7 @@ module ViddlRb
32
32
  puts "Unknown audio format: #{audio_format}, using name as extension: '.#{audio_format}'."
33
33
  output_extension = audio_format
34
34
  end
35
- output_filename = "#{no_ext_filename}.#{output_extension}"
35
+ output_filename = File.join(save_dir, "#{no_ext_filename}.#{output_extension}")
36
36
  if File.exist?(output_filename)
37
37
  puts "Audio file seems to exist already, removing it before extraction."
38
38
  File.delete(output_filename)
@@ -46,4 +46,3 @@ module ViddlRb
46
46
  end
47
47
 
48
48
  end
49
-
@@ -29,12 +29,11 @@ module ViddlRb
29
29
  file_path = File.expand_path(File.join(save_dir, file_name))
30
30
  #Some providers seem to flake out every now end then
31
31
  amount_of_retries.times do |i|
32
- if os_has?("wget")
32
+ if UtilityHelper.os_has?("wget")
33
33
  puts "using wget"
34
34
  `wget \"#{file_uri}\" -O #{file_path.inspect}`
35
- elsif os_has?("curl")
35
+ elsif UtilityHelper.os_has?("curl")
36
36
  puts "using curl"
37
- #require "pry"; binding.pry; exit
38
37
  #-L means: follow redirects, We set an agent because Vimeo seems to want one
39
38
  `curl -A 'Wget/1.8.1' --retry 10 --retry-delay 5 --retry-max-time 4 -L \"#{file_uri}\" -o #{file_path.inspect}`
40
39
  else
@@ -54,33 +53,5 @@ module ViddlRb
54
53
  $? == 0
55
54
  end
56
55
 
57
- #checks to see whether the os has a certain utility like wget or curl
58
- #`` returns the standard output of the process
59
- #system returns the exit code of the process
60
- def self.os_has?(utility)
61
- windows = ENV['OS'] =~ /windows/i
62
-
63
- unless windows # if os is not Windows
64
- `which #{utility}`.include?(utility)
65
- else
66
- if has_where?
67
- system("where /q #{utility}") #/q is the quiet mode flag
68
- else
69
- begin #as a fallback we just run the utility itself
70
- system(utility)
71
- rescue Errno::ENOENT
72
- false
73
- end
74
- end
75
- end
76
- end
77
-
78
- #checks if Windows has the where utility (Server 2003 and later)
79
- #system only return nil if the command is not found
80
- def self.has_where?
81
- !system("where /q where").nil?
82
- end
83
56
  end
84
-
85
57
  end
86
-
@@ -10,7 +10,27 @@ module ViddlRb
10
10
  ViddlRb.class_eval(File.read(plugin))
11
11
  end
12
12
  end
13
- end
14
13
 
15
- end
14
+ #checks to see whether the os has a certain utility like wget or curl
15
+ #`` returns the standard output of the process
16
+ #system returns the exit code of the process
17
+ def self.os_has?(utility)
18
+ windows = ENV['OS'] =~ /windows/i
16
19
 
20
+ unless windows
21
+ `which #{utility}`.include?(utility)
22
+ else
23
+ if !system("where /q where").nil? #if Windows has the where utility
24
+ system("where /q #{utility}") #/q is the quiet mode flag
25
+ else
26
+ begin #as a fallback we just run the utility itself
27
+ system(utility)
28
+ rescue Errno::ENOENT
29
+ false
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ end
36
+ end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: viddl-rb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 155
4
+ hash: 153
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 72
9
- version: "0.72"
8
+ - 73
9
+ version: "0.73"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Marc Seeger
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2012-10-03 00:00:00 +02:00
17
+ date: 2012-10-05 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -112,7 +112,6 @@ files:
112
112
  - plugins/veoh.rb
113
113
  - plugins/vimeo.rb
114
114
  - plugins/youtube.rb
115
- - CHANGELOG.txt
116
115
  - Gemfile
117
116
  - Gemfile.lock
118
117
  - Rakefile
@@ -1,14 +0,0 @@
1
- === 0.6
2
- * Soundcloud support
3
- * Removed megavideo plugin
4
-
5
- === 0.4.x
6
- * New Features
7
- * Use wget or curl (if present)
8
-
9
- * Bugfixes
10
- * Fixed Vimeo Plugin
11
-
12
- === 0.3
13
- * New Features
14
- * added megavideo plugin