Scrobbling to lastfm! w00t
This commit is contained in:
parent
8c33730252
commit
672276d37d
|
@ -44,26 +44,48 @@ class LastFmController < ApplicationController
|
||||||
|
|
||||||
def listening
|
def listening
|
||||||
return unless authorized?
|
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])
|
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 = Lastfm.new(@@api_key, @@secret)
|
||||||
lastfm.session = user.lastfm_key
|
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
|
end
|
||||||
|
|
||||||
def scrobble
|
def scrobble
|
||||||
return unless authorized?
|
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])
|
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 = Lastfm.new(@@api_key, @@secret)
|
||||||
lastfm.session = user.lastfm_key
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
var Audio = {
|
var Audio = {
|
||||||
|
|
||||||
audio: null,
|
audio: null,
|
||||||
|
|
||||||
tid: null,
|
tid: null,
|
||||||
|
tinterval: 300,
|
||||||
|
ttime: 0,
|
||||||
|
|
||||||
play: function() {
|
play: function() {
|
||||||
try {
|
try {
|
||||||
|
@ -36,6 +39,7 @@ var Audio = {
|
||||||
$('#player .controls .pause').hide();
|
$('#player .controls .pause').hide();
|
||||||
$('#player .controls .play').show();
|
$('#player .controls .play').show();
|
||||||
});
|
});
|
||||||
|
this.ttime = 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
getLoadedPercent: function() {
|
getLoadedPercent: function() {
|
||||||
|
@ -52,6 +56,16 @@ var Audio = {
|
||||||
return 0;
|
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) {
|
setPlayedPercent: function(val) {
|
||||||
if (typeof(this.audio.duration) != undefined) {
|
if (typeof(this.audio.duration) != undefined) {
|
||||||
this.audio.currentTime = Math.round(val * this.audio.duration / 100);
|
this.audio.currentTime = Math.round(val * this.audio.duration / 100);
|
||||||
|
@ -68,10 +82,18 @@ var Audio = {
|
||||||
|
|
||||||
startListener: function() {
|
startListener: function() {
|
||||||
Audio.killListener();
|
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(
|
Player.updateUI(
|
||||||
Audio.getLoadedPercent(),
|
Audio.getLoadedPercent(),
|
||||||
Audio.getPlayedPercent()
|
Audio.getPlayedPercent(),
|
||||||
|
pp
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,7 @@ var Player = {
|
||||||
$('#player .loaded, #player .played').width(0);
|
$('#player .loaded, #player .played').width(0);
|
||||||
|
|
||||||
loadTracksData(tmp[0], tmp[1], length, Player.playSource);
|
loadTracksData(tmp[0], tmp[1], length, Player.playSource);
|
||||||
|
Player.updateNowListening(tmp[0], tmp[1]);
|
||||||
},
|
},
|
||||||
|
|
||||||
playSource: function(url) {
|
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 .loaded').width(Math.round(buffered * this.bar_width));
|
||||||
$('#player .played').width(Math.round(played * this.bar_width));
|
$('#player .played').width(Math.round(played * this.bar_width));
|
||||||
if (played == 1) {
|
if (played == 1) {
|
||||||
Audio.killListener();
|
Audio.killListener();
|
||||||
Player.setTrack(Player.nextTrack());
|
Player.setTrack(Player.nextTrack());
|
||||||
}
|
}
|
||||||
|
if (listened > 0.4) {
|
||||||
|
var tmp = $('#player .now-playing').text().split(' — ');
|
||||||
|
Player.scrobble(tmp[0], tmp[1]);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
reset: function() {
|
reset: function() {
|
||||||
|
@ -80,6 +85,14 @@ var Player = {
|
||||||
Audio.killListener();
|
Audio.killListener();
|
||||||
$('#player .loaded, #player .played').width(0);
|
$('#player .loaded, #player .played').width(0);
|
||||||
$('#player .now-playing').text('Select track');
|
$('#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})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue