Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lastfm update #268

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
26 changes: 26 additions & 0 deletions modules/scrobbler.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,30 @@ exports.setup = function (mstream, program) {
res.json({ scrobble: true });
});
});

mstream.post("/lastfm/nowplaying-by-metadata", function (req, res) {
var artist = req.body.artist;
var album = req.body.album;
var track = req.body.track;
var duration = Math.round(req.body.duration);

if (!req.user["lastfm-user"] || !req.user["lastfm-password"]) {
res.json({ scrobble: false});
return;
}

Scrobbler.NowPlaying(
{
artist: artist,
track: track,
album: album,
duration: duration //The length of the track in seconds - Optional, but not here otherwise api_sig is wrong
},
req.user['lastfm-user'],
function (post_return_data) {
res.json({ scrobble: true, return: post_return_data });
}
);

});
}
7 changes: 6 additions & 1 deletion public/js/api2.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,16 @@ var MSTREAMAPI = (function () {
makeGETRequest("/federation/stats", false, callback);
}

// Scrobble
// Lastfm - Scrobble
mstreamModule.scrobbleByMetadata = function (artist, album, trackName, callback) {
makePOSTRequest("/lastfm/scrobble-by-metadata", { artist: artist, album: album, track: trackName }, callback);
}

// Lastfm - Now Playing
mstreamModule.nowPlayingByMetadata = function (artist, album, trackName, duration, callback) {
makePOSTRequest("/lastfm/nowplaying-by-metadata", { artist: artist, album: album, track: trackName, duration: duration }, callback);
}

// LOGIN
mstreamModule.login = function (username, password, callback) {
makePOSTRequest("/login", { username: username, password: password }, callback);
Expand Down
11 changes: 10 additions & 1 deletion public/js/mstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ $(document).ready(function () {
iziToast.error(iziStuff);
});

// Setup scrobbling
// Lastfm - Setup scrobbling
MSTREAMPLAYER.scrobble = function () {
if (MSTREAMPLAYER.playerStats.metadata.artist && MSTREAMPLAYER.playerStats.metadata.title) {
MSTREAMAPI.scrobbleByMetadata(MSTREAMPLAYER.playerStats.metadata.artist, MSTREAMPLAYER.playerStats.metadata.album, MSTREAMPLAYER.playerStats.metadata.title, function (response, error) {
Expand All @@ -253,6 +253,15 @@ $(document).ready(function () {
}
}

// Lastfm - Setup update Now Playing
MSTREAMPLAYER.updateNowPlaying = function () {
if (MSTREAMPLAYER.playerStats.metadata.artist && MSTREAMPLAYER.playerStats.metadata.title) {
MSTREAMAPI.nowPlayingByMetadata(MSTREAMPLAYER.playerStats.metadata.artist, MSTREAMPLAYER.playerStats.metadata.album, MSTREAMPLAYER.playerStats.metadata.title, MSTREAMPLAYER.playerStats.duration, function (response, error) {

});
}
}

// Auto Focus
Vue.directive('focus', {
// When the bound element is inserted into the DOM...
Expand Down
45 changes: 41 additions & 4 deletions public/js/mstream.player.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
var MSTREAMPLAYER = (function () {
let mstreamModule = {};

//Lastfm - Scrobble after conditions met and updateNowPlaying
//Wait 1sec to prevent from spamming lastfm while fast skipping tracks
function updateLastfm() {
clearTimeout(scrobbleTimer);
clearTimeout(scrobbleTimer2);
scrobbleTimer = setTimeout(function () {
mstreamModule.updateNowPlaying();
const duration = Math.round(mstreamModule.playerStats.duration);
//First condition: The track must be longer than 30 seconds!
if (duration > 30) {
//Second condition: the track has been played for at least half its duration, or for 4 minutes (240s) (whichever occurs earlier.)
if (duration/2 <= 240) {
scrobbleTimer2 = setTimeout(function () {
mstreamModule.scrobble();
}, duration/2*1000);
} else {
scrobbleTimer2 = setTimeout(function () {
mstreamModule.scrobble();
}, 240*1000);
}

}
}, 1000);
}

// Playlist variables
mstreamModule.positionCache = { val: -1 };
mstreamModule.playlist = [];
Expand Down Expand Up @@ -34,11 +59,18 @@ var MSTREAMPLAYER = (function () {

// Scrobble function
// This is a placeholder function that the API layer can take hold of to implement the scrobble call
var scrobbleTimer;
var scrobbleTimer2;
mstreamModule.scrobble = function () {
return false;
}

// update now playing function
// This is a placeholder function that the API layer can take hold of to implement the scrobble call
var scrobbleTimer;
mstreamModule.updateNowPlaying = function () {
return false;
};

// The audioData looks like this
// var song = {
// "url":"vPath/path/to/song.mp3?token=xxx",
Expand Down Expand Up @@ -438,6 +470,11 @@ var MSTREAMPLAYER = (function () {
mstreamModule.resetCurrentMetadata();
}

//Start LastFM update (nowplaying + scrobble)
lPlayer.playerObject.once("play", function() {
updateLastfm();
})

// connect to visualizer
if (VIZ) {
var audioCtx = VIZ.get();
Expand Down Expand Up @@ -484,9 +521,6 @@ var MSTREAMPLAYER = (function () {

}, cacheTimeout);

// Scrobble song after 30 seconds
clearTimeout(scrobbleTimer);
scrobbleTimer = setTimeout(function () { mstreamModule.scrobble() }, 30000);
return true;
}

Expand Down Expand Up @@ -640,6 +674,9 @@ var MSTREAMPLAYER = (function () {
onpause: function () {
},
onstop: function () {
//Don't scrobble on stop if not already scrobbled
clearTimeout(scrobbleTimer);
clearTimeout(scrobbleTimer2);
},
onplay: function () {
},
Expand Down