Load a local library and objects

In this example, you will:
  • Load a local library including tracks, playlists, and other local objects

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
import sys

from musify.logger import STAT

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

Create a library object

You can create one of any of the supported local library types for this guide as follows:

  • Generic local library

    from musify.libraries.local.library import LocalLibrary
    
    library = LocalLibrary(
        library_folders=["<PATH TO YOUR LIBRARY FOLDER>", ...],
        playlist_folder="<PATH TO YOUR PLAYLIST FOLDER>",
    )
    
  • MusicBee

    Note

    To be able to use a MusicBee library, you will need to have installed the musicbee optional dependencies. See Installation for more details.

    from musify.libraries.local.library import MusicBee
    
    library = MusicBee(musicbee_folder="<PATH TO YOUR MUSICBEE FOLDER>")
    

Load your library and other objects

  1. Load your library:

    import asyncio
    
    # if you have a very large library, this will take some time...
    asyncio.run(library.load())
    
    # ...or you may also just load distinct sections of your library
    asyncio.run(library.load_tracks())
    asyncio.run(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. Get collections from your library:

    playlist = library.playlists["<NAME OF YOUR PLAYLIST>"]  # case sensitive
    album = next(album for album in library.albums if album.name == "<ALBUM NAME>")
    artist = next(artist for artist in library.artists if artist.name == "<ARTIST NAME>")
    folder = next(folder for folder in library.folders if folder.name == "<FOLDER NAME>")
    genre = next(genre for genre in library.genres if genre.name == "<GENRE NAME>")
    
    # pretty print information about the loaded objects
    print(playlist, album, artist, folder, genre, sep="\n")
    
  3. Get a track from your library using any of the following identifiers:

    # get a track via its title
    track = library["<TRACK TITLE>"]  # if multiple tracks have the same title, the first matching one if returned
    
    # get a track via its path
    track = library["<PATH TO YOUR TRACK>"]  # must be an absolute path
    
    # get a track according to a specific tag
    track = next(track for track in library if track.artist == "<ARTIST NAME>")
    track = next(track for track in library if "<GENRE>" in (track.genres or []))
    
    # pretty print information about this track
    print(track)