Module: YtDlp::Support

Included in:
YtDlp, Runner
Defined in:
lib/yt-dlp/support.rb

Overview

Some support methods and glue logic.

Instance Method Summary collapse

Instance Method Details

#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



38
39
40
# File 'lib/yt-dlp/support.rb', line 38

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

#terrapin_line(command, executable_path = nil) ⇒ Terrapin::CommandLine

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

Parameters:

  • command (String)

    command switches to run

  • executable_path (String) (defaults to: nil)

    executable to run. Defaults to usable yt-dlp.

Returns:

  • (Terrapin::CommandLine)

    initialized Terrapin instance



29
30
31
32
# File 'lib/yt-dlp/support.rb', line 29

def terrapin_line(command, executable_path = nil)
  executable_path = executable_path_for('yt-dlp') if executable_path.nil?
  Terrapin::CommandLine.new(executable_path, command)
end

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

Returns a usable yt-dlp executable (system or vendor)

Parameters:

  • exe (String)

    Executable to search for

Returns:

  • (String)

    executable path



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/yt-dlp/support.rb', line 10

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



49
50
51
52
53
54
55
56
57
58
# File 'lib/yt-dlp/support.rb', line 49

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