Skip to content

Commit 1f20818

Browse files
committed
Ignore internal browse uri images
1 parent 2d26b54 commit 1f20818

File tree

4 files changed

+76
-10
lines changed

4 files changed

+76
-10
lines changed

mopidy_spotify/browse.py

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
]
3636

3737

38+
BROWSE_DIR_URIS = set(u.uri for u in [ROOT_DIR] + _ROOT_DIR_CONTENTS + _TOP_LIST_DIR_CONTENTS + _YOUR_MUSIC_DIR_CONTENTS + _PLAYLISTS_DIR_CONTENTS)
39+
40+
3841
def browse(*, config, session, web_client, uri):
3942
if uri == ROOT_DIR.uri:
4043
return _ROOT_DIR_CONTENTS

mopidy_spotify/images.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import urllib.parse
55

66
from mopidy import models
7+
from mopidy_spotify.browse import BROWSE_DIR_URIS
78

89
_API_MAX_IDS_PER_REQUEST = 50
910

@@ -34,24 +35,33 @@ def get_images(web_client, uris):
3435

3536

3637
def _parse_uri(uri):
38+
if uri in BROWSE_DIR_URIS:
39+
return # These are internal to the extension.
3740
try:
3841
parsed_uri = urllib.parse.urlparse(uri)
3942
uri_type, uri_id = None, None
4043

4144
if parsed_uri.scheme == "spotify":
45+
fragments = parsed_uri.path.split(":")
46+
if len(fragments) < 2:
47+
raise ValueError("Too few fragments")
4248
uri_type, uri_id = parsed_uri.path.split(":")[:2]
4349
elif parsed_uri.scheme in ("http", "https"):
4450
if parsed_uri.netloc in ("open.spotify.com", "play.spotify.com"):
4551
uri_type, uri_id = parsed_uri.path.split("/")[1:3]
4652

4753
supported_types = ("track", "album", "artist", "playlist")
48-
if uri_type and uri_type in supported_types and uri_id:
49-
return {
50-
"uri": uri,
51-
"type": uri_type,
52-
"id": uri_id,
53-
"key": (uri_type, uri_id),
54-
}
54+
if uri_type:
55+
if uri_type not in supported_types:
56+
logger.warning(f"Unsupported image type '{uri_type}' in {repr(uri)}")
57+
return
58+
elif uri_id:
59+
return {
60+
"uri": uri,
61+
"type": uri_type,
62+
"id": uri_id,
63+
"key": (uri_type, uri_id),
64+
}
5565
raise ValueError("Unknown error")
5666
except Exception as e:
5767
logger.exception(f"Could not parse {repr(uri)} as a Spotify URI ({e})")

tests/test_browse.py

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import pytest
44
from mopidy import models
55

6+
from mopidy_spotify.browse import BROWSE_DIR_URIS
7+
68

79
def test_has_a_root_directory(provider):
810
assert provider.root_directory == models.Ref.directory(
@@ -24,6 +26,19 @@ def test_browse_root_directory(provider):
2426
)
2527

2628

29+
def test_browse_dir_uris(provider):
30+
assert provider.root_directory.uri in BROWSE_DIR_URIS
31+
count = 1
32+
for u1 in provider.browse(provider.root_directory.uri):
33+
assert u1.uri in BROWSE_DIR_URIS
34+
count = count + 1
35+
for u2 in provider.browse(u1.uri):
36+
assert u2.uri in BROWSE_DIR_URIS
37+
count = count + 1
38+
39+
assert len(BROWSE_DIR_URIS) == count
40+
41+
2742
def test_browse_root_when_offline(web_client_mock, provider):
2843
web_client_mock.logged_in = False
2944

tests/test_images.py

+41-3
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,49 @@ def test_max_50_ids_per_request(web_client_mock, img_provider):
252252
assert request_ids_2 == "50"
253253

254254

255-
def test_invalid_uri(img_provider, caplog):
255+
@pytest.mark.parametrize(
256+
"uri",
257+
[
258+
"foo:bar",
259+
"spotify:baz",
260+
"spotify:artist",
261+
"spotify:album",
262+
"spotify:user",
263+
"spotify:playlist",
264+
],
265+
)
266+
def test_invalid_uri(img_provider, caplog, uri):
256267
with caplog.at_level(5):
257-
result = img_provider.get_images(["foo:bar"])
268+
result = img_provider.get_images([uri])
269+
assert result == {}
270+
assert f"Could not parse '{uri}' as a Spotify URI" in caplog.text
271+
272+
273+
@pytest.mark.parametrize(
274+
"uri", ["spotify:dog:cat", "spotify:your:fish", "spotify:top:hat"]
275+
)
276+
def test_unsupported_image_type(img_provider, caplog, uri):
277+
with caplog.at_level(5):
278+
result = img_provider.get_images([uri])
279+
assert result == {}
280+
assert f"Unsupported image type '{uri.split(':')[1]}'" in caplog.text
281+
282+
283+
@pytest.mark.parametrize(
284+
"uri",
285+
[
286+
"spotify:directory",
287+
"spotify:your",
288+
"spotify:top",
289+
"spotify:your:albums",
290+
"spotify:top:tracks",
291+
"spotify:playlists:featured",
292+
],
293+
)
294+
def test_browse_internal_uris_no_result(img_provider, caplog, uri):
295+
result = img_provider.get_images([uri])
296+
258297
assert result == {}
259-
assert "Could not parse 'foo:bar' as a Spotify URI" in caplog.text
260298

261299

262300
def test_no_uris_gives_no_results(img_provider):

0 commit comments

Comments
 (0)