class Subfinder::Subtitle
Find and match subtitles
Public Class Methods
new()
click to toggle source
# File lib/subfinder/subtitle.rb, line 6 def initialize @success = 0 @failure = 0 end
Public Instance Methods
match()
click to toggle source
# File lib/subfinder/subtitle.rb, line 11 def match Parser::Files.list.each do |video_file| # only consider video file types listed in confi next unless Config.video_formats.include? File.extname(video_file) next if subtitle_exists? video_file if episode_number(video_file).nil? Logger.info 'Can not find the episode and season number for:'\ " #{File.basename(video_file)}".red @failure += 1 next else find_subtitle_for video_file end end Logger.info "Subtitle added: #{@success}, "\ " Not found: #{@failure}, "\ "Total file proccessed: #{@failure + @success}".green end
Private Instance Methods
episode_number(video_file)
click to toggle source
# File lib/subfinder/subtitle.rb, line 33 def episode_number(video_file) episode = video_file.scan(/[a-zA-Z]+\d+\d+[a-zA-Z]+\d+\d/) episode.empty? ? nil : episode.first.downcase end
find_subtitle_for(video_file)
click to toggle source
# File lib/subfinder/subtitle.rb, line 43 def find_subtitle_for(video_file) subtitles = Parser::Files.list.select { |item| item[/#{episode_number(video_file)}/i] && item[/.srt/] } if subtitles.empty? Logger.info "Subtitle for #{File.basename(video_file)}"\ ' is not exists on disk. Trying to download...' subscene = Parser::Subscene.new(video_file) if !Config.url.nil? && subscene.get Parser::Files.prepare_file_list Logger.info "Subtitle downloaded for #{File.basename(video_file)}" else Logger.info "Subtitle can not found on Subscene for #{File.basename(video_file)}".red Logger.info 'Please provide a url with "-u" siwtch'.red if Config.url.nil? @failure += 1 return end end rename_subtitle video_file end
rename_subtitle(video_file)
click to toggle source
# File lib/subfinder/subtitle.rb, line 62 def rename_subtitle(video_file) subtitles = Parser::Files.list.select do |item| item[/#{episode_number(video_file)}/i] && item[/.srt/] end video_name = File.basename(video_file).split('.')[0..-2].join('.') subtitle_name = File.dirname(subtitles.first) + '/' + video_name + '.srt' File.rename(subtitles.first, subtitle_name) Logger.debug "Subtitle renamed for: #{File.basename(video_file)}" @success += 1 end
subtitle_exists?(video_file)
click to toggle source
# File lib/subfinder/subtitle.rb, line 38 def subtitle_exists?(video_file) subtitle_file_name = video_file.split('.')[0..-2].join('.') + '.srt' return true if Parser::Files.list.include? subtitle_file_name end