Module: Subs

Defined in:
lib/subs.rb,
lib/subs/version.rb,
lib/subs/language.rb,
lib/subs/sub_rip_time.rb,
lib/subs/providers/sub_db.rb,
lib/subs/providers/provider.rb,
lib/subs/providers/open_subtitles.rb

Overview

Top-level namespace for the gem.

Defined Under Namespace

Modules: CredentialProvider, FilenameSearcher, HashSearcher, IMDbSearcher, LoginProvider Classes: Language, Movie, OpenSubtitles, Provider, SearchResult, SubDB, SubRipTime

Constant Summary collapse

VIDEO_EXTENSIONS =

Video extensions to search for.

%w(.avi .mkv .mp4 .mov .mpg .wmv .rm .rmvb .divx).freeze
SUB_EXTENSIONS =

Subtitle extensions to search for.

%w(.srt .sub).freeze
VERSION =
"1.0.1"

Class Method Summary collapse

Class Method Details

.build_subtitle_path(path, language = nil, ext = '.srt') ⇒ String

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



182
183
184
185
186
187
188
189
190
# 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) ⇒ Logger

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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

.fuzzy_search(path) ⇒ Movie?

Convenience method to attempt getting basic movie information with the specified file.



167
168
169
170
171
172
# File 'lib/subs.rb', line 167

def self.fuzzy_search(path)
  return nil unless  File.exist?(path)
  result = nil
  OpenSubtitles.new { |provider| result = provider.fuzzy_search(File.basename(path)) }
  result
end

.logLogger



96
97
98
# File 'lib/subs.rb', line 96

def self.log
  @log ||= create_log(STDOUT)
end

.query_string(**params) ⇒ String

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



149
150
151
152
153
154
155
156
157
158
159
# 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) ⇒ Boolean

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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 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

.video_search(directory, recursive) ⇒ Array<String>

Searches the specified directory for supported video files.



108
109
110
111
112
# File 'lib/subs.rb', line 108

def self.video_search(directory, recursive)
  VIDEO_EXTENSIONS.flat_map do |ext|
    Dir.glob(File.join(directory, recursive ? "**/*#{ext}" : "*#{ext}"))
  end
end