mirror of
https://code.eliotberriot.com/funkwhale/funkwhale.git
synced 2025-10-05 08:39:24 +02:00
Fix #288: Huge performance boost during CLI import that queries MusicBrainz
This commit is contained in:
parent
9d9676aa17
commit
bbae4e323b
5 changed files with 171 additions and 25 deletions
|
@ -437,7 +437,40 @@ class Track(APIModelMixin):
|
|||
title__iexact=title,
|
||||
defaults=kwargs)
|
||||
|
||||
@classmethod
|
||||
def get_or_create_from_release(cls, release_mbid, mbid):
|
||||
release_mbid = str(release_mbid)
|
||||
mbid = str(mbid)
|
||||
try:
|
||||
return cls.objects.get(mbid=mbid), False
|
||||
except cls.DoesNotExist:
|
||||
pass
|
||||
|
||||
album = Album.get_or_create_from_api(release_mbid)[0]
|
||||
data = musicbrainz.client.api.releases.get(
|
||||
str(album.mbid), includes=Album.api_includes)
|
||||
tracks = [
|
||||
t
|
||||
for m in data['release']['medium-list']
|
||||
for t in m['track-list']
|
||||
]
|
||||
track_data = None
|
||||
for track in tracks:
|
||||
if track['recording']['id'] == mbid:
|
||||
track_data = track
|
||||
break
|
||||
if not track_data:
|
||||
raise ValueError('No track found matching this ID')
|
||||
|
||||
return cls.objects.update_or_create(
|
||||
mbid=mbid,
|
||||
defaults={
|
||||
'position': int(track['position']),
|
||||
'title': track['recording']['title'],
|
||||
'album': album,
|
||||
'artist': album.artist,
|
||||
}
|
||||
)
|
||||
class TrackFile(models.Model):
|
||||
uuid = models.UUIDField(
|
||||
unique=True, db_index=True, default=uuid.uuid4)
|
||||
|
|
|
@ -12,25 +12,43 @@ from funkwhale_api.music import models, metadata
|
|||
@transaction.atomic
|
||||
def import_track_data_from_path(path):
|
||||
data = metadata.Metadata(path)
|
||||
artist = models.Artist.objects.get_or_create(
|
||||
name__iexact=data.get('artist'),
|
||||
defaults={
|
||||
'name': data.get('artist'),
|
||||
'mbid': data.get('musicbrainz_artistid', None),
|
||||
},
|
||||
)[0]
|
||||
album = None
|
||||
track_mbid = data.get('musicbrainz_recordingid', None)
|
||||
album_mbid = data.get('musicbrainz_albumid', None)
|
||||
|
||||
if album_mbid and track_mbid:
|
||||
# to gain performance and avoid additional mb lookups,
|
||||
# we import from the release data, which is already cached
|
||||
return models.Track.get_or_create_from_release(
|
||||
album_mbid, track_mbid)[0]
|
||||
elif track_mbid:
|
||||
return models.Track.get_or_create_from_api(track_mbid)[0]
|
||||
elif album_mbid:
|
||||
album = models.Album.get_or_create_from_api(album_mbid)[0]
|
||||
|
||||
artist = album.artist if album else None
|
||||
artist_mbid = data.get('musicbrainz_artistid', None)
|
||||
if not artist:
|
||||
if artist_mbid:
|
||||
artist = models.Artist.get_or_create_from_api(artist_mbid)[0]
|
||||
else:
|
||||
artist = models.Artist.objects.get_or_create(
|
||||
name__iexact=data.get('artist'),
|
||||
defaults={
|
||||
'name': data.get('artist'),
|
||||
},
|
||||
)[0]
|
||||
|
||||
release_date = data.get('date', default=None)
|
||||
album = models.Album.objects.get_or_create(
|
||||
title__iexact=data.get('album'),
|
||||
artist=artist,
|
||||
defaults={
|
||||
'title': data.get('album'),
|
||||
'release_date': release_date,
|
||||
'mbid': data.get('musicbrainz_albumid', None),
|
||||
},
|
||||
)[0]
|
||||
|
||||
if not album:
|
||||
album = models.Album.objects.get_or_create(
|
||||
title__iexact=data.get('album'),
|
||||
artist=artist,
|
||||
defaults={
|
||||
'title': data.get('album'),
|
||||
'release_date': release_date,
|
||||
},
|
||||
)[0]
|
||||
position = data.get('track_number', default=None)
|
||||
track = models.Track.objects.get_or_create(
|
||||
title__iexact=data.get('title'),
|
||||
|
@ -38,7 +56,6 @@ def import_track_data_from_path(path):
|
|||
defaults={
|
||||
'title': data.get('title'),
|
||||
'position': position,
|
||||
'mbid': data.get('musicbrainz_recordingid', None),
|
||||
},
|
||||
)[0]
|
||||
return track
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue