module Subs

Top-level namespace for the gem.

Constants

Movie

Represent a movie.

SUB_EXTENSIONS

Subtitle extensions to search for.

SearchResult

Represents a generic search result for a subtitle

VERSION
VIDEO_EXTENSIONS

Video extensions to search for.

Public Class Methods

build_subtitle_path(path, language = nil, ext = '.srt') click to toggle source

Uses the path of a video file to create a path to a matching subtitle file.

@param path [String] The path to the video file. @param language [Language] A language for applying a 3-letter language suffix to the file, or `nil` to omit suffix. @param ext [String] The language file extension, including leading dot.

@return [String] The created subtitle path.

# File lib/subs.rb, line 182
def self.build_subtitle_path(path, language = nil, ext = '.srt')
  dir = File.dirname(path)
  base = File.basename(path, File.extname(path))
  if language
    File.join(dir, "#{base}.#{language.alpha3}#{ext}")
  else
    File.join(dir, "#{base}#{ext}")
  end
end
create_log(io = STDOUT, verbosity = :info) click to toggle source

Creates the logger with the specified output stream and verbosity level.

@param io [IO] An IO instance that can be written to. @param verbosity [:info|:warn|:error|:fatal|:debug] The logger verbosity level.

@return [Logger] the created Logger instance.

# File lib/subs.rb, line 76
def self.create_log(io = STDOUT, verbosity = :info)
  unless @log
    require 'logger'
    @log = Logger.new(io)
    @log.formatter = proc do |severity, datetime, _, msg|
      "[%s] %s -- %s\n" % [datetime.strftime('%Y-%m-%d %H:%M:%S'), severity, msg]
    end
    @log.level = case verbosity
    when :warn then Logger::WARN
    when :error then Logger::ERROR
    when :fatal then Logger::FATAL
    when :debug then Logger::DEBUG
    else Logger::INFO
    end
  end
  @log
end
log() click to toggle source

@return [Logger] the logger instance for the module.

# File lib/subs.rb, line 96
def self.log
  @log ||= create_log(STDOUT)
end
query_string(**params) click to toggle source

Creates a query string to be used within a URI based on specified parameters.

@param params [Hash<Symbol, Object>] A hash of keyword arguments that are used to build the query.

@return [String] The constructed query string.

# File lib/subs.rb, line 149
def self.query_string(**params)
  query = ''
  params.each_pair do |key, value|
    next unless value
    query << (query.size.zero? ? '?' : '&')
    query << CGI.escape(key.to_s)
    query << '='
    query << CGI.escape(value.to_s)
  end
  query
end
subtitle_exist?(video_path, language = nil) click to toggle source

Checks the specified video file for the existence of a subtitles, using common naming conventions and optional language.

@param video_path [String] The path to the video file to check. @param language [Language] A specific language to check.

@return [Boolean] `true` if a matching subtitle was found, otherwise `false`.

# File lib/subs.rb, line 123
def self.subtitle_exist?(video_path, language = nil)
  dir = File.dirname(video_path)
  # ex. '/home/me/Videos/MyFavoriteMovie.2019.mp4' => 'MyFavoriteMovie.2019'
  base = File.basename(video_path, File.extname(video_path))
  # Check each supported subtitle extension
  SUB_EXTENSIONS.each do |ext|
    # ex. MyFavoriteMovie.2019.srt
    return true if File.exist?(File.join(dir, "#{base}#{ext}"))
    next unless language
    if language.alpha2
      # ex. MyFavoriteMovie.2019.en.srt
      return true if File.exist?(File.join(dir, "#{base}.#{language.alpha2}#{ext}"))
    end
    # ex. MyFavoriteMovie.2019.eng.srt
    return true if File.exist?(File.join(dir, "#{base}.#{language.alpha3}#{ext}"))
  end
  # Not found
  false
end