Module: Suby

Extended by:
Interface
Defined in:
lib/suby.rb,
lib/suby/interface.rb,
lib/suby/downloader.rb,
lib/suby/movie_hasher.rb,
lib/suby/filename_parser.rb,
lib/suby/downloader/addic7ed.rb,
lib/suby/downloader/tvsubtitles.rb,
lib/suby/downloader/opensubtitles.rb

Defined Under Namespace

Modules: FilenameParser, Interface, MovieHasher Classes: Downloader

Constant Summary collapse

NotFoundError =
Class.new StandardError
DownloaderError =
Class.new StandardError
SUB_EXTENSIONS =
%w[srt sub]
TMPDIR =
Path.tmpdir
TEMP_ARCHIVE =
TMPDIR / 'archive'
TEMP_SUBTITLES =
TMPDIR / 'subtitles'

Class Method Summary collapse

Methods included from Interface

error, failure, success

Class Method Details

.download_subtitles(files, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/suby.rb', line 19

def download_subtitles(files, options = {})
  Zip.on_exists_proc = options[:force]
  files.each { |file|
    file = Path(file)
    if file.dir?
      download_subtitles(file.children, options)
    elsif SUB_EXTENSIONS.include?(file.ext)
      # ignore already downloaded subtitles
    elsif !options[:force] and SUB_EXTENSIONS.any? { |ext| f = file.sub_ext(ext) and f.exist? and !f.empty? }
      puts "Skipping: #{file}"
    elsif !file.exist? or video?(file)
      download_subtitles_for_file(file, options)
    end
  }
ensure
  TMPDIR.rm_rf
end

.download_subtitles_for_file(file, options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/suby.rb', line 41

def download_subtitles_for_file(file, options)
  begin
    puts file
    success = Downloader::DOWNLOADERS.find { |downloader_class|
      try_downloader(downloader_class.new(file, options[:lang]))
    }
    error "\nNo downloader could find subtitles for #{file}" unless success
  rescue
    error "\nThe download of the subtitles failed for #{file}:"
    error "#{$!.class}: #{$!.message}"
    $stderr.puts $!.backtrace
  end
end

.extract_sub_from_archive(archive, format, file) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/suby.rb', line 72

def extract_sub_from_archive(archive, format, file)
  case format
  when :zip
    Zip::File.open(archive.to_s) { |zip|
      sub = zip.entries.find { |entry|
        entry.to_s =~ /\.#{Regexp.union SUB_EXTENSIONS}$/
      }
      raise "no subtitles in #{archive}" unless sub
      sub.extract(file.to_s)
    }
  else
    raise "unknown archive type (#{archive})"
  end
ensure
  archive.unlink if archive.exist?
end

.try_downloader(downloader) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/suby.rb', line 55

def try_downloader(downloader)
  return false unless downloader.support_video_type?
  begin
    print "  #{downloader.to_s.ljust(20)}"
    downloader.download
  rescue Suby::NotFoundError => error
    failure "Failed: #{error.message}"
    false
  rescue Suby::DownloaderError => error
    error "Error: #{error.message}"
    false
  else
    success downloader.success_message
    true
  end
end

.video?(file) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/suby.rb', line 37

def video?(file)
  MIME::Types.type_for(file.path).any? { |type| type.media_type == "video" }
end