1
0
Fork 0
musicbrainz/lib/models/music_brainz/artist.rb

62 lines
2.2 KiB
Ruby

module MusicBrainz
class Artist
attr_accessor :id, :type, :name, :country, :date_begin, :date_end
@release_groups
def release_groups
if @release_groups.nil? and not self.id.nil?
@release_groups = []
Nokogiri::XML(MusicBrainz.load(
'http://musicbrainz.org/ws/2/release-group/?artist=' + self.id
)).css('release-group').each do |rg|
@release_groups << MusicBrainz::ReleaseGroup.parse_xml(rg)
end
end
@release_groups
end
def self.find mbid
@artist = self.parse_xml(Nokogiri::XML(MusicBrainz.load('http://musicbrainz.org/ws/2/artist/' + mbid)))
end
def self.parse_xml xml
@artist = MusicBrainz::Artist.new
@artist.id = xml.css('artist').attr('id').value
@artist.type = xml.css('artist').attr('type').value
@artist.name = xml.css('artist > name').text
@artist.country = xml.css('artist > country').text unless xml.css('artist > country').empty?
@artist.date_begin = xml.css('artist > life-span > begin').text unless xml.css('artist > life-span > begin').empty?
@artist.date_end = xml.css('artist > life-span > end').text unless xml.css('artist > life-span > end').empty?
@artist
end
private
def self.search name
artists = []
xml = Nokogiri::XML(MusicBrainz.load(
'http://musicbrainz.org/ws/2/artist/?query='+ URI.escape(query).gsub(/\&/, '%26').gsub(/\?/, '%3F') +'~&limit=50'
))
xml.css('artist-list > artist').each do |a|
artist = {
:name => a.css('name').text,
:weight => 0,
:desc => a.css('disambiguation').text unless a.css('disambiguation').empty?,
:type => a.css('type').text.capitalize,
:mbid => a.attr('id')
}
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
end
end