Class: OSDb::Movie

Inherits:
Object
  • Object
show all
Defined in:
lib/osdb/movie.rb

Constant Summary collapse

CHUNK_SIZE =

in bytes

64 * 1024

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Movie

Returns a new instance of Movie.



26
27
28
# File 'lib/osdb/movie.rb', line 26

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/osdb/movie.rb', line 4

def path
  @path
end

Class Method Details

.compute_hash(path) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/osdb/movie.rb', line 51

def self.compute_hash(path)
  begin
    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)
  rescue Errno::EPERM
    OSDb.log("* could not read #{path}")
  rescue RuntimeError
    OSDb.log("* could not read #{path}")
  end
end

.get_movie_listObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/osdb/movie.rb', line 7

def get_movie_list

  if dir = OSDb.options[:dir]
    path   = Dir.glob(File.join(dir, '**', "*.{#{OSDb.options[:movie_exts]}}"))
    movies = path.map { |path| new(path) }
  else
    movies = ARGV.map{ |path| OSDb::Movie.new(path) }
  end
  movies.reject!(&:has_sub?) unless OSDb.options[:force]
  
  if movies.empty?
    OSDb.log "All movies in #{dir} already have subtitles."
    exit 1
  end
  
  movies
end

Instance Method Details

#has_sub?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/osdb/movie.rb', line 30

def has_sub?
  exist = false
  %w(.srt .sub .smi).each{ |ext| exist ||= File.exist?(path.gsub(File.extname(path), ext)) }
  exist
end

#hashObject



40
41
42
# File 'lib/osdb/movie.rb', line 40

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

#sizeObject



44
45
46
# File 'lib/osdb/movie.rb', line 44

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

#sub_path(format) ⇒ Object



36
37
38
# File 'lib/osdb/movie.rb', line 36

def sub_path(format)
  path.gsub(File.extname(path), ".#{format}")
end