-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from mikz/youtube-dl
proper youtube-dl suppport
- Loading branch information
Showing
11 changed files
with
221 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
require 'shellwords' | ||
|
||
module AirPlayer | ||
class YoutubeDl | ||
|
||
attr_reader :path | ||
|
||
def initialize(path = self.class.path || self.class.default_path) | ||
@path = path | ||
|
||
if @path | ||
File.exist?(@path) or raise "youtube-dl could not be found at #{@path}" | ||
end | ||
|
||
@output = '2> /dev/null'.freeze | ||
end | ||
|
||
def enabled? | ||
@path && ! @path.empty? | ||
end | ||
|
||
EXTRACT_EXTRACTOR_AND_URL = %r{ | ||
(?<extractor>.+)\n # extractor name and new line | ||
\s+(:?.+) # the url if the extractor supports it | ||
}x | ||
|
||
GENERIC_EXTRACTOR = 'generic'.freeze | ||
|
||
def supports?(url) | ||
extractors = list_extractors(url).scan(EXTRACT_EXTRACTOR_AND_URL).flatten | ||
extractors.delete(GENERIC_EXTRACTOR) | ||
! extractors.empty? | ||
end | ||
|
||
def list_extractors(*urls) | ||
execute('--list-extractors', *urls) | ||
end | ||
|
||
def get_url(url) | ||
execute('--get-url', url) | ||
end | ||
|
||
def get_filename(url) | ||
execute('--get-filename', url) | ||
end | ||
|
||
def get_title(url) | ||
execute('--get-title', url) | ||
end | ||
|
||
def execute(*args) | ||
return '' unless enabled? | ||
escape = Shellwords.method(:escape) | ||
parts = [ path, args.flat_map(&escape), @output ] | ||
%x`#{parts.flatten.join(' ')}`.strip | ||
end | ||
|
||
class << self | ||
attr_accessor :path | ||
|
||
def enabled? | ||
path && ! path.empty? | ||
end | ||
|
||
def default_path | ||
@default_path ||= `which youtube-dl 2> /dev/null`.strip | ||
end | ||
|
||
def get_url(uri) | ||
if enabled? | ||
new.get_url(uri) | ||
else | ||
uri | ||
end | ||
end | ||
|
||
def supports?(path) | ||
if enabled? | ||
new.supports?(path) | ||
end | ||
end | ||
|
||
def filename(url) | ||
if enabled? | ||
new.get_filename(url) | ||
else | ||
File.basename(url) | ||
end | ||
end | ||
|
||
def get_title(uri) | ||
if enabled? | ||
new.get_title(uri) | ||
else | ||
File.basename(uri) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.