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): ...@@ -126,6 +126,16 @@ class APIClient(object):
response.raise_for_status() response.raise_for_status()
return response.json() 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): def list_tracks(self, filters):
response = self.session.get("tracks/", params=filters) response = self.session.get("tracks/", params=filters)
response.raise_for_status() response.raise_for_status()
......
...@@ -307,27 +307,47 @@ class FunkwhaleLibraryProvider(backend.LibraryProvider): ...@@ -307,27 +307,47 @@ class FunkwhaleLibraryProvider(backend.LibraryProvider):
result = {} result = {}
for uri in uris: for uri in uris:
track_id = uri.split(":")[-1] try:
cache_key = "funkwhale:images:%s" % track_id 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) from_cache = self.cache.get(cache_key)
if from_cache: if from_cache:
result[uri] = from_cache result[uri] = from_cache
continue continue
payload = self.backend.client.get_track(track_id) cover = None
if not payload["album"]["cover"]: 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 continue
result[uri] = [] result[uri] = []
for type, cover_url in payload["album"]["cover"]["urls"].items(): for size, cover_url in cover["urls"].items():
if not cover_url: if not cover_url:
continue continue
if type == "large_square_crop": if size == "large_square_crop":
image = models.Image(uri=cover_url, width=600, height=600) 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) image = models.Image(uri=cover_url, width=200, height=200)
else: else:
image = models.Image(uri=cover_url) image = models.Image(uri=cover_url)
...@@ -388,8 +408,15 @@ class FunkwhaleLibraryProvider(backend.LibraryProvider): ...@@ -388,8 +408,15 @@ class FunkwhaleLibraryProvider(backend.LibraryProvider):
def parse_uri(uri): def parse_uri(uri):
uri = uri.replace("funkwhale:", "", 1) uri = uri.replace("funkwhale:", "", 1)
parts = uri.split(":") parts = uri.split(":")
type = parts[0].rstrip("s")
id = int(parts[1]) # 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])
return type, id return type, id
......
...@@ -17,6 +17,20 @@ def test_client_get_track(client, requests_mock): ...@@ -17,6 +17,20 @@ def test_client_get_track(client, requests_mock):
assert result == {"hello": "world"} 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): def test_client_list_tracks(client, requests_mock):
requests_mock.get( requests_mock.get(
client.session.url_base + "tracks/?artist=12", json={"hello": "world"} client.session.url_base + "tracks/?artist=12", json={"hello": "world"}
......
...@@ -90,6 +90,8 @@ def test_convert_track_to_model(): ...@@ -90,6 +90,8 @@ def test_convert_track_to_model():
("funkwhale:albums:42", ("album", 42)), ("funkwhale:albums:42", ("album", 42)),
("funkwhale:tracks:42", ("track", 42)), ("funkwhale:tracks:42", ("track", 42)),
("funkwhale:artists:42", ("artist", 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): 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