2011-06-15 04:38:29 +00:00
|
|
|
require 'open-uri'
|
2011-09-16 11:35:12 +00:00
|
|
|
require 'musicbrainz'
|
2011-06-15 04:38:29 +00:00
|
|
|
|
2011-06-14 18:06:46 +00:00
|
|
|
class ArtistController < ApplicationController
|
2011-06-14 20:01:42 +00:00
|
|
|
def data
|
2011-09-09 22:13:02 +00:00
|
|
|
# Bad params
|
2011-10-20 11:01:57 +00:00
|
|
|
if params[:name].nil? or params[:name].empty?
|
|
|
|
return render :json => { :status => 'loading_failed' }
|
2011-06-29 04:11:54 +00:00
|
|
|
end
|
2011-09-09 22:13:02 +00:00
|
|
|
|
|
|
|
# Searching for artist
|
2011-10-20 11:01:57 +00:00
|
|
|
artist_name = get_artist_name_from_query
|
|
|
|
@artist = Artist.find_by_name(artist_name, include: {artist_links: {}, albums: {tracks: {}}})
|
2011-09-09 22:13:02 +00:00
|
|
|
|
2011-10-20 11:01:57 +00:00
|
|
|
# Artist not found in DB
|
2011-09-09 22:13:02 +00:00
|
|
|
unless @artist
|
2011-10-20 11:01:57 +00:00
|
|
|
results = MusicBrainz::Artist.search(artist_name)
|
2011-06-18 15:58:51 +00:00
|
|
|
if results.empty?
|
2011-10-20 11:01:57 +00:00
|
|
|
return render :json => { :status => 'not_found' }
|
|
|
|
end
|
|
|
|
best_match = results[0][:name]
|
|
|
|
if best_match != artist_name and (best_match.downcase == artist_name.downcase or best_match.downcase == 'the '+ artist_name.downcase)
|
|
|
|
return render :json => { :status => 'corrected', :correct_name => best_match }
|
|
|
|
elsif results[0][:name] == artist_name
|
|
|
|
queue_loading(artist_name, results[0][:mbid])
|
2011-09-09 22:13:02 +00:00
|
|
|
@artist = { :artist => @artist, :albums => [] }
|
|
|
|
@loading = true
|
2011-10-20 11:01:57 +00:00
|
|
|
return render :json => { :status => 'loading', :html => render_compact_partial(:page) }
|
2011-06-15 04:38:29 +00:00
|
|
|
else
|
2011-09-09 22:13:02 +00:00
|
|
|
@suggestions = results.take(5)
|
2011-10-20 11:01:57 +00:00
|
|
|
return render :json => { :status => 'suggestions', :html => render_compact_partial(:suggestions) }
|
2011-06-15 04:38:29 +00:00
|
|
|
end
|
2011-09-09 22:13:02 +00:00
|
|
|
end
|
|
|
|
|
2011-10-20 11:01:57 +00:00
|
|
|
# Artist loading failed
|
|
|
|
if @artist.status == 2
|
|
|
|
return render :json => { :status => 'fail', :html => render_compact_partial(:fail) }
|
2011-06-15 04:38:29 +00:00
|
|
|
end
|
2011-09-14 18:16:06 +00:00
|
|
|
|
2011-10-20 11:01:57 +00:00
|
|
|
render json: { status: @artist.status_str, artist: @artist, html: render_compact_partial(:page) }, include: {albums: {include: {tracks: {}}}}
|
2011-09-09 22:13:02 +00:00
|
|
|
end
|
|
|
|
|
2011-10-20 11:01:57 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def get_artist_name_from_query
|
|
|
|
params[:name].gsub('%20', ' ').gsub('+', ' ').gsub('.html', '')
|
2011-09-09 22:13:02 +00:00
|
|
|
end
|
|
|
|
|
2011-10-20 11:01:57 +00:00
|
|
|
def queue_loading artist_name, mbid
|
|
|
|
@artist = Artist.create( :name => artist_name, :mbid => mbid, :status => 0 )
|
|
|
|
Delayed::Job.enqueue(LoadArtistJob.new(artist_name))
|
2011-06-14 20:01:42 +00:00
|
|
|
end
|
2011-06-14 18:06:46 +00:00
|
|
|
end
|