Skip to content

Commit

Permalink
Add support for listen-count endpoint (metabrainz#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
deafmute1 authored Dec 11, 2021
1 parent 03f1a2b commit 7952e22
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pylistenbrainz/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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'])

0 comments on commit 7952e22

Please sign in to comment.