Skip to content

Commit 096bea9

Browse files
authored
Add method to get audiobook chapters (#476)
1 parent b35b29f commit 096bea9

File tree

5 files changed

+5275
-1
lines changed

5 files changed

+5275
-1
lines changed

src/spotifyaio/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,13 @@ class Chapter(DataClassORJSONMixin):
648648
external_urls: dict[str, str]
649649

650650

651+
@dataclass
652+
class AudiobookChapterResponse(DataClassORJSONMixin):
653+
"""Audiobook chapter response model."""
654+
655+
items: list[Chapter]
656+
657+
651658
@dataclass
652659
class Author(DataClassORJSONMixin):
653660
"""Author model."""

src/spotifyaio/spotify.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
ArtistResponse,
2222
ArtistTopTracksResponse,
2323
Audiobook,
24+
AudiobookChapterResponse,
2425
AudiobooksResponse,
2526
AudioFeatures,
2627
BasePlaylist,
2728
BaseUserProfile,
2829
CategoriesResponse,
2930
Category,
3031
CategoryPlaylistResponse,
32+
Chapter,
3133
CurrentPlaying,
3234
Device,
3335
Devices,
@@ -287,7 +289,14 @@ async def get_audiobooks(self, audiobook_ids: list[str]) -> list[Audiobook]:
287289
response = await self._get("v1/audiobooks", params=params)
288290
return AudiobooksResponse.from_json(response).audiobooks
289291

290-
# Get an audiobook's episodes
292+
async def get_audiobook_chapters(self, audiobook_id: str) -> list[Chapter]:
293+
"""Get audiobook chapters."""
294+
identifier = get_identifier(audiobook_id)
295+
params: dict[str, Any] = {"limit": 50}
296+
response = await self._get(
297+
f"v1/audiobooks/{identifier}/chapters", params=params
298+
)
299+
return AudiobookChapterResponse.from_json(response).items
291300

292301
# Get saved audiobooks
293302

0 commit comments

Comments
 (0)