Class: AirPlayer::YoutubeDl

Inherits:
Object
  • Object
show all
Defined in:
lib/airplayer/youtube_dl.rb

Constant Summary collapse

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = self.class.path || self.class.default_path) ⇒ YoutubeDl



8
9
10
11
12
13
14
15
16
# File 'lib/airplayer/youtube_dl.rb', line 8

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

Class Attribute Details

.pathObject

Returns the value of attribute path.



59
60
61
# File 'lib/airplayer/youtube_dl.rb', line 59

def path
  @path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/airplayer/youtube_dl.rb', line 6

def path
  @path
end

Class Method Details

.default_pathObject



65
66
67
# File 'lib/airplayer/youtube_dl.rb', line 65

def default_path
  @default_path ||= `which youtube-dl 2> /dev/null`.strip
end

.enabled?Boolean



61
62
63
# File 'lib/airplayer/youtube_dl.rb', line 61

def enabled?
  path && ! path.empty?
end

.filename(url) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/airplayer/youtube_dl.rb', line 83

def filename(url)
  if enabled?
    new.get_filename(url)
  else
    File.basename(url)
  end
end

.get_title(uri) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/airplayer/youtube_dl.rb', line 91

def get_title(uri)
  if enabled?
    new.get_title(uri)
  else
    File.basename(uri)
  end
end

.get_url(uri) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/airplayer/youtube_dl.rb', line 69

def get_url(uri)
  if enabled?
    new.get_url(uri)
  else
    uri
  end
end

.supports?(path) ⇒ Boolean



77
78
79
80
81
# File 'lib/airplayer/youtube_dl.rb', line 77

def supports?(path)
  if enabled?
    new.supports?(path)
  end
end

Instance Method Details

#enabled?Boolean



18
19
20
# File 'lib/airplayer/youtube_dl.rb', line 18

def enabled?
  @path && ! @path.empty?
end

#execute(*args) ⇒ Object



51
52
53
54
55
56
# File 'lib/airplayer/youtube_dl.rb', line 51

def execute(*args)
  return '' unless enabled?
  escape = Shellwords.method(:escape)
  parts = [ path, args.flat_map(&escape), @output ]
  %x`#{parts.flatten.join(' ')}`.strip
end

#get_filename(url) ⇒ Object



43
44
45
# File 'lib/airplayer/youtube_dl.rb', line 43

def get_filename(url)
  execute('--get-filename', url)
end

#get_title(url) ⇒ Object



47
48
49
# File 'lib/airplayer/youtube_dl.rb', line 47

def get_title(url)
  execute('--get-title', url)
end

#get_url(url) ⇒ Object



39
40
41
# File 'lib/airplayer/youtube_dl.rb', line 39

def get_url(url)
  execute('--get-url', url)
end

#list_extractors(*urls) ⇒ Object



35
36
37
# File 'lib/airplayer/youtube_dl.rb', line 35

def list_extractors(*urls)
  execute('--list-extractors', *urls)
end

#supports?(url) ⇒ Boolean



29
30
31
32
33
# File 'lib/airplayer/youtube_dl.rb', line 29

def supports?(url)
  extractors = list_extractors(url).scan(EXTRACT_EXTRACTOR_AND_URL).flatten
  extractors.delete(GENERIC_EXTRACTOR)
  ! extractors.empty?
end