Skip to content

Commit 6e2f765

Browse files
committed
fix(lastfm): Fix mishandled properties in scrobble request causing ignored track
Removing undefined properties from request data for track scrobble fixes an issue where last.fm ignores the track and returns error code 1 Fixes #33
1 parent 57fd885 commit 6e2f765

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

clients/LastfmScrobbler.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import dayjs from 'dayjs';
33

44
import {
55
buildTrackString,
6-
playObjDataMatch,
6+
playObjDataMatch, removeUndefinedKeys,
77
setIntersection, sleep,
88
sortByPlayDate,
99
truncateStringToLength,
@@ -259,13 +259,19 @@ export default class LastfmScrobbler extends AbstractScrobbleClient {
259259

260260
try {
261261
const response = await this.api.callApi(client => client.trackScrobble(
262-
{
262+
// i don't know if its lastfm-node-client building the request params incorrectly
263+
// or the last.fm api not handling the params correctly...
264+
//
265+
// ...but in either case if any of the below properties is undefined (possibly also null??)
266+
// then last.fm responds with an IGNORED scrobble and error code 1 (totally unhelpful)
267+
// so remove all undefined keys from the object before passing to the api client
268+
removeUndefinedKeys({
263269
artist: artists.join(', '),
264270
duration,
265271
track,
266272
album,
267273
timestamp: playDate.unix(),
268-
}));
274+
})));
269275
const {
270276
scrobbles: {
271277
'@attr': {

utils.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,25 @@ export const spreadDelay = (retries, multiplier) => {
327327
}
328328
return s;
329329
}
330+
331+
export const removeUndefinedKeys = (obj) => {
332+
let newObj = {};
333+
Object.keys(obj).forEach((key) => {
334+
if(Array.isArray(obj[key])) {
335+
newObj[key] = obj[key];
336+
} else if (obj[key] === Object(obj[key])) {
337+
newObj[key] = removeUndefinedKeys(obj[key]);
338+
} else if (obj[key] !== undefined) {
339+
newObj[key] = obj[key];
340+
}
341+
});
342+
if(Object.keys(newObj).length === 0) {
343+
return undefined;
344+
}
345+
Object.keys(newObj).forEach(key => {
346+
if(newObj[key] === undefined || (null !== newObj[key] && typeof newObj[key] === 'object' && Object.keys(newObj[key]).length === 0)) {
347+
delete newObj[key]
348+
}
349+
});
350+
return newObj;
351+
}

0 commit comments

Comments
 (0)