fix images for YTM
authorMarcel van der Veldt <m.vanderveldt@outlook.com>
Tue, 16 Apr 2024 09:59:16 +0000 (11:59 +0200)
committerMarcel van der Veldt <m.vanderveldt@outlook.com>
Tue, 16 Apr 2024 09:59:16 +0000 (11:59 +0200)
music_assistant/server/providers/ytmusic/__init__.py

index 9c341e3ad814e340f7f3f32103363212dd0fd5e2..607a70a8e2581111a2fc08ee43b68361ca19763d 100644 (file)
@@ -872,16 +872,30 @@ class YoutubeMusicProvider(MusicProvider):
         return self._get_item_mapping(MediaType.ARTIST, artist_id, artist_obj.get("name"))
 
     async def _parse_thumbnails(self, thumbnails_obj: dict) -> list[MediaItemImage]:
-        """Parse and sort a list of thumbnails and return the highest quality."""
-        thumb = sorted(thumbnails_obj, key=itemgetter("width"), reverse=True)[0]
-        return [
-            MediaItemImage(
-                type=ImageType.THUMB,
-                path=thumb["url"],
-                provider=self.instance_id,
-                remotely_accessible=True,
+        """Parse and YTM thumbnails to MediaItemImage."""
+        result: list[MediaItemImage] = []
+        processed_images = set()
+        for img in thumbnails_obj:
+            url: str = img["url"]
+            url_base = url.split("=w")[0]
+            if url_base in processed_images:
+                continue
+            width: int = img["width"]
+            if "=w" not in url and width < 500:
+                continue
+            processed_images.add(url_base)
+            # if the size is in the url, we can actually request a higher thumb
+            if "=w" in url:
+                url = f"{url_base}=w600-h600-p"
+            result.append(
+                MediaItemImage(
+                    type=ImageType.THUMB,
+                    path=url,
+                    provider=self.instance_id,
+                    remotely_accessible=True,
+                )
             )
-        ]
+        return result
 
     @classmethod
     async def _parse_stream_format(cls, track_obj: dict) -> dict: