Class: MovieSearcher

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ MovieSearcher

Returns a new instance of MovieSearcher.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/movie_searcher.rb', line 10

def initialize(args)
  args.keys.each { |name| instance_variable_set "@" + name.to_s, args[name] unless name == :options }
  
  @options = {
    :long => 15,
    :split => /\s+|\./,
    :imdb => ImdbParty::Imdb.new,
    :limit => 0.4
  }
  
  @options.merge!(args[:options]) unless args[:options].nil?
  
  @cleaners = YAML.load(File.read("#{File.dirname(__FILE__)}/imdb_party/exclude.yaml"))["excluded"]
end

Instance Attribute Details

#cleanersObject

Returns the value of attribute cleaners.



8
9
10
# File 'lib/movie_searcher.rb', line 8

def cleaners
  @cleaners
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/movie_searcher.rb', line 8

def options
  @options
end

Class Method Details

.find_by_download(unknown) ⇒ Object

Try to figure out what method to use, folder or file



73
74
75
76
# File 'lib/movie_searcher.rb', line 73

def self.find_by_download(unknown)
  unknown = self.relative?(unknown)
  File.directory?(unknown) ? self.find_by_folder(unknown) : self.find_by_release_name(File.split(unknown).last)
end

.find_by_file(file_path) ⇒ Object

Finds the movie based on the nfo file (or similar file)



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/movie_searcher.rb', line 35

def self.find_by_file(file_path)
  data = File.read(file_path)
  
  # If the file is encoded in something non valid (according to ruby), then it can't be read here
  # The {valid_encoding} method is not included in ruby 1.8.7, that's why i'm using {respond_to?}
  # More info here: http://blog.grayproductions.net/articles/ruby_19s_string
  # If the user uses 1.8.7 and the string isn't encoded in UTF-8, nothing happens.
  # The script won't throw an exception as it does in 1.9.2
  return if data.respond_to?(:valid_encoding?) and not data.valid_encoding?
    
  if data =~ /imdb\.com\/title\/(tt\d+)/
    return self.find_movie_by_id($1)
  end
end

.find_by_folder(folder_path) ⇒ Object

Finds the movie based on the folder



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

def self.find_by_folder(folder_path)
  
  # Relative path?
  folder_path = self.relative?(folder_path)
  
  # Makes sure that every directory looks the same, an raises an exception if the dir does not exist
  folder_path = Dir.new(folder_path).path
  
  %x{cd '#{folder_path}' && find '#{folder_path}' -maxdepth 4}.split(/\n/).each do |file|
    # Locating every textfile in the directory
    # We're hoping to find a nfo file
    if Mimer.identify(file).text?
      result = self.find_by_file(file)
      return result unless result.nil?
    end
  end
  
  # Last resort, we hope that the folder contain the release name
  return self.find_by_release_name(File.split(folder_path).last)
end

.find_by_release_name(search_value, options = {}) ⇒ Object

Finds the movie based on the release name of the movie



26
27
28
29
30
31
32
# File 'lib/movie_searcher.rb', line 26

def self.find_by_release_name(search_value, options = {})
  return if search_value.nil? or search_value.length.zero?
  this = MovieSearcher.new(options.merge(:search_value => search_value.to_s))
  return if this.to_long?
  
  this.find_the_movie!
end

.method_missing(method, *args, &block) ⇒ Object



109
110
111
112
# File 'lib/movie_searcher.rb', line 109

def self.method_missing(method, *args, &block)  
  result = ImdbParty::Imdb.new.send(method, *args)
  result.class == Array ? result.map{|r| ImdbParty::Movie.new(r)} : result
end

.relative?(unknown) ⇒ Boolean

Returns the full path of the file/folder

Returns:

  • (Boolean)


79
80
81
# File 'lib/movie_searcher.rb', line 79

def self.relative?(unknown)
  unknown.match(/^\//) ? unknown : File.expand_path(unknown) 
end

Instance Method Details

#cleaner(string) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/movie_searcher.rb', line 114

def cleaner(string)
  @cleaners.each do |clean|
    string = string.gsub(/#{clean}/i, ' ')
  end
  
  [/(19|20\d{2})/, /\./, /\s*-\s*/, /\s{2,}/].each do |regex|
    string = string.gsub(regex, ' ')
  end
  
  string.strip
end

#find_the_movie!Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/movie_searcher.rb', line 88

def find_the_movie!
  search = Spot.new.clean!(cleaner(@search_value)).gsub(/\s+/, "_")

  run = true
  count = 0
  while run
    run = false
    begin
      data = RestClient.get "http://sg.media-imdb.com/suggests/#{search[0]}/#{search[0..(search.length - count)]}.json"
    rescue RestClient::Exception
      run = true
      count += 1
    end
    
    if not run or count >= search.length
      iid = data.match(/(tt\d+)/).to_a[1]
    end
  end
  MovieSearcher.find_movie_by_id(iid) if iid
end

#shortest(a, b) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/movie_searcher.rb', line 126

def shortest(a,b)
  # If the release contains a year, then the corresponding movie from IMDB should have the same year
  if year = @search_value.match(/(19|20\d{2})/)
    return nil unless year[1] == a[:year]
  end
   
  Levenshtein.distance(self.super_cleaner(a[:title]), self.super_cleaner(self.cleaner(b)), @options[:limit])
end

#super_cleaner(string) ⇒ Object



135
136
137
# File 'lib/movie_searcher.rb', line 135

def super_cleaner(string)
  string.gsub(/[^a-z0-9]/i, '')
end

#to_long?Boolean

Returns:

  • (Boolean)


83
84
85
86
# File 'lib/movie_searcher.rb', line 83

def to_long?
  @split = self.cleaner(@search_value).split(@options[:split])    
  @split.length > @options[:long]
end