diff --git a/pylistenbrainz/client.py b/pylistenbrainz/client.py index 354d16b..3e74ae3 100644 --- a/pylistenbrainz/client.py +++ b/pylistenbrainz/client.py @@ -405,3 +405,20 @@ def get_user_recommendation_recordings(self, username, artist_type='top', count= return None else: raise + + def get_user_listen_count(self, username): + """ Get total number of listens for user + + :param username: The username of the user whose listens are to be fetched + :type username: str + + :return: Number of listens returned by the Listenbrainz API + :rtype: int + """ + try: + return self._get('/1/user/{}/listen-count'.format(username))['payload']['count'] + except errors.ListenBrainzAPIException as e: + if e.status_code == 204: + return None + else: + raise \ No newline at end of file diff --git a/pylistenbrainz/tests/test_client.py b/pylistenbrainz/tests/test_client.py index c782244..235790d 100644 --- a/pylistenbrainz/tests/test_client.py +++ b/pylistenbrainz/tests/test_client.py @@ -326,3 +326,14 @@ def test_get_user_recommendation_recordings(self, mock_requests_get): }, headers={}, ) + + def test_get_user_listen_count(self): + self.client._get = mock.MagicMock() + + test_response = {"payload":{"count":111487}} + self.client._get.return_value = test_response + returned_count = self.client.get_user_listen_count('iliekcomputers') + self.client._get.assert_called_once_with('/1/user/iliekcomputers/listen-count') + + self.assertEqual(returned_count, test_response['payload']['count']) +