Class: Opensubtitles::MovieFile

Inherits:
Object
  • Object
show all
Defined in:
lib/opensubtitles/movie_file.rb

Constant Summary collapse

EXTENSIONS =
%w(avi mpg m4v mkv mov ogv mp4)
CHUNK_SIZE =

in bytes

64 * 1024

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, language = Opensubtitles.default_language) ⇒ MovieFile

Returns a new instance of MovieFile.



8
9
10
11
# File 'lib/opensubtitles/movie_file.rb', line 8

def initialize(path, language = Opensubtitles.default_language)
  @path = path
  @language = language
end

Instance Attribute Details

#languageObject (readonly)

Returns the value of attribute language.



6
7
8
# File 'lib/opensubtitles/movie_file.rb', line 6

def language
  @language
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/opensubtitles/movie_file.rb', line 6

def path
  @path
end

Class Method Details

.compute_hash(path) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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

Instance Method Details

#has_sub?Boolean

Returns:

  • (Boolean)


13
14
15
16
17
# 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

#hashObject



28
29
30
# File 'lib/opensubtitles/movie_file.rb', line 28

def hash
  @hash ||= self.class.compute_hash(path)
end

#nameObject



36
37
38
# File 'lib/opensubtitles/movie_file.rb', line 36

def name
  @name ||= File.basename(path, File.extname(path))
end

#sizeObject



32
33
34
# File 'lib/opensubtitles/movie_file.rb', line 32

def size
  @size ||= File.size(path)
end

#sub_path(format) ⇒ Object



19
20
21
22
23
24
25
26
# 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