Exemplo n.º 1
0
def create_track(sp_track, client, username=None):
    
    client = client
    client_unique = sp_track['id'] 
    name = sp_track['name']
    uri = sp_track['uri'] 
    artist_name = sp_track['artists'][0]['name']
    code=create_code(name, artist_name)
    new_track = Track(
                    client=client,
                    client_unique=client_unique,
                    name=name,
                    uri=uri,
                    artist_name=artist_name,
                    tb_code=code
                    )
    album_id = sp_track['album']['id']
    
    if album_id is not None:
        # Check if album exists
        try:
            album = Album.objects.get(client_unique=album_id)
            new_track.album = album
        except Album.DoesNotExist:
            if username is not None:

                token = util.prompt_for_user_token(username)
                sp = spotipy.Spotify(auth=token)
                sp_album = sp.album(album_id)
                album = create_album(sp_album, client)
                new_track.album = album
    
    new_track.save()
    for artist in sp_track['artists']:
        artist_id = artist['id']
        if artist_id is not None:
            try:
                artist = Artist.objects.get(client_unique=artist_id)
                new_track.artists.add(artist)
            except Artist.DoesNotExist:
                if username is not None:
                    sp_artist = sp.artist(artist_id)
                    artist = create_artist(sp_artist, client)
                    new_track.artists.add(artist)
        
    new_track.save()
    return new_track
Exemplo n.º 2
0
 def writeModel(self, root, title, album, artist, track, cover, source):
     obj = Track()
     obj.title = title
     obj.album = album
     obj.artist = artist
     obj.track = track
     obj.source = source
     obj.cover = cover
     obj.save()