Module: Crawler::Movie

Includes:
Base
Defined in:
lib/crawler/movie.rb,
lib/crawler/movie/core/version.rb

Defined Under Namespace

Modules: Core

Constant Summary collapse

PROVIDERS =
[]
SCORES =
{}

Class Method Summary collapse

Class Method Details

.add_provider(provider_name, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/crawler/movie.rb', line 14

def self.add_provider(provider_name, options = {})
  options.assert_valid_keys :score, :insert_at

  PROVIDERS.insert(options[:insert_at] || -1, provider_name)

  if (score = options[:score])
    SCORES[provider_name] = score
  end
end

.best(query, year: nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/crawler/movie.rb', line 48

def self.best(query, year: nil)
  data = search(query, year: year).max_by do |_, movies|
    movie = movies.max_by do |movie|
      movie[:score]
    end

    movie[:score]
  end

  data&.last
end

.search(query, year: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/crawler/movie.rb', line 24

def self.search(query, year: nil)
  movies = PROVIDERS.flat_map do |provider_name|
    camelized = ActiveSupport::Inflector.camelize("crawler/movie/providers/#{provider_name.to_s}")
    klass = ActiveSupport::Inflector.constantize(camelized)
    movies = klass.search(Utils.transliterate(query))

    movies.map do |movie|
      provider_score = SCORES[provider_name] || 0.5
      title_score = Utils.levenshtein_score(query, movie[:title])
      year_score = 1.0 unless year
      year_score ||= movie[:release_date] && year.to_s == movie[:release_date].year.to_s ? 1.0 : 0.9

      {
        data: movie,
        score: provider_score * title_score * year_score
      }
    end
  end

  movies.group_by do |movie|
    [Utils.transliterate(movie[:data][:title]), movie[:data][:release_date] && movie[:data][:release_date].year]
  end
end