Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Erin
mopidy
Commits
71888095
Commit
71888095
authored
Aug 16, 2021
by
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
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
mopidy_funkwhale/client.py
View file @
71888095
...
...
@@ -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
()
...
...
mopidy_funkwhale/library.py
View file @
71888095
...
...
@@ -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
typ
e
,
cover_url
in
payload
[
"album"
][
"
cover
"
]
[
"urls"
].
items
():
for
siz
e
,
cover_url
in
cover
[
"urls"
].
items
():
if
not
cover_url
:
continue
if
typ
e
==
"large_square_crop"
:
if
siz
e
==
"large_square_crop"
:
image
=
models
.
Image
(
uri
=
cover_url
,
width
=
600
,
height
=
600
)
elif
typ
e
==
"medium_square_crop"
:
elif
siz
e
==
"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
(
":"
)
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
...
...
tests/test_client.py
View file @
71888095
...
...
@@ -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"
}
...
...
tests/test_library.py
View file @
71888095
...
...
@@ -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
):
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment