1
0
Fork 0

Lastfm scrobbling backend ready

This commit is contained in:
magnolia-fan 2011-06-23 02:56:32 +04:00
parent 4326a4960b
commit 8c33730252
9 changed files with 78 additions and 49 deletions

View File

@ -7,3 +7,5 @@ gem 'pg'
gem 'awesome_print', :require => 'ap'
gem 'delayed_job'
gem 'lastfm', :git => 'git://github.com/magnolia-fan/ruby-lastfm.git'

View File

@ -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
remote: http://rubygems.org/
specs:
@ -31,12 +40,15 @@ GEM
arel (2.0.10)
awesome_print (0.4.0)
builder (2.1.2)
crack (0.1.8)
daemons (1.1.4)
delayed_job (2.1.4)
activesupport (~> 3.0)
daemons
erubis (2.6.6)
abstract (>= 1.0.0)
httparty (0.7.8)
crack (= 0.1.8)
i18n (0.5.0)
mail (2.2.19)
activesupport (>= 2.3.6)
@ -70,6 +82,7 @@ GEM
treetop (1.4.9)
polyglot (>= 0.3.1)
tzinfo (0.3.27)
xml-simple (1.1.0)
PLATFORMS
ruby
@ -77,6 +90,7 @@ PLATFORMS
DEPENDENCIES
awesome_print
delayed_job
lastfm!
pg
rails (= 3.0.8)
sqlite3

View File

@ -1,4 +1,5 @@
require 'open-uri'
require 'lastfm'
class LastFmController < ApplicationController
@ -9,7 +10,10 @@ class LastFmController < ApplicationController
unless params[:sid].nil? or params[:token].nil?
session = Session.find_by_key(params[:sid])
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
render :text => '<script>window.close();</script>'
else
@ -21,52 +25,45 @@ class LastFmController < ApplicationController
end
def getinfo
#return unless authorized?
return unless authorized?
@res = {}
user = User.find_by_vkid(1217744)#params[:mid])
@res[:connected] = false
@res[:lastfm_login_url] = 'http://www.last.fm/api/auth?api_key='+ @@api_key +'&cb=http://localhost/lastfm/connect/?sid='+ user.session.key
unless user.lastfm_token.nil?
lastfm_response = auth_query({:method => 'auth.getSession', :token => user.lastfm_token})
render :json => lastfm_response
return
if lastfm_response
user.lastfm_token = lastfm_response[1]
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
user = User.find_by_vkid(params[:mid])
unless user.lastfm_key.nil?
render :json => {
:connected => true,
:username => user.lastfm_username
}
else
render :json => {
:connected => false,
:lastfm_login_url => 'http://www.last.fm/api/auth?api_key='+ @@api_key +'&cb=http://localhost/lastfm/connect/?sid='+ user.session.key
}
end
end
def get_signature params
params = params.to_a.sort! { |a, b| a[0] <=> b[0] }
params = Hash[params]
str = '';
params.each do |k, v|
str << k.to_s << v
end
Digest::MD5.hexdigest(str + @@secret)
def listening
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.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

View File

@ -59,9 +59,10 @@ Beathaven::Application.routes.draw do
match 'user/auth' => 'user#auth'
match 'user/update' => 'user#update'
match 'lastfm/login' => 'last_fm#login'
match 'lastfm/connect' => 'last_fm#connect'
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/(:name)/' => 'artist#data', :constraints => { :name => /[^\/]*/ }

View File

@ -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

View File

@ -10,7 +10,7 @@
#
# 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|
t.string "name"
@ -98,7 +98,8 @@ ActiveRecord::Schema.define(:version => 20110622053238) do
t.integer "vkid"
t.datetime "created_at"
t.datetime "updated_at"
t.string "lastfm_token"
t.string "lastfm_key"
t.string "lastfm_username"
end
end

View File

@ -69,7 +69,7 @@ var Pages = {
.height(($('#data-container').height() - $('#search_form').height()) / 2);
setTimeout(function(){
$('#search_field').autocomplete({
$('#search_field').bh_autocomplete({
serviceUrl: '/artist/autocomplete', // Страница для обработки запросов автозаполнения
minChars: 2, // Минимальная длина запроса для срабатывания автозаполнения
delimiter: /(,|;)\s*/, // Разделитель для нескольких запросов, символ или регулярное выражение

View File

@ -33,8 +33,11 @@ $('.settings-container .tabs .tab').live('click', function(){
}
if ($(this).attr('data-fieldset') == 'lastfm') {
Session.query('/lastfm/getinfo', {}, function(data){
console.log(data);
Settings.lastfm_login_url = data.lastfm_login_url;
if (data.connected) {
$('.form-container input[name$="username"]').first().val(data.username);
} else {
Settings.lastfm_login_url = data.lastfm_login_url;
}
})
}
});

View File

@ -50,7 +50,7 @@
this.setOptions(options);
}
$.fn.autocomplete = function(options) {
$.fn.bh_autocomplete = function(options) {
return new Autocomplete(this.get(0)||$('<input />'), options);
};