parse contenttype from url
authorMarcel van der Veldt <m.vanderveldt@outlook.com>
Thu, 7 Apr 2022 12:39:23 +0000 (14:39 +0200)
committerMarcel van der Veldt <m.vanderveldt@outlook.com>
Thu, 7 Apr 2022 12:39:23 +0000 (14:39 +0200)
music_assistant/helpers/audio.py
music_assistant/models/media_items.py

index 3da9c27439cf471659fe5c62ba7e20a9b4e90982..a1d4988d70ace120d091ee3057c2bb62687c86d4 100644 (file)
@@ -191,7 +191,7 @@ async def get_stream_details(
             provider="url",
             item_id=queue_item.item_id,
             path=queue_item.uri,
-            content_type=ContentType(queue_item.uri.split(".")[-1]),
+            content_type=ContentType.try_parse(queue_item.uri),
         )
     else:
         # always request the full db track as there might be other qualities available
index 8bd307dec35d481a14ce5e77a250d76356a46e39..a1e93656905e1f711e51baf0137358eb315739ce 100755 (executable)
@@ -259,6 +259,21 @@ class ContentType(Enum):
     PCM_F32LE = "f32le"  # PCM 32-bit floating-point little-endian
     PCM_F64LE = "f64le"  # PCM 64-bit floating-point little-endian
 
+    @classmethod
+    def try_parse(
+        cls: "ContentType", string: str, fallback: str = "mp3"
+    ) -> "ContentType":
+        """Try to parse ContentType from (url)string."""
+        tempstr = string.lower()
+        if "." in tempstr:
+            tempstr = tempstr.split(".")[-1]
+        tempstr = tempstr.split("?")[0]
+        tempstr = tempstr.split("&")[0]
+        try:
+            return cls(tempstr)
+        except ValueError:
+            return cls(fallback)
+
     def is_pcm(self):
         """Return if contentype is PCM."""
         return self.name.startswith("PCM")