feat: implement user audiobook endpoints

This commit is contained in:
soumitradev 2025-08-15 21:16:08 +00:00
parent a91d9feb51
commit 0835b60486
2 changed files with 49 additions and 0 deletions

View File

@ -1599,6 +1599,37 @@ class Spotify:
before=before,
)
def current_user_saved_audiobooks(self, limit=20, offset=0):
""" Get the current user's saved audiobooks
Parameters:
- limit - the number of audibooks to return
- offset - the index of the first audibook to return
"""
return self._get("me/audiobooks", limit=limit, offset=offset)
def current_user_saved_audiobooks_add(self, audiobooks=None):
""" Add one or more audiobooks to the current user's library.
Parameters:
- audiobooks - a list of audiobook URIs, URLs or IDs
"""
elist = []
if audiobooks is not None:
elist = [self._get_id("audiobook", e) for e in audiobooks]
return self._put("me/audiobooks/?ids=" + ",".join(elist))
def current_user_saved_audiobooks_delete(self, audiobooks=None):
""" Remove one or more audiobooks from the current user's library.
Parameters:
- audiobooks - a list of audiobook URIs, URLs or IDs
"""
elist = []
if audiobooks is not None:
elist = [self._get_id("audiobook", e) for e in audiobooks]
return self._delete("me/audiobooks/?ids=" + ",".join(elist))
def user_follow_artists(self, ids=[]):
""" Follow one or more artists
Parameters:

View File

@ -221,6 +221,11 @@ class SpotipyLibraryApiTests(unittest.TestCase):
"spotify:episode:3OEdPEYB69pfXoBrhvQYeC",
"spotify:episode:5LEFdZ9pYh99wSz7Go2D0g"
]
cls.audiobook_ids = [
"spotify:audiobook:7iHfbu1YPACw6oZPAFJtqe",
"spotify:audiobook:1HGw3J3NxZO1TP1BTtVhpZ",
"spotify:audiobook:7iHfbu1YPACw6oZPAFJtqe"
]
cls.username = os.getenv(CCEV['client_username'])
scope = (
@ -248,6 +253,19 @@ class SpotipyLibraryApiTests(unittest.TestCase):
tracks = self.spotify.current_user_saved_tracks()
self.assertGreaterEqual(len(tracks['items']), 0)
def test_current_user_saved_audiobooks(self):
audiobooks = self.spotify.current_user_saved_audiobooks()
total = audiobooks['total']
self.spotify.current_user_saved_audiobooks_add(self.audiobook_ids)
new_audiobooks = self.spotify.current_user_saved_audiobooks()
new_total = new_audiobooks['total']
self.assertEqual(new_total - total, len(self.audiobook_ids))
self.spotify.current_user_saved_audiobooks_delete(self.audiobook_ids)
new_audiobooks = self.spotify.current_user_saved_audiobooks()
new_total = new_audiobooks['total']
def test_current_user_save_tracks(self):
tracks = self.spotify.current_user_saved_tracks()
total = tracks['total']