Importing favorites from vkontakte

This commit is contained in:
magnolia-fan
2011-11-30 03:08:00 +04:00
parent 41e9165ff4
commit f8d86216c8
4 changed files with 58 additions and 8 deletions
+45 -6
View File
@@ -22,11 +22,6 @@ class UserController < ApplicationController
render json: @res
end
def settings
@user = User.find_by_vkid(params[:mid])
render partial: 'settings'
end
def update
allowed_params = [:name, :email, :lang, :show, :lastfm_username, :lastfm_key]
@@ -45,7 +40,7 @@ class UserController < ApplicationController
end
end
render json: {user: build_user_info(user)}
render json: { user: build_user_info(user) }
end
def fav
@@ -75,6 +70,46 @@ class UserController < ApplicationController
render json: @res
end
def set_first_favorites
return render json: { status: "unauthorized" } unless current_user?
return render json: { status: "already imported" } unless current_user.favorites.empty?
artists = {}
params[:tracks].each do |i, track|
artists[track["artist"]] ||= 0
artists[track["artist"]] += 1
end
top10 = []
artists.sort_by{ |k, v| v }.reverse.each do |info|
artist_name = info[0]
artist = Artist.find_by_name(artist_name)
if artist.nil?
results = MusicBrainz::Artist.search(artist_name)
next if results.empty?
best_match = results[0][:sort_name]
if best_match != artist_name and similar_names(artist_name, best_match)
artist = Artist.find_by_name(best_match)
if artist.nil?
artist = Artist.create( name: best_match, mbid: results[0][:mbid], status: 0 )
Delayed::Job.enqueue(LoadArtistJob.new(best_match))
end
elsif best_match == artist_name
artist = Artist.create( name: best_match, mbid: results[0][:mbid], status: 0 )
Delayed::Job.enqueue(LoadArtistJob.new(best_match))
end
end
top10 << artist unless artist.nil?
break if top10.length == 10
end
top10.each do |artist|
Favorite.create( user: current_user, artist: artist )
end
render json: top10.map(&:name)
end
private
def build_user_info user
@@ -89,4 +124,8 @@ private
"&cb=http://#{request.host}:#{request.port.to_s}/lastfm/connect/?sid=#{user.session.key}"
}
end
def similar_names name1, name2
(name2.downcase == name1.downcase or name2.downcase == 'the '+ name1.downcase)
end
end