Module: YoutubeDL::Support

Included in:
YoutubeDL, Runner
Defined in:
lib/youtube-dl/support.rb

Overview

Some support methods and glue logic.

Instance Method Summary collapse

Instance Method Details

#cocaine_line(command, executable_path = nil) ⇒ Cocaine::CommandLine

Helper for doing lines of cocaine (initializing, auto executable stuff, etc)

Parameters:

  • command (String)

    command switches to run

  • executable_path (String) (defaults to: nil)

    executable to run. Defaults to usable youtube-dl.

Returns:

  • (Cocaine::CommandLine)

    initialized Cocaine instance



27
28
29
30
# File 'lib/youtube-dl/support.rb', line 27

def cocaine_line(command, executable_path = nil)
  executable_path = executable_path_for('youtube-dl') if executable_path.nil?
  Cocaine::CommandLine.new(executable_path, command)
end

#quoted(url) ⇒ String

Helper to add quotes to beginning and end of a URL or whatever you want.…

Parameters:

  • url (String)

    Raw URL

Returns:

  • (String)

    Quoted URL



36
37
38
# File 'lib/youtube-dl/support.rb', line 36

def quoted(url)
  "\"#{url}\""
end

#usable_executable_path_for(exe) ⇒ String Also known as: executable_path_for

Returns a usable youtube-dl executable (system or vendor)

Parameters:

  • exe (String)

    Executable to search for

Returns:

  • (String)

    executable path



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/youtube-dl/support.rb', line 8

def usable_executable_path_for(exe)
  system_path = which(exe)
  if system_path.nil?
    # TODO: Search vendor bin for executable before just saying it's there.
    vendor_path = File.absolute_path("#{__FILE__}/../../../vendor/bin/#{exe}")
    File.chmod(775, vendor_path) unless File.executable?(vendor_path) # Make sure vendor binary is executable
    vendor_path
  else
    system_path.strip
  end
end

#which(cmd) ⇒ String

Cross-platform way of finding an executable in the $PATH. Stolen from stackoverflow.com/a/5471032

which('ruby') #=> /usr/bin/ruby

Parameters:

  • cmd (String)

    cmd to search for

Returns:

  • (String)

    full path for the cmd



47
48
49
50
51
52
53
54
55
56
# File 'lib/youtube-dl/support.rb', line 47

def which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    end
  end
  nil
end