From 10aef4e15a1cc5a54bd2c20b8fdba15eba37251a Mon Sep 17 00:00:00 2001 From: magnolia-fan Date: Sat, 18 Jun 2011 19:58:51 +0400 Subject: [PATCH] Search fixes --- app/controllers/artist_controller.rb | 43 +++++++++++++++++++++++--- app/controllers/import_controller.rb | 14 +++------ config/database.yml | 18 +---------- lib/tasks/db_import.rake | 3 +- public/demo/search.html | 18 +++++------ public/javascripts/beathaven/search.js | 5 +-- public/stylesheets/search.css | 9 +++--- 7 files changed, 64 insertions(+), 46 deletions(-) diff --git a/app/controllers/artist_controller.rb b/app/controllers/artist_controller.rb index 015126e..f7d0562 100644 --- a/app/controllers/artist_controller.rb +++ b/app/controllers/artist_controller.rb @@ -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 diff --git a/app/controllers/import_controller.rb b/app/controllers/import_controller.rb index 974b7ed..1774013 100644 --- a/app/controllers/import_controller.rb +++ b/app/controllers/import_controller.rb @@ -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(/.*?(.+?)<\/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(/(.+?)<\/title>/m) diff --git a/config/database.yml b/config/database.yml index c631e65..aa6a16c 100644 --- a/config/database.yml +++ b/config/database.yml @@ -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 \ No newline at end of file + adapter: postgresql \ No newline at end of file diff --git a/lib/tasks/db_import.rake b/lib/tasks/db_import.rake index 51af1d2..88afac9 100644 --- a/lib/tasks/db_import.rake +++ b/lib/tasks/db_import.rake @@ -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 \ No newline at end of file diff --git a/public/demo/search.html b/public/demo/search.html index 5aac29b..e83acab 100644 --- a/public/demo/search.html +++ b/public/demo/search.html @@ -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> \ No newline at end of file diff --git a/public/javascripts/beathaven/search.js b/public/javascripts/beathaven/search.js index 3a7693e..970e61f 100644 --- a/public/javascripts/beathaven/search.js +++ b/public/javascripts/beathaven/search.js @@ -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() { diff --git a/public/stylesheets/search.css b/public/stylesheets/search.css index 7c9756f..e6c7c60 100644 --- a/public/stylesheets/search.css +++ b/public/stylesheets/search.css @@ -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; } \ No newline at end of file