1
0
Fork 0

Search fixes

This commit is contained in:
magnolia-fan 2011-06-18 19:58:51 +04:00
parent aa5d5a51c6
commit 10aef4e15a
7 changed files with 64 additions and 46 deletions

View File

@ -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

View File

@ -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)

View File

@ -1,14 +1,9 @@
# SQLite version 3.x
# gem install sqlite3
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database: db/test.sqlite3
@ -16,15 +11,4 @@ test:
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
musicbrainz:
host: 192.168.0.128
adapter: postgresql
database: beathaven
username: postgres
password: password
adapter: postgresql

View File

@ -3,9 +3,10 @@ require 'open-uri'
namespace :db do
desc 'Imports test data from MusicBrainz database'
task :import => :environment do
ati = ['The White Stripes']
ati = ['KISS']
ati.each do |name|
ImportController.importArtist(name)
#ap res = ArtistController.musicBrainzExactSearch(name)
end
end
end

View File

@ -8,15 +8,15 @@
<div>Misspelled?</div>
<ul>
<li><a class="data artist">The Raconters</a></li>
<li>The Raconteurs</li>
<li>The Encounters</li>
<li>Encounters</li>
<li>The Encounter</li>
<li>Carpenters</li>
<li>The Dearhunters</li>
<li>Jamhunters</li>
<li>The Go Go Haunters</li>
<li>The Beathunters</li>
<li><a class="data artist">The Raconteurs</a></li>
<li><a class="data artist">The Encounters</a></li>
<li><a class="data artist">Encounters</a></li>
<li><a class="data artist">The Encounter</a></li>
<li><a class="data artist">Carpenters</a></li>
<li><a class="data artist">The Dearhunters</a></li>
<li><a class="data artist">Jamhunters</a></li>
<li><a class="data artist">The Go Go Haunters</a></li>
<li><a class="data artist">The Beathunters</a></li>
</ul>
</div>
</div>

View File

@ -16,11 +16,12 @@ var Search = {
for (var i = 0; i < values.length; i++) {
$('.suggestions ul').append('\
<li>\
<a class="data artist">'+ values[i] +'</a>\
<a class="data artist">'+ values[i].name +'</a>\
'+ (values[i].desc ? '<span>('+ values[i].desc +')</span>' : '') +'\
</li>\
');
}
$('.suggestions').show();
$('.suggestions').css('margin-left', $('#search_field').offset().left +'px').show();
},
hideSuggestions: function() {

View File

@ -42,10 +42,8 @@
.suggestions {
display: none;
position: absolute;
left: 50%;
margin: 30px 0 0 -380px;
width: 400px;
margin: 30px 0 0 250px;
width: 500px;
text-align: left;
font-size: 24px;
}
@ -61,4 +59,7 @@
color: #04F;
border-bottom: #04F 1px dotted;
cursor: pointer;
}
.suggestions li span {
font-size: 14px;
}