Skip to content

Commit 88f6e21

Browse files
committed
Add support for playlist images from webapi
1 parent da26e3f commit 88f6e21

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

mopidy_spotify/images.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def get_images(web_client, uris):
2626
for uri in group:
2727
if uri["key"] in _cache:
2828
result[uri["uri"]] = _cache[uri["key"]]
29+
elif uri_type == "playlist":
30+
result.update(_process_uri(web_client, uri))
2931
else:
3032
batch.append(uri)
3133
if len(batch) >= _API_MAX_IDS_PER_REQUEST:
@@ -45,7 +47,8 @@ def _parse_uri(uri):
4547
if parsed_uri.netloc in ("open.spotify.com", "play.spotify.com"):
4648
uri_type, uri_id = parsed_uri.path.split("/")[1:3]
4749

48-
if uri_type and uri_type in ("track", "album", "artist") and uri_id:
50+
supported_types = ("track", "album", "artist", "playlist")
51+
if uri_type and uri_type in supported_types and uri_id:
4952
return {
5053
"uri": uri,
5154
"type": uri_type,
@@ -56,6 +59,13 @@ def _parse_uri(uri):
5659
raise ValueError(f"Could not parse {repr(uri)} as a Spotify URI")
5760

5861

62+
def _process_uri(web_client, uri):
63+
data = web_client.get(f"{uri['type']}s/{uri['id']}")
64+
print(data)
65+
_cache[uri["key"]] = tuple(_translate_image(i) for i in data["images"])
66+
return {uri["uri"]: _cache[uri["key"]]}
67+
68+
5969
def _process_uris(web_client, uri_type, uris):
6070
result = {}
6171
ids = [u["id"] for u in uris]

tests/test_images.py

+25
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,31 @@ def test_get_track_images(web_client_mock, img_provider):
131131
assert image.width == 640
132132

133133

134+
def test_get_track_images(web_client_mock, img_provider):
135+
uris = ["spotify:playlist:41shEpOKyyadtG6lDclooa"]
136+
137+
web_client_mock.get.return_value = {
138+
"id": "41shEpOKyyadtG6lDclooa",
139+
"images": [{"height": 640, "url": "img://1/a", "width": 640}],
140+
}
141+
142+
result = img_provider.get_images(uris)
143+
144+
web_client_mock.get.assert_called_once_with(
145+
"playlists/41shEpOKyyadtG6lDclooa"
146+
)
147+
148+
assert len(result) == 1
149+
assert sorted(result.keys()) == sorted(uris)
150+
assert len(result[uris[0]]) == 1
151+
152+
image = result[uris[0]][0]
153+
assert isinstance(image, models.Image)
154+
assert image.uri == "img://1/a"
155+
assert image.height == 640
156+
assert image.width == 640
157+
158+
134159
def test_results_are_cached(web_client_mock, img_provider):
135160
uris = [
136161
"spotify:track:41shEpOKyyadtG6lDclooa",

0 commit comments

Comments
 (0)