Skip to content

Commit

Permalink
sessions for audiobooks if multiple tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
fmunkes committed Jan 19, 2025
1 parent 4f4c91c commit bc3ed05
Showing 1 changed file with 13 additions and 45 deletions.
58 changes: 13 additions & 45 deletions music_assistant/providers/audiobookshelf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@
CONF_USERNAME = "username"
CONF_PASSWORD = "password"
CONF_VERIFY_SSL = "verify_ssl"
CONF_USE_SESSION_AUDIOBOOK = "use_session_audiobook"
CONF_USE_SESSION_PODCAST = "use_session_podcast"


async def setup(
Expand Down Expand Up @@ -102,24 +100,6 @@ async def get_config_entries(
required=False,
description="The password to authenticate to the remote server.",
),
ConfigEntry(
key=CONF_USE_SESSION_AUDIOBOOK,
type=ConfigEntryType.BOOLEAN,
label="Use session/ HLS stream for audiobooks.",
required=False,
description="Opens a session at abs, enables support for multiple file audiobooks.",
category="advanced",
default_value=True,
),
ConfigEntry(
key=CONF_USE_SESSION_PODCAST,
type=ConfigEntryType.BOOLEAN,
label="Use session/ HLS stream for podcasts.",
required=False,
description="Opens a session at abs for podcasts.",
category="advanced",
default_value=False,
),
ConfigEntry(
key=CONF_VERIFY_SSL,
type=ConfigEntryType.BOOLEAN,
Expand Down Expand Up @@ -442,38 +422,26 @@ async def get_stream_details(
"""Get stream of item."""
# self.logger.debug(f"Streamdetails: {item_id}")
if media_type == MediaType.PODCAST_EPISODE:
if not bool(self.config.get_value(CONF_USE_SESSION_PODCAST)):
return await self._get_stream_details_podcast_episode(item_id)
abs_podcast_id, abs_episode_id = item_id.split(" ")
session = await self._client.get_playback_session_podcast(
device_info=self.device_info,
podcast_id=abs_podcast_id,
episode_id=abs_episode_id,
)
return await self.get_streamdetails_from_playback_session(session)
return await self._get_stream_details_podcast_episode(item_id)
elif media_type == MediaType.AUDIOBOOK:
if not bool(self.config.get_value(CONF_USE_SESSION_AUDIOBOOK)):
return await self._get_stream_details_audiobook(item_id)
session = await self._client.get_playback_session_audiobook(
device_info=self.device_info, audiobook_id=item_id
)
return await self.get_streamdetails_from_playback_session(session)
abs_audiobook = await self._client.get_audiobook(item_id)
tracks = abs_audiobook.media.tracks
if len(tracks) == 0:
raise MediaNotFoundError("Stream not found")
if len(tracks) > 1:
session = await self._client.get_playback_session_audiobook(
device_info=self.device_info, audiobook_id=item_id
)
return await self.get_streamdetails_from_playback_session(session)
return await self._get_stream_details_audiobook(abs_audiobook)
raise MediaNotFoundError("Stream unknown")

async def _get_stream_details_audiobook(self, audiobook_id: str) -> StreamDetails:
async def _get_stream_details_audiobook(self, abs_audiobook: ABSAudioBook) -> StreamDetails:
"""Only single audio file in audiobook."""
abs_audiobook = await self._client.get_audiobook(audiobook_id)
self.logger.debug(
f"Using direct playback for audiobook {abs_audiobook.media.metadata.title}"
)
tracks = abs_audiobook.media.tracks
if len(tracks) == 0:
raise MediaNotFoundError("Stream not found")
if len(tracks) > 1:
self.logger.warning(
"Music Assistant only supports single file based audiobooks. "
"Switch to HLS stream for multiple file audiobooks."
)
token = self._client.token
base_url = str(self.config.get_value(CONF_URL))
media_url = tracks[0].content_url
Expand All @@ -482,7 +450,7 @@ async def _get_stream_details_audiobook(self, audiobook_id: str) -> StreamDetail
# to lift unknown at some point.
return StreamDetails(
provider=self.lookup_key,
item_id=audiobook_id,
item_id=abs_audiobook.id_,
audio_format=AudioFormat(
content_type=ContentType.UNKNOWN,
),
Expand Down

0 comments on commit bc3ed05

Please sign in to comment.