Verified Commit 93a52134 authored by Eliot Berriot's avatar Eliot Berriot 💬

Fixed bitrate / duration error due to API change in Funkwhale

parent 2924bcd9
Pipeline #3093 passed with stage
in 1 minute
......@@ -303,6 +303,10 @@ def convert_to_album(payload, uri_prefix="funkwhale:albums"):
def convert_to_track(payload, uri_prefix="funkwhale:tracks"):
artist = convert_to_artist(payload["artist"])
album = convert_to_album(payload["album"])
try:
upload = payload["uploads"][0]
except (KeyError, IndexError):
upload = {}
return models.Track(
uri=uri_prefix + ":%s" % payload["id"],
name=payload["title"],
......@@ -310,8 +314,8 @@ def convert_to_track(payload, uri_prefix="funkwhale:tracks"):
artists=[artist],
album=album,
date=payload["album"]["release_date"],
bitrate=(payload["bitrate"] or 0) / 1000,
length=(payload["duration"] or 0) * 1000,
bitrate=(upload.get("bitrate") or 0) / 1000,
length=(upload.get("duration") or 0) * 1000,
track_no=payload["position"],
)
......
......@@ -34,6 +34,14 @@ class AlbumJSONFactory(factory.Factory):
model = dict
class UploadJSONFactory(factory.Factory):
uuid = factory.Faker("uuid4")
bitrate = factory.Iterator([i * 1000 for i in (128, 256, 360)])
class Meta:
model = dict
class TrackJSONFactory(factory.Factory):
id = factory.Sequence(int)
mbid = factory.Faker("uuid4")
......@@ -41,9 +49,9 @@ class TrackJSONFactory(factory.Factory):
position = factory.Faker("pyint")
duration = factory.Faker("pyint")
creation_date = factory.Faker("date")
bitrate = factory.Iterator([i * 1000 for i in (128, 256, 360)])
artist = factory.SubFactory(ArtistJSONFactory)
album = factory.SubFactory(AlbumJSONFactory)
uploads = factory.LazyAttribute(lambda o: [UploadJSONFactory()])
class Meta:
model = dict
......
......@@ -47,15 +47,13 @@ def test_convert_album_to_model():
assert result.images == frozenset([payload["cover"]["original"]])
def test_convert_album_to_model():
def test_convert_track_to_model():
payload = {
"id": 2,
"title": "Test track",
"mbid": str(uuid.uuid4()),
"creation_date": "2017-01-01",
"position": 12,
"bitrate": 128000,
"duration": 120,
"artist": {"id": 43, "mbid": str(uuid.uuid4()), "name": "Test artist 2"},
"album": {
"id": 3,
......@@ -68,6 +66,7 @@ def test_convert_album_to_model():
"original": "/media/albums/covers/2018/10/03/b4e94b07e-da27-4df4-ae2a-d924a9448544.jpg"
},
},
"uploads": [{"bitrate": 128000, "duration": 120}],
}
result = mopidy_funkwhale.library.convert_to_track(payload)
......@@ -77,8 +76,8 @@ def test_convert_album_to_model():
assert result.uri == "funkwhale:tracks:%s" % (payload["id"],)
assert result.name == payload["title"]
assert result.date == payload["album"]["release_date"]
assert result.length == payload["duration"] * 1000
assert result.bitrate == payload["bitrate"] / 1000
assert result.length == payload["uploads"][0]["duration"] * 1000
assert result.bitrate == payload["uploads"][0]["bitrate"] / 1000
assert result.album == mopidy_funkwhale.library.convert_to_album(payload["album"])
assert result.artists == frozenset(
......
Markdown is supported
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