mirror of
https://code.eliotberriot.com/funkwhale/funkwhale.git
synced 2025-10-04 20:29:32 +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
|
@ -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