1
0
Fork 0

Search method added, request time check fixed

This commit is contained in:
magnolia-fan 2011-09-14 10:10:56 +04:00
parent af218ad951
commit 05d009f3ac
2 changed files with 32 additions and 2 deletions

View File

@ -15,12 +15,12 @@ module MusicBrainz
5.times do
begin
response = open(url, "User-Agent" => "gem musicbrainz (https://github.com/magnolia-fan/musicbrainz) @ " + Socket.gethostname)
self.last_query_time = Time.now.to_f
rescue => e
p "MusicBrainz: 503"
end
break unless response.nil?
end
self.last_query_time = Time.now.to_f
response
end

View File

@ -6,7 +6,9 @@ module MusicBrainz
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|
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
@ -27,5 +29,33 @@ module MusicBrainz
@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