Backup and restore your library data

In this example, you will:
  • Backup a local library to a JSON file

  • Restore tags for tracks from a JSON file backup

  • Backup a remote library to a JSON file

  • Restore remote playlists from a JSON file backup

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)

Backup and restore a local library

  1. Load a local library. For more information on how to do this see Load a local library and objects

    from musify.libraries.local.library import LocalLibrary
    
    library = LocalLibrary(
        library_folders=["<PATH TO YOUR LIBRARY FOLDER>", ...],
        playlist_folder="<PATH TO YOUR PLAYLIST FOLDER>",
    )
    
    # 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_tracks()
    library.load_playlists()
    
    # optionally log stats about these sections
    library.log_tracks()
    library.log_playlists()
    
    # pretty print an overview of your library
    print(library)
    
  2. Backup your library to JSON:

    import json
    
    path = "local_backup.json"
    with open(path, "w") as file:
        json.dump(library.json(), file, indent=2)
    
  3. Restore the tags for all tracks in your library from a JSON file:

    with open(path, "r") as file:
        backup = json.load(file)
    tracks = {track["path"]: track for track in backup["tracks"]}
    
    library.restore_tracks(tracks)
    

    … or restore only a specific set of tags:

    from musify.libraries.local.track.field import LocalTrackField
    
    tags = [
        LocalTrackField.TITLE,
        LocalTrackField.GENRES,
        LocalTrackField.KEY,
        LocalTrackField.BPM,
        LocalTrackField.DATE,
        LocalTrackField.COMPILATION,
        LocalTrackField.IMAGES
    ]
    
    library.restore_tracks(tracks, tags=tags)
    
  4. Save the tags to the track files:

    results = library.save_tracks(tags=tags, replace=True, dry_run=False)
    library.log_save_tracks_result(results)
    

Backup and restore a remote library

  1. Load a remote library. For more information on how to do this see any of the relevant guides for loading remote libraries.

    Note

    This step uses the SpotifyLibrary, but any supported music streaming service can be used in generally the same way. Just modify the imports and classes as required.

    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)
    
  2. Backup your library to JSON:

    import json
    
    path = "remote_backup.json"
    with open(path, "w") as file:
        json.dump(library.json(), file, indent=2)
    
  3. Restore the playlists in your library from a JSON file and sync the playlists:

    with open(path, "r") as file:
        backup = json.load(file)
    
    library.restore_playlists(backup["playlists"])
    results = library.sync(kind="refresh", reload=False, dry_run=False)
    library.log_sync(results)