From 552c45f18f37c801add8d954518786d69b417a2f Mon Sep 17 00:00:00 2001 From: hugovk Date: Thu, 27 Feb 2014 20:14:32 +0200 Subject: [PATCH] Get user's registration date (and as a UNIX timestamp) --- pylast.py | 18 ++++++++++++++-- test_pylast.py | 56 ++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/pylast.py b/pylast.py index 2671419..e608434 100644 --- a/pylast.py +++ b/pylast.py @@ -2063,7 +2063,7 @@ class Library(_BaseObject): """Remove a scrobble from a user's Last.fm library. Parameters: artist (Required) : The artist that composed the track title (Required) : The name of the track - timestamp (Required) : The unix timestamp of the scrobble + timestamp (Required) : The unix timestamp of the scrobble that you wish to remove """ @@ -2830,7 +2830,7 @@ class User(_BaseObject): return {"user": self.get_name()} def get_name(self, properly_capitalized=False): - """Returns the nuser name.""" + """Returns the user name.""" if properly_capitalized: self.name = _extract(self._request("user.getInfo", True), "name") @@ -3051,6 +3051,20 @@ class User(_BaseObject): return _number(_extract(doc, "playcount")) + def get_registered(self): + """Returns the user's registration date.""" + + doc = self._request("user.getInfo", True) + + return _extract(doc, "registered") + + def get_unixtime_registered(self): + """Returns the user's registration date as a UNIX timestamp.""" + + doc = self._request("user.getInfo", True) + + return doc.getElementsByTagName("registered")[0].getAttribute("unixtime") + def get_top_albums(self, period = PERIOD_OVERALL): """Returns the top albums played by a user. * period: The period of time. Possible values: diff --git a/test_pylast.py b/test_pylast.py index 0ff9a9d..f522ed4 100644 --- a/test_pylast.py +++ b/test_pylast.py @@ -20,7 +20,7 @@ class TestSequenceFunctions(unittest.TestCase): API_KEY = "TODO" API_SECRET = "TODO" - self.network = pylast.LastFMNetwork(api_key = API_KEY, api_secret = + self.network = pylast.LastFMNetwork(api_key = API_KEY, api_secret = API_SECRET, username = self.username, password_hash = password_hash) @@ -30,17 +30,17 @@ class TestSequenceFunctions(unittest.TestCase): title = "Test Title" timestamp = self.unix_timestamp() lastfm_user = self.network.get_user(self.username) - +# # Act self.network.scrobble(artist = artist, title = title, timestamp = timestamp) - +# # Assert last_scrobble = lastfm_user.get_recent_tracks(limit = 1)[0] self.assertEqual(str(last_scrobble.track.artist), str(artist)) self.assertEqual(str(last_scrobble.track.title), str(title)) self.assertEqual(str(last_scrobble.timestamp), str(timestamp)) - - +# +# def test_unscrobble(self): # Arrange artist = "Test Artist 2" @@ -49,40 +49,66 @@ class TestSequenceFunctions(unittest.TestCase): library = pylast.Library(user = self.username, network = self.network) self.network.scrobble(artist = artist, title = title, timestamp = timestamp) lastfm_user = self.network.get_user(self.username) - +# # Act library.remove_scrobble(artist = artist, title = title, timestamp = timestamp) - +# # Assert last_scrobble = lastfm_user.get_recent_tracks(limit = 1)[0] self.assertNotEqual(str(last_scrobble.timestamp), str(timestamp)) - - +# +# def test_add_album(self): # Arrange library = pylast.Library(user = self.username, network = self.network) album = self.network.get_album("Test Artist", "Test Album") - +# # Act library.add_album(album) - +# # Assert # Nothing here, just that no exception occurred - - +# +# def test_get_venue(self): # Arrange venue_name = "Last.fm Office" country_name = "United Kingom" - +# # Act venue_search = self.network.search_for_venue(venue_name, country_name) venue = venue_search.get_next_page()[0] - +# # Assert self.assertEqual(str(venue.id), "8778225") + def test_get_user_registration(self): + # Arrange + username = "RJ" + user = self.network.get_user(username) + + # Act + registered = user.get_registered() + + # Assert + # Just check date because of timezones + self.assertIn(u"2002-11-20 ", registered) + + + def test_get_user_unixtime_registration(self): + # Arrange + username = "RJ" + user = self.network.get_user(username) + + # Act + unixtime_registered = user.get_unixtime_registered() + + # Assert + # Just check date because of timezones + self.assertEquals(unixtime_registered, u"1037793040") + + if __name__ == '__main__': unittest.main()