diff --git a/app/controllers/artist_controller.rb b/app/controllers/artist_controller.rb index 3599c52..13224f2 100644 --- a/app/controllers/artist_controller.rb +++ b/app/controllers/artist_controller.rb @@ -1,4 +1,5 @@ require 'open-uri' +require 'musicbrainz' class ArtistController < ApplicationController @@default_album_types = ['Album', 'Soundtrack'] @@ -23,7 +24,7 @@ class ArtistController < ApplicationController # Artist not found unless @artist - results = ArtistController.musicBrainzExactSearch(name) + results = MusicBrainz::Artist.search(name) if results.empty? @data[:status] = 'not_found' render :json => @data @@ -35,6 +36,7 @@ class ArtistController < ApplicationController # Saving artist and queueing job @artist = Artist.new @artist.name = name + @artist.mbid = results[0][:mbid] @artist.status = 0 @artist.save Delayed::Job.enqueue LoadArtistJob.new(name) @@ -110,69 +112,24 @@ class ArtistController < ApplicationController end def autocomplete - autocomplete = getLastFmAutocomplete(params[:query]) - return render :nothing => true if autocomplete.nil? - suggestions = [] + autocomplete = getLastFmAutocomplete(params[:query]) + return render :nothing => true if autocomplete.nil? + suggestions = [] autocomplete["response"]["docs"].each do |doc| - suggestions << doc["artist"] unless suggestions.include?(doc["artist"]) or doc["artist"].nil? or doc['restype'] != 6 + suggestions << doc["artist"] unless suggestions.include?(doc["artist"]) or doc["artist"].nil? or doc['restype'] != 6 end - render :json => { - :query => params[:query], - :suggestions => suggestions.take(5) - } - end - - def getLastFmAutocomplete query - return nil if query.nil? or query.strip.empty? - json = ActiveSupport::JSON.decode(open( - 'http://www.last.fm/search/autocomplete' << - '?rows=30&q=' << URI.escape(query) + render :json => { + :query => params[:query], + :suggestions => suggestions.take(5) + } + end + + def getLastFmAutocomplete query + return nil if query.nil? or query.strip.empty? + json = ActiveSupport::JSON.decode(open( + 'http://www.last.fm/search/autocomplete' << + '?rows=30&q=' << URI.escape(query) ).read) json.empty? ? nil : json - end - - def 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') +'~&limit=100').read) - artists = [] - response['artist-list']['artist'].each do |artist| - artists << artist['name'] unless artist['type'] == 'unknown' - end - artists.take(10) - rescue - {} - 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 - {} - end - end + end end