From 785139dce6291a6353dc5ee90b1770cf17fa4f2f Mon Sep 17 00:00:00 2001 From: hugovk Date: Mon, 3 Mar 2014 12:45:16 +0200 Subject: [PATCH] Add Track.get_userloved() with test, closes #70 --- pylast.py | 18 +++++++++++++++--- test_pylast.py | 15 +++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/pylast.py b/pylast.py index 6a5bb4c..46db7e6 100644 --- a/pylast.py +++ b/pylast.py @@ -903,7 +903,7 @@ class SessionKeyGenerator(object): c. password_hash = pylast.md5(raw_input("Please enter your password: ") d. session_key = SessionKeyGenerator(network).get_session_key(username, password_hash) - A session key's lifetime is infinie, unless the user provokes the rights of the given API Key. + A session key's lifetime is infinite, unless the user provokes the rights of the given API Key. If you create a Network object with just a API_KEY and API_SECRET and a username and a password_hash, a SESSION_KEY will be automatically generated for that network and stored in it so you don't have to do this @@ -1291,7 +1291,7 @@ class Album(_BaseObject, _Taggable): return _number(_extract(self._request("album.getInfo", True, params), "userplaycount")) def get_listener_count(self): - """Returns the number of liteners on the network""" + """Returns the number of listeners on the network""" return _number(_extract(self._request("album.getInfo", cacheable = True), "listeners")) @@ -1448,7 +1448,7 @@ class Artist(_BaseObject, _Taggable): return _extract(doc, "mbid") def get_listener_count(self): - """Returns the number of liteners on the network.""" + """Returns the number of listeners on the network.""" if hasattr(self, "listener_count"): return self.listener_count @@ -2529,6 +2529,18 @@ class Track(_BaseObject, _Taggable): doc = self._request("track.getInfo", True, params) return _number(_extract(doc, "userplaycount")) + def get_userloved(self): + """Whether the user loved this track""" + + if not self.username: return + + params = self._get_params() + params['username'] = self.username + + doc = self._request("track.getInfo", True, params) + loved = _number(_extract(doc, "userloved")) + return bool(loved) + def is_streamable(self): """Returns True if the track is available at Last.fm.""" diff --git a/test_pylast.py b/test_pylast.py index 397620e..1a7221d 100755 --- a/test_pylast.py +++ b/test_pylast.py @@ -498,6 +498,21 @@ class TestPyLast(unittest.TestCase): self.assertGreaterEqual(count, 0) + def test_user_loved_in_track_info(self): + # Arrange + artist = "Test Artist" + title = "Test Title" + track = pylast.Track(artist = artist, title = title, network = self.network, username = self.username) + + # Act + loved = track.get_userloved() + + # Assert + self.assertIsNotNone(loved) + self.assertIsInstance(loved, bool) + self.assertNotIsInstance(loved, str) + + if __name__ == '__main__': # For quick testing of a single case (eg. test = "test_scrobble")