Mass artist controller refactoring
This commit is contained in:
@@ -15,12 +15,11 @@ class ApplicationController < ActionController::Base
|
||||
render :partial => 'about'
|
||||
end
|
||||
|
||||
protected
|
||||
protected
|
||||
|
||||
def authorize
|
||||
unless Vkontakte.check(params)
|
||||
render :json => { :status => 'login failed' }
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
@@ -40,4 +39,8 @@ class ApplicationController < ActionController::Base
|
||||
end
|
||||
cookies[:beathaven_sid]
|
||||
end
|
||||
|
||||
def render_compact_partial partial_name
|
||||
(render_to_string :partial => partial_name.to_s).gsub(/\n(\s+)?/, '')
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,121 +2,52 @@ require 'open-uri'
|
||||
require 'musicbrainz'
|
||||
|
||||
class ArtistController < ApplicationController
|
||||
@@default_album_types = [:album, :soundtrack]
|
||||
def data
|
||||
@data = {
|
||||
:status => '',
|
||||
:correct_name => '',
|
||||
:html => ''
|
||||
}
|
||||
@loading = false
|
||||
|
||||
# Bad params
|
||||
if params[:name].nil? or params[:name].length == 0
|
||||
@data[:status] = 'loading_failed';
|
||||
render :json => @data
|
||||
return
|
||||
if params[:name].nil? or params[:name].empty?
|
||||
return render :json => { :status => 'loading_failed' }
|
||||
end
|
||||
|
||||
# Searching for artist
|
||||
name = params[:name].gsub('%20', ' ').gsub('+', ' ').gsub('.html', '')
|
||||
@artist = Artist.find_by_name(name)
|
||||
artist_name = get_artist_name_from_query
|
||||
@artist = Artist.find_by_name(artist_name, include: {artist_links: {}, albums: {tracks: {}}})
|
||||
|
||||
# Artist not found
|
||||
# Artist not found in DB
|
||||
unless @artist
|
||||
results = MusicBrainz::Artist.search(name)
|
||||
results = MusicBrainz::Artist.search(artist_name)
|
||||
if results.empty?
|
||||
@data[:status] = 'not_found'
|
||||
render :json => @data
|
||||
elsif results[0][:name] != name and (results[0][:name].downcase == name.downcase or results[0][:name].downcase == 'the '+ name.downcase)
|
||||
@data[:status] = 'corrected'
|
||||
@data[:correct_name] = results[0][:name]
|
||||
render :json => @data
|
||||
elsif results[0][:name] == name
|
||||
# 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)
|
||||
# Rendering loading info
|
||||
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])
|
||||
@artist = { :artist => @artist, :albums => [] }
|
||||
@loading = true
|
||||
@data[:status] = 'loading'
|
||||
@data[:html] = (render_to_string :partial => 'page').gsub(/\n\s+/, '').gsub(/\n/, '')
|
||||
render :json => @data
|
||||
return render :json => { :status => 'loading', :html => render_compact_partial(:page) }
|
||||
else
|
||||
@suggestions = results.take(5)
|
||||
@data[:status] = 'suggestions'
|
||||
@data[:html] = (render_to_string :partial => 'suggestions').gsub(/\n\s+/, '').gsub(/\n/, '')
|
||||
render :json => @data
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
if @artist and @artist.status == 2
|
||||
@data[:status] = 'fail'
|
||||
@data[:html] = (render_to_string :partial => 'fail').gsub(/\n\s+/, '').gsub(/\n/, '')
|
||||
render :json => @data
|
||||
return
|
||||
end
|
||||
|
||||
if @artist.status == 1
|
||||
@data[:status] = 'ok'
|
||||
else
|
||||
@data[:status] = 'loading'
|
||||
@loading = true
|
||||
end
|
||||
|
||||
@data[:albums], @data[:html] = buildArtistHTML(@artist)
|
||||
render :json => @data
|
||||
end
|
||||
|
||||
def buildArtistHTML artist
|
||||
@artist[:artist] = {
|
||||
id: artist.id,
|
||||
name: artist.name,
|
||||
desc: ActionController::Base.helpers.strip_tags(artist.desc),
|
||||
pic: artist.pic_url,
|
||||
urls: artist.artist_links
|
||||
}
|
||||
|
||||
media_types = @@default_album_types
|
||||
session = Session.find_by_key(session_key)
|
||||
unless session.nil?
|
||||
media_types = session.user.music
|
||||
end
|
||||
|
||||
@artist[:albums] = []
|
||||
artist.albums.each do |album|
|
||||
if true # media_types.include? album.album_type.downcase.to_sym and album.status == 1
|
||||
tmp_album = {:id => album.id, :name => album.name, :year => album.year, :pic => album.pic_url, :tracks => []}
|
||||
album.tracks.each do |track|
|
||||
tmp_track = {id: track.id, name: track.name, live: track.live, acoustic: track.acoustic}
|
||||
tmp_track[:length] = track.length unless track.length.nil?
|
||||
tmp_track[:duration] = formatTrackDuration(track.length)
|
||||
tmp_track[:position] = track.position.to_s 36
|
||||
# tmp_track[:mbid] = track.mbid
|
||||
# (track.bonus == 0 ? album_tracks : bonus_tracks) << tmp_track
|
||||
tmp_album[:tracks] << tmp_track
|
||||
end
|
||||
# tmp_album[:tracks] = {album: album_tracks, bonus: bonus_tracks}
|
||||
@artist[:albums] << tmp_album unless tmp_album[:tracks].empty?
|
||||
return render :json => { :status => 'suggestions', :html => render_compact_partial(:suggestions) }
|
||||
end
|
||||
end
|
||||
|
||||
return @artist[:albums], (render_to_string :partial => 'page').gsub(/\n\s+/, '').gsub(/\n/, '')
|
||||
# Artist loading failed
|
||||
if @artist.status == 2
|
||||
return render :json => { :status => 'fail', :html => render_compact_partial(:fail) }
|
||||
end
|
||||
|
||||
render json: { status: @artist.status_str, artist: @artist, html: render_compact_partial(:page) }, include: {albums: {include: {tracks: {}}}}
|
||||
end
|
||||
|
||||
def formatTrackDuration length
|
||||
if length
|
||||
time = length # (length / 1000).round
|
||||
time_m = (time / 60).floor
|
||||
time_s = time - time_m * 60
|
||||
time_m.to_s + ':' + (time_s < 10 ? '0' : '') + time_s.to_s
|
||||
else
|
||||
'0:00'
|
||||
end
|
||||
private
|
||||
|
||||
def get_artist_name_from_query
|
||||
params[:name].gsub('%20', ' ').gsub('+', ' ').gsub('.html', '')
|
||||
end
|
||||
|
||||
def queue_loading artist_name, mbid
|
||||
@artist = Artist.create( :name => artist_name, :mbid => mbid, :status => 0 )
|
||||
Delayed::Job.enqueue(LoadArtistJob.new(artist_name))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -8,8 +8,7 @@ class TrackController < ApplicationController
|
||||
if track.length == nil or track.length.to_i == 0 and params[:length].to_i > 0
|
||||
track.length = params[:length].to_i
|
||||
track.save
|
||||
render :json => { :result => :success }
|
||||
return
|
||||
return render :json => { :result => :success }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -21,8 +20,7 @@ class TrackController < ApplicationController
|
||||
unless track.nil? or track.available
|
||||
track.available = true
|
||||
track.save
|
||||
render :json => { :status => :success }
|
||||
return
|
||||
return render :json => { :status => :success }
|
||||
end
|
||||
render :json => { :status => :failed }
|
||||
end
|
||||
@@ -32,8 +30,7 @@ class TrackController < ApplicationController
|
||||
unless track.nil?
|
||||
# Saving track file
|
||||
vote(params[:track_id], params[:owner_id], params[:audio_id], -1)
|
||||
render :json => { :status => :success }
|
||||
return
|
||||
return render :json => { :status => :success }
|
||||
end
|
||||
render :json => { :status => :failed }
|
||||
end
|
||||
|
||||
@@ -5,10 +5,7 @@ class UserController < ApplicationController
|
||||
@res = {:status => 'hello', :newbie => false}
|
||||
user = User.find_by_vkid(params[:mid])
|
||||
if user.nil?
|
||||
user = User.new
|
||||
user.vkid = params[:mid]
|
||||
user.lang = 'ru'
|
||||
user.save
|
||||
user = User.new(:vkid => params[:mid], :lang => 'ru').save
|
||||
@res[:newbie] = true
|
||||
end
|
||||
|
||||
@@ -17,8 +14,7 @@ class UserController < ApplicationController
|
||||
session.key = session_key
|
||||
session.save
|
||||
@res[:ok_reload] = true
|
||||
render :json => @res
|
||||
return
|
||||
return render :json => @res
|
||||
end
|
||||
|
||||
@res[:user] = {
|
||||
@@ -52,7 +48,6 @@ class UserController < ApplicationController
|
||||
if update_params.include? :show
|
||||
update_params[:show] = update_params[:show].map{ |k, v| k.to_sym }
|
||||
end
|
||||
|
||||
params[:params].each do |k, v|
|
||||
user[k] = v
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user