1
0
Fork 0
oldhaven/app/controllers/artist_controller.rb

145 lines
4.6 KiB
Ruby

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
end
# Searching for artist
name = params[:name].gsub('%20', ' ').gsub('+', ' ').gsub('.html', '')
@artist = Artist.find_by_name(name)
# Artist not found
unless @artist
results = MusicBrainz::Artist.search(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
@artist = { :artist => @artist, :albums => [] }
@loading = true
@data[:status] = 'loading'
@data[:html] = (render_to_string :partial => 'page').gsub(/\n\s+/, '').gsub(/\n/, '')
render :json => @data
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
}
ap 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 / 1000).round 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?
end
end
return @artist[:albums], (render_to_string :partial => 'page').gsub(/\n\s+/, '').gsub(/\n/, '')
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
end
def autocomplete
autocomplete = getLastFmAutocomplete(params[:query])
return render :nothing => true if autocomplete.nil?
suggestions = []
autocomplete["response"]["docs"].each do |doc|
suggestions << doc["artist"] unless suggestions.include?(doc["artist"]) or doc["artist"].nil? or doc['restype'] != 6
end
render :json => {
:query => params[:query],
:suggestions => suggestions.take(5)
}
end
def getLastFmAutocomplete query
return nil if query.nil? or query.strip.empty?
json = ActiveSupport::JSON.decode(open(
'http://www.last.fm/search/autocomplete' <<
'?rows=30&q=' << URI.escape(query)
).read)
json.empty? ? nil : json
end
end