From 242a3210da9c2f6f42df63df86f3c28eb1ec4c69 Mon Sep 17 00:00:00 2001 From: Nick Steel Date: Thu, 29 Feb 2024 17:05:36 +0000 Subject: [PATCH] Fix floats api images (#377) * move web api image translator * Fixed image dimensions as floats --- mopidy_spotify/images.py | 12 ++---- mopidy_spotify/translator.py | 8 ++++ tests/test_translator.py | 73 ++++++++++++++++++++++++++++-------- 3 files changed, 70 insertions(+), 23 deletions(-) diff --git a/mopidy_spotify/images.py b/mopidy_spotify/images.py index 684302b5..b7cfd8f1 100644 --- a/mopidy_spotify/images.py +++ b/mopidy_spotify/images.py @@ -3,8 +3,8 @@ import operator import urllib.parse -from mopidy import models from mopidy_spotify.browse import BROWSE_DIR_URIS +from mopidy_spotify.translator import web_to_image _API_MAX_IDS_PER_REQUEST = 50 @@ -72,7 +72,7 @@ def _parse_uri(uri): def _process_uri(web_client, uri): data = web_client.get(f"{uri['type']}s/{uri['id']}") _cache[uri["key"]] = tuple( - _translate_image(i) for i in data.get("images") or [] + web_to_image(i) for i in data.get("images") or [] ) return {uri["uri"]: _cache[uri["key"]]} @@ -113,18 +113,14 @@ def _process_uris(web_client, uri_type, uris): album_key = album["key"] if album_key not in _cache: _cache[album_key] = tuple( - _translate_image(i) + web_to_image(i) for i in item["album"].get("images") or [] ) _cache[uri["key"]] = _cache[album_key] else: _cache[uri["key"]] = tuple( - _translate_image(i) for i in item.get("images") or [] + web_to_image(i) for i in item.get("images") or [] ) result[uri["uri"]] = _cache[uri["key"]] return result - - -def _translate_image(i): - return models.Image(uri=i["url"], height=i["height"], width=i["width"]) diff --git a/mopidy_spotify/translator.py b/mopidy_spotify/translator.py index 00c6fdf0..8cfe49a5 100644 --- a/mopidy_spotify/translator.py +++ b/mopidy_spotify/translator.py @@ -264,3 +264,11 @@ def web_to_track(web_track, bitrate=None, album=None): track_no=int_or_none(web_track.get("track_number")), bitrate=bitrate, ) + + +def web_to_image(web_image): + return models.Image( + uri=web_image["url"], + height=int_or_none(web_image.get("height")), + width=int_or_none(web_image.get("width")), + ) diff --git a/tests/test_translator.py b/tests/test_translator.py index db778e1b..1778d342 100644 --- a/tests/test_translator.py +++ b/tests/test_translator.py @@ -684,21 +684,6 @@ def test_ignores_invalid_album(self, web_track_mock): assert track.name == "ABC 123" assert track.album is None - @pytest.mark.parametrize( - "data", - [ - (123), - (123.0), - ("123"), - ("123.0"), - ], - ) - def test_int_or_none_number(self, data): - assert translator.int_or_none(data) == 123 - - def test_int_or_none_none(self): - assert translator.int_or_none(None) is None - def test_ints_might_be_floats(self, web_track_mock): web_track_mock["duration_ms"] = 123.0 web_track_mock["disc_number"] = "456.0" @@ -709,3 +694,61 @@ def test_ints_might_be_floats(self, web_track_mock): assert track.length == 123 assert track.disc_no == 456 assert track.track_no == 99 + + +@pytest.mark.parametrize( + "data", + [ + (123), + (123.0), + ("123"), + ("123.0"), + ], +) +def test_int_or_none_number(data): + assert translator.int_or_none(data) == 123 + + +def test_int_or_none_none(): + assert translator.int_or_none(None) is None + + +def test_web_to_image(): + data = {"height": 640, "url": "img://1/a", "width": 200} + + image = translator.web_to_image(data) + + assert isinstance(image, models.Image) + assert image.uri == "img://1/a" + assert image.height == 640 + assert image.width == 200 + + +def test_web_to_image_no_dimensions(): + data = {"height": 640, "url": "img://1/a"} + + image = translator.web_to_image(data) + + assert isinstance(image, models.Image) + assert image.uri == "img://1/a" + assert image.height == 640 + assert image.width is None + + +@pytest.mark.parametrize( + "height,width", + [ + (600, 400), + (600.0, 400.0), + ("600", "400"), + ("600.0", "400.0"), + ], +) +def test_web_to_image_ints_might_be_floats(height, width): + data = {"height": height, "url": "img://1/a", "width": width} + + image = translator.web_to_image(data) + + assert isinstance(image, models.Image) + assert image.height == 600 + assert image.width == 400