Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for listen-count endpoint #7

Merged
merged 1 commit into from
Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pylistenbrainz/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,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
11 changes: 11 additions & 0 deletions pylistenbrainz/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])