Search fixes
This commit is contained in:
@@ -6,13 +6,16 @@ class ArtistController < ApplicationController
|
||||
name = params[:name].gsub('%20', ' ').gsub('+', ' ')
|
||||
artist = Artist.find_by_name(name)
|
||||
unless artist
|
||||
results = ArtistController.musicBrainzSearch(name)
|
||||
if results[0] == name
|
||||
results = ArtistController.musicBrainzExactSearch(name)
|
||||
if results.empty?
|
||||
render :json => {status: 'not founds'}
|
||||
return
|
||||
elsif results[0][:name] == name
|
||||
ImportController.importArtist(name)
|
||||
render :json => {status: 'loaded'}
|
||||
return
|
||||
elsif (results[0].downcase == name.downcase or results[0].downcase == 'the '+ name.downcase) and results[0] != name
|
||||
render :json => {status: 'corrected', page: results[0]}
|
||||
elsif results[0][:name].downcase == name.downcase or results[0][:name].downcase == 'the '+ name.downcase
|
||||
render :json => {status: 'corrected', page: results[0][:name]}
|
||||
return
|
||||
else
|
||||
render :json => {status: 'suggestions', values: results}
|
||||
@@ -80,4 +83,36 @@ class ArtistController < ApplicationController
|
||||
return {}
|
||||
end
|
||||
end
|
||||
|
||||
def self.musicBrainzExactSearch(query)
|
||||
begin
|
||||
response = ActiveSupport::JSON.decode(open('http://search.test.musicbrainz.org/ws/2/artist/?fmt=json&query='+
|
||||
URI.escape(query).gsub(/\&/, '%26').gsub(/\?/, '%3F') +'~&limit=50',
|
||||
'User-Agent' => 'BeatHaven.org'
|
||||
).read)
|
||||
artists = []
|
||||
i = 0
|
||||
response['artist-list']['artist'].each do |artist|
|
||||
i++
|
||||
artist['weight'] = (response['artist-list']['artist'].length - i)
|
||||
unless artist['alias-list'].nil?
|
||||
artist['weight'] += 20 if artist['alias-list']['alias'].include?(query)
|
||||
artist['alias-list']['alias'].each do |aliass|
|
||||
artist['weight'] += 10 if aliass.downcase == query.downcase
|
||||
artist['weight'] += 3 if aliass.downcase.include?(query.downcase)
|
||||
end
|
||||
end
|
||||
unless artist['tag-list'].nil?
|
||||
artist['weight'] += artist['tag-list']['tag'].length * 4
|
||||
end
|
||||
end
|
||||
response['artist-list']['artist'].each do |artist|
|
||||
artists << {name: artist['name'], weight: artist['weight'], desc: artist['disambiguation'], type: artist['type'].capitalize, mbid: artist['id']} unless artist['type'] == 'unknown'
|
||||
end
|
||||
artists.sort! { |a, b| b[:weight] <=> a[:weight] }
|
||||
artists.take(10)
|
||||
rescue
|
||||
return {}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,11 +4,7 @@ class ImportController < ApplicationController
|
||||
def self.importArtist name
|
||||
|
||||
# Get artist info
|
||||
artist_mb_xml = open(
|
||||
'http://musicbrainz.org/ws/2/artist/?query='+ URI.escape(name).gsub(/\&/, '%26').gsub(/\?/, '%3F') +'&limit=5',
|
||||
'User-Agent' => @@user_agent
|
||||
).read
|
||||
artist_mb_data = artist_mb_xml.scan(/<artist.*?type=\"(.+?)\"\sid=\"([a-f0-9-]+?)\">.*?<name>(.+?)<\/name>/m)
|
||||
artist_mb_data = ArtistController.musicBrainzExactSearch(name).first
|
||||
begin
|
||||
artist_lastfm_xml = open(
|
||||
'http://ws.audioscrobbler.com/2.0/'+
|
||||
@@ -24,16 +20,16 @@ class ImportController < ApplicationController
|
||||
|
||||
# Save artist
|
||||
artist = Artist.new
|
||||
artist.name = artist_mb_data[0][2]
|
||||
artist.name = artist_mb_data[:name]
|
||||
artist.desc = artist_desc
|
||||
artist.pic_url = artist_pic
|
||||
artist.artist_type = artist_mb_data[0][0]
|
||||
artist.mbid = artist_mb_data[0][1]
|
||||
artist.artist_type = artist_mb_data[:type]
|
||||
artist.mbid = artist_mb_data[:mbid]
|
||||
artist.save
|
||||
|
||||
# Get albums from MB
|
||||
release_groups_mb_xml = open(
|
||||
'http://musicbrainz.org/ws/2/release-group/?artist='+ artist_mb_data[0][1],
|
||||
'http://musicbrainz.org/ws/2/release-group/?artist='+ artist_mb_data[:mbid],
|
||||
'User-Agent' => @@user_agent
|
||||
).read
|
||||
release_groups_mb_data = release_groups_mb_xml.scan(/<release-group\stype=\"([a-zA-Z]+?)\"\sid=\"([a-f0-9-]+?)\"><title>(.+?)<\/title>/m)
|
||||
|
||||
Reference in New Issue
Block a user