2011-11-15 19:20:21 +04:00
|
|
|
|
# encoding: UTF-8
|
|
|
|
|
|
2011-09-13 14:06:52 +04:00
|
|
|
|
module MusicBrainz
|
2011-10-06 15:31:17 +04:00
|
|
|
|
class Artist < MusicBrainz::Base
|
2011-10-06 16:39:14 +04:00
|
|
|
|
attr_accessor :id, :type, :name, :country, :date_begin, :date_end, :urls
|
2011-09-13 14:06:52 +04:00
|
|
|
|
@release_groups
|
|
|
|
|
|
|
|
|
|
def release_groups
|
|
|
|
|
if @release_groups.nil? and not self.id.nil?
|
|
|
|
|
@release_groups = []
|
2011-10-06 15:31:17 +04:00
|
|
|
|
Nokogiri::XML(MusicBrainz.load(:release_group, :artist => self.id)).css('release-group').each do |rg|
|
2011-09-13 14:06:52 +04:00
|
|
|
|
@release_groups << MusicBrainz::ReleaseGroup.parse_xml(rg)
|
|
|
|
|
end
|
|
|
|
|
end
|
2011-09-14 16:30:52 +04:00
|
|
|
|
@release_groups.sort{ |a, b| a.first_release_date <=> b.first_release_date }
|
2011-09-13 14:06:52 +04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.find mbid
|
2011-10-06 16:39:14 +04:00
|
|
|
|
res = MusicBrainz.load :artist, :id => mbid, :inc => [:url_rels]
|
2011-09-19 03:39:03 +04:00
|
|
|
|
return nil if res.nil?
|
2011-09-19 03:47:45 +04:00
|
|
|
|
@artist = self.parse_xml(Nokogiri::XML(res))
|
2011-09-13 14:06:52 +04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.parse_xml xml
|
|
|
|
|
@artist = MusicBrainz::Artist.new
|
2011-10-06 15:31:17 +04:00
|
|
|
|
@artist.id = self.safe_get_attr(xml, 'artist', 'id')
|
|
|
|
|
@artist.type = self.safe_get_attr(xml, 'artist', 'type')
|
2011-11-15 19:11:30 +04:00
|
|
|
|
@artist.name = self.safe_get_value(xml, 'artist > name').gsub(/[`’]/, "'")
|
2011-10-06 15:31:17 +04:00
|
|
|
|
@artist.country = self.safe_get_value(xml, 'artist > country')
|
|
|
|
|
@artist.date_begin = self.safe_get_value(xml, 'artist > life-span > begin')
|
|
|
|
|
@artist.date_end = self.safe_get_value(xml, 'artist > life-span > end')
|
2011-10-06 16:39:14 +04:00
|
|
|
|
@artist.urls = {}
|
|
|
|
|
xml.css('relation-list[target-type="url"] > relation').each do |rel|
|
|
|
|
|
@artist.urls[rel.attr('type').downcase.split(' ').join('_').to_sym] = rel.css('target').text
|
|
|
|
|
end
|
2011-09-13 14:06:52 +04:00
|
|
|
|
@artist
|
|
|
|
|
end
|
2011-09-14 10:10:56 +04:00
|
|
|
|
|
2011-09-14 13:08:03 +04:00
|
|
|
|
def self.discography mbid
|
|
|
|
|
artist = self.find(mbid)
|
|
|
|
|
artist.release_groups.each {|rg| rg.releases.each {|r| r.tracks } }
|
|
|
|
|
artist
|
|
|
|
|
end
|
|
|
|
|
|
2011-09-14 11:45:42 +04:00
|
|
|
|
def self.find_by_name name
|
|
|
|
|
matches = self.search name
|
|
|
|
|
matches.length.zero? ? nil : self.find(matches.first[:mbid])
|
|
|
|
|
end
|
2011-09-14 10:10:56 +04:00
|
|
|
|
|
|
|
|
|
def self.search name
|
|
|
|
|
artists = []
|
2011-11-15 19:32:49 +04:00
|
|
|
|
xml = Nokogiri::XML(MusicBrainz.load(:artist, :query => CGI.escape(name).gsub(/\!/, '\!') + '~', :limit => 50))
|
2011-09-14 10:10:56 +04:00
|
|
|
|
xml.css('artist-list > artist').each do |a|
|
|
|
|
|
artist = {
|
2011-11-15 19:11:30 +04:00
|
|
|
|
:name => a.first_element_child.text.gsub(/[`’]/, "'"),
|
|
|
|
|
:sort_name => self.safe_get_value(a, 'sort-name').gsub(/[`’]/, "'"),
|
2011-09-14 10:10:56 +04:00
|
|
|
|
:weight => 0,
|
2011-10-06 15:31:17 +04:00
|
|
|
|
:desc => self.safe_get_value(a, 'disambiguation'),
|
2011-10-06 16:30:46 +04:00
|
|
|
|
:type => self.safe_get_attr(a, nil, 'type'),
|
|
|
|
|
:mbid => self.safe_get_attr(a, nil, 'id')
|
2011-09-14 10:10:56 +04:00
|
|
|
|
}
|
|
|
|
|
aliases = a.css('alias-list > alias').map{ |item| item.text }
|
|
|
|
|
if aliases.include? name
|
|
|
|
|
artist[:weight] += 20
|
|
|
|
|
elsif aliases.map{ |item| item.downcase }.include? name.downcase
|
|
|
|
|
artist[:weight] += 10
|
|
|
|
|
elsif aliases.map{ |item| item.downcase.gsub(/\s/, '') }.include? name.downcase.gsub(/\s/, '')
|
|
|
|
|
artist[:weight] += 5
|
|
|
|
|
end
|
|
|
|
|
artists << artist
|
|
|
|
|
end
|
|
|
|
|
artists.sort{ |a, b| b[:weight] <=> a[:weight] }.take(10)
|
|
|
|
|
end
|
2011-09-13 14:06:52 +04:00
|
|
|
|
end
|
|
|
|
|
end
|