class Opensubtitles::MovieFile

Constants

CHUNK_SIZE
EXTENSIONS

Attributes

language[R]
path[R]

Public Class Methods

compute_hash(path) click to toggle source

from trac.opensubtitles.org/projects/opensubtitles/wiki/HashSourceCodes

# File lib/opensubtitles/movie_file.rb, line 43
def self.compute_hash(path)
  filesize = File.size(path)
  hash = filesize
  
  # Read 64 kbytes, divide up into 64 bits and add each
  # to hash. Do for beginning and end of file.
  File.open(path, 'rb') do |f|    
    # Q = unsigned long long = 64 bit
    f.read(CHUNK_SIZE).unpack("Q*").each do |n|
      hash = hash + n & 0xffffffffffffffff # to remain as 64 bit number
    end
    
    f.seek([0, filesize - CHUNK_SIZE].max, IO::SEEK_SET)
    
    # And again for the end of the file
    f.read(CHUNK_SIZE).unpack("Q*").each do |n|
      hash = hash + n & 0xffffffffffffffff
    end
  end
  
  sprintf("%016x", hash)
end
new(path, language = Opensubtitles.default_language) click to toggle source
# File lib/opensubtitles/movie_file.rb, line 8
def initialize(path, language = Opensubtitles.default_language)
  @path = path
  @language = language
end

Public Instance Methods

has_sub?() click to toggle source
# File lib/opensubtitles/movie_file.rb, line 13
def has_sub?
  exist = false
  %w(srt sub).each{ |ext| exist ||= File.exist?(sub_path(ext)) }
  exist
end
hash() click to toggle source
# File lib/opensubtitles/movie_file.rb, line 28
def hash
  @hash ||= self.class.compute_hash(path)
end
name() click to toggle source
# File lib/opensubtitles/movie_file.rb, line 36
def name
  @name ||= File.basename(path, File.extname(path))
end
size() click to toggle source
# File lib/opensubtitles/movie_file.rb, line 32
def size
  @size ||= File.size(path)
end
sub_path(format) click to toggle source
# File lib/opensubtitles/movie_file.rb, line 19
def sub_path(format)
  extension = if @language
    ".#{@language}.#{format}"
  else
    ".#{format}"
  end
  File.join(File.dirname(path), File.basename(path, File.extname(path)) + extension)
end