From 0ec5cc0a00b477dd54c61c2eb9f0591934bb9d13 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Wed, 18 Dec 2019 21:33:46 +0100 Subject: [PATCH] Add support for playlist images from webapi --- mopidy_spotify/images.py | 11 ++++++++++- tests/test_images.py | 25 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/mopidy_spotify/images.py b/mopidy_spotify/images.py index 39bb9b22..9d8b90bf 100644 --- a/mopidy_spotify/images.py +++ b/mopidy_spotify/images.py @@ -26,6 +26,8 @@ def get_images(web_client, uris): for uri in group: if uri["key"] in _cache: result[uri["uri"]] = _cache[uri["key"]] + elif uri_type == "playlist": + result.update(_process_uri(web_client, uri)) else: batch.append(uri) if len(batch) >= _API_MAX_IDS_PER_REQUEST: @@ -45,7 +47,8 @@ def _parse_uri(uri): if parsed_uri.netloc in ("open.spotify.com", "play.spotify.com"): uri_type, uri_id = parsed_uri.path.split("/")[1:3] - if uri_type and uri_type in ("track", "album", "artist") and uri_id: + supported_types = ("track", "album", "artist", "playlist") + if uri_type and uri_type in supported_types and uri_id: return { "uri": uri, "type": uri_type, @@ -56,6 +59,12 @@ def _parse_uri(uri): raise ValueError(f"Could not parse {repr(uri)} as a Spotify URI") +def _process_uri(web_client, uri): + data = web_client.get(f"{uri['type']}s/{uri['id']}") + _cache[uri["key"]] = tuple(_translate_image(i) for i in data["images"]) + return {uri["uri"]: _cache[uri["key"]]} + + def _process_uris(web_client, uri_type, uris): result = {} ids = [u["id"] for u in uris] diff --git a/tests/test_images.py b/tests/test_images.py index 48309576..4d2220ef 100644 --- a/tests/test_images.py +++ b/tests/test_images.py @@ -131,6 +131,31 @@ def test_get_track_images(web_client_mock, img_provider): assert image.width == 640 +def test_get_playlist_image(web_client_mock, img_provider): + uris = ["spotify:playlist:41shEpOKyyadtG6lDclooa"] + + web_client_mock.get.return_value = { + "id": "41shEpOKyyadtG6lDclooa", + "images": [{"height": 640, "url": "img://1/a", "width": 640}], + } + + result = img_provider.get_images(uris) + + web_client_mock.get.assert_called_once_with( + "playlists/41shEpOKyyadtG6lDclooa" + ) + + assert len(result) == 1 + assert sorted(result.keys()) == sorted(uris) + assert len(result[uris[0]]) == 1 + + image = result[uris[0]][0] + assert isinstance(image, models.Image) + assert image.uri == "img://1/a" + assert image.height == 640 + assert image.width == 640 + + def test_results_are_cached(web_client_mock, img_provider): uris = [ "spotify:track:41shEpOKyyadtG6lDclooa",