mirror of
https://code.eliotberriot.com/funkwhale/funkwhale.git
synced 2025-10-04 20:49:16 +02:00
Blacked the code
This commit is contained in:
parent
b6fc0051fa
commit
62ca3bd736
279 changed files with 8861 additions and 9527 deletions
|
@ -13,50 +13,40 @@ from funkwhale_api.music import models, metadata
|
|||
def import_track_data_from_path(path):
|
||||
data = metadata.Metadata(path)
|
||||
album = None
|
||||
track_mbid = data.get('musicbrainz_recordingid', None)
|
||||
album_mbid = data.get('musicbrainz_albumid', 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]
|
||||
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)
|
||||
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'),
|
||||
},
|
||||
name__iexact=data.get("artist"), defaults={"name": data.get("artist")}
|
||||
)[0]
|
||||
|
||||
release_date = data.get('date', default=None)
|
||||
release_date = data.get("date", default=None)
|
||||
if not album:
|
||||
album = models.Album.objects.get_or_create(
|
||||
title__iexact=data.get('album'),
|
||||
title__iexact=data.get("album"),
|
||||
artist=artist,
|
||||
defaults={
|
||||
'title': data.get('album'),
|
||||
'release_date': release_date,
|
||||
},
|
||||
defaults={"title": data.get("album"), "release_date": release_date},
|
||||
)[0]
|
||||
position = data.get('track_number', default=None)
|
||||
position = data.get("track_number", default=None)
|
||||
track = models.Track.objects.get_or_create(
|
||||
title__iexact=data.get('title'),
|
||||
title__iexact=data.get("title"),
|
||||
album=album,
|
||||
defaults={
|
||||
'title': data.get('title'),
|
||||
'position': position,
|
||||
},
|
||||
defaults={"title": data.get("title"), "position": position},
|
||||
)[0]
|
||||
return track
|
||||
|
||||
|
@ -65,31 +55,27 @@ def import_metadata_with_musicbrainz(path):
|
|||
pass
|
||||
|
||||
|
||||
@celery.app.task(name='audiofile.from_path')
|
||||
@celery.app.task(name="audiofile.from_path")
|
||||
def from_path(path):
|
||||
acoustid_track_id = None
|
||||
try:
|
||||
client = get_acoustid_client()
|
||||
result = client.get_best_match(path)
|
||||
acoustid_track_id = result['id']
|
||||
acoustid_track_id = result["id"]
|
||||
except acoustid.WebServiceError:
|
||||
track = import_track_data_from_path(path)
|
||||
except (TypeError, KeyError):
|
||||
track = import_metadata_without_musicbrainz(path)
|
||||
else:
|
||||
track, created = models.Track.get_or_create_from_api(
|
||||
mbid=result['recordings'][0]['id']
|
||||
mbid=result["recordings"][0]["id"]
|
||||
)
|
||||
|
||||
if track.files.count() > 0:
|
||||
raise ValueError('File already exists for track {}'.format(track.pk))
|
||||
raise ValueError("File already exists for track {}".format(track.pk))
|
||||
|
||||
track_file = models.TrackFile(
|
||||
track=track, acoustid_track_id=acoustid_track_id)
|
||||
track_file.audio_file.save(
|
||||
os.path.basename(path),
|
||||
File(open(path, 'rb'))
|
||||
)
|
||||
track_file = models.TrackFile(track=track, acoustid_track_id=acoustid_track_id)
|
||||
track_file.audio_file.save(os.path.basename(path), File(open(path, "rb")))
|
||||
track_file.save()
|
||||
|
||||
return track_file
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue