Lastfm scrobbling backend ready
This commit is contained in:
parent
4326a4960b
commit
8c33730252
2
Gemfile
2
Gemfile
|
@ -7,3 +7,5 @@ gem 'pg'
|
||||||
gem 'awesome_print', :require => 'ap'
|
gem 'awesome_print', :require => 'ap'
|
||||||
|
|
||||||
gem 'delayed_job'
|
gem 'delayed_job'
|
||||||
|
|
||||||
|
gem 'lastfm', :git => 'git://github.com/magnolia-fan/ruby-lastfm.git'
|
||||||
|
|
14
Gemfile.lock
14
Gemfile.lock
|
@ -1,3 +1,12 @@
|
||||||
|
GIT
|
||||||
|
remote: git://github.com/magnolia-fan/ruby-lastfm.git
|
||||||
|
revision: 1c82d5616c99689162afa8df854d16ebaa604e23
|
||||||
|
specs:
|
||||||
|
lastfm (0.2.0)
|
||||||
|
activesupport
|
||||||
|
httparty
|
||||||
|
xml-simple
|
||||||
|
|
||||||
GEM
|
GEM
|
||||||
remote: http://rubygems.org/
|
remote: http://rubygems.org/
|
||||||
specs:
|
specs:
|
||||||
|
@ -31,12 +40,15 @@ GEM
|
||||||
arel (2.0.10)
|
arel (2.0.10)
|
||||||
awesome_print (0.4.0)
|
awesome_print (0.4.0)
|
||||||
builder (2.1.2)
|
builder (2.1.2)
|
||||||
|
crack (0.1.8)
|
||||||
daemons (1.1.4)
|
daemons (1.1.4)
|
||||||
delayed_job (2.1.4)
|
delayed_job (2.1.4)
|
||||||
activesupport (~> 3.0)
|
activesupport (~> 3.0)
|
||||||
daemons
|
daemons
|
||||||
erubis (2.6.6)
|
erubis (2.6.6)
|
||||||
abstract (>= 1.0.0)
|
abstract (>= 1.0.0)
|
||||||
|
httparty (0.7.8)
|
||||||
|
crack (= 0.1.8)
|
||||||
i18n (0.5.0)
|
i18n (0.5.0)
|
||||||
mail (2.2.19)
|
mail (2.2.19)
|
||||||
activesupport (>= 2.3.6)
|
activesupport (>= 2.3.6)
|
||||||
|
@ -70,6 +82,7 @@ GEM
|
||||||
treetop (1.4.9)
|
treetop (1.4.9)
|
||||||
polyglot (>= 0.3.1)
|
polyglot (>= 0.3.1)
|
||||||
tzinfo (0.3.27)
|
tzinfo (0.3.27)
|
||||||
|
xml-simple (1.1.0)
|
||||||
|
|
||||||
PLATFORMS
|
PLATFORMS
|
||||||
ruby
|
ruby
|
||||||
|
@ -77,6 +90,7 @@ PLATFORMS
|
||||||
DEPENDENCIES
|
DEPENDENCIES
|
||||||
awesome_print
|
awesome_print
|
||||||
delayed_job
|
delayed_job
|
||||||
|
lastfm!
|
||||||
pg
|
pg
|
||||||
rails (= 3.0.8)
|
rails (= 3.0.8)
|
||||||
sqlite3
|
sqlite3
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
require 'open-uri'
|
require 'open-uri'
|
||||||
|
require 'lastfm'
|
||||||
|
|
||||||
class LastFmController < ApplicationController
|
class LastFmController < ApplicationController
|
||||||
|
|
||||||
|
@ -9,7 +10,10 @@ class LastFmController < ApplicationController
|
||||||
unless params[:sid].nil? or params[:token].nil?
|
unless params[:sid].nil? or params[:token].nil?
|
||||||
session = Session.find_by_key(params[:sid])
|
session = Session.find_by_key(params[:sid])
|
||||||
unless session.nil?
|
unless session.nil?
|
||||||
session.user.lastfm_token = params[:token]
|
lastfm = Lastfm.new(@@api_key, @@secret)
|
||||||
|
lastfm.session = lastfm.auth.get_session(params[:token])
|
||||||
|
session.user.lastfm_key = lastfm.session['key']
|
||||||
|
session.user.lastfm_username = lastfm.session['name']
|
||||||
session.user.save
|
session.user.save
|
||||||
render :text => '<script>window.close();</script>'
|
render :text => '<script>window.close();</script>'
|
||||||
else
|
else
|
||||||
|
@ -21,52 +25,45 @@ class LastFmController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def getinfo
|
def getinfo
|
||||||
#return unless authorized?
|
return unless authorized?
|
||||||
@res = {}
|
@res = {}
|
||||||
|
|
||||||
user = User.find_by_vkid(1217744)#params[:mid])
|
user = User.find_by_vkid(params[:mid])
|
||||||
@res[:connected] = false
|
unless user.lastfm_key.nil?
|
||||||
@res[:lastfm_login_url] = 'http://www.last.fm/api/auth?api_key='+ @@api_key +'&cb=http://localhost/lastfm/connect/?sid='+ user.session.key
|
render :json => {
|
||||||
|
:connected => true,
|
||||||
unless user.lastfm_token.nil?
|
:username => user.lastfm_username
|
||||||
lastfm_response = auth_query({:method => 'auth.getSession', :token => user.lastfm_token})
|
}
|
||||||
render :json => lastfm_response
|
else
|
||||||
return
|
render :json => {
|
||||||
if lastfm_response
|
:connected => false,
|
||||||
user.lastfm_token = lastfm_response[1]
|
:lastfm_login_url => 'http://www.last.fm/api/auth?api_key='+ @@api_key +'&cb=http://localhost/lastfm/connect/?sid='+ user.session.key
|
||||||
user.save
|
}
|
||||||
@res[:connected] = true
|
|
||||||
@res[:username] = lastfm_response[0];
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
render :json => @res
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def auth_query params
|
|
||||||
url = 'http://ws.audioscrobbler.com/2.0/?'
|
|
||||||
params[:api_key] = @@api_key
|
|
||||||
params.each do |k, v|
|
|
||||||
url << k.to_s << '=' << v << '&'
|
|
||||||
end
|
|
||||||
url << 'api_sig=' << get_signature(params)
|
|
||||||
begin
|
|
||||||
open(url).read.match(/<name>(.*?)<\/name>.*?<key>(.*?)<\/key>/m)
|
|
||||||
rescue
|
|
||||||
false
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_signature params
|
def listening
|
||||||
params = params.to_a.sort! { |a, b| a[0] <=> b[0] }
|
return unless authorized?
|
||||||
params = Hash[params]
|
return if params[:artist].nil? or params[:track].nil?
|
||||||
str = '';
|
|
||||||
params.each do |k, v|
|
user = User.find_by_vkid(params[:mid])
|
||||||
str << k.to_s << v
|
return unless user.lastfm_key.nil?
|
||||||
end
|
|
||||||
Digest::MD5.hexdigest(str + @@secret)
|
lastfm = Lastfm.new(@@api_key, @@secret)
|
||||||
|
lastfm.session = user.lastfm_key
|
||||||
|
lastfm.update_now_playing(params[:artist], params[:track])
|
||||||
|
end
|
||||||
|
|
||||||
|
def scrobble
|
||||||
|
return unless authorized?
|
||||||
|
return if params[:artist].nil? or params[:track].nil?
|
||||||
|
|
||||||
|
user = User.find_by_vkid(params[:mid])
|
||||||
|
return unless user.lastfm_key.nil?
|
||||||
|
|
||||||
|
lastfm = Lastfm.new(@@api_key, @@secret)
|
||||||
|
lastfm.session = user.lastfm_key
|
||||||
|
lastfm.scrobble(params[:artist], params[:track])
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -59,9 +59,10 @@ Beathaven::Application.routes.draw do
|
||||||
match 'user/auth' => 'user#auth'
|
match 'user/auth' => 'user#auth'
|
||||||
match 'user/update' => 'user#update'
|
match 'user/update' => 'user#update'
|
||||||
|
|
||||||
match 'lastfm/login' => 'last_fm#login'
|
|
||||||
match 'lastfm/connect' => 'last_fm#connect'
|
match 'lastfm/connect' => 'last_fm#connect'
|
||||||
match 'lastfm/getinfo' => 'last_fm#getinfo'
|
match 'lastfm/getinfo' => 'last_fm#getinfo'
|
||||||
|
match 'lastfm/listening' => 'last_fm#listening'
|
||||||
|
match 'lastfm/scrobble' => 'last_fm#scrobble'
|
||||||
|
|
||||||
match 'artist/autocomplete' => 'artist#autocomplete'
|
match 'artist/autocomplete' => 'artist#autocomplete'
|
||||||
match 'artist/(:name)/' => 'artist#data', :constraints => { :name => /[^\/]*/ }
|
match 'artist/(:name)/' => 'artist#data', :constraints => { :name => /[^\/]*/ }
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
class AddLastfmUsernameToUsers < ActiveRecord::Migration
|
||||||
|
def self.up
|
||||||
|
add_column :users, :lastfm_username, :string
|
||||||
|
rename_column :users, :lastfm_token, :lastfm_key
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.down
|
||||||
|
rename_column :users, :lastfm_key, :lastfm_token
|
||||||
|
remove_column :users, :lastfm_username
|
||||||
|
end
|
||||||
|
end
|
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended to check this file into your version control system.
|
# It's strongly recommended to check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(:version => 20110622053238) do
|
ActiveRecord::Schema.define(:version => 20110622204123) do
|
||||||
|
|
||||||
create_table "albums", :force => true do |t|
|
create_table "albums", :force => true do |t|
|
||||||
t.string "name"
|
t.string "name"
|
||||||
|
@ -98,7 +98,8 @@ ActiveRecord::Schema.define(:version => 20110622053238) do
|
||||||
t.integer "vkid"
|
t.integer "vkid"
|
||||||
t.datetime "created_at"
|
t.datetime "created_at"
|
||||||
t.datetime "updated_at"
|
t.datetime "updated_at"
|
||||||
t.string "lastfm_token"
|
t.string "lastfm_key"
|
||||||
|
t.string "lastfm_username"
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -69,7 +69,7 @@ var Pages = {
|
||||||
.height(($('#data-container').height() - $('#search_form').height()) / 2);
|
.height(($('#data-container').height() - $('#search_form').height()) / 2);
|
||||||
|
|
||||||
setTimeout(function(){
|
setTimeout(function(){
|
||||||
$('#search_field').autocomplete({
|
$('#search_field').bh_autocomplete({
|
||||||
serviceUrl: '/artist/autocomplete', // Страница для обработки запросов автозаполнения
|
serviceUrl: '/artist/autocomplete', // Страница для обработки запросов автозаполнения
|
||||||
minChars: 2, // Минимальная длина запроса для срабатывания автозаполнения
|
minChars: 2, // Минимальная длина запроса для срабатывания автозаполнения
|
||||||
delimiter: /(,|;)\s*/, // Разделитель для нескольких запросов, символ или регулярное выражение
|
delimiter: /(,|;)\s*/, // Разделитель для нескольких запросов, символ или регулярное выражение
|
||||||
|
|
|
@ -33,8 +33,11 @@ $('.settings-container .tabs .tab').live('click', function(){
|
||||||
}
|
}
|
||||||
if ($(this).attr('data-fieldset') == 'lastfm') {
|
if ($(this).attr('data-fieldset') == 'lastfm') {
|
||||||
Session.query('/lastfm/getinfo', {}, function(data){
|
Session.query('/lastfm/getinfo', {}, function(data){
|
||||||
console.log(data);
|
if (data.connected) {
|
||||||
Settings.lastfm_login_url = data.lastfm_login_url;
|
$('.form-container input[name$="username"]').first().val(data.username);
|
||||||
|
} else {
|
||||||
|
Settings.lastfm_login_url = data.lastfm_login_url;
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -50,7 +50,7 @@
|
||||||
this.setOptions(options);
|
this.setOptions(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
$.fn.autocomplete = function(options) {
|
$.fn.bh_autocomplete = function(options) {
|
||||||
return new Autocomplete(this.get(0)||$('<input />'), options);
|
return new Autocomplete(this.get(0)||$('<input />'), options);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue