Skip to content

Commit a1486c6

Browse files
authored
Merge pull request #46 from droans/improve-image-search
Check all possible image attributes
2 parents 9161008 + 00e8de0 commit a1486c6

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

custom_components/mass_queue/actions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
REMOVE_QUEUE_ITEM_SERVICE_SCHEMA,
5858
SEND_COMMAND_SERVICE_SCHEMA,
5959
)
60+
from .util import find_image
6061

6162
if TYPE_CHECKING:
6263
from music_assistant_client import MusicAssistantClient
@@ -160,8 +161,7 @@ def _format_queue_item(self, queue_item: dict) -> dict:
160161
media_album = media.get("album")
161162
media_album_name = "" if media_album is None else media_album.get("name", "")
162163
media_content_id = media["uri"]
163-
img = queue_item.get("image")
164-
media_image = "" if img is None else img.get("path", "")
164+
media_image = find_image(queue_item)
165165
favorite = media["favorite"]
166166

167167
artists = media["artists"]

custom_components/mass_queue/utils.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,68 @@ def get_queue_id_from_player_data(player_data):
3232
if current_media is None:
3333
return None
3434
return current_media.get("queue_id")
35+
36+
37+
def return_image_or_none(img_data: dict):
38+
"""Returns None if image is not present or not remotely accessible."""
39+
if type(img_data) is dict:
40+
img = img_data.get("path")
41+
remote = img_data.get("remotely_accessible")
42+
if remote:
43+
return img
44+
return None
45+
46+
47+
def search_image_list(images: list):
48+
"""Checks through a list of image data and attempts to find an image."""
49+
result = None
50+
for item in images:
51+
image = return_image_or_none(item)
52+
if image is not None:
53+
result = image
54+
break
55+
return result
56+
57+
58+
def find_image_from_image(data: dict):
59+
"""Attempts to find the image via the image key."""
60+
img_data = data.get("image")
61+
return return_image_or_none(img_data)
62+
63+
64+
def find_image_from_metadata(data: dict):
65+
"""Attempts to find the image via the metadata key."""
66+
media_item = data.get("media_item", {})
67+
metadata = media_item.get("metadata", {})
68+
img_data = metadata.get("images")
69+
if img_data is None:
70+
return None
71+
return search_image_list(img_data)
72+
73+
74+
def find_image_from_album(data: dict):
75+
"""Attempts to find the image via the album key."""
76+
album = data.get("album", {})
77+
metadata = album.get("metadata", {})
78+
img_data = metadata.get("images")
79+
if img_data is None:
80+
return None
81+
return search_image_list(img_data)
82+
83+
84+
def find_image_from_artists(data: dict):
85+
"""Attempts to find the image via the artists key."""
86+
artist = data.get("artist", {})
87+
img_data = artist.get("image")
88+
if img_data is list:
89+
return search_image_list(img_data)
90+
return return_image_or_none(img_data)
91+
92+
93+
def find_image(data: dict):
94+
"""Returns None if image is not present or not remotely accessible."""
95+
from_image = find_image_from_image(data)
96+
from_metadata = find_image_from_metadata(data)
97+
from_album = find_image_from_album(data)
98+
from_artists = find_image_from_artists(data)
99+
return from_image or from_metadata or from_album or from_artists

0 commit comments

Comments
 (0)