2011-06-15 04:38:29 +00:00
|
|
|
require 'uri'
|
|
|
|
require 'open-uri'
|
|
|
|
|
2011-06-14 18:06:46 +00:00
|
|
|
class ArtistController < ApplicationController
|
2011-06-14 20:01:42 +00:00
|
|
|
def data
|
|
|
|
data = {}
|
2011-06-15 04:38:29 +00:00
|
|
|
name = params[:name].gsub('%20', ' ').gsub('+', ' ')
|
|
|
|
artist = Artist.find_by_name(name)
|
|
|
|
unless artist
|
|
|
|
mb_artist = MusicBrainzArtist.getByName(name)
|
|
|
|
if mb_artist
|
|
|
|
ImportController.importArtist(name)
|
|
|
|
render :json => {error:'loading'}
|
|
|
|
return
|
|
|
|
else
|
|
|
|
render :json => {error:404}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2011-06-14 21:29:36 +00:00
|
|
|
data['artist'] = {name: artist.name, desc: ActionController::Base.helpers.strip_tags(artist.desc), pic: artist.pic_url}
|
2011-06-14 20:01:42 +00:00
|
|
|
data['albums'] = []
|
|
|
|
albums = artist.albums
|
|
|
|
albums.each do |album|
|
2011-06-17 21:49:32 +00:00
|
|
|
if album.album_type == 'Album'
|
|
|
|
tmp_album = {name: album.name, year: album.year, pic: album.pic_url}
|
|
|
|
album_tracks = []
|
|
|
|
bonus_tracks = []
|
|
|
|
album.tracks.each do |track|
|
|
|
|
tmp_track = {name: track.name, live: track.live, acoustic: track.acoustic}
|
|
|
|
if track.length
|
|
|
|
time = (track.length / 1000).round
|
|
|
|
time_m = (time / 60).floor
|
|
|
|
time_s = time - time_m * 60
|
|
|
|
tmp_track['duration'] = time_m.to_s + ':' + (time_s < 10 ? '0' : '') + time_s.to_s
|
|
|
|
else
|
|
|
|
tmp_track['duration'] = '0:00'
|
|
|
|
end
|
|
|
|
(track.bonus == 0 ? album_tracks : bonus_tracks) << tmp_track
|
2011-06-15 04:38:29 +00:00
|
|
|
end
|
2011-06-17 21:49:32 +00:00
|
|
|
tmp_album['tracks'] = {album: album_tracks, bonus: bonus_tracks}
|
|
|
|
data['albums'] << tmp_album
|
2011-06-14 20:01:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
render :json => data
|
|
|
|
end
|
2011-06-15 04:38:29 +00:00
|
|
|
|
|
|
|
def autocomplete
|
|
|
|
autocomplete = ArtistController.getLastFmAutocomplete(params[:query])
|
|
|
|
return render :nothing => true if autocomplete.nil?
|
|
|
|
suggestions = []
|
|
|
|
autocomplete["response"]["docs"].each do |doc|
|
2011-06-16 00:31:37 +00:00
|
|
|
suggestions << doc["artist"] unless suggestions.include?(doc["artist"]) or doc["artist"].nil?
|
2011-06-15 04:38:29 +00:00
|
|
|
end
|
|
|
|
render :json => {
|
|
|
|
:query => params[:query],
|
|
|
|
:suggestions => suggestions
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.getLastFmAutocomplete(query)
|
|
|
|
return nil if query.nil? or query.strip.empty?
|
|
|
|
json = ActiveSupport::JSON.decode(open(
|
|
|
|
'http://www.last.fm/search/autocomplete' <<
|
|
|
|
'?q=' << URI.escape(query)
|
|
|
|
).read)
|
|
|
|
return json.empty? ? nil : json
|
|
|
|
end
|
2011-06-14 18:06:46 +00:00
|
|
|
end
|