Commit 71888095 authored by Erin's avatar Erin
Browse files

Support artist and album images in library.get_images

parent 63cd47d3
Pipeline #15797 passed with stage
in 1 minute and 8 seconds
......@@ -126,6 +126,16 @@ class APIClient(object):
response.raise_for_status()
return response.json()
def get_artist(self, id):
response = self.session.get("artists/{}/".format(id))
response.raise_for_status()
return response.json()
def get_album(self, id):
response = self.session.get("albums/{}/".format(id))
response.raise_for_status()
return response.json()
def list_tracks(self, filters):
response = self.session.get("tracks/", params=filters)
response.raise_for_status()
......
......@@ -307,27 +307,47 @@ class FunkwhaleLibraryProvider(backend.LibraryProvider):
result = {}
for uri in uris:
track_id = uri.split(":")[-1]
cache_key = "funkwhale:images:%s" % track_id
try:
type, id = parse_uri(uri)
except ValueError as err:
logger.info(f"Image lookup failed: {err}")
continue
if not id:
logger.info(f"Image lookup failed: invalid uri '{uri}', id expected")
continue
cache_key = f"funkwhale:images:{type}:{id}"
from_cache = self.cache.get(cache_key)
if from_cache:
result[uri] = from_cache
continue
payload = self.backend.client.get_track(track_id)
if not payload["album"]["cover"]:
cover = None
if type == "album":
payload = self.backend.client.get_album(id)
cover = payload["cover"]
elif type == "artist":
payload = self.backend.client.get_artist(id)
cover = payload["albums"][0]["cover"] if payload["albums"] else None;
elif type == "track":
payload = self.backend.client.get_track(id)
cover = payload["album"]["cover"]
if not cover:
logger.debug(f"no image for {uri}")
continue
result[uri] = []
for type, cover_url in payload["album"]["cover"]["urls"].items():
for size, cover_url in cover["urls"].items():
if not cover_url:
continue
if type == "large_square_crop":
if size == "large_square_crop":
image = models.Image(uri=cover_url, width=600, height=600)
elif type == "medium_square_crop":
elif size == "medium_square_crop":
image = models.Image(uri=cover_url, width=200, height=200)
else:
image = models.Image(uri=cover_url)
......@@ -388,8 +408,15 @@ class FunkwhaleLibraryProvider(backend.LibraryProvider):
def parse_uri(uri):
uri = uri.replace("funkwhale:", "", 1)
parts = uri.split(":")
# NOTE: some browse directories refer to valid resources e.g.
# funkwhale:directory:albums:by-name:123
if parts[0] == "directory":
type = parts[1].rstrip("s")
else:
type = parts[0].rstrip("s")
id = int(parts[1])
id = int(parts[-1])
return type, id
......
......@@ -17,6 +17,20 @@ def test_client_get_track(client, requests_mock):
assert result == {"hello": "world"}
def test_client_get_artist(client, requests_mock):
requests_mock.get(client.session.url_base + "artists/12/", json={"hello": "world"})
result = client.get_artist(12)
assert result == {"hello": "world"}
def test_client_get_album(client, requests_mock):
requests_mock.get(client.session.url_base + "albums/12/", json={"hello": "world"})
result = client.get_album(12)
assert result == {"hello": "world"}
def test_client_list_tracks(client, requests_mock):
requests_mock.get(
client.session.url_base + "tracks/?artist=12", json={"hello": "world"}
......
......@@ -90,6 +90,8 @@ def test_convert_track_to_model():
("funkwhale:albums:42", ("album", 42)),
("funkwhale:tracks:42", ("track", 42)),
("funkwhale:artists:42", ("artist", 42)),
("funkwhale:directory:albums:by-name:42", ("album", 42)),
("funkwhale:directory:artists:by-name:42", ("artist", 42)),
],
)
def test_parse_uri(uri, expected):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment