Skip to content

Commit

Permalink
Merge pull request #245 from adamcik/fix-171
Browse files Browse the repository at this point in the history
Follow linked-from to get id in image fetching (fixes #171)
  • Loading branch information
adamcik committed Dec 19, 2019
2 parents 3e82e54 + 66652c3 commit 33ebddc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
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

0 comments on commit 33ebddc

Please sign in to comment.