Class: MusicbrainzAutomatcher
- Inherits:
-
Object
- Object
- MusicbrainzAutomatcher
- Defined in:
- lib/musicbrainz_automatcher.rb
Overview
Class to automatically match an artist and track to a MusicBrainz artist
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#mbws ⇒ Object
readonly
Returns the value of attribute mbws.
-
#network_retries ⇒ Object
Returns the value of attribute network_retries.
-
#network_timeout ⇒ Object
Returns the value of attribute network_timeout.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ MusicbrainzAutomatcher
constructor
A new instance of MusicbrainzAutomatcher.
-
#match_artist(artists, title = nil) ⇒ Object
Given an array of artists and a track title, return an rbrainz artist object.
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(={}) # Configuration options @network_timeout = [:network_timeout] || 15 # seconds @network_retries = [:network_retries] || 3 # Create MusicBrainz webservice host = [:musicbrainz_host] || 'musicbrainz.org' @mbws = MusicBrainz::Webservice::Webservice.new(:host => host, :proxy => [:proxy]) @mbws.open_timeout = @network_timeout @mbws.read_timeout = @network_timeout # Create a query cache @cache = ActiveSupport::Cache.lookup_store([:cache_type] || :memory_store) # Create a logger @logger = [:logger] || Logger.new(STDOUT) end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
20 21 22 |
# File 'lib/musicbrainz_automatcher.rb', line 20 def cache @cache end |
#logger ⇒ Object
Returns the value of attribute logger.
17 18 19 |
# File 'lib/musicbrainz_automatcher.rb', line 17 def logger @logger end |
#mbws ⇒ Object (readonly)
Returns the value of attribute mbws.
21 22 23 |
# File 'lib/musicbrainz_automatcher.rb', line 21 def mbws @mbws end |
#network_retries ⇒ Object
Returns the value of attribute network_retries.
19 20 21 |
# File 'lib/musicbrainz_automatcher.rb', line 19 def network_retries @network_retries end |
#network_timeout ⇒ Object
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 = 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? = lookup_by_track( artist, title ) ## Second: try removing brackets from the track name if ! matches = title.match(/^(.+)\s+\(.+\)$/) = 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 ! # Also cache the lookup, just based on the artist name = do_cached( "artist_name=#{artist}" ) do lookup_by_artist( artist ) end end end # Response is the MusicBrainz ID end end |