Create, load, and update a local playlist

In this example, you will:
  • Create a local playlist from scratch

  • Load a local playlist

  • Modify the tracks in a playlist and save the changes to the file

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 playlist

You can create a playlist from scratch as follows:

import asyncio

from musify.libraries.local.playlist import M3U
from musify.libraries.local.track import load_track

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

track_tasks = asyncio.gather(
    load_track("<PATH TO A TRACK>"),
    load_track("<PATH TO A TRACK>"),
    load_track("<PATH TO A TRACK>"),
    load_track("<PATH TO A TRACK>"),
)
tracks = loop.run_until_complete(track_tasks)
loop.close()

playlist = M3U("<PATH TO AN M3U PLAYLIST>")
playlist.extend(tracks)

# pretty print information about this playlist
print(playlist)

Load a playlist

You can load a playlist as per the blow code.

If you already have some tracks loaded, and you want the playlist to only use those tracks instead of loading the tracks itself, you can pass these preloaded tracks to the playlist too. This will still load the playlist from the given file, but it will use the given track objects instead of loading and creating new ones.

Note

To be able to use the XAutoPF playlist type, you will need to have installed the musicbee optional dependencies. See Installation for more details.

from musify.libraries.local.playlist import XAutoPF

playlist = M3U("<PATH TO AN M3U PLAYLIST>")
asyncio.run(playlist.load())

# for some playlist types, you will need to provide
# a list of tracks to choose from in order to load them
playlist = XAutoPF("<PATH TO AN XAUTOPF PLAYLIST>")
asyncio.run(playlist.load(tracks=tracks))

You can also just have Musify automatically determine the playlist type to load based on the file’s extension:

from musify.libraries.local.playlist import load_playlist

# providing tracks is optional
playlist = asyncio.run(load_playlist("<PATH TO A PLAYLIST>", tracks=tracks))

There may also be cases where the files in the file need mapping to be loaded e.g. if the paths contained in the playlist file are relative paths. You may give the playlist object a PathMapper or PathStemMapper to handle this.

from musify.file.path_mapper import PathMapper

playlist = asyncio.run(load_playlist("<PATH TO A PLAYLIST>", path_mapper=PathMapper()))

If you want to be able to read/update URIs on the loaded tracks, you’ll need to provide a RemoteDataWrangler to the playlist object for the relevant music streaming source.

The following is an example for doing this with Spotify as the data source:

from musify.libraries.remote.spotify.wrangle import SpotifyDataWrangler

playlist = asyncio.run(load_playlist("<PATH TO A PLAYLIST>", remote_wrangler=SpotifyDataWrangler()))

Modify and save the playlist

  1. Add some tracks to the playlist:

    # add a track to the playlist
    track = asyncio.run(load_track("<PATH TO A TRACK>"))
    playlist.append(track)
    
    # add album's and artist's tracks to the playlist using either of the following
    playlist.extend(tracks)
    playlist += tracks
    
  2. Save the playlist:

    result = asyncio.run(playlist.save(dry_run=False))
    print(result)