Artist controller indentation fix and cleanup
This commit is contained in:
parent
52df1f69c4
commit
82506f51f8
|
@ -1,4 +1,5 @@
|
||||||
require 'open-uri'
|
require 'open-uri'
|
||||||
|
require 'musicbrainz'
|
||||||
|
|
||||||
class ArtistController < ApplicationController
|
class ArtistController < ApplicationController
|
||||||
@@default_album_types = ['Album', 'Soundtrack']
|
@@default_album_types = ['Album', 'Soundtrack']
|
||||||
|
@ -23,7 +24,7 @@ class ArtistController < ApplicationController
|
||||||
|
|
||||||
# Artist not found
|
# Artist not found
|
||||||
unless @artist
|
unless @artist
|
||||||
results = ArtistController.musicBrainzExactSearch(name)
|
results = MusicBrainz::Artist.search(name)
|
||||||
if results.empty?
|
if results.empty?
|
||||||
@data[:status] = 'not_found'
|
@data[:status] = 'not_found'
|
||||||
render :json => @data
|
render :json => @data
|
||||||
|
@ -35,6 +36,7 @@ class ArtistController < ApplicationController
|
||||||
# Saving artist and queueing job
|
# Saving artist and queueing job
|
||||||
@artist = Artist.new
|
@artist = Artist.new
|
||||||
@artist.name = name
|
@artist.name = name
|
||||||
|
@artist.mbid = results[0][:mbid]
|
||||||
@artist.status = 0
|
@artist.status = 0
|
||||||
@artist.save
|
@artist.save
|
||||||
Delayed::Job.enqueue LoadArtistJob.new(name)
|
Delayed::Job.enqueue LoadArtistJob.new(name)
|
||||||
|
@ -110,69 +112,24 @@ class ArtistController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def autocomplete
|
def autocomplete
|
||||||
autocomplete = getLastFmAutocomplete(params[:query])
|
autocomplete = getLastFmAutocomplete(params[:query])
|
||||||
return render :nothing => true if autocomplete.nil?
|
return render :nothing => true if autocomplete.nil?
|
||||||
suggestions = []
|
suggestions = []
|
||||||
autocomplete["response"]["docs"].each do |doc|
|
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
|
end
|
||||||
render :json => {
|
render :json => {
|
||||||
:query => params[:query],
|
:query => params[:query],
|
||||||
:suggestions => suggestions.take(5)
|
:suggestions => suggestions.take(5)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def getLastFmAutocomplete query
|
def getLastFmAutocomplete query
|
||||||
return nil if query.nil? or query.strip.empty?
|
return nil if query.nil? or query.strip.empty?
|
||||||
json = ActiveSupport::JSON.decode(open(
|
json = ActiveSupport::JSON.decode(open(
|
||||||
'http://www.last.fm/search/autocomplete' <<
|
'http://www.last.fm/search/autocomplete' <<
|
||||||
'?rows=30&q=' << URI.escape(query)
|
'?rows=30&q=' << URI.escape(query)
|
||||||
).read)
|
).read)
|
||||||
json.empty? ? nil : json
|
json.empty? ? nil : json
|
||||||
end
|
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
|
||||||
|
|
Loading…
Reference in New Issue