Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow linked-from to get id in image fetching (fixes #171) #245

Merged
merged 2 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion mopidy_spotify/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ def _process_uris(web_client, uri_type, uris):
for item in data.get(uri_type + "s", []):
if not item:
continue
uri = ids_to_uris[item["id"]]

if "linked_from" in item:
uri = ids_to_uris[item["linked_from"]["id"]]
else:
uri = ids_to_uris[item["id"]]

if uri["key"] not in _cache:
if uri_type == "track":
album_key = _parse_uri(item["album"]["uri"])["key"]
Expand Down
51 changes: 42 additions & 9 deletions tests/test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ def test_get_artist_images(web_client_mock, img_provider):


def test_get_album_images(web_client_mock, img_provider):
uris = [
"http://play.spotify.com/album/1utFPuvgBHXzLJdqhCDOkg",
]
uris = ["http://play.spotify.com/album/1utFPuvgBHXzLJdqhCDOkg"]

web_client_mock.get.return_value = {
"albums": [
Expand Down Expand Up @@ -96,9 +94,7 @@ def test_get_album_images(web_client_mock, img_provider):


def test_get_track_images(web_client_mock, img_provider):
uris = [
"spotify:track:41shEpOKyyadtG6lDclooa",
]
uris = ["spotify:track:41shEpOKyyadtG6lDclooa"]

web_client_mock.get.return_value = {
"tracks": [
Expand Down Expand Up @@ -131,6 +127,45 @@ def test_get_track_images(web_client_mock, img_provider):
assert image.width == 640


def test_get_relinked_track_images(web_client_mock, img_provider):
uris = ["spotify:track:4nqN0p0FjfH39G3hxeuKad"]

web_client_mock.get.return_value = {
"tracks": [
{
"id": "39S0DVDKeneEjsq4pV45PT",
"linked_from": {
"id": "4nqN0p0FjfH39G3hxeuKad",
"type": "track",
"uri": "spotify:track:4nqN0p0FjfH39G3hxeuKad",
},
"album": {
"uri": "spotify:album:1utFPuvgBHXzLJdqhCDOkg",
"images": [
{"height": 640, "url": "img://1/a", "width": 640}
],
},
}
]
}

result = img_provider.get_images(uris)

web_client_mock.get.assert_called_once_with(
"tracks", params={"ids": "4nqN0p0FjfH39G3hxeuKad"}
)

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_get_playlist_image(web_client_mock, img_provider):
uris = ["spotify:playlist:41shEpOKyyadtG6lDclooa"]

Expand All @@ -157,9 +192,7 @@ def test_get_playlist_image(web_client_mock, img_provider):


def test_results_are_cached(web_client_mock, img_provider):
uris = [
"spotify:track:41shEpOKyyadtG6lDclooa",
]
uris = ["spotify:track:41shEpOKyyadtG6lDclooa"]

web_client_mock.get.return_value = {
"tracks": [
Expand Down