Search fixes
This commit is contained in:
parent
aa5d5a51c6
commit
10aef4e15a
|
@ -6,13 +6,16 @@ class ArtistController < ApplicationController
|
||||||
name = params[:name].gsub('%20', ' ').gsub('+', ' ')
|
name = params[:name].gsub('%20', ' ').gsub('+', ' ')
|
||||||
artist = Artist.find_by_name(name)
|
artist = Artist.find_by_name(name)
|
||||||
unless artist
|
unless artist
|
||||||
results = ArtistController.musicBrainzSearch(name)
|
results = ArtistController.musicBrainzExactSearch(name)
|
||||||
if results[0] == name
|
if results.empty?
|
||||||
|
render :json => {status: 'not founds'}
|
||||||
|
return
|
||||||
|
elsif results[0][:name] == name
|
||||||
ImportController.importArtist(name)
|
ImportController.importArtist(name)
|
||||||
render :json => {status: 'loaded'}
|
render :json => {status: 'loaded'}
|
||||||
return
|
return
|
||||||
elsif (results[0].downcase == name.downcase or results[0].downcase == 'the '+ name.downcase) and results[0] != name
|
elsif results[0][:name].downcase == name.downcase or results[0][:name].downcase == 'the '+ name.downcase
|
||||||
render :json => {status: 'corrected', page: results[0]}
|
render :json => {status: 'corrected', page: results[0][:name]}
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
render :json => {status: 'suggestions', values: results}
|
render :json => {status: 'suggestions', values: results}
|
||||||
|
@ -80,4 +83,36 @@ class ArtistController < ApplicationController
|
||||||
return {}
|
return {}
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
@ -4,11 +4,7 @@ class ImportController < ApplicationController
|
||||||
def self.importArtist name
|
def self.importArtist name
|
||||||
|
|
||||||
# Get artist info
|
# Get artist info
|
||||||
artist_mb_xml = open(
|
artist_mb_data = ArtistController.musicBrainzExactSearch(name).first
|
||||||
'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)
|
|
||||||
begin
|
begin
|
||||||
artist_lastfm_xml = open(
|
artist_lastfm_xml = open(
|
||||||
'http://ws.audioscrobbler.com/2.0/'+
|
'http://ws.audioscrobbler.com/2.0/'+
|
||||||
|
@ -24,16 +20,16 @@ class ImportController < ApplicationController
|
||||||
|
|
||||||
# Save artist
|
# Save artist
|
||||||
artist = Artist.new
|
artist = Artist.new
|
||||||
artist.name = artist_mb_data[0][2]
|
artist.name = artist_mb_data[:name]
|
||||||
artist.desc = artist_desc
|
artist.desc = artist_desc
|
||||||
artist.pic_url = artist_pic
|
artist.pic_url = artist_pic
|
||||||
artist.artist_type = artist_mb_data[0][0]
|
artist.artist_type = artist_mb_data[:type]
|
||||||
artist.mbid = artist_mb_data[0][1]
|
artist.mbid = artist_mb_data[:mbid]
|
||||||
artist.save
|
artist.save
|
||||||
|
|
||||||
# Get albums from MB
|
# Get albums from MB
|
||||||
release_groups_mb_xml = open(
|
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
|
'User-Agent' => @@user_agent
|
||||||
).read
|
).read
|
||||||
release_groups_mb_data = release_groups_mb_xml.scan(/<release-group\stype=\"([a-zA-Z]+?)\"\sid=\"([a-f0-9-]+?)\"><title>(.+?)<\/title>/m)
|
release_groups_mb_data = release_groups_mb_xml.scan(/<release-group\stype=\"([a-zA-Z]+?)\"\sid=\"([a-f0-9-]+?)\"><title>(.+?)<\/title>/m)
|
||||||
|
|
|
@ -1,14 +1,9 @@
|
||||||
# SQLite version 3.x
|
|
||||||
# gem install sqlite3
|
|
||||||
development:
|
development:
|
||||||
adapter: sqlite3
|
adapter: sqlite3
|
||||||
database: db/development.sqlite3
|
database: db/development.sqlite3
|
||||||
pool: 5
|
pool: 5
|
||||||
timeout: 5000
|
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:
|
test:
|
||||||
adapter: sqlite3
|
adapter: sqlite3
|
||||||
database: db/test.sqlite3
|
database: db/test.sqlite3
|
||||||
|
@ -16,15 +11,4 @@ test:
|
||||||
timeout: 5000
|
timeout: 5000
|
||||||
|
|
||||||
production:
|
production:
|
||||||
adapter: sqlite3
|
adapter: postgresql
|
||||||
database: db/production.sqlite3
|
|
||||||
pool: 5
|
|
||||||
timeout: 5000
|
|
||||||
|
|
||||||
|
|
||||||
musicbrainz:
|
|
||||||
host: 192.168.0.128
|
|
||||||
adapter: postgresql
|
|
||||||
database: beathaven
|
|
||||||
username: postgres
|
|
||||||
password: password
|
|
|
@ -3,9 +3,10 @@ require 'open-uri'
|
||||||
namespace :db do
|
namespace :db do
|
||||||
desc 'Imports test data from MusicBrainz database'
|
desc 'Imports test data from MusicBrainz database'
|
||||||
task :import => :environment do
|
task :import => :environment do
|
||||||
ati = ['The White Stripes']
|
ati = ['KISS']
|
||||||
ati.each do |name|
|
ati.each do |name|
|
||||||
ImportController.importArtist(name)
|
ImportController.importArtist(name)
|
||||||
|
#ap res = ArtistController.musicBrainzExactSearch(name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -8,15 +8,15 @@
|
||||||
<div>Misspelled?</div>
|
<div>Misspelled?</div>
|
||||||
<ul>
|
<ul>
|
||||||
<li><a class="data artist">The Raconters</a></li>
|
<li><a class="data artist">The Raconters</a></li>
|
||||||
<li>The Raconteurs</li>
|
<li><a class="data artist">The Raconteurs</a></li>
|
||||||
<li>The Encounters</li>
|
<li><a class="data artist">The Encounters</a></li>
|
||||||
<li>Encounters</li>
|
<li><a class="data artist">Encounters</a></li>
|
||||||
<li>The Encounter</li>
|
<li><a class="data artist">The Encounter</a></li>
|
||||||
<li>Carpenters</li>
|
<li><a class="data artist">Carpenters</a></li>
|
||||||
<li>The Dearhunters</li>
|
<li><a class="data artist">The Dearhunters</a></li>
|
||||||
<li>Jamhunters</li>
|
<li><a class="data artist">Jamhunters</a></li>
|
||||||
<li>The Go Go Haunters</li>
|
<li><a class="data artist">The Go Go Haunters</a></li>
|
||||||
<li>The Beathunters</li>
|
<li><a class="data artist">The Beathunters</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
|
@ -16,11 +16,12 @@ var Search = {
|
||||||
for (var i = 0; i < values.length; i++) {
|
for (var i = 0; i < values.length; i++) {
|
||||||
$('.suggestions ul').append('\
|
$('.suggestions ul').append('\
|
||||||
<li>\
|
<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>\
|
</li>\
|
||||||
');
|
');
|
||||||
}
|
}
|
||||||
$('.suggestions').show();
|
$('.suggestions').css('margin-left', $('#search_field').offset().left +'px').show();
|
||||||
},
|
},
|
||||||
|
|
||||||
hideSuggestions: function() {
|
hideSuggestions: function() {
|
||||||
|
|
|
@ -42,10 +42,8 @@
|
||||||
|
|
||||||
.suggestions {
|
.suggestions {
|
||||||
display: none;
|
display: none;
|
||||||
position: absolute;
|
margin: 30px 0 0 250px;
|
||||||
left: 50%;
|
width: 500px;
|
||||||
margin: 30px 0 0 -380px;
|
|
||||||
width: 400px;
|
|
||||||
text-align: left;
|
text-align: left;
|
||||||
font-size: 24px;
|
font-size: 24px;
|
||||||
}
|
}
|
||||||
|
@ -61,4 +59,7 @@
|
||||||
color: #04F;
|
color: #04F;
|
||||||
border-bottom: #04F 1px dotted;
|
border-bottom: #04F 1px dotted;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
.suggestions li span {
|
||||||
|
font-size: 14px;
|
||||||
}
|
}
|
Loading…
Reference in New Issue