Working search! Weee!
This commit is contained in:
@@ -3,82 +3,114 @@ require 'open-uri'
|
||||
class ArtistController < ApplicationController
|
||||
@@default_album_types = ['Album', 'Soundtrack']
|
||||
def data
|
||||
data = {}
|
||||
@data = {
|
||||
:status => '',
|
||||
:correct_name => '',
|
||||
:html => ''
|
||||
}
|
||||
@loading = false
|
||||
|
||||
# Bad params
|
||||
if params[:name].nil? or params[:name].length == 0
|
||||
render :json => {status: 'loading_failed', pics: []}
|
||||
@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)
|
||||
if artist and artist.status == 0
|
||||
pics = []
|
||||
pics << artist.pic_url unless artist.pic_url.nil?
|
||||
unless artist.albums.empty?
|
||||
artist.albums.each do |album|
|
||||
pics << album.pic_url unless album.pic_url.nil? or album.pic_url.empty?
|
||||
end
|
||||
end
|
||||
render :json => {status: 'loading', pics: pics}
|
||||
return
|
||||
elsif artist and artist.status == 2
|
||||
render :json => {status: 'loading_failed', pics: []}
|
||||
return
|
||||
end
|
||||
unless artist
|
||||
@artist = Artist.find_by_name(name)
|
||||
|
||||
# Artist not found
|
||||
unless @artist
|
||||
results = ArtistController.musicBrainzExactSearch(name)
|
||||
if results.empty?
|
||||
render :json => {status: 'not found'}
|
||||
return
|
||||
@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.status = 0
|
||||
artist.save
|
||||
@artist = Artist.new
|
||||
@artist.name = name
|
||||
@artist.status = 0
|
||||
@artist.save
|
||||
Delayed::Job.enqueue LoadArtistJob.new(name)
|
||||
render :json => {status: 'loading', pics: []}
|
||||
return
|
||||
elsif results[0][:name].downcase == name.downcase or results[0][:name].downcase == 'the '+ name.downcase
|
||||
render :json => {status: 'corrected', page: results[0][:name]}
|
||||
return
|
||||
# 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
|
||||
render :json => {status: 'suggestions', values: results.take(5)}
|
||||
return
|
||||
@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
|
||||
data[:artist] = {id: artist.id, name: artist.name, desc: ActionController::Base.helpers.strip_tags(artist.desc), pic: artist.pic_url}
|
||||
data[:albums] = []
|
||||
albums = artist.albums
|
||||
albums.each do |album|
|
||||
|
||||
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
|
||||
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|
|
||||
if @@default_album_types.include? album.album_type
|
||||
tmp_album = {id: album.id, name: album.name, year: album.year, pic: album.pic_url}
|
||||
album_tracks = []
|
||||
bonus_tracks = []
|
||||
album.tracks.each do |track|
|
||||
tmp_track = {id: track.id, 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
|
||||
tmp_track[:duration] = formatTrackDuration(track.length)
|
||||
(track.bonus == 0 ? album_tracks : bonus_tracks) << tmp_track
|
||||
end
|
||||
tmp_album[:tracks] = {album: album_tracks, bonus: bonus_tracks}
|
||||
data[:albums] << tmp_album
|
||||
@artist[:albums] << tmp_album
|
||||
end
|
||||
end
|
||||
@data = data
|
||||
respond_to do |format|
|
||||
format.html { render :partial => 'data' }
|
||||
format.json { render :json => @data }
|
||||
|
||||
(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'
|
||||
end
|
||||
end
|
||||
|
||||
def autocomplete
|
||||
autocomplete = ArtistController.getLastFmAutocomplete(params[:query])
|
||||
autocomplete = getLastFmAutocomplete(params[:query])
|
||||
return render :nothing => true if autocomplete.nil?
|
||||
suggestions = []
|
||||
autocomplete["response"]["docs"].each do |doc|
|
||||
@@ -90,16 +122,16 @@ class ArtistController < ApplicationController
|
||||
}
|
||||
end
|
||||
|
||||
def self.getLastFmAutocomplete query
|
||||
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)
|
||||
return json.empty? ? nil : json
|
||||
json.empty? ? nil : json
|
||||
end
|
||||
|
||||
def self.musicBrainzSearch(query)
|
||||
def musicBrainzSearch(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=100').read)
|
||||
artists = []
|
||||
@@ -108,7 +140,7 @@ class ArtistController < ApplicationController
|
||||
end
|
||||
artists.take(10)
|
||||
rescue
|
||||
return {}
|
||||
{}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -140,7 +172,7 @@ class ArtistController < ApplicationController
|
||||
artists.sort! { |a, b| b[:weight] <=> a[:weight] }
|
||||
artists.take(10)
|
||||
rescue
|
||||
return {}
|
||||
{}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -18,9 +18,8 @@ class ImportController < ApplicationController
|
||||
lastfm = Lastfm.new(@@lastfm_api_key, @@lastfm_secret)
|
||||
|
||||
artist = Artist.find_or_create_by_name(name)
|
||||
|
||||
begin
|
||||
|
||||
begin
|
||||
# Get artist info
|
||||
artist_mb_data = ArtistController.musicBrainzExactSearch(name).first
|
||||
begin
|
||||
@@ -30,13 +29,16 @@ class ImportController < ApplicationController
|
||||
ap e.message
|
||||
ap e.backtrace
|
||||
end
|
||||
|
||||
|
||||
# Save artist
|
||||
artist.desc = artist_lastfm['bio']['summary']
|
||||
artist.pic_url = artist_lastfm['image'][3]['content']
|
||||
artist.artist_type = artist_mb['artist']['type']
|
||||
artist.mbid = artist_mb_data[:mbid]
|
||||
|
||||
artist.save! unless dry_run
|
||||
end
|
||||
|
||||
begin
|
||||
# Get albums from MB
|
||||
release_groups_mb = brainz.release_group(nil, :artist => artist_mb_data[:mbid], :limit => 500)
|
||||
|
||||
@@ -167,7 +169,7 @@ class ImportController < ApplicationController
|
||||
tracks_mb.each do |mb_track|
|
||||
unless ['[silence]', '[untitled]'].include? mb_track['recording']['title']
|
||||
track = Track.new
|
||||
track.name = mb_track['recording']['title'].gsub(/\s\\\s\[.*?\]/, '')
|
||||
track.name = mb_track['recording']['title'].gsub(/\s\/\s\[.*?\]/, '')
|
||||
track.album_id = album.id
|
||||
track.position = mb_track['position']
|
||||
track.length = mb_track['length'] unless mb_track['length'].nil?
|
||||
|
||||
Reference in New Issue
Block a user