Create a playlist of your followed artists’ latest music

In this example, you will:
  • Load information about your followed artists

  • Load the tracks released by these artists between two dates

  • Create a playlist containing these tracks

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)

Create the playlist

  1. Create a remote library object:

    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. Load data about your followed artists:

    library.load_saved_artists()
    library.enrich_saved_artists()
    
  3. Define the date range you wish to get track for and define this helper function for filtering albums:

    from datetime import datetime, date
    
    start_date = date(2024, 1, 1)
    end_date = datetime.now().date()
    
    
    def match_date(alb) -> bool:
        """Match start and end dates to the release date of the given ``alb``"""
        if alb.date:
            return start_date <= alb.date <= end_date
        if alb.month:
            return start_date.year <= alb.year <= end_date.year and start_date.month <= alb.month <= end_date.month
        if alb.year:
            return start_date.year <= alb.year <= end_date.year
        return False
    
  4. Filter the albums and load the tracks for only these albums:

    from musify.libraries.remote.core.enum import RemoteObjectType
    
    albums = [album for artist in library.artists for album in artist.albums if match_date(album)]
    albums_need_extend = [album for album in albums if len(album.tracks) < album.track_total]
    if albums_need_extend:
        kind = RemoteObjectType.ALBUM
        key = api.collection_item_map[kind]
    
        bar = library.logger.get_iterator(iterable=albums_need_extend, desc="Getting album tracks", unit="albums")
        for album in bar:
            api.extend_items(album.response, kind=kind, key=key)
            album.refresh(skip_checks=False)
    
    # log stats about the loaded artists
    
  5. Create a new playlist and add these tracks:

    from musify.libraries.remote.spotify.object import SpotifyPlaylist
    
    name = "New Music Playlist"
    playlist = SpotifyPlaylist.create(api=api, name=name)
    
    tracks = [track for album in sorted(albums, key=lambda x: x.date, reverse=True) for track in album]
    playlist.extend(tracks, allow_duplicates=False)
    
    # sync the object with Spotify and log the results
    results = playlist.sync(kind="refresh", reload=False, dry_run=False)