Log scrobblings
This commit is contained in:
parent
74b5619748
commit
41e9165ff4
|
@ -183,8 +183,7 @@ class window.Player
|
||||||
false
|
false
|
||||||
|
|
||||||
scrobble: (track) ->
|
scrobble: (track) ->
|
||||||
if _session.getUser().lastfm_username
|
_session.query '/lastfm/scrobble?r=' +Math.random(), track
|
||||||
_session.query '/lastfm/scrobble?r=' +Math.random(), track
|
|
||||||
false
|
false
|
||||||
|
|
||||||
emptyPlaylist: ->
|
emptyPlaylist: ->
|
||||||
|
|
|
@ -9,8 +9,8 @@ class window.Settings
|
||||||
false
|
false
|
||||||
|
|
||||||
updateLastfmLogin: ->
|
updateLastfmLogin: ->
|
||||||
log "Updating Last.fm info ..."
|
|
||||||
if window.lastfm_popup.closed
|
if window.lastfm_popup.closed
|
||||||
|
log "Updating Last.fm info ..."
|
||||||
_session.query '/user/auth', {}, (ar) ->
|
_session.query '/user/auth', {}, (ar) ->
|
||||||
_session.setUser ar.user
|
_session.setUser ar.user
|
||||||
if _session.user.lastfm_username not in ["", null]
|
if _session.user.lastfm_username not in ["", null]
|
||||||
|
@ -68,7 +68,7 @@ $('.lastfm-disconnect').live 'click', ->
|
||||||
_settings.saveAccountInfo {
|
_settings.saveAccountInfo {
|
||||||
params:
|
params:
|
||||||
lastfm_username: ""
|
lastfm_username: ""
|
||||||
lastfm_token: ""
|
lastfm_key: ""
|
||||||
}, (ar) ->
|
}, (ar) ->
|
||||||
_session.setUser ar.user
|
_session.setUser ar.user
|
||||||
$('#settings .lastfm-on').hide()
|
$('#settings .lastfm-on').hide()
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
#login, .ad_here {
|
#login, .ad_here {
|
||||||
display: block;
|
display: block;
|
||||||
}
|
}
|
||||||
#authorized, .player, .set-playlist, .dynamic-playlist {
|
#authorized, .player, .playlist, .set-playlist, .dynamic-playlist {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
.album {
|
.album {
|
||||||
.art .button-container {
|
.art .button-container {
|
||||||
display: none;
|
display: none;
|
||||||
|
@ -20,6 +19,12 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
body.authorized {
|
body.authorized {
|
||||||
|
#login, .ad_here {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
#authorized, .player, .playlist, .set-playlist, .dynamic-playlist {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
.album {
|
.album {
|
||||||
.art .button-container {
|
.art .button-container {
|
||||||
display: block;
|
display: block;
|
||||||
|
@ -33,13 +38,4 @@ body.authorized {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.set-playlist, .dynamic-playlist {
|
|
||||||
display: block;
|
|
||||||
}
|
|
||||||
#login, .ad_here {
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
#authorized, .player {
|
|
||||||
display: block;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,10 +14,6 @@
|
||||||
height: 200px;
|
height: 200px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.player, .playlist {
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
.ad_here {
|
.ad_here {
|
||||||
display: block;
|
display: block;
|
||||||
border: #e0e0e0 1px solid;
|
border: #e0e0e0 1px solid;
|
||||||
|
|
|
@ -50,8 +50,22 @@ class ApplicationController < ActionController::Base
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def cache_key_for object
|
def session
|
||||||
"#{object.class.to_s.underscore}_#{object.id}"
|
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
|
end
|
||||||
|
|
||||||
def authorize
|
def authorize
|
||||||
|
@ -60,13 +74,6 @@ protected
|
||||||
end
|
end
|
||||||
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
|
def session_key
|
||||||
if cookies[:beathaven_sid].nil?
|
if cookies[:beathaven_sid].nil?
|
||||||
cookies[:beathaven_sid] = {
|
cookies[:beathaven_sid] = {
|
||||||
|
@ -77,6 +84,10 @@ protected
|
||||||
cookies[:beathaven_sid]
|
cookies[:beathaven_sid]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def cache_key_for object
|
||||||
|
"#{object.class.to_s.underscore}_#{object.id}"
|
||||||
|
end
|
||||||
|
|
||||||
def render_compact_partial partial_name
|
def render_compact_partial partial_name
|
||||||
(render_to_string :partial => partial_name.to_s).gsub(/\n(\s+)?/, '')
|
(render_to_string :partial => partial_name.to_s).gsub(/\n(\s+)?/, '')
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,22 +2,22 @@ require 'open-uri'
|
||||||
require 'net/http'
|
require 'net/http'
|
||||||
|
|
||||||
class LastFmController < ApplicationController
|
class LastFmController < ApplicationController
|
||||||
before_filter :authorize, :except => [:connect, :autocomplete]
|
before_filter :authorize, except: [:connect, :autocomplete]
|
||||||
|
|
||||||
def connect
|
def connect
|
||||||
unless params[:sid].nil? or params[:token].nil?
|
unless params[:sid].nil? or params[:token].nil?
|
||||||
session = Session.find_by_key(params[:sid])
|
if current_user?
|
||||||
unless session.nil?
|
lastfm_api_session = LastFM::Auth.get_session(token: params[:token], api_sig: true)
|
||||||
lastfm_api_session = LastFM::Auth.get_session(:token => params[:token], :api_sig => true)
|
user = current_user
|
||||||
session.user.lastfm_key = lastfm_api_session["session"]['key']
|
user.lastfm_key = lastfm_api_session["session"]['key']
|
||||||
session.user.lastfm_username = lastfm_api_session["session"]['name']
|
user.lastfm_username = lastfm_api_session["session"]['name']
|
||||||
session.user.save
|
user.save
|
||||||
render :text => '<script>window.close();</script>'
|
return render text: "<script>window.close();</script>"
|
||||||
else
|
else
|
||||||
return render :text => 'You Don\'t Fool Me', status: 403
|
return render text: "You Don't Fool Me", status: 403
|
||||||
end
|
end
|
||||||
else
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -25,15 +25,15 @@ class LastFmController < ApplicationController
|
||||||
@res = {}
|
@res = {}
|
||||||
user = User.find_by_vkid(params[:mid])
|
user = User.find_by_vkid(params[:mid])
|
||||||
unless user.lastfm_key.nil?
|
unless user.lastfm_key.nil?
|
||||||
render :json => {
|
render json: {
|
||||||
:connected => true,
|
connected: true,
|
||||||
:username => user.lastfm_username
|
username: user.lastfm_username
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
render :json => {
|
render json: {
|
||||||
:connected => false,
|
connected: false,
|
||||||
:lastfm_login_url => 'http://www.last.fm/api/auth?api_key='+ LastFM.api_key +
|
lastfm_login_url: "http://www.last.fm/api/auth?api_key=#{LastFM.api_key}"+
|
||||||
'&cb=http://'+ request.host << '/lastfm/connect/?sid='+ user.session.key
|
"&cb=http://#{request.host}:#{request.port.to_s}/lastfm/connect/?sid=#{user.session.key}"
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -41,42 +41,43 @@ class LastFmController < ApplicationController
|
||||||
def listening
|
def listening
|
||||||
@res = {}
|
@res = {}
|
||||||
if params[:artist].nil? or params[:album].nil? or params[:name].nil?
|
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
|
end
|
||||||
user = User.find_by_vkid(params[:mid])
|
if !current_user? or (current_user.lastfm_key.nil? or current_user.lastfm_key == "")
|
||||||
if user.lastfm_key.nil?
|
return render json: {status: 'lastfm account is not connected' }
|
||||||
return render :json => {:status => 'lastfm account is not connected', :user => user}
|
|
||||||
end
|
end
|
||||||
r = LastFM::Track.update_now_playing(
|
r = LastFM::Track.update_now_playing(
|
||||||
:track => params[:name],
|
track: params[:name],
|
||||||
:artist => params[:artist],
|
artist: params[:artist],
|
||||||
:album => params[:album],
|
album: params[:album],
|
||||||
:trackNumber => params[:position].gsub(/[a-z]/, "").to_i,
|
trackNumber: params[:position].gsub(/[a-z]/, "").to_i,
|
||||||
:duration => params[:length].to_i,
|
duration: params[:length].to_i,
|
||||||
:sk => user.lastfm_key # Auth session key
|
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
|
end
|
||||||
|
|
||||||
def scrobble
|
def scrobble
|
||||||
@res = {}
|
@res = {}
|
||||||
if params[:artist].nil? or params[:album].nil? or params[:name].nil?
|
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
|
end
|
||||||
user = User.find_by_vkid(params[:mid])
|
if current_user? and track = Track.find_by_id(params[:id])
|
||||||
if user.lastfm_key.nil?
|
Scrobble.create(user: current_user, track: track, scrobbled_at: DateTime.now)
|
||||||
return render :json => { :status => 'lastfm account is not connected', :user => user }
|
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
|
end
|
||||||
r = LastFM::Track.scrobble(
|
r = LastFM::Track.scrobble(
|
||||||
:track => params[:name],
|
track: params[:name],
|
||||||
:timestamp => Time.now.utc.to_i,
|
timestamp: Time.now.utc.to_i,
|
||||||
:artist => params[:artist],
|
artist: params[:artist],
|
||||||
:album => params[:album],
|
album: params[:album],
|
||||||
:trackNumber => params[:position].gsub(/[a-z]/, "").to_i,
|
trackNumber: params[:position].gsub(/[a-z]/, "").to_i,
|
||||||
:duration => params[:length].to_i,
|
duration: params[:length].to_i,
|
||||||
:sk => user.lastfm_key # Auth session key
|
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
|
end
|
||||||
|
|
||||||
def self.top_playlist artist
|
def self.top_playlist artist
|
||||||
|
@ -89,16 +90,16 @@ class LastFmController < ApplicationController
|
||||||
|
|
||||||
def autocomplete
|
def autocomplete
|
||||||
autocomplete = getSuggestions(params[:query])
|
autocomplete = getSuggestions(params[:query])
|
||||||
return render :nothing => true if autocomplete.nil?
|
return render nothing: true if autocomplete.nil?
|
||||||
suggestions = []
|
suggestions = []
|
||||||
autocomplete["response"]["docs"].each do |doc|
|
autocomplete["response"]["docs"].each do |doc|
|
||||||
suggestions << doc["artist"] unless suggestions.include?(doc["artist"]) or doc["artist"].nil? or doc['restype'] != 6
|
suggestions << doc["artist"] unless suggestions.include?(doc["artist"]) or doc["artist"].nil? or doc['restype'] != 6
|
||||||
end
|
end
|
||||||
|
|
||||||
response.headers['Cache-Control'] = 'public, max-age='+1.week.seconds.to_s
|
response.headers['Cache-Control'] = 'public, max-age='+1.week.seconds.to_s
|
||||||
render :json => {
|
render json: {
|
||||||
:query => params[:query],
|
query: params[:query],
|
||||||
:suggestions => suggestions.take(5)
|
suggestions: suggestions.take(5)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@ class UserController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def update
|
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])
|
user = User.find_by_vkid(params[:mid])
|
||||||
unless params[:params].nil?
|
unless params[:params].nil?
|
||||||
|
@ -85,9 +85,8 @@ private
|
||||||
vkid: user.vkid,
|
vkid: user.vkid,
|
||||||
lang: user.lang,
|
lang: user.lang,
|
||||||
lastfm_username: user.lastfm_username,
|
lastfm_username: user.lastfm_username,
|
||||||
lastfm_login_url: ('http://www.last.fm/api/auth?api_key=' << LastFM.api_key <<
|
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=' <<
|
"&cb=http://#{request.host}:#{request.port.to_s}/lastfm/connect/?sid=#{user.session.key}"
|
||||||
user.session.key if user.lastfm_username.nil? or user.lastfm_username == "")
|
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
class Scrobble < ActiveRecord::Base
|
||||||
|
belongs_to :user
|
||||||
|
belongs_to :track
|
||||||
|
end
|
|
@ -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
|
|
@ -11,7 +11,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 => 20111127102836) do
|
ActiveRecord::Schema.define(:version => 20111129193103) do
|
||||||
|
|
||||||
create_table "album_formats", :force => true do |t|
|
create_table "album_formats", :force => true do |t|
|
||||||
t.integer "album_id"
|
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"
|
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|
|
create_table "sessions", :force => true do |t|
|
||||||
t.integer "user_id"
|
t.integer "user_id"
|
||||||
t.string "key"
|
t.string "key"
|
||||||
|
|
|
@ -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
|
|
@ -0,0 +1,7 @@
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class ScrobbleTest < ActiveSupport::TestCase
|
||||||
|
# test "the truth" do
|
||||||
|
# assert true
|
||||||
|
# end
|
||||||
|
end
|
Loading…
Reference in New Issue