Skip to content

Commit

Permalink
Fix floats api images (#377)
Browse files Browse the repository at this point in the history
* move web api image translator

* Fixed image dimensions as floats
  • Loading branch information
kingosticks authored Feb 29, 2024
1 parent 112d4ab commit 242a321
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 23 deletions.
12 changes: 4 additions & 8 deletions mopidy_spotify/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"]]}

Expand Down Expand Up @@ -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"])
8 changes: 8 additions & 0 deletions mopidy_spotify/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
)
73 changes: 58 additions & 15 deletions tests/test_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

0 comments on commit 242a321

Please sign in to comment.