From 672276d37d8535a7759e42a633a3d35ff6692c97 Mon Sep 17 00:00:00 2001 From: magnolia-fan Date: Thu, 23 Jun 2011 03:44:27 +0400 Subject: [PATCH] Scrobbling to lastfm! w00t --- app/controllers/last_fm_controller.rb | 34 +++++++++++++++++++++----- public/javascripts/beathaven/audio.js | 26 ++++++++++++++++++-- public/javascripts/beathaven/player.js | 15 +++++++++++- 3 files changed, 66 insertions(+), 9 deletions(-) diff --git a/app/controllers/last_fm_controller.rb b/app/controllers/last_fm_controller.rb index 721a653..b5919ee 100644 --- a/app/controllers/last_fm_controller.rb +++ b/app/controllers/last_fm_controller.rb @@ -44,26 +44,48 @@ class LastFmController < ApplicationController def listening return unless authorized? - return if params[:artist].nil? or params[:track].nil? + @res = {} + + if params[:artist].nil? or params[:track].nil? + render :json => {:status => 'bad params'} + return + end user = User.find_by_vkid(params[:mid]) - return unless user.lastfm_key.nil? + if user.lastfm_key.nil? + render :json => {:status => 'lastfm account is not connected', :user => user} + return + end lastfm = Lastfm.new(@@api_key, @@secret) lastfm.session = user.lastfm_key - lastfm.update_now_playing(params[:artist], params[:track]) + r = lastfm.track.update_now_playing(params[:artist], params[:track]) + + render :json => {:status => r ? 'success' : 'failed'} + return end def scrobble return unless authorized? - return if params[:artist].nil? or params[:track].nil? + @res = {} + + if params[:artist].nil? or params[:track].nil? + render :json => {:status => 'bad params'} + return + end user = User.find_by_vkid(params[:mid]) - return unless user.lastfm_key.nil? + if user.lastfm_key.nil? + render :json => {:status => 'lastfm account is not connected', :user => user} + return + end lastfm = Lastfm.new(@@api_key, @@secret) lastfm.session = user.lastfm_key - lastfm.scrobble(params[:artist], params[:track]) + r = lastfm.track.scrobble(params[:artist], params[:track]) + + render :json => {:status => r ? 'success' : 'failed'} + return end end diff --git a/public/javascripts/beathaven/audio.js b/public/javascripts/beathaven/audio.js index a5eb1b3..5260539 100644 --- a/public/javascripts/beathaven/audio.js +++ b/public/javascripts/beathaven/audio.js @@ -1,7 +1,10 @@ var Audio = { audio: null, + tid: null, + tinterval: 300, + ttime: 0, play: function() { try { @@ -36,6 +39,7 @@ var Audio = { $('#player .controls .pause').hide(); $('#player .controls .play').show(); }); + this.ttime = 0; }, getLoadedPercent: function() { @@ -52,6 +56,16 @@ var Audio = { return 0; }, + getListenedPercent: function() { + try { + if (Audio.audio.duration < 30) { + return -1; + } + return Audio.ttime / 1000 / Audio.audio.duration; + } catch(e) {} + return 0; + }, + setPlayedPercent: function(val) { if (typeof(this.audio.duration) != undefined) { this.audio.currentTime = Math.round(val * this.audio.duration / 100); @@ -68,10 +82,18 @@ var Audio = { startListener: function() { Audio.killListener(); - Audio.tid = window.setTimeout(Audio.startListener, 300); + Audio.tid = window.setTimeout(Audio.startListener, Audio.tinterval); + if (Audio.ttime !== -1) { + Audio.ttime += Audio.tinterval; + } + var pp = Audio.getListenedPercent(); + if (pp > 0.4) { + Audio.ttime = -1; + } Player.updateUI( Audio.getLoadedPercent(), - Audio.getPlayedPercent() + Audio.getPlayedPercent(), + pp ); }, diff --git a/public/javascripts/beathaven/player.js b/public/javascripts/beathaven/player.js index 9a3be2c..5adb3d9 100644 --- a/public/javascripts/beathaven/player.js +++ b/public/javascripts/beathaven/player.js @@ -34,6 +34,7 @@ var Player = { $('#player .loaded, #player .played').width(0); loadTracksData(tmp[0], tmp[1], length, Player.playSource); + Player.updateNowListening(tmp[0], tmp[1]); }, playSource: function(url) { @@ -66,13 +67,17 @@ var Player = { } }, - updateUI: function(buffered, played) { + updateUI: function(buffered, played, listened) { $('#player .loaded').width(Math.round(buffered * this.bar_width)); $('#player .played').width(Math.round(played * this.bar_width)); if (played == 1) { Audio.killListener(); Player.setTrack(Player.nextTrack()); } + if (listened > 0.4) { + var tmp = $('#player .now-playing').text().split(' — '); + Player.scrobble(tmp[0], tmp[1]); + } }, reset: function() { @@ -80,6 +85,14 @@ var Player = { Audio.killListener(); $('#player .loaded, #player .played').width(0); $('#player .now-playing').text('Select track'); + }, + + updateNowListening: function(artist, track) { + Session.query('/lastfm/listening', {'artist': artist, 'track': track}) + }, + + scrobble: function(artist, track) { + Session.query('/lastfm/scrobble', {'artist': artist, 'track': track}) } }