diff --git a/app/controllers/artist_controller.rb b/app/controllers/artist_controller.rb index c8d7f2a..53daa41 100644 --- a/app/controllers/artist_controller.rb +++ b/app/controllers/artist_controller.rb @@ -1,4 +1,3 @@ -require 'uri' require 'open-uri' class ArtistController < ApplicationController @@ -8,15 +7,15 @@ class ArtistController < ApplicationController artist = Artist.find_by_name(name) unless artist results = ArtistController.musicBrainzSearch(name) - if results[0].downcase == name.downcase and results[0] != name - render :json => {correct: results[0]} - return - elsif results[0] == name + if results[0] == name ImportController.importArtist(name) - render :json => {status: 'loading'} + 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]} return else - render :json => {suggestions: results} + render :json => {status: 'suggestions', values: results} return end end @@ -71,12 +70,12 @@ class ArtistController < ApplicationController def self.musicBrainzSearch(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') +'~').read) + response = ActiveSupport::JSON.decode(open('http://search.test.musicbrainz.org/ws/2/artist/?fmt=json&query='+ URI.escape(query).gsub(/\&/, '%26').gsub(/\?/, '%3F') +'~&limit=100').read) artists = [] response['artist-list']['artist'].each do |artist| - artists << artist['name'] + artists << artist['name'] unless artist['type'] == 'unknown' end - return artists + artists.take(10) rescue return {} end diff --git a/app/controllers/import_controller.rb b/app/controllers/import_controller.rb index 5d5a760..974b7ed 100644 --- a/app/controllers/import_controller.rb +++ b/app/controllers/import_controller.rb @@ -5,21 +5,28 @@ class ImportController < ApplicationController # Get artist info artist_mb_xml = open( - 'http://musicbrainz.org/ws/2/artist/?query='+ URI.escape(name).gsub(/\&/, '%26').gsub(/\?/, '%3F') +'&limit=1', + '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_lastfm_xml = open( - 'http://ws.audioscrobbler.com/2.0/'+ - '?method=artist.getinfo&api_key='+ @@lastfm_api_key + - '&artist='+ URI.escape(name).gsub(/\&/, '%26').gsub(/\?/, '%3F') - ).read + begin + artist_lastfm_xml = open( + 'http://ws.audioscrobbler.com/2.0/'+ + '?method=artist.getinfo&api_key='+ @@lastfm_api_key + + '&artist='+ URI.escape(name).gsub(/\&/, '%26').gsub(/\?/, '%3F') + ).read + artist_desc = artist_lastfm_xml.scan(/<\!\[CDATA\[?(.*)\]\]><\/summary>/m)[0][0] + artist_pic = artist_lastfm_xml.scan(/?(.*)<\/image>/)[0][0] + rescue + artist_desc = '' + artist_pic = '' + end # Save artist artist = Artist.new artist.name = artist_mb_data[0][2] - artist.desc = artist_lastfm_xml.scan(/<\!\[CDATA\[?(.*)\]\]><\/summary>/m)[0][0] - artist.pic_url = artist_lastfm_xml.scan(/?(.*)<\/image>/)[0][0] + 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.save @@ -42,6 +49,8 @@ class ImportController < ApplicationController releases_mb_data.each do |item| if item[2].length == 4 item[2] += '-01-01' + elsif item[2].length == 7 + item[2] += '-01' end end diff --git a/config/routes.rb b/config/routes.rb index d19efb3..037e429 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -57,6 +57,5 @@ Beathaven::Application.routes.draw do # match ':controller(/:action(/:id(.:format)))' match 'artist/autocomplete' => 'artist#autocomplete' - match 'artist/search' => 'artist#search' match 'artist/(:name)/' => 'artist#data', :constraints => { :name => /[^\/]*/ } end diff --git a/lib/tasks/db_import.rake b/lib/tasks/db_import.rake index 09f0170..51af1d2 100644 --- a/lib/tasks/db_import.rake +++ b/lib/tasks/db_import.rake @@ -3,7 +3,7 @@ require 'open-uri' namespace :db do desc 'Imports test data from MusicBrainz database' task :import => :environment do - ati = ['Jet'] + ati = ['The White Stripes'] ati.each do |name| ImportController.importArtist(name) end diff --git a/public/demo/index.html b/public/demo/index.html index ad64a32..70fd908 100644 --- a/public/demo/index.html +++ b/public/demo/index.html @@ -4,6 +4,7 @@ BetaHaven + @@ -13,6 +14,7 @@ + diff --git a/public/demo/search.html b/public/demo/search.html index a7effbf..5aac29b 100644 --- a/public/demo/search.html +++ b/public/demo/search.html @@ -4,4 +4,19 @@ +
+
Misspelled?
+
    +
  • The Raconters
  • +
  • The Raconteurs
  • +
  • The Encounters
  • +
  • Encounters
  • +
  • The Encounter
  • +
  • Carpenters
  • +
  • The Dearhunters
  • +
  • Jamhunters
  • +
  • The Go Go Haunters
  • +
  • The Beathunters
  • +
+
\ No newline at end of file diff --git a/public/javascripts/beathaven/ajax.js b/public/javascripts/beathaven/ajax.js index 4b42cab..988ae07 100644 --- a/public/javascripts/beathaven/ajax.js +++ b/public/javascripts/beathaven/ajax.js @@ -1,20 +1,25 @@ var Ajax = { loadArtistData: function(name) { - $('#search-container input').attr('disabled', 'disabled').blur(); - $('#search-container img').show(); + Search.showSpinner(); name = name.split(' ').join('+'); - Ajax.setArchor('/artist/'+ name +'/'); $.get('/artist/'+ name +'/', function(data){ - if (typeof data.error != 'undefined') { - if (data.error == 'loading') { + if (typeof data.status != 'undefined') { + if (data.status == 'loaded') { Ajax.loadArtistData(name); - } else if (data.error == 404) { + } else if (data.status == 'corrected') { + Ajax.loadArtistData(data.page); + } else if (data.status == 'suggestions') { + Search.hideSpinner(); + Search.showSuggestions(data.values); + } else if (data.status == 'error') { Ajax.load404Page(); } return false; + } else { + Ajax.setArchor('/artist/'+ name +'/'); + Pages.renderArtist(data); + beathaven.redrawScrollbar(); } - Pages.renderArtist(data); - beathaven.redrawScrollbar(); }) }, @@ -75,14 +80,5 @@ $(function(){ Ajax.loadArtistData($(this).html()); return false; }); - $('.search').live('click', function(){ - Ajax.loadSearchPage(); - return false; - }); - $('#search_form').live('submit', function(){ - $('.autocomplete-container').remove(); - Ajax.loadArtistData($('#search_field').val()); - return false; - }); $(window).bind('hashchange', Ajax.detectPage); }) \ No newline at end of file diff --git a/public/javascripts/beathaven/search.js b/public/javascripts/beathaven/search.js new file mode 100644 index 0000000..3a7693e --- /dev/null +++ b/public/javascripts/beathaven/search.js @@ -0,0 +1,46 @@ +var Search = { + + showSpinner: function() { + $('#search-container input').attr('disabled', 'disabled').blur(); + $('#search-container img').show(); + Search.hideSuggestions(); + }, + + hideSpinner: function() { + $('#search-container input').removeAttr('disabled'); + $('#search_field').focus(); + $('#search-container img').hide(); + }, + + showSuggestions: function(values) { + for (var i = 0; i < values.length; i++) { + $('.suggestions ul').append('\ +
  • \ + '+ values[i] +'\ +
  • \ + '); + } + $('.suggestions').show(); + }, + + hideSuggestions: function() { + $('.suggestions ul li').remove(); + $('.suggestions').hide(); + } +} + +$(function(){ + $('.search').live('click', function(){ + Ajax.loadSearchPage(); + return false; + }); + $('#search_form').live('submit', function(){ + $('.autocomplete-container').remove(); + Ajax.loadArtistData($('#search_field').val()); + return false; + }); + $('.suggestions a').live('click', function(){ + $('#search_field').val($(this).text()); + return false; + }); +}); \ No newline at end of file diff --git a/public/stylesheets/misc.css b/public/stylesheets/misc.css index 3e445a0..1bb6235 100644 --- a/public/stylesheets/misc.css +++ b/public/stylesheets/misc.css @@ -79,48 +79,6 @@ background: #AAA; } -#search-container { - width: 100%; - text-align: center; - margin-top: 50px; -} - #search-container #search_field { - width: 300px; - font-size: 24px; - padding: 4px; - border: #CCC 1px solid; - -webkit-border-radius: 3px; - -moz-border-radius: 3px; - border-radius: 3px; - } - #search-container #search_button { - width: 100px; - font-size: 24px; - padding: 4px; - background-color: #DDD; - border: #CCC 1px solid; - -webkit-border-radius: 3px; - -moz-border-radius: 3px; - border-radius: 3px; - } - #search-container img { - margin-top: 30px; - display: none; - } - -.autocomplete { - margin: 4px 0 0 -1px; -} - .autocomplete div { - font-size: 24px; - padding: 6px 5px; - background-color: #FAFAFA; - margin-bottom: 1px; - } - .autocomplete div.selected { - background-color: #EAEAEA; - } - #error_page { position: relative; width: 600px; diff --git a/public/stylesheets/search.css b/public/stylesheets/search.css new file mode 100644 index 0000000..7c9756f --- /dev/null +++ b/public/stylesheets/search.css @@ -0,0 +1,64 @@ +#search-container { + width: 100%; + text-align: center; + margin-top: 50px; +} + #search-container #search_field { + width: 300px; + font-size: 24px; + padding: 4px; + border: #CCC 1px solid; + -webkit-border-radius: 3px; + -moz-border-radius: 3px; + border-radius: 3px; + } + #search-container #search_button { + width: 100px; + font-size: 24px; + padding: 4px; + background-color: #DDD; + border: #CCC 1px solid; + -webkit-border-radius: 3px; + -moz-border-radius: 3px; + border-radius: 3px; + } + #search-container img { + margin-top: 30px; + display: none; + } + +.autocomplete { + margin: 4px 0 0 -1px; +} + .autocomplete div { + font-size: 24px; + padding: 6px 5px; + background-color: #FAFAFA; + margin-bottom: 1px; + } + .autocomplete div.selected { + background-color: #EAEAEA; + } + +.suggestions { + display: none; + position: absolute; + left: 50%; + margin: 30px 0 0 -380px; + width: 400px; + text-align: left; + font-size: 24px; +} + .suggestions div { + margin-bottom: 20px; + } + .suggestions ul li { + display: block; + width: auto; + margin-bottom: 6px; + } + .suggestions .data.artist { + color: #04F; + border-bottom: #04F 1px dotted; + cursor: pointer; + } \ No newline at end of file