Skip to content

Commit

Permalink
Added lastfm.trackScrobbleMany()
Browse files Browse the repository at this point in the history
  • Loading branch information
rattletone committed Mar 5, 2019
2 parents 700d97e + 1df5d0b commit 4d49930
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Expand Up @@ -90,3 +90,28 @@ lastFm.userGetRecentTracks({
```

When callback is passed, methods do not return a `promise`, instead return the LastFm instance the method was called on. This allows you to chain requests.

## Utility methods

These methods do not correspond to an exact API endpoint, but are abstractions of the already provided methods to provide easier usage.

### `lastFm.trackScrobbleMany`

This method takes an array of objects that match the `params` parameter accepted by `lastFm.trackScrobble`. It allows for multiple tracks to be submitted to the API in one request.

```js
lastFm.trackScrobbleMany([
{
"artist": "ARTIST",
"album": "ALBUM",
"track": "TRACK",
"timestamp": "TIMESTAMP"
},
{
"artist": "ARTIST",
"album": "ALBUM",
"track": "TRACK",
"timestamp": "TIMESTAMP"
}
]);
```
66 changes: 66 additions & 0 deletions __test__/LastFm.test.js
Expand Up @@ -841,6 +841,72 @@ describe("LastFm", () => {
});
});

test("scrobble many tracks", done => {
const lastFm = new LastFm({ apiKey, secret, sessionKey });

nock("http://ws.audioscrobbler.com")
.post("/2.0/", {
"api_key": apiKey,
"api_sig": /.+/u,
"format": "json",
"method": "track.scrobble",
"sk": sessionKey
})
.reply(200, { "status": "ok" });

lastFm.trackScrobbleMany([], (err, data) => {
expect(err).toBeNull();
expect(data).toEqual({ "status": "ok" });
done();
});
});

describe("trackScrobbleMany()", () => {
test("concatenate params array into single object, using array notation to distinguish each set of params, and call LastFm.trackScrobble() with that as the first argument", done => {
const lastFm = new LastFm({ apiKey, secret, sessionKey });
const spy = jest.spyOn(lastFm, "trackScrobble");
const params = [{ "param": "<value>" }, { "param": "<value>" }];

nock("http://ws.audioscrobbler.com")
.post("/2.0/")
.reply(200, { "status": "ok" });

lastFm.trackScrobbleMany(params, (err, data) => {
expect(err).toBeNull();
expect(data).toEqual({ "status": "ok" });
expect(spy).toBeCalledTimes(1);

const [firstCallArgs] = spy.mock.calls;
const [firstCallFirstArg] = firstCallArgs;

expect(JSON.stringify(firstCallFirstArg)).toBe("{\"param[1]\":\"<value>\",\"param[0]\":\"<value>\"}");
done();
});
});

test("if called with one params object, call LastFm.trackScrobble() with that as the first argument", done => {
const lastFm = new LastFm({ apiKey, secret, sessionKey });
const spy = jest.spyOn(lastFm, "trackScrobble");
const params = [{ "param": "<value>" }];

nock("http://ws.audioscrobbler.com")
.post("/2.0/")
.reply(200, { "status": "ok" });

lastFm.trackScrobbleMany(params, (err, data) => {
expect(err).toBeNull();
expect(data).toEqual({ "status": "ok" });
expect(spy).toBeCalledTimes(1);

const [firstCallArgs] = spy.mock.calls;
const [firstCallFirstArg] = firstCallArgs;

expect(JSON.stringify(firstCallFirstArg)).toBe("{\"param\":\"<value>\"}");
done();
});
});
});

test("search for a track", done => {
const lastFm = new LastFm({ apiKey, secret, sessionKey });

Expand Down
33 changes: 33 additions & 0 deletions lib/LastFm.js
Expand Up @@ -962,6 +962,39 @@ class LastFm {
return apiRequest || this;
}

/**
* Scrobble many tracks
* @param {Object[]} paramsArr
* @param {string} paramsArr[].artist - Artist whose track to scrobble
* @param {string} paramsArr[].track - Track to scobble
* @param {string} [paramsArr[].album] - Album the track to scrobble is from
* @param {string} [paramsArr[].albumArist] - Artist whose album the track to scrobble is from
* @param {number} paramsArr[].timestamp - Timestamp to scrobble track at
* @param {number} [paramsArr[].trackNumber] - Number of track to scrobble on the album
* @param {number} [paramsArr[].duration] - Length of the track to scrobble in seconds
* @param {(0|1)} [paramsArr[].chosenByUser] - Whether the user chose the track to scrobble
* @param {string} [paramsArr.streamId] - Stream ID if track to scrobble is from Last.Fm radio
* @param {callback} [callback]
* @returns {(Promise|LastFm)}
*/

trackScrobbleMany(paramsArr, callback) {
if(paramsArr.length === 1) {
return this.trackScrobble(paramsArr[0], callback);
}

const paramsMany = {};
let i = paramsArr.length;

while(i--) {
for(const [param, value] of Object.entries(paramsArr[i])) {
paramsMany[`${param}[${i}]`] = value;
}
}

return this.trackScrobble(paramsMany, callback);
}

/**
* Search for a track
* @param {Object} params
Expand Down

0 comments on commit 4d49930

Please sign in to comment.