youtube-dl.rb 0.0.5 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6f871ed15e269c05860f9784244e7b8a8b50f233
4
- data.tar.gz: de5a4e5606cba55abaedb53936d42a1099ccdef9
3
+ metadata.gz: dc95699d01465f5cb0881852a1e8d37e1a9c0794
4
+ data.tar.gz: ab8b547a79cfa7a04ac8e3c56ab46da1f7df31dd
5
5
  SHA512:
6
- metadata.gz: 7fadfe8fca0a98fd27690767f657e0b7e9174c725b3a8b29846dbfd4c7cc167165dad6115cd352f76ef8b88709a48cce9e806f2e344bd4215eb79c2c66fc2c2b
7
- data.tar.gz: 049911e6cbea32b12ff588d19f881814ad42f5ee4ce726c9984e0c2d93b51fcadb05a9fd0189b88b00ba78c548c188006fde20a888129893f5ea0846e7590b4e
6
+ metadata.gz: f4620ff0634fd5cfad55178b623ccc267e57576f4ab5f79fb8f1f967cd926c3f9e3062f5b26fd994de52c1536813c94ec5be2c0f33fecbc1d8f70d9f3fa403e9
7
+ data.tar.gz: 5f3f979b7fa7ea34cdf7f170d5a2e2466502ad753704baa11a065016e0ddac735bbab08fbfdacd2d7c0403c95651f02c7d6bde175841d5d473a78d1aa7ae07f6
@@ -1,9 +1,11 @@
1
1
  require 'youtube-dl/version'
2
+ require 'youtube-dl/support'
2
3
  require 'youtube-dl/options'
3
4
  require 'youtube-dl/runner'
4
5
 
5
6
  module YoutubeDL
6
7
  extend self
8
+ extend Support
7
9
 
8
10
  # Downloads given array of URLs with any options passed
9
11
  #
@@ -20,4 +22,12 @@ module YoutubeDL
20
22
  end
21
23
 
22
24
  alias_method :get, :download
25
+
26
+ def extractors
27
+ Cocaine::CommandLine.new(usable_executable_path_for('youtube-dl'), '--list-extractors').run.split("\n")
28
+ end
29
+
30
+ def binary_version
31
+ Cocaine::CommandLine.new(usable_executable_path_for('youtube-dl'), '--version').run.chomp
32
+ end
23
33
  end
@@ -54,14 +54,19 @@ module YoutubeDL
54
54
  end
55
55
  end
56
56
 
57
- # Symbolizes keys in the option store
58
- def symbolize_keys!
59
- @store.keys.each do |key_name|
60
- unless key_name.is_a? Symbol
61
- @store[key_name.to_sym] = @store[key_name]
62
- @store.delete(key_name)
63
- end
64
- end
57
+ # Symbolizes and sanitizes keys in the option store
58
+ def sanitize_keys!
59
+ # Symbolize
60
+ manipulate_keys! { |key_name| key_name.is_a?(Symbol) ? key_name : key_name.to_sym }
61
+
62
+ # Underscoreize
63
+ manipulate_keys! { |key_name| key_name.to_s.tr('-', '_').to_sym }
64
+ end
65
+
66
+ def sanitize_keys
67
+ safe_copy = self.dup
68
+ safe_copy.sanitize_keys!
69
+ safe_copy
65
70
  end
66
71
 
67
72
  private
@@ -72,5 +77,15 @@ module YoutubeDL
72
77
  def paramize(key)
73
78
  key.to_s.tr("_", '-')
74
79
  end
80
+
81
+ def manipulate_keys!(&block)
82
+ @store.keys.each do |old_name|
83
+ new_name = block.call(old_name)
84
+ unless new_name == old_name
85
+ @store[new_name] = @store[old_name]
86
+ @store.delete(old_name)
87
+ end
88
+ end
89
+ end
75
90
  end
76
91
  end
@@ -2,6 +2,8 @@ require 'cocaine'
2
2
 
3
3
  module YoutubeDL
4
4
  class Runner
5
+ include YoutubeDL::Support
6
+
5
7
  attr_accessor :url
6
8
  attr_accessor :options
7
9
  attr_accessor :executable_path
@@ -13,7 +15,7 @@ module YoutubeDL
13
15
  def initialize(url, options=YoutubeDL::Options.new)
14
16
  @url = url
15
17
  @options = YoutubeDL::Options.new(options.to_hash)
16
- @executable_path = usable_executable_path
18
+ @executable_path = usable_executable_path_for('youtube-dl')
17
19
  end
18
20
 
19
21
  # Returns Cocaine's runner engine
@@ -42,6 +44,11 @@ module YoutubeDL
42
44
  def run
43
45
  cocaine_line(options_to_commands).run(@options.store)
44
46
  end
47
+ alias_method :download, :run
48
+
49
+ def formats
50
+ parse_format_output(cocaine_line("--list-formats #{quoted(url)}").run)
51
+ end
45
52
 
46
53
  private
47
54
 
@@ -50,17 +57,21 @@ module YoutubeDL
50
57
  # @return [String] commands ready to do cocaine
51
58
  def options_to_commands
52
59
  commands = []
53
- options.each_paramized_key do |key, paramized_key|
60
+ options.sanitize_keys.each_paramized_key do |key, paramized_key|
54
61
  if options[key].to_s == 'true'
55
62
  commands.push "--#{paramized_key}"
56
63
  else
57
64
  commands.push "--#{paramized_key} :#{key}"
58
65
  end
59
66
  end
60
- commands.push url
67
+ commands.push quoted(url)
61
68
  commands.join(' ')
62
69
  end
63
70
 
71
+ def quoted(url)
72
+ "\"#{url}\""
73
+ end
74
+
64
75
  # Helper for doing lines of cocaine (initializing, auto executable stuff, etc)
65
76
  #
66
77
  # @param command [String] command switches to run
@@ -69,18 +80,20 @@ module YoutubeDL
69
80
  Cocaine::CommandLine.new(@executable_path, command)
70
81
  end
71
82
 
72
- # Returns a usable executable (system or vendor)
83
+ # Do you like voodoo?
73
84
  #
74
- # @return [String] youtube-dl executable path
75
- def usable_executable_path
76
- system_path = `which youtube-dl`
77
- if $?.exitstatus == 0
78
- system_path.strip
79
- else
80
- vendor_path = File.absolute_path("#{__FILE__}/../../../vendor/bin/youtube-dl")
81
- File.chmod(775, vendor_path) unless File.executable?(vendor_path) # Make sure vendor binary is executable
82
- vendor_path
85
+ # @param format_output [String] output from youtube-dl --list-formats
86
+ # @return [Array] Magic.
87
+ def parse_format_output(format_output)
88
+ # WARNING: This shit won't be documented or even properly tested. It's almost 3 in the morning and I have no idea what I'm doing.
89
+ this_shit = []
90
+ format_output.slice(format_output.index('format code')..-1).split("\n").each do |line|
91
+ a = {}
92
+ a[:format_code], a[:extension], a[:resolution], a[:note] = line.scan(/\A(\d+)\s+(\w+)\s+(\S+)\s(.*)/)[0]
93
+ this_shit.push a
83
94
  end
95
+ this_shit.shift
96
+ this_shit.map { |gipo| gipo[:note].strip!; gipo }
84
97
  end
85
98
  end
86
99
  end
@@ -0,0 +1,20 @@
1
+ module YoutubeDL
2
+ module Support
3
+ # Some support methods and glue logic.
4
+
5
+ # Returns a usable youtube-dl executable (system or vendor)
6
+ #
7
+ # @param exe [String] Executable to search for
8
+ # @return [String] executable path
9
+ def usable_executable_path_for(exe)
10
+ system_path = `which #{exe} 2> /dev/null` # This will currently only work on Unix systems. TODO: Add Windows support
11
+ if $?.exitstatus == 0 # $? is an object with information on that last command run with backticks.
12
+ system_path.strip
13
+ else
14
+ vendor_path = File.absolute_path("#{__FILE__}/../../../vendor/bin/#{exe}")
15
+ File.chmod(775, vendor_path) unless File.executable?(vendor_path) # Make sure vendor binary is executable
16
+ vendor_path
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module YoutubeDL
2
- VERSION = "0.0.5"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -5,18 +5,23 @@ else
5
5
  require "codeclimate-test-reporter"
6
6
  CodeClimate::TestReporter.start
7
7
  end
8
+
9
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
10
+
8
11
  gem 'minitest'
9
12
  require 'minitest/autorun'
10
13
  require 'minitest/spec'
11
14
  require 'purdytest' # minitest-colorize is broken in minitest version 5
12
15
  require 'pry'
16
+ require 'fileutils'
13
17
 
14
- require_relative '../lib/youtube-dl.rb'
18
+ require 'youtube-dl'
15
19
 
16
- TEST_URL = "https://www.youtube.com/watch?v=gvdf5n-zI14"
20
+ TEST_URL = "https://www.youtube.com/watch?feature=endscreen&v=gvdf5n-zI14"
17
21
  TEST_URL2 = "https://www.youtube.com/watch?v=Mt0PUjh-nDM"
18
22
  TEST_FILENAME = "nope.avi.mp4"
19
23
  TEST_FORMAT = "5"
24
+ TEST_GLOB = "nope*"
20
25
 
21
26
  def remove_downloaded_files
22
27
  Dir.glob("**/nope*").each do |nope|
@@ -7,8 +7,8 @@ describe YoutubeDL::Options do
7
7
 
8
8
  it 'should symbolize option keys' do
9
9
  @options.store['key'] = "value"
10
- @options.symbolize_keys!
11
- assert @options.store == {:key => 'value'}
10
+ @options.sanitize_keys!
11
+ assert_equal({key: 'value'}, @options.store)
12
12
  end
13
13
 
14
14
  it 'should be able to set options with method_missing' do
@@ -64,4 +64,20 @@ describe YoutubeDL::Options do
64
64
  assert_equal 'some-key', paramized_key
65
65
  end
66
66
  end
67
+
68
+ it 'should convert hyphens to underscores in keys' do # See issue #9
69
+ @options.store[:"hyphenated-key"] = 'value'
70
+ @options.sanitize_keys!
71
+ assert_equal({hyphenated_key: 'value'}, @options.to_h)
72
+ end
73
+
74
+ it 'should not modify the original by calling sanitize_keys without bang' do
75
+ @options.store['some-key'] = "some_value"
76
+ refute_equal @options.sanitize_keys, @options
77
+ end
78
+
79
+ it 'should return instance of Options when calling sanitize_keys' do
80
+ @options.store['some-key'] = "some_value"
81
+ assert_instance_of YoutubeDL::Options, @options.sanitize_keys
82
+ end
67
83
  end
@@ -57,4 +57,27 @@ describe YoutubeDL::Runner do
57
57
  assert_includes r.to_command, "--some-key"
58
58
  assert_includes r.to_command, "--another-key"
59
59
  end
60
+
61
+ it 'might list formats' do
62
+ formats = @runner.formats
63
+ assert_instance_of Array, formats
64
+ assert_instance_of Hash, formats.first
65
+ assert_equal 4, formats.first.size
66
+ [:format_code, :resolution, :extension, :note].each do |key|
67
+ assert_includes formats.first, key
68
+ assert_includes formats.last, key
69
+ end
70
+ end
71
+
72
+ it 'should handle strangely-formatted options correctly' do # See issue #9
73
+ options = {
74
+ format: 'bestaudio',
75
+ :"prefer-ffmpeg" => "true",
76
+ :"extract-audio" => true,
77
+ :"audio-format" => "mp3"
78
+ }
79
+
80
+ @runner.options = YoutubeDL::Options.new(options)
81
+ assert_match /youtube-dl --format 'bestaudio' --prefer-ffmpeg --extract-audio --audio-format 'mp3'/, @runner.to_command
82
+ end
60
83
  end
@@ -0,0 +1,36 @@
1
+ require_relative '../test_helper'
2
+
3
+ TestKlass = Class.new do
4
+ include YoutubeDL::Support
5
+
6
+ def executable_path
7
+ usable_executable_path_for 'youtube-dl'
8
+ end
9
+ end
10
+
11
+ describe YoutubeDL::Support do
12
+ before do
13
+ @klass = TestKlass.new
14
+ end
15
+
16
+ describe '#usable_executable_path' do
17
+ it 'should detect system executable' do
18
+ vendor_bin = File.join(Dir.pwd, 'vendor', 'bin', 'youtube-dl')
19
+ Dir.mktmpdir do |tmpdir|
20
+ FileUtils.cp vendor_bin, tmpdir
21
+
22
+ old_path = ENV["PATH"]
23
+ ENV["PATH"] = "#{tmpdir}:#{old_path}"
24
+
25
+ usable_path = @klass.usable_executable_path_for('youtube-dl')
26
+ assert_match usable_path, "#{tmpdir}/youtube-dl"
27
+
28
+ ENV["PATH"] = old_path
29
+ end
30
+ end
31
+
32
+ it 'should not have a newline char in the executable_path' do
33
+ assert_match /youtube-dl\z/, @klass.executable_path
34
+ end
35
+ end
36
+ end
@@ -1,17 +1,41 @@
1
1
  require_relative '../test_helper'
2
2
 
3
3
  describe YoutubeDL do
4
- after do
5
- remove_downloaded_files
4
+ describe '.download' do
5
+ after do
6
+ remove_downloaded_files
7
+ end
8
+
9
+ it 'should download videos' do
10
+ YoutubeDL.get TEST_URL, output: TEST_FILENAME, format: TEST_FORMAT
11
+ assert File.exist? TEST_FILENAME
12
+ end
13
+
14
+ it 'should download multiple videos' do
15
+ YoutubeDL.download [TEST_URL, TEST_URL2]
16
+ assert_equal Dir.glob('nope*').length, 2
17
+ end
6
18
  end
7
19
 
8
- it 'should download videos' do
9
- YoutubeDL.get TEST_URL, output: TEST_FILENAME, format: TEST_FORMAT
10
- assert File.exist? TEST_FILENAME
20
+ describe '.extractors' do
21
+ it 'should return an Array of Strings' do
22
+ extractors = YoutubeDL.extractors
23
+ assert_instance_of Array, extractors
24
+ assert_instance_of String, extractors.first
25
+ end
11
26
  end
12
27
 
13
- it 'should download multiple videos' do
14
- YoutubeDL.download [TEST_URL, TEST_URL2]
15
- assert_equal Dir.glob('nope*').length, 2
28
+ describe '.binary_version' do
29
+ before do
30
+ @version = YoutubeDL.binary_version
31
+ end
32
+
33
+ it 'should return a string' do
34
+ assert_instance_of String, @version
35
+ end
36
+
37
+ it 'should be a specific format with no newlines' do
38
+ assert_match /\d+.\d+.\d+\z/, @version
39
+ end
16
40
  end
17
41
  end
Binary file
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: youtube-dl.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - sapslaj
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-08-25 00:00:00.000000000 Z
12
+ date: 2015-08-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cocaine
@@ -126,10 +126,12 @@ files:
126
126
  - lib/youtube-dl.rb
127
127
  - lib/youtube-dl/options.rb
128
128
  - lib/youtube-dl/runner.rb
129
+ - lib/youtube-dl/support.rb
129
130
  - lib/youtube-dl/version.rb
130
131
  - test/test_helper.rb
131
132
  - test/youtube-dl/options_test.rb
132
133
  - test/youtube-dl/runner_test.rb
134
+ - test/youtube-dl/support_test.rb
133
135
  - test/youtube-dl/youtube-dl_test.rb
134
136
  - vendor/bin/youtube-dl
135
137
  - vendor/bin/youtube-dl.exe
@@ -162,4 +164,5 @@ test_files:
162
164
  - test/test_helper.rb
163
165
  - test/youtube-dl/options_test.rb
164
166
  - test/youtube-dl/runner_test.rb
167
+ - test/youtube-dl/support_test.rb
165
168
  - test/youtube-dl/youtube-dl_test.rb