oldhaven/app/controllers/artist_controller.rb

54 lines
1.6 KiB
Ruby
Raw Normal View History

2011-06-14 22:06:46 +04:00
class ArtistController < ApplicationController
2011-06-15 00:01:42 +04:00
def data
2011-09-10 02:13:02 +04:00
# Bad params
if params[:artist].nil? or params[:artist].empty?
return render json: { status: 'loading_failed' }, status: 404
2011-06-29 08:11:54 +04:00
end
2011-09-10 02:13:02 +04:00
# Searching for artist
2011-10-20 15:01:57 +04:00
artist_name = get_artist_name_from_query
@artist = Artist.find_by_name(artist_name)
2011-09-10 02:13:02 +04:00
2011-10-20 15:01:57 +04:00
# Artist not found in DB
2011-09-10 02:13:02 +04:00
unless @artist
2011-10-20 15:01:57 +04:00
results = MusicBrainz::Artist.search(artist_name)
2011-06-18 19:58:51 +04:00
if results.empty?
return render json: { status: 'not_found' }, status: 404
2011-10-20 15:01:57 +04:00
end
2011-11-24 02:12:15 +04:00
best_match = results[0][:sort_name]
2011-10-20 15:06:26 +04:00
if best_match != artist_name and similar_names(artist_name, best_match)
2011-10-20 15:02:39 +04:00
return render json: { status: 'corrected', correct_name: best_match }
2011-10-20 15:20:19 +04:00
elsif best_match == artist_name
2011-10-20 15:01:57 +04:00
queue_loading(artist_name, results[0][:mbid])
else
2011-09-10 02:13:02 +04:00
@suggestions = results.take(5)
2011-10-20 15:02:39 +04:00
return render json: { status: 'suggestions', html: render_compact_partial(:suggestions) }
end
2011-09-10 02:13:02 +04:00
end
compile_page(
2011-11-28 16:24:47 +04:00
data: @artist,
partial: "artist/page",
title: @artist.name,
2011-10-20 15:06:26 +04:00
status: @artist.status_str,
2011-11-28 16:24:47 +04:00
callback: {object: :player, action: :updateLibrary},
cache_for: (1.day if @artist.status_str == "ok")
)
2011-09-10 02:13:02 +04:00
end
2011-11-30 12:54:40 +04:00
def search
redirect_to "/#/artist/#{params[:q].gsub(/\s/, '+')}"
end
2011-10-20 15:01:57 +04:00
private
2011-09-10 02:13:02 +04:00
2011-10-20 15:01:57 +04:00
def queue_loading artist_name, mbid
2011-10-20 15:02:39 +04:00
@artist = Artist.create( name: artist_name, mbid: mbid, status: 0 )
2011-10-20 15:01:57 +04:00
Delayed::Job.enqueue(LoadArtistJob.new(artist_name))
2011-06-15 00:01:42 +04:00
end
2011-10-20 15:06:26 +04:00
def similar_names name1, name2
(name2.downcase == name1.downcase or name2.downcase == 'the '+ name1.downcase)
end
2011-06-14 22:06:46 +04:00
end