Skip to content

AGnias47/spotify-playlist-generator

Repository files navigation

Spotify Playlist Generator

The Spotify Playlist Generator creates a playlist in a user's Spotify library from a file of the form

Artist, Song

A playlist can be generated by either running a Python script, or by running the provided Flask application.

This project also contains an authorization app for generating an initial access and refresh token. This app was pulled from the authorization_code directory within spotify/web-api-auth-examples. The License for that project is included in the auth_page repo.

Source Code

Link: https://github.com/AGnias47/SpotifyAPI

System Requirements

  • python >= 3.7
  • pip
  • node
  • npm

Playlist Format

The playlist should be in a text file of the form

Artist, Song

If using a spreadsheet tool, such as Excel, add the artist to Column A and the song to Column B. Do not add column titles (ex. Row 1 should be the first song in your playlist). Ensure the file is saved as a CSV (i.e. not xlsx).

The playlist uses fuzzy matching to determine which songs should be added. This makes the playlist generator forgiving of misspellings, missing commas, and incomplete titles of songs, among other things, but it does increase the likelihood of an unwanted song being added to your playlist. The playlist generator currently uses a Levenshtein distance ratio of 0.75. This can be altered if too many / not enough songs from a playlist are being added.

Playlist Generation via Python Script

Initial Token Generation

Use of the playlist generator requires users to obtain their own client_id and client_secret token from the Spotify Developer's Dashboard. Once these tokens are obtained, follow the steps below to obtain your initial access_token and a refresh_token.

  1. In the project root directory, create a keys.json file with the following contents (Note: The access_token and refresh_token are intentionally left blank for now, but should conform to valid JSON standards):

    {
        "client_id": "<Your Client ID>",
        "client_secret": "<Your Client Secret ID>",
        "access_token": "",
        "refresh_token": ""
    }
  2. In the auth_page directory, run npm install to download the dependencies needed to run the auth page

  3. In the project root directory, run node ./auth_page/app.js

  4. Go to localhost:8888 in a web browser and click "Log in with Spotify"

  5. Authorize the app for access by logging in with your Spotify user credentials

  6. Copy the access_token and refresh_token and use those values for the corresponding keys in the keys.json file

Refreshing the Access Token

At this point, an access token has been generated, and the playlist generator is able to authenticate with Spotify. However, the access token expires after 1 hour. The access token can be regenerated using your refresh token via the following steps.

  1. Ensure your keys.json file exists with all required fields. client_id, client_secret, and refresh_token must be accurate. If they are not, rerun the steps for "Initial Token Generation".
  2. Run ./spotify_token_refresh/refresh.py. The access_token in keys.json will be updated in place and should now be able to authenticate with Spotify.

The GUI method can always be used to generate a new access token, but this method allows for it to be updated directly from the command line.

Running the Playlist Generator

Once the access token has been updated in keys.json, the playlist generator can be executed with the following command:

./playlist_generator.py -f <Path to csv playlist> -n <Playlist name> -d <Playlist description (Optional)>

The script will print to stdout which songs were and were not added to the playlist.

Playlist Generation via Flask App

The Flask application is in its initial development phase. While functional, it is error prone and needs a UI/UX update.

Use of the Flask app requires users to obtain their own client_id and client_secret token from the Spotify Developer's Dashboard. Once these have been obtained, create a keys.json file in the root directory with the following contents:

{
    "client_id": "<Your Client ID>",
    "client_secret": "<Your Client Secret ID>"
}

Running the Flask App

In the project root directory, run

flask run

Navigate to localhost:5000 and log in when prompted. Enter your desired playlist name, description, and the playlist content. Tracks should be separated by a line break. The dash (-) delimiter is used by default to separate artists and song titles, but a comma or slash can also be used. Click submit, and the playlist should be created.

Planned Improvements

  • User testing with more diverse array of songs
  • Allow for usage of already created playlists

Resources

The following resources were referenced significantly in the development of this project: