Autocomplete, error pages, anchors, lots morrrr
This commit is contained in:
@@ -1,7 +1,22 @@
|
||||
require 'uri'
|
||||
require 'open-uri'
|
||||
|
||||
class ArtistController < ApplicationController
|
||||
def data
|
||||
data = {}
|
||||
artist = Artist.find_by_name(params[:name].gsub('+', ' '))
|
||||
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
|
||||
data['artist'] = {name: artist.name, desc: ActionController::Base.helpers.strip_tags(artist.desc), pic: artist.pic_url}
|
||||
data['albums'] = []
|
||||
albums = artist.albums
|
||||
@@ -11,10 +26,14 @@ class ArtistController < ApplicationController
|
||||
bonus_tracks = []
|
||||
album.tracks.each do |track|
|
||||
tmp_track = {name: track.name, live: track.live, acoustic: track.acoustic}
|
||||
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
|
||||
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
|
||||
end
|
||||
tmp_album['tracks'] = {album: album_tracks, bonus: bonus_tracks}
|
||||
@@ -22,4 +41,26 @@ class ArtistController < ApplicationController
|
||||
end
|
||||
render :json => data
|
||||
end
|
||||
|
||||
def autocomplete
|
||||
autocomplete = ArtistController.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"])
|
||||
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
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user