From f8d86216c83b78eff5cfd7a6f74f65b981eca829 Mon Sep 17 00:00:00 2001 From: magnolia-fan Date: Wed, 30 Nov 2011 03:08:00 +0400 Subject: [PATCH] Importing favorites from vkontakte --- app/assets/javascripts/vkontakte.coffee | 11 ++++++ app/controllers/user_controller.rb | 51 ++++++++++++++++++++++--- app/models/beathaven/user.rb | 1 + config/routes.rb | 3 +- 4 files changed, 58 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/vkontakte.coffee b/app/assets/javascripts/vkontakte.coffee index 747fb9a..4297985 100644 --- a/app/assets/javascripts/vkontakte.coffee +++ b/app/assets/javascripts/vkontakte.coffee @@ -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 diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index bbf3951..638cda3 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -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 diff --git a/app/models/beathaven/user.rb b/app/models/beathaven/user.rb index 1e123f3..931fc98 100644 --- a/app/models/beathaven/user.rb +++ b/app/models/beathaven/user.rb @@ -1,4 +1,5 @@ class User < ActiveRecord::Base bitmask :music, :as => [:album, :single, :live, :bootleg, :soundtrack] has_one :session + has_many :favorites end diff --git a/config/routes.rb b/config/routes.rb index cdc4d41..b5dfaa2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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