old playlist imports

This commit is contained in:
2024-07-23 00:59:06 +02:00
parent ea3ad0e0c4
commit 1bacafd8b2
7 changed files with 107 additions and 36 deletions
+31 -1
View File
@@ -1,4 +1,4 @@
from .models import Track, Vote
from .models import Track, Vote, Album, Artist
from random import choice, seed
def from_json(cls, json):
@@ -25,3 +25,33 @@ def get_unvoted(user):
return (Track.objects.get(pk=random_track), skipped)
except IndexError:
return (None, False)
def track_from_json(json):
"""Creates the track, album and artist from spotify response."""
# Album artists
album_artists = []
for artist in json["album"]["artists"]:
album_artists.append(from_json(Artist, artist))
del json["album"]["artists"]
# Album
album = from_json(Album, json["album"])
album.image_url = json["album"]["images"][0]["url"]
album.save()
del json["album"]
for artist in album_artists:
album.artists.add(artist)
# Track artists
track_artists = []
for artist in json["artists"]:
track_artists.append(from_json(Artist, artist))
del json["artists"]
# Track
track = from_json(Track, json)
for artist in track_artists:
track.artists.add(artist)
track.album = album
track.save()
return track