2011-06-15 04:38:29 +00:00
|
|
|
require 'open-uri'
|
|
|
|
|
2011-06-14 18:06:46 +00:00
|
|
|
class ArtistController < ApplicationController
|
2011-06-24 09:36:44 +00:00
|
|
|
@@default_album_types = ['Album', 'Soundtrack']
|
2011-06-14 20:01:42 +00:00
|
|
|
def data
|
2011-09-09 22:13:02 +00:00
|
|
|
@data = {
|
|
|
|
:status => '',
|
|
|
|
:correct_name => '',
|
|
|
|
:html => ''
|
|
|
|
}
|
|
|
|
@loading = false
|
|
|
|
|
|
|
|
# Bad params
|
2011-06-29 04:11:54 +00:00
|
|
|
if params[:name].nil? or params[:name].length == 0
|
2011-09-09 22:13:02 +00:00
|
|
|
@data[:status] = 'loading_failed';
|
|
|
|
render :json => @data
|
2011-06-29 04:11:54 +00:00
|
|
|
return
|
|
|
|
end
|
2011-09-09 22:13:02 +00:00
|
|
|
|
|
|
|
# Searching for artist
|
2011-09-08 01:48:33 +00:00
|
|
|
name = params[:name].gsub('%20', ' ').gsub('+', ' ').gsub('.html', '')
|
2011-09-09 22:13:02 +00:00
|
|
|
@artist = Artist.find_by_name(name)
|
|
|
|
|
|
|
|
# Artist not found
|
|
|
|
unless @artist
|
2011-06-18 15:58:51 +00:00
|
|
|
results = ArtistController.musicBrainzExactSearch(name)
|
|
|
|
if results.empty?
|
2011-09-09 22:13:02 +00:00
|
|
|
@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
|
2011-06-18 15:58:51 +00:00
|
|
|
elsif results[0][:name] == name
|
2011-06-19 21:23:22 +00:00
|
|
|
# Saving artist and queueing job
|
2011-09-09 22:13:02 +00:00
|
|
|
@artist = Artist.new
|
|
|
|
@artist.name = name
|
|
|
|
@artist.status = 0
|
|
|
|
@artist.save
|
2011-06-19 21:23:22 +00:00
|
|
|
Delayed::Job.enqueue LoadArtistJob.new(name)
|
2011-09-09 22:13:02 +00:00
|
|
|
# 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
|
2011-06-15 04:38:29 +00:00
|
|
|
else
|
2011-09-09 22:13:02 +00:00
|
|
|
@suggestions = results.take(5)
|
|
|
|
@data[:status] = 'suggestions'
|
|
|
|
@data[:html] = (render_to_string :partial => 'suggestions').gsub(/\n\s+/, '').gsub(/\n/, '')
|
|
|
|
render :json => @data
|
2011-06-15 04:38:29 +00:00
|
|
|
end
|
2011-09-09 22:13:02 +00:00
|
|
|
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
|
2011-06-15 04:38:29 +00:00
|
|
|
end
|
2011-09-09 22:13:02 +00:00
|
|
|
ap @artist
|
|
|
|
@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
|
|
|
|
}
|
|
|
|
|
|
|
|
@artist[:albums] = []
|
|
|
|
artist.albums.each do |album|
|
2011-09-14 16:54:39 +00:00
|
|
|
if @@default_album_types.include? album.album_type and album.status == 1
|
2011-06-28 23:30:27 +00:00
|
|
|
tmp_album = {id: album.id, name: album.name, year: album.year, pic: album.pic_url}
|
2011-06-17 21:49:32 +00:00
|
|
|
album_tracks = []
|
|
|
|
bonus_tracks = []
|
|
|
|
album.tracks.each do |track|
|
2011-06-28 23:30:27 +00:00
|
|
|
tmp_track = {id: track.id, name: track.name, live: track.live, acoustic: track.acoustic}
|
2011-09-09 22:13:02 +00:00
|
|
|
tmp_track[:duration] = formatTrackDuration(track.length)
|
2011-06-17 21:49:32 +00:00
|
|
|
(track.bonus == 0 ? album_tracks : bonus_tracks) << tmp_track
|
2011-06-15 04:38:29 +00:00
|
|
|
end
|
2011-09-08 01:48:33 +00:00
|
|
|
tmp_album[:tracks] = {album: album_tracks, bonus: bonus_tracks}
|
2011-09-09 22:13:02 +00:00
|
|
|
@artist[:albums] << tmp_album
|
2011-06-14 20:01:42 +00:00
|
|
|
end
|
|
|
|
end
|
2011-09-09 22:13:02 +00:00
|
|
|
|
|
|
|
(render_to_string :partial => 'page').gsub(/\n\s+/, '').gsub(/\n/, '')
|
|
|
|
end
|
|
|
|
|
|
|
|
def formatTrackDuration length
|
|
|
|
if length
|
|
|
|
time = (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'
|
2011-09-08 01:48:33 +00:00
|
|
|
end
|
2011-06-14 20:01:42 +00:00
|
|
|
end
|
2011-06-15 04:38:29 +00:00
|
|
|
|
|
|
|
def autocomplete
|
2011-09-09 22:13:02 +00:00
|
|
|
autocomplete = getLastFmAutocomplete(params[:query])
|
2011-06-15 04:38:29 +00:00
|
|
|
return render :nothing => true if autocomplete.nil?
|
|
|
|
suggestions = []
|
|
|
|
autocomplete["response"]["docs"].each do |doc|
|
2011-06-17 23:37:49 +00:00
|
|
|
suggestions << doc["artist"] unless suggestions.include?(doc["artist"]) or doc["artist"].nil? or doc['restype'] != 6
|
2011-06-15 04:38:29 +00:00
|
|
|
end
|
|
|
|
render :json => {
|
|
|
|
:query => params[:query],
|
2011-07-04 15:09:44 +00:00
|
|
|
:suggestions => suggestions.take(5)
|
2011-06-15 04:38:29 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2011-09-09 22:13:02 +00:00
|
|
|
def getLastFmAutocomplete query
|
2011-06-15 04:38:29 +00:00
|
|
|
return nil if query.nil? or query.strip.empty?
|
|
|
|
json = ActiveSupport::JSON.decode(open(
|
|
|
|
'http://www.last.fm/search/autocomplete' <<
|
2011-06-18 01:17:10 +00:00
|
|
|
'?rows=30&q=' << URI.escape(query)
|
2011-06-15 04:38:29 +00:00
|
|
|
).read)
|
2011-09-09 22:13:02 +00:00
|
|
|
json.empty? ? nil : json
|
2011-06-15 04:38:29 +00:00
|
|
|
end
|
2011-06-17 23:37:49 +00:00
|
|
|
|
2011-09-09 22:13:02 +00:00
|
|
|
def musicBrainzSearch(query)
|
2011-06-17 23:37:49 +00:00
|
|
|
begin
|
2011-06-18 01:09:07 +00:00
|
|
|
response = ActiveSupport::JSON.decode(open('http://search.test.musicbrainz.org/ws/2/artist/?fmt=json&query='+ URI.escape(query).gsub(/\&/, '%26').gsub(/\?/, '%3F') +'~&limit=100').read)
|
2011-06-17 23:37:49 +00:00
|
|
|
artists = []
|
|
|
|
response['artist-list']['artist'].each do |artist|
|
2011-06-18 01:09:07 +00:00
|
|
|
artists << artist['name'] unless artist['type'] == 'unknown'
|
2011-06-17 23:37:49 +00:00
|
|
|
end
|
2011-06-18 01:09:07 +00:00
|
|
|
artists.take(10)
|
2011-06-17 23:37:49 +00:00
|
|
|
rescue
|
2011-09-09 22:13:02 +00:00
|
|
|
{}
|
2011-06-17 23:37:49 +00:00
|
|
|
end
|
|
|
|
end
|
2011-06-18 15:58:51 +00:00
|
|
|
|
|
|
|
def self.musicBrainzExactSearch(query)
|
|
|
|
begin
|
|
|
|
response = ActiveSupport::JSON.decode(open('http://search.test.musicbrainz.org/ws/2/artist/?fmt=json&query='+
|
|
|
|
URI.escape(query).gsub(/\&/, '%26').gsub(/\?/, '%3F') +'~&limit=50',
|
|
|
|
'User-Agent' => 'BeatHaven.org'
|
|
|
|
).read)
|
|
|
|
artists = []
|
|
|
|
i = 0
|
|
|
|
response['artist-list']['artist'].each do |artist|
|
|
|
|
i++
|
|
|
|
artist['weight'] = (response['artist-list']['artist'].length - i)
|
|
|
|
unless artist['alias-list'].nil?
|
|
|
|
artist['weight'] += 20 if artist['alias-list']['alias'].include?(query)
|
|
|
|
artist['alias-list']['alias'].each do |aliass|
|
|
|
|
artist['weight'] += 10 if aliass.downcase == query.downcase
|
|
|
|
artist['weight'] += 3 if aliass.downcase.include?(query.downcase)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
unless artist['tag-list'].nil?
|
|
|
|
artist['weight'] += artist['tag-list']['tag'].length * 4
|
|
|
|
end
|
|
|
|
end
|
|
|
|
response['artist-list']['artist'].each do |artist|
|
|
|
|
artists << {name: artist['name'], weight: artist['weight'], desc: artist['disambiguation'], type: artist['type'].capitalize, mbid: artist['id']} unless artist['type'] == 'unknown'
|
|
|
|
end
|
|
|
|
artists.sort! { |a, b| b[:weight] <=> a[:weight] }
|
|
|
|
artists.take(10)
|
|
|
|
rescue
|
2011-09-09 22:13:02 +00:00
|
|
|
{}
|
2011-06-18 15:58:51 +00:00
|
|
|
end
|
|
|
|
end
|
2011-06-14 18:06:46 +00:00
|
|
|
end
|