Use image proxy in volumio media browser (#43819)

* Use image proxy in volumio media browser

* Add thumbnail cache

* Clear thumbnail cache on browse

* Use built-in hash instead of hashlib
This commit is contained in:
On Freund 2021-01-25 02:16:10 +02:00 committed by GitHub
parent 616328c7c4
commit 47e34bb129
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 12 deletions

View File

@ -88,7 +88,7 @@ def _item_to_media_class(item, parent_item=None):
return MEDIA_CLASS_DIRECTORY
def _list_payload(media_library, item, children=None):
def _list_payload(item, children=None):
return BrowseMedia(
title=item["name"],
media_class=MEDIA_CLASS_DIRECTORY,
@ -100,11 +100,13 @@ def _list_payload(media_library, item, children=None):
)
def _raw_item_payload(media_library, item, parent_item=None, title=None, info=None):
def _raw_item_payload(entity, item, parent_item=None, title=None, info=None):
if "type" in item:
thumbnail = item.get("albumart")
if thumbnail:
thumbnail = media_library.canonic_url(thumbnail)
item_hash = str(hash(thumbnail))
entity.thumbnail_cache.setdefault(item_hash, thumbnail)
thumbnail = entity.get_browse_image_url(MEDIA_TYPE_MUSIC, item_hash)
else:
# don't use the built-in volumio white-on-white icons
thumbnail = None
@ -121,16 +123,14 @@ def _raw_item_payload(media_library, item, parent_item=None, title=None, info=No
}
def _item_payload(media_library, item, parent_item):
return BrowseMedia(
**_raw_item_payload(media_library, item, parent_item=parent_item)
)
def _item_payload(entity, item, parent_item):
return BrowseMedia(**_raw_item_payload(entity, item, parent_item=parent_item))
async def browse_top_level(media_library):
"""Browse the top-level of a Volumio media hierarchy."""
navigation = await media_library.browse()
children = [_list_payload(media_library, item) for item in navigation["lists"]]
children = [_list_payload(item) for item in navigation["lists"]]
return BrowseMedia(
media_class=MEDIA_CLASS_DIRECTORY,
media_content_id="library",
@ -142,7 +142,7 @@ async def browse_top_level(media_library):
)
async def browse_node(media_library, media_content_type, media_content_id):
async def browse_node(entity, media_library, media_content_type, media_content_id):
"""Browse a node of a Volumio media hierarchy."""
json_item = json.loads(media_content_id)
navigation = await media_library.browse(json_item["uri"])
@ -152,7 +152,7 @@ async def browse_node(media_library, media_content_type, media_content_id):
# we only use the first list since the second one could include all tracks
first_list = navigation["lists"][0]
children = [
_item_payload(media_library, item, parent_item=json_item)
_item_payload(entity, item, parent_item=json_item)
for item in first_list["items"]
]
info = navigation.get("info")
@ -163,5 +163,5 @@ async def browse_node(media_library, media_content_type, media_content_id):
else:
title = "Media Library"
payload = _raw_item_payload(media_library, json_item, title=title, info=info)
payload = _raw_item_payload(entity, json_item, title=title, info=info)
return BrowseMedia(**payload, children=children)

View File

@ -83,6 +83,7 @@ class Volumio(MediaPlayerEntity):
self._state = {}
self._playlists = []
self._currentplaylist = None
self.thumbnail_cache = {}
async def async_update(self):
"""Update state."""
@ -257,7 +258,18 @@ class Volumio(MediaPlayerEntity):
async def async_browse_media(self, media_content_type=None, media_content_id=None):
"""Implement the websocket media browsing helper."""
self.thumbnail_cache = {}
if media_content_type in [None, "library"]:
return await browse_top_level(self._volumio)
return await browse_node(self._volumio, media_content_type, media_content_id)
return await browse_node(
self, self._volumio, media_content_type, media_content_id
)
async def async_get_browse_image(
self, media_content_type, media_content_id, media_image_id=None
):
"""Get album art from Volumio."""
cached_url = self.thumbnail_cache.get(media_content_id)
image_url = self._volumio.canonic_url(cached_url)
return await self._async_fetch_image(image_url)