From da473448f48ce1cb7a9b1aae95ea66e84e1c3337 Mon Sep 17 00:00:00 2001 From: Hugo Date: Wed, 27 Feb 2019 19:47:31 +0200 Subject: [PATCH] Add support for new user.getTrackScrobbles --- pylast/__init__.py | 26 ++++++++++++++++++++++++++ tests/test_user.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/pylast/__init__.py b/pylast/__init__.py index 1cd1a2e..3d1e4ab 100644 --- a/pylast/__init__.py +++ b/pylast/__init__.py @@ -2533,6 +2533,32 @@ class User(_BaseObject, _Chartable): return self._get_things("getTopTracks", "track", Track, params, cacheable) + def get_track_scrobbles(self, artist, track, cacheable=False): + """ + Get a list of scrobbles of this tracks by this artist scrobbled by this user, + including scrobble time. + """ + + params = self._get_params() + params["artist"] = artist + params["track"] = track + + seq = [] + for track in _collect_nodes( + None, self, self.ws_prefix + ".getTrackScrobbles", cacheable, params + ): + title = _extract(track, "name") + artist = _extract(track, "artist") + date = _extract(track, "date") + album = _extract(track, "album") + timestamp = track.getElementsByTagName("date")[0].getAttribute("uts") + + seq.append( + PlayedTrack(Track(artist, title, self.network), album, date, timestamp) + ) + + return seq + def get_image(self, size=SIZE_EXTRA_LARGE): """ Returns the user's avatar diff --git a/tests/test_user.py b/tests/test_user.py index bd019ea..cf37c09 100755 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -433,6 +433,34 @@ class TestPyLastUser(TestPyLastWithLastFm): self.assertIsNotNone(track) self.assertIsInstance(track.network, pylast.LastFMNetwork) + def test_user_get_track_scrobbles(self): + # Arrange + artist = "France Gall" + title = "Laisse Tomber Les Filles" + user = self.network.get_user("bbc6music") + + # Act + scrobbles = user.get_track_scrobbles(artist, title) + + # Assert + self.assertGreater(len(scrobbles), 0) + self.assertEqual(str(scrobbles[0].track.artist), "France Gall") + self.assertEqual(scrobbles[0].track.title, "Laisse Tomber Les Filles") + + def test_cacheable_user_get_track_scrobbles(self): + # Arrange + artist = "France Gall" + title = "Laisse Tomber Les Filles" + user = self.network.get_user("bbc6music") + + # Act + result1 = user.get_track_scrobbles(artist, title, cacheable=False) + result2 = user.get_track_scrobbles(artist, title, cacheable=True) + result3 = user.get_track_scrobbles(artist, title) + + # Assert + self.helper_validate_results(result1, result2, result3) + if __name__ == "__main__": unittest.main(failfast=True)