Class: MusicbrainzAutomatcher

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

Overview

Class to automatically match an artist and track to a MusicBrainz artist

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MusicbrainzAutomatcher

Returns a new instance of MusicbrainzAutomatcher.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/musicbrainz_automatcher.rb', line 23

def initialize(options={})
  # Configuration options
  @network_timeout = options[:network_timeout] || 15  # seconds
  @network_retries = options[:network_retries] || 3
  
  # Create MusicBrainz webservice
  host = options[:musicbrainz_host] || 'musicbrainz.org'
  @mbws = MusicBrainz::Webservice::Webservice.new(:host => host, :proxy => options[:proxy])
  @mbws.open_timeout = @network_timeout
  @mbws.read_timeout = @network_timeout
  
  # Create a query cache
  @cache = ActiveSupport::Cache.lookup_store(options[:cache_type] || :memory_store)
  
  # Create a logger
  @logger = options[:logger] || Logger.new(STDOUT)
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



20
21
22
# File 'lib/musicbrainz_automatcher.rb', line 20

def cache
  @cache
end

#loggerObject

Returns the value of attribute logger.



17
18
19
# File 'lib/musicbrainz_automatcher.rb', line 17

def logger
  @logger
end

#mbwsObject (readonly)

Returns the value of attribute mbws.



21
22
23
# File 'lib/musicbrainz_automatcher.rb', line 21

def mbws
  @mbws
end

#network_retriesObject

Returns the value of attribute network_retries.



19
20
21
# File 'lib/musicbrainz_automatcher.rb', line 19

def network_retries
  @network_retries
end

#network_timeoutObject

Returns the value of attribute network_timeout.



18
19
20
# File 'lib/musicbrainz_automatcher.rb', line 18

def network_timeout
  @network_timeout
end

Instance Method Details

#match_artist(artists, title = nil) ⇒ Object

Given an array of artists and a track title, return an rbrainz artist object. If there is no match in MusicBrainz, then false is returned



44
45
46
47
48
49
50
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/musicbrainz_automatcher.rb', line 44

def match_artist(artists, title=nil)
  # Only interested in first item of title array
  title = title.first if title.is_a?(Array)
  
  # Remove excess whitespace from the title
  title.strip! unless title.nil?
  
  # Clean and split the artist names
  artists = clean_artists( artists )
  
  # Return false if no artist names given
  return false if artists.empty?
  
  # Set title to nil, if it is an empty string
  title = nil if !title.nil? and title.size<1

  # Perform the query if it isn't already cached
  artist = join_artists( artists )
  do_cached( "artists=#{artist} title=#{title}" ) do
  
    # Remove items from the artist array until we get a match
    mbartist_id = false
    
    ## Ignore if artist name contains two consecutive stars (they contain a sware words)
    unless artist =~ /\*\*/
      
      ## First: lookup based on track name and artist
      unless title.nil?
        mbartist_id = lookup_by_track( artist, title ) 
        
        ## Second: try removing brackets from the track name
        if !mbartist_id
          matches = title.match(/^(.+)\s+\(.+\)$/)
          mbartist_id = lookup_by_track( artist, matches[1] ) unless matches.nil?
        end
      end

      ## Third: look-up just based on artist name
      # (but not after we have removed an artist from the stack)
      if !mbartist_id
        # Also cache the lookup, just based on the artist name
        mbartist_id = do_cached( "artist_name=#{artist}" ) do
          lookup_by_artist( artist ) 
        end
      end
    end
    
    # Response is the MusicBrainz ID
    mbartist_id
  end

end