Merge branch 'library-cover-images' into 'master'

Add `library.get_images()` support for cover art

See merge request funkwhale/mopidy!11
main
Agate 2020-09-25 09:10:30 +02:00
commit 21bdb99dd5
1 changed files with 36 additions and 2 deletions

View File

@ -255,6 +255,41 @@ class FunkwhaleLibraryProvider(backend.LibraryProvider):
]
return libraries, True
def get_images(self, uris):
logger.debug("Handling get images: %s", uris)
result = {}
for uri in uris:
track_id = uri.split(":")[-1]
cache_key = "funkwhale:images:%s" % track_id
from_cache = self.cache.get(cache_key)
if from_cache:
result[uri] = from_cache
continue
payload = self.backend.client.get_track(track_id)
if not payload["album"]["cover"]:
continue
result[uri] = []
for type, cover_url in payload["album"]["cover"]["urls"].items():
if not cover_url:
continue
if type == "large_square_crop":
image = models.Image(uri=cover_url, width=600, height=600)
elif type == "medium_square_crop":
image = models.Image(uri=cover_url, width=200, height=200)
else:
image = models.Image(uri=cover_url)
result[uri].append(image)
self.cache.set(cache_key, result[uri])
return result
def search(self, query=None, uris=None, exact=False):
# TODO Support exact search
@ -333,8 +368,6 @@ def convert_to_artist(payload, uri_prefix="funkwhale:artists"):
@cast_to_ref
def convert_to_album(payload, uri_prefix="funkwhale:albums"):
artist = convert_to_artist(payload["artist"])
image = payload["cover"]["original"] if payload["cover"] else None
return models.Album(
uri=uri_prefix + ":%s" % payload["id"],
name=payload["title"],
@ -349,6 +382,7 @@ def convert_to_album(payload, uri_prefix="funkwhale:albums"):
def convert_to_track(payload, uri_prefix="funkwhale:tracks"):
artist = convert_to_artist(payload["artist"])
album = convert_to_album(payload["album"])
try:
upload = payload["uploads"][0]
except (KeyError, IndexError):