1
0
Fork 0

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

View File

@ -25,6 +25,7 @@ class window.Vkontakte
_session.query '/user/auth', {}, (ar) ->
log "Authenticating user ..."
if ar.newbie or ar.user.name == ""
log "Requesting user name from Vkontakte ..."
VK.Api.call 'getVariable', key: 1281, (r) ->
_session.query '/user/update', params: { name: r.response }, (ar2) ->
_session.setUser ar2.user
@ -34,6 +35,16 @@ class window.Vkontakte
if ar.ok_reload
window.location.reload()
false
log "Requesting user tracks from Vkontakte ..."
VK.Api.call 'audio.get', uid: ar.user.vkid, (r) ->
_session.query "/user/set_first_favorites", tracks: r.response, (r2) ->
log "Sending your Vkontakte media collection to BeatHaven ..."
if r2.status?
log "Got error: #{r2.status}"
else
log "We believe your favorite artists are #{r2.join(', ')}"
false
false
else if ar.ok_reload
window.location.reload()
false

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

View File

@ -1,4 +1,5 @@
class User < ActiveRecord::Base
bitmask :music, :as => [:album, :single, :live, :bootleg, :soundtrack]
has_one :session
has_many :favorites
end

View File

@ -6,6 +6,7 @@ Beathaven::Application.routes.draw do
match 'user/auth' => 'user#auth'
match 'user/update' => 'user#update'
match 'user/set_first_favorites' => 'user#set_first_favorites'
match 'lastfm/connect' => 'last_fm#connect'
match 'lastfm/getinfo' => 'last_fm#getinfo'
@ -18,8 +19,6 @@ Beathaven::Application.routes.draw do
match 'playlist/(:id)' => 'playlist#data'
match 'playlist/lastfm-top50/(:artist)' => 'playlist#lastfm_top50', constraints: { artist: /.*/ }
match 'settings' => 'user#settings'
match 'artist/autocomplete' => 'last_fm#autocomplete'
match 'artist/(:artist)' => 'artist#data', constraints: { artist: /.*/ }
end