Skip to content

Commit

Permalink
Ignore internal browse uri images
Browse files Browse the repository at this point in the history
  • Loading branch information
kingosticks committed Jan 2, 2024
1 parent 2d26b54 commit 1f20818
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 10 deletions.
3 changes: 3 additions & 0 deletions mopidy_spotify/browse.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
]


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)


def browse(*, config, session, web_client, uri):
if uri == ROOT_DIR.uri:
return _ROOT_DIR_CONTENTS
Expand Down
24 changes: 17 additions & 7 deletions mopidy_spotify/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import urllib.parse

from mopidy import models
from mopidy_spotify.browse import BROWSE_DIR_URIS

_API_MAX_IDS_PER_REQUEST = 50

Expand Down Expand Up @@ -34,24 +35,33 @@ def get_images(web_client, uris):


def _parse_uri(uri):
if uri in BROWSE_DIR_URIS:
return # These are internal to the extension.
try:
parsed_uri = urllib.parse.urlparse(uri)
uri_type, uri_id = None, None

if parsed_uri.scheme == "spotify":
fragments = parsed_uri.path.split(":")
if len(fragments) < 2:
raise ValueError("Too few fragments")
uri_type, uri_id = parsed_uri.path.split(":")[:2]
elif parsed_uri.scheme in ("http", "https"):
if parsed_uri.netloc in ("open.spotify.com", "play.spotify.com"):
uri_type, uri_id = parsed_uri.path.split("/")[1:3]

supported_types = ("track", "album", "artist", "playlist")
if uri_type and uri_type in supported_types and uri_id:
return {
"uri": uri,
"type": uri_type,
"id": uri_id,
"key": (uri_type, uri_id),
}
if uri_type:
if uri_type not in supported_types:
logger.warning(f"Unsupported image type '{uri_type}' in {repr(uri)}")
return
elif uri_id:
return {
"uri": uri,
"type": uri_type,
"id": uri_id,
"key": (uri_type, uri_id),
}
raise ValueError("Unknown error")
except Exception as e:
logger.exception(f"Could not parse {repr(uri)} as a Spotify URI ({e})")
Expand Down
15 changes: 15 additions & 0 deletions tests/test_browse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import pytest
from mopidy import models

from mopidy_spotify.browse import BROWSE_DIR_URIS


def test_has_a_root_directory(provider):
assert provider.root_directory == models.Ref.directory(
Expand All @@ -24,6 +26,19 @@ def test_browse_root_directory(provider):
)


def test_browse_dir_uris(provider):
assert provider.root_directory.uri in BROWSE_DIR_URIS
count = 1
for u1 in provider.browse(provider.root_directory.uri):
assert u1.uri in BROWSE_DIR_URIS
count = count + 1
for u2 in provider.browse(u1.uri):
assert u2.uri in BROWSE_DIR_URIS
count = count + 1

assert len(BROWSE_DIR_URIS) == count


def test_browse_root_when_offline(web_client_mock, provider):
web_client_mock.logged_in = False

Expand Down
44 changes: 41 additions & 3 deletions tests/test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,49 @@ def test_max_50_ids_per_request(web_client_mock, img_provider):
assert request_ids_2 == "50"


def test_invalid_uri(img_provider, caplog):
@pytest.mark.parametrize(
"uri",
[
"foo:bar",
"spotify:baz",
"spotify:artist",
"spotify:album",
"spotify:user",
"spotify:playlist",
],
)
def test_invalid_uri(img_provider, caplog, uri):
with caplog.at_level(5):
result = img_provider.get_images(["foo:bar"])
result = img_provider.get_images([uri])
assert result == {}
assert f"Could not parse '{uri}' as a Spotify URI" in caplog.text


@pytest.mark.parametrize(
"uri", ["spotify:dog:cat", "spotify:your:fish", "spotify:top:hat"]
)
def test_unsupported_image_type(img_provider, caplog, uri):
with caplog.at_level(5):
result = img_provider.get_images([uri])
assert result == {}
assert f"Unsupported image type '{uri.split(':')[1]}'" in caplog.text


@pytest.mark.parametrize(
"uri",
[
"spotify:directory",
"spotify:your",
"spotify:top",
"spotify:your:albums",
"spotify:top:tracks",
"spotify:playlists:featured",
],
)
def test_browse_internal_uris_no_result(img_provider, caplog, uri):
result = img_provider.get_images([uri])

assert result == {}
assert "Could not parse 'foo:bar' as a Spotify URI" in caplog.text


def test_no_uris_gives_no_results(img_provider):
Expand Down

0 comments on commit 1f20818

Please sign in to comment.