1
0
Fork 0

Player improvements, scrobbling with album name

This commit is contained in:
magnolia-fan 2011-06-26 10:13:16 +04:00
parent cef23c43e9
commit 0b6bad1a24
2 changed files with 30 additions and 19 deletions

View File

@ -47,7 +47,7 @@ class LastFmController < ApplicationController
return unless authorized? return unless authorized?
@res = {} @res = {}
if params[:artist].nil? or params[:track].nil? if params[:artist].nil? or params[:album].nil? or params[:track].nil?
render :json => {:status => 'bad params'} render :json => {:status => 'bad params'}
return return
end end
@ -70,7 +70,7 @@ class LastFmController < ApplicationController
return unless authorized? return unless authorized?
@res = {} @res = {}
if params[:artist].nil? or params[:track].nil? if params[:artist].nil? or params[:album].nil? or params[:track].nil?
render :json => {:status => 'bad params'} render :json => {:status => 'bad params'}
return return
end end
@ -83,7 +83,7 @@ class LastFmController < ApplicationController
lastfm = Lastfm.new(@@api_key, @@secret) lastfm = Lastfm.new(@@api_key, @@secret)
lastfm.session = user.lastfm_key lastfm.session = user.lastfm_key
r = lastfm.track.scrobble(params[:artist], params[:track], nil, Time.now.utc.to_i) r = lastfm.track.scrobble(params[:artist], params[:track], params[:album], Time.now.utc.to_i)
render :json => {:status => r ? 'success' : 'failed', time: Time.now.utc} render :json => {:status => r ? 'success' : 'failed', time: Time.now.utc}
return return

View File

@ -28,8 +28,8 @@ var Player = {
this.jp.bind($.jPlayer.event.timeupdate, function(e){ this.jp.bind($.jPlayer.event.timeupdate, function(e){
data = e.jPlayer.status; data = e.jPlayer.status;
if (!Player.scrobbled && data.currentPercentAbsolute > 50) { if (!Player.scrobbled && data.currentPercentAbsolute > 50) {
var tmp = $('#player .now-playing').text().split(' — '); var $obj = $('.playlist-tracks li.now');
Player.scrobble(tmp[0], tmp[1]); Player.scrobble($obj.attr('data-artist'), $obj.attr('data-album'), $obj.attr('data-track'));
Player.scrobbled = true; Player.scrobbled = true;
} }
$('#player .progress .loaded').width(data.seekPercent * Player.bar_width / 100); $('#player .progress .loaded').width(data.seekPercent * Player.bar_width / 100);
@ -40,10 +40,13 @@ var Player = {
}); });
}, },
addTrack: function(artist, track, length) { addTrack: function(artist, album, track, length, autoplay) {
if (typeof autoplay === 'undefined') {
autoplay = false;
}
var initial_count = $('.playlist-tracks li').length; var initial_count = $('.playlist-tracks li').length;
$('.playlist-tracks').append('\ $('.playlist-tracks').append('\
<li id="i'+ Math.round(Math.random() * 999999) +'">\ <li id="i'+ Math.round(Math.random() * 999999) +'" data-artist="'+ artist +'" data-album="'+ album +'" data-track="'+ track +'" data-length="'+ length +'">\
<div class="item">\ <div class="item">\
<div class="fade"></div>\ <div class="fade"></div>\
<span class="title">'+ artist +' &mdash; '+ track +'</span>\ <span class="title">'+ artist +' &mdash; '+ track +'</span>\
@ -56,24 +59,24 @@ var Player = {
$('#playlist').html($('.playlist-tracks')).scrollbar(); $('#playlist').html($('.playlist-tracks')).scrollbar();
$('.playlist-tracks').sortable(); $('.playlist-tracks').sortable();
if (initial_count === 0 && !Player.hasTrack()) { if (autoplay) {
Player.setTrack($('.playlist-tracks li').last().attr('id').split('i')[1]);
} else if (initial_count === 0 && !Player.hasTrack()) {
Player.setTrack($('.playlist-tracks li').first().attr('id').split('i')[1]); Player.setTrack($('.playlist-tracks li').first().attr('id').split('i')[1]);
} }
}, },
setTrack: function(id) { setTrack: function(id) {
var $obj = $('#i'+ id); var $obj = $('#i'+ id);
var query = $obj.find('.title').text(); var query = $obj.attr('data-artist') +'&mdash;'+ $obj.attr('data-track');
var length = $obj.find('.duration').text();
var tmp = query.split(' — ');
$('#player .now-playing').html(query); $('#player .now-playing').html(query);
$('.playlist-tracks li').removeClass('now'); $('.playlist-tracks li').removeClass('now');
$obj.addClass('now'); $obj.addClass('now');
$('#player .loaded, #player .played').width(0); $('#player .loaded, #player .played').width(0);
loadTracksData(tmp[0], tmp[1], length, Player.playSource); loadTracksData($obj.attr('data-artist'), $obj.attr('data-track'), $obj.attr('data-length'), Player.playSource);
Player.updateNowListening(tmp[0], tmp[1]); Player.updateNowListening($obj.attr('data-artist'), $obj.attr('data-album'), $obj.attr('data-track'));
}, },
hasTrack: function() { hasTrack: function() {
@ -111,12 +114,12 @@ var Player = {
} }
}, },
updateNowListening: function(artist, track) { updateNowListening: function(artist, album, track) {
Session.query('/lastfm/listening?r='+ Math.random(), {'artist': artist, 'track': track}) Session.query('/lastfm/listening?r='+ Math.random(), {'artist': artist, 'album': album, 'track': track})
}, },
scrobble: function(artist, track) { scrobble: function(artist, album, track) {
Session.query('/lastfm/scrobble?r='+ Math.random(), {'artist': artist, 'track': track}) Session.query('/lastfm/scrobble?r='+ Math.random(), {'artist': artist, 'album': album, 'track': track})
} }
} }
@ -134,17 +137,19 @@ $('#player .controls .next').live('click', function(){
$('.add-album').live('click', function() { $('.add-album').live('click', function() {
var artist = $('.artist-info .name').html(); var artist = $('.artist-info .name').html();
var album = $(this).parent().parent().parent().find('h2.name').text().replace(/\s\([\d]{4}\)$/, '');
$(this).parent().parent().parent().find('.tracklist li').each(function(i, item){ $(this).parent().parent().parent().find('.tracklist li').each(function(i, item){
var track_name = $(item).find('.trackname').html(); var track_name = $(item).find('.trackname').html();
var length = $(item).find('.length').html(); var length = $(item).find('.length').html();
Player.addTrack(artist, track_name, length); Player.addTrack(artist, album, track_name, length);
}); });
}) })
$('.add-track').live('click', function(){ $('.add-track').live('click', function(){
var artist = $('.artist-info .name').html(); var artist = $('.artist-info .name').html();
var album = $(this).parent().parent().parent().parent().find('h2.name').text().replace(/\s\([\d]{4}\)$/, '');
var track_name = $(this).parent().find('.trackname').html(); var track_name = $(this).parent().find('.trackname').html();
var length = $(this).parent().find('.length').html(); var length = $(this).parent().find('.length').html();
Player.addTrack(artist, track_name, length); Player.addTrack(artist, album, track_name, length);
}); });
$('#player .play').live('click', function(){ $('#player .play').live('click', function(){
@ -159,6 +164,12 @@ $('.tracklist li').live('mouseover mouseout', function(e){
} else { } else {
$(this).find('.add-track').hide(); $(this).find('.add-track').hide();
} }
}).live('dblclick', function(e){
var artist = $('.artist-info .name').html();
var album = $(this).parent().parent().parent().find('h2.name').text().replace(/\s\([\d]{4}\)$/, '');
var track_name = $(this).find('.trackname').html();
var length = $(this).find('.length').html();
Player.addTrack(artist, album, track_name, length, true);
}); });
$('.playlist-tracks li .fade, .playlist-tracks li .duration, .playlist-tracks li .remove').live('mouseover mouseout', function(e){ $('.playlist-tracks li .fade, .playlist-tracks li .duration, .playlist-tracks li .remove').live('mouseover mouseout', function(e){