1
0
Fork 0

Artist controller indentation fix and cleanup

This commit is contained in:
Gregory Eremin 2011-09-16 15:35:12 +04:00
parent 52df1f69c4
commit 82506f51f8
1 changed files with 19 additions and 62 deletions

View File

@ -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)
@ -130,49 +132,4 @@ class ArtistController < ApplicationController
).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