Class: SceneToolkit::CLI::GoogleMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/scene_toolkit/cli/auto_rename.rb

Class Method Summary collapse

Class Method Details

.match(name) ⇒ Object



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

def self.match(name)
  response = Nestful.get("http://www.google.com/search", :params => {:q => name, :num => 100})
  response = Nokogiri::HTML(response).search(".g").text.encode("utf-8", "utf-8", :invalid => :replace)

  if name =~ /[\d+]$/ # artist-ablum-2012
    regex = /#{Regexp.escape(name)}(?:-[A-Za-z0-9]+)*/i
    matches = response.scan(regex).reject(&:downcase?).reject { |m| m.size == name.size }.uniq

  else # artist-ablum-2012-group
    regex = /#{Regexp.escape(name)}/i
    matches = response.scan(regex).reject(&:downcase?).select { |m| m.size == name.size }.uniq
  end

  matches.reject { |m| m == name }.select { |m| m =~ Release::Validations::Name::REGEXP }
end