Load a Spotify library and objects

In this example, you will:
  • Authorise access to the Spotify Web API

  • Load your Spotify library

  • Load some other Spotify objects

  • Add some tracks to a playlist

Set up logging

Set up logging to ensure you can see all info reported by the later operations. Libraries log info about loaded objects to the custom STAT level.

import logging
from musify.log import STAT

logging.basicConfig(format="%(message)s", level=STAT)

Set up the Spotify API

  1. If you don’t already have one, create a Spotify for Developers account.

  2. If you don’t already have one, create an app. Select “Web API” when asked which APIs you are planning on using. To use this program, you will only need to take note of the client ID and client secret.

  3. Create a SpotifyAPI object and authorise the program access to Spotify data as follows:

    Note

    The scopes listed in this example will allow access to read your library data and write to your playlists. See Spotify Web API documentation for more information about scopes

    from musify.libraries.remote.spotify.api import SpotifyAPI
    
    api = SpotifyAPI(
        client_id="<YOUR CLIENT ID>",
        client_secret="<YOUR CLIENT SECRET>",
        scopes=[
            "user-library-read",
            "user-follow-read",
            "playlist-read-collaborative",
            "playlist-read-private",
            "playlist-modify-public",
            "playlist-modify-private"
        ],
        # providing a `token_file_path` will save the generated token to your system
        # for quicker authorisations in future
        token_file_path="<PATH TO JSON TOKEN>"
    )
    
    # authorise the program to access your Spotify data in your web browser
    api.authorise()
    
    from musify.libraries.remote.spotify.library import SpotifyLibrary
    library = SpotifyLibrary(api=api)
    

Load your library

  1. Create a SpotifyLibrary object and load your library data as follows:

    from musify.libraries.remote.spotify.library import SpotifyLibrary
    
    library = SpotifyLibrary(api=api)
    
    # if you have a very large library, this will take some time...
    library.load()
    
    # ...or you may also just load distinct sections of your library
    library.load_playlists()
    library.load_tracks()
    library.load_saved_albums()
    library.load_saved_artists()
    
    # enrich the loaded objects; see each function's docstring for more info on arguments
    # each of these will take some time depending on the size of your library
    library.enrich_tracks(features=True, analysis=False, albums=False, artists=False)
    library.enrich_saved_albums()
    library.enrich_saved_artists(tracks=True, types=("album", "single"))
    
    # optionally log stats about these sections
    library.log_playlists()
    library.log_tracks()
    library.log_albums()
    library.log_artists()
    
    # pretty print an overview of your library
    print(library)
    
  2. Load some Spotify objects using any of the supported identifiers as follows:

    from musify.libraries.remote.spotify.object import SpotifyTrack, SpotifyAlbum, SpotifyPlaylist, SpotifyArtist
    
    # load by ID
    track1 = SpotifyTrack.load("6fWoFduMpBem73DMLCOh1Z", api=api)
    # load by URI
    track2 = SpotifyTrack.load("spotify:track:4npv0xZO9fVLBmDS2XP9Bw", api=api)
    # load by open/external style URL
    track3 = SpotifyTrack.load("https://open.spotify.com/track/1TjVbzJUAuOvas1bL00TiH", api=api)
    # load by API style URI
    track4 = SpotifyTrack.load("https://api.spotify.com/v1/tracks/6pmSweeisgfxxsiLINILdJ", api=api)
    
    # load many different kinds of supported Spotify types
    playlist = SpotifyPlaylist.load("spotify:playlist:37i9dQZF1E4zg1xOOORiP1", api=api, extend_tracks=True)
    album = SpotifyAlbum.load("https://open.spotify.com/album/0rAWaAAMfzHzCbYESj4mfx", api=api, extend_tracks=True)
    artist = SpotifyArtist.load("1odSzdzUpm3ZEEb74GdyiS", api=api, extend_tracks=True)
    
    # pretty print information about the loaded objects
    print(track1, track2, track3, playlist, album, artist, sep="\n")
    
  3. Add some tracks to a playlist in your library, synchronise with Spotify, and log the results as follows:

    Note

    This step will only work if you chose to load either your playlists or your entire library in step 4.

    my_playlist = library.playlists["<YOUR PLAYLIST'S NAME>"]  # case sensitive
    
    # add a track to the playlist
    my_playlist.append(track1)
    
    # add an album to the playlist using either of the following
    my_playlist.extend(album)
    my_playlist += album
    
    # sync the object with Spotify and log the results
    result = my_playlist.sync(dry_run=False)
    library.log_sync(result)