1
0
Fork 0

Log scrobblings

This commit is contained in:
magnolia-fan 2011-11-29 23:42:21 +04:00
parent 74b5619748
commit 41e9165ff4
12 changed files with 116 additions and 77 deletions

View File

@ -183,8 +183,7 @@ class window.Player
false
scrobble: (track) ->
if _session.getUser().lastfm_username
_session.query '/lastfm/scrobble?r=' +Math.random(), track
_session.query '/lastfm/scrobble?r=' +Math.random(), track
false
emptyPlaylist: ->

View File

@ -9,8 +9,8 @@ class window.Settings
false
updateLastfmLogin: ->
log "Updating Last.fm info ..."
if window.lastfm_popup.closed
log "Updating Last.fm info ..."
_session.query '/user/auth', {}, (ar) ->
_session.setUser ar.user
if _session.user.lastfm_username not in ["", null]
@ -68,7 +68,7 @@ $('.lastfm-disconnect').live 'click', ->
_settings.saveAccountInfo {
params:
lastfm_username: ""
lastfm_token: ""
lastfm_key: ""
}, (ar) ->
_session.setUser ar.user
$('#settings .lastfm-on').hide()

View File

@ -1,10 +1,9 @@
#login, .ad_here {
display: block;
}
#authorized, .player, .set-playlist, .dynamic-playlist {
#authorized, .player, .playlist, .set-playlist, .dynamic-playlist {
display: none;
}
.album {
.art .button-container {
display: none;
@ -20,6 +19,12 @@
}
body.authorized {
#login, .ad_here {
display: none;
}
#authorized, .player, .playlist, .set-playlist, .dynamic-playlist {
display: block;
}
.album {
.art .button-container {
display: block;
@ -33,13 +38,4 @@ body.authorized {
}
}
}
.set-playlist, .dynamic-playlist {
display: block;
}
#login, .ad_here {
display: none;
}
#authorized, .player {
display: block;
}
}

View File

@ -14,10 +14,6 @@
height: 200px;
}
.player, .playlist {
display: none;
}
.ad_here {
display: block;
border: #e0e0e0 1px solid;

View File

@ -50,8 +50,22 @@ class ApplicationController < ActionController::Base
protected
def cache_key_for object
"#{object.class.to_s.underscore}_#{object.id}"
def session
Session.find_by_key(session_key)
end
def current_user
session.user if current_user?
end
def current_user?
!session.nil? and !session.user.nil?
end
def set_locale
unless session.nil?
I18n.locale = session.user.lang
end
end
def authorize
@ -60,13 +74,6 @@ protected
end
end
def set_locale
session = Session.find_by_key(session_key)
unless session.nil?
I18n.locale = session.user.lang
end
end
def session_key
if cookies[:beathaven_sid].nil?
cookies[:beathaven_sid] = {
@ -77,6 +84,10 @@ protected
cookies[:beathaven_sid]
end
def cache_key_for object
"#{object.class.to_s.underscore}_#{object.id}"
end
def render_compact_partial partial_name
(render_to_string :partial => partial_name.to_s).gsub(/\n(\s+)?/, '')
end

View File

@ -2,22 +2,22 @@ require 'open-uri'
require 'net/http'
class LastFmController < ApplicationController
before_filter :authorize, :except => [:connect, :autocomplete]
before_filter :authorize, except: [:connect, :autocomplete]
def connect
unless params[:sid].nil? or params[:token].nil?
session = Session.find_by_key(params[:sid])
unless session.nil?
lastfm_api_session = LastFM::Auth.get_session(:token => params[:token], :api_sig => true)
session.user.lastfm_key = lastfm_api_session["session"]['key']
session.user.lastfm_username = lastfm_api_session["session"]['name']
session.user.save
render :text => '<script>window.close();</script>'
if current_user?
lastfm_api_session = LastFM::Auth.get_session(token: params[:token], api_sig: true)
user = current_user
user.lastfm_key = lastfm_api_session["session"]['key']
user.lastfm_username = lastfm_api_session["session"]['name']
user.save
return render text: "<script>window.close();</script>"
else
return render :text => 'You Don\'t Fool Me', status: 403
return render text: "You Don't Fool Me", status: 403
end
else
return render :text => 'So Much Trouble In The World', status: 403
return render text: "So Much Trouble In The World", status: 403
end
end
@ -25,15 +25,15 @@ class LastFmController < ApplicationController
@res = {}
user = User.find_by_vkid(params[:mid])
unless user.lastfm_key.nil?
render :json => {
:connected => true,
:username => user.lastfm_username
render json: {
connected: true,
username: user.lastfm_username
}
else
render :json => {
:connected => false,
:lastfm_login_url => 'http://www.last.fm/api/auth?api_key='+ LastFM.api_key +
'&cb=http://'+ request.host << '/lastfm/connect/?sid='+ user.session.key
render json: {
connected: false,
lastfm_login_url: "http://www.last.fm/api/auth?api_key=#{LastFM.api_key}"+
"&cb=http://#{request.host}:#{request.port.to_s}/lastfm/connect/?sid=#{user.session.key}"
}
end
end
@ -41,42 +41,43 @@ class LastFmController < ApplicationController
def listening
@res = {}
if params[:artist].nil? or params[:album].nil? or params[:name].nil?
return render :json => { :status => 'bad params' }, status: 403
return render json: { status: 'bad params' }, status: 403
end
user = User.find_by_vkid(params[:mid])
if user.lastfm_key.nil?
return render :json => {:status => 'lastfm account is not connected', :user => user}
if !current_user? or (current_user.lastfm_key.nil? or current_user.lastfm_key == "")
return render json: {status: 'lastfm account is not connected' }
end
r = LastFM::Track.update_now_playing(
:track => params[:name],
:artist => params[:artist],
:album => params[:album],
:trackNumber => params[:position].gsub(/[a-z]/, "").to_i,
:duration => params[:length].to_i,
:sk => user.lastfm_key # Auth session key
track: params[:name],
artist: params[:artist],
album: params[:album],
trackNumber: params[:position].gsub(/[a-z]/, "").to_i,
duration: params[:length].to_i,
sk: current_user.lastfm_key # Auth session key
)
render :json => { :status => r['error'].nil? ? 'success' : r }, :status => r['error'].nil? ? 200 : 403
render json: { status: r['error'].nil? ? 'success' : r }, status: r['error'].nil? ? 200 : 403
end
def scrobble
@res = {}
if params[:artist].nil? or params[:album].nil? or params[:name].nil?
return render :json => { :status => 'bad params' }, status: 403
return render json: { status: 'bad params' }, status: 403
end
user = User.find_by_vkid(params[:mid])
if user.lastfm_key.nil?
return render :json => { :status => 'lastfm account is not connected', :user => user }
if current_user? and track = Track.find_by_id(params[:id])
Scrobble.create(user: current_user, track: track, scrobbled_at: DateTime.now)
end
if !current_user? or (current_user.lastfm_key.nil? or current_user.lastfm_key == "")
return render json: { status: 'lastfm account is not connected' }
end
r = LastFM::Track.scrobble(
:track => params[:name],
:timestamp => Time.now.utc.to_i,
:artist => params[:artist],
:album => params[:album],
:trackNumber => params[:position].gsub(/[a-z]/, "").to_i,
:duration => params[:length].to_i,
:sk => user.lastfm_key # Auth session key
track: params[:name],
timestamp: Time.now.utc.to_i,
artist: params[:artist],
album: params[:album],
trackNumber: params[:position].gsub(/[a-z]/, "").to_i,
duration: params[:length].to_i,
sk: current_user.lastfm_key # Auth session key
)
render :json => { :status => r['error'].nil? ? 'success' : r }, :status => r['error'].nil? ? 200 : 403
render json: { status: r['error'].nil? ? 'success' : r }, status: r['error'].nil? ? 200 : 403
end
def self.top_playlist artist
@ -89,16 +90,16 @@ class LastFmController < ApplicationController
def autocomplete
autocomplete = getSuggestions(params[:query])
return render :nothing => true if autocomplete.nil?
return render nothing: true if autocomplete.nil?
suggestions = []
autocomplete["response"]["docs"].each do |doc|
suggestions << doc["artist"] unless suggestions.include?(doc["artist"]) or doc["artist"].nil? or doc['restype'] != 6
end
response.headers['Cache-Control'] = 'public, max-age='+1.week.seconds.to_s
render :json => {
:query => params[:query],
:suggestions => suggestions.take(5)
render json: {
query: params[:query],
suggestions: suggestions.take(5)
}
end

View File

@ -28,7 +28,7 @@ class UserController < ApplicationController
end
def update
allowed_params = [:name, :email, :lang, :show, :lastfm_username, :lastfm_token]
allowed_params = [:name, :email, :lang, :show, :lastfm_username, :lastfm_key]
user = User.find_by_vkid(params[:mid])
unless params[:params].nil?
@ -85,9 +85,8 @@ private
vkid: user.vkid,
lang: user.lang,
lastfm_username: user.lastfm_username,
lastfm_login_url: ('http://www.last.fm/api/auth?api_key=' << LastFM.api_key <<
'&cb=http://' << request.host << ':' << request.port.to_s << '/lastfm/connect/?sid=' <<
user.session.key if user.lastfm_username.nil? or user.lastfm_username == "")
lastfm_login_url: "http://www.last.fm/api/auth?api_key=#{LastFM.api_key}"+
"&cb=http://#{request.host}:#{request.port.to_s}/lastfm/connect/?sid=#{user.session.key}"
}
end
end

View File

@ -0,0 +1,4 @@
class Scrobble < ActiveRecord::Base
belongs_to :user
belongs_to :track
end

View File

@ -0,0 +1,9 @@
class CreateScrobbles < ActiveRecord::Migration
def change
create_table :scrobbles do |t|
t.integer :user_id
t.integer :track_id
t.datetime :scrobbled_at
end
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20111127102836) do
ActiveRecord::Schema.define(:version => 20111129193103) do
create_table "album_formats", :force => true do |t|
t.integer "album_id"
@ -166,6 +166,12 @@ ActiveRecord::Schema.define(:version => 20111127102836) do
add_index "release_formats", ["hash"], :name => "index_release_formats_on_hash"
create_table "scrobbles", :force => true do |t|
t.integer "user_id"
t.integer "track_id"
t.datetime "scrobbled_at"
end
create_table "sessions", :force => true do |t|
t.integer "user_id"
t.string "key"

11
test/fixtures/scrobbles.yml vendored Normal file
View File

@ -0,0 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html
one:
user_id: 1
track_id: 1
scrobbled_at: 2011-11-29 23:31:03
two:
user_id: 1
track_id: 1
scrobbled_at: 2011-11-29 23:31:03

View File

@ -0,0 +1,7 @@
require 'test_helper'
class ScrobbleTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end