Skip to main content

Python package which takes the songs of a greater playlist as starting point to make recommendations of songs based on up to 5 specific songs within that playlist, using K-Nearest-Neighbors Technique

Project description

spotify-recommender

Setup

Requirements:

  • Python installed
    The ideal version, to run the package is 3.8.x, the version in which the package was built over,
    however, older versions of python 3 shouldn't have any issues, as the package does not use any
    fancy, new methods, not supported by older versions of Python 3.x
  • Network Connection
    So that a wide range of songs can be analised, it is imperative to have a network connection, at least for the first time executing a script using this package
  • A fitting playlist
    The perfect use case for this package is that of one big playlist (500+ songs), which you feel like listening to some of them, then others but never all of them Still, in the first versions of this package, this playlist will have to have at least two of your favorite songs.
  • Patience

It may seem funny or a joke, but the first mapping process of the playlist to a local pandas DataFrame, it will take a good while, up to 2.5 to 3 second per song, at 20-40Mbps Internet connection, being in Latam. All these factors play a part in the time for it to load. Just to make it clear, cpu, ram, these will not help much, the issue is to have up to 5 different http requests per song, which make this take so long

  • Jupyter Notebook
    Not exactly a requirement but it is advised that a jupyter notebook is used ( even more advised to use the vscode extension for jupyter notebooks ), because it is important, or at least more confortable, to have the variable still in memory and then decide how to use it, without having to run the script multiple times

  • Spotify access
    I mean, you know thJsonat already, right?

  • Installing the package

pip install spotify-recommender
  • Importing the package

Firstly, it's necessary to import the method start_api from the package spotify_recommender.api:

from spotify_recommender.api import start_api

Starting the api

  • Gathering the initial information: (playlist_url, user_id) --- Playlist URL: The playlist url is available when right clicking the playlist name / or going to the three dots that represent the playlist options

--- User ID: The use rid is available when clicking the account profile information

  • Calling the function:
api = start_api(playlist_url='<PLAYLIST_URL>', user_id='<USER_ID>')
  • Getting the Auth Token: It is a hash token that expires 60 minutes after it is generated, first you need to say that you want to be redirected (y), then press "Get Token", and then select the 5 scope options, but if it is not the first time you are executing the script in less than an hour, then press(n) and paste the token:

Then generate it, after that hit crtl+A / command+A to select it all then crtl+C / command+C to copy it Then paste it in the field requiring it and press enter Then if you already have a previously generated CSV file format playlist, type csv then enter, if you do not have the playlist as previously generated, press web, but know that it will take a good while as said here,and if this is the case, go get a coffee, or tea if you are into that sort of thing.

Methods

  • get_playlist()
api.get_playlist()
# Function that returns the pandas DataFrame representing the base playlist
  • playlist_to_csv()
api.playlist_to_csv()
# Function that creates a csv format file containing the items in the playlist
# Especially useful when re running the script without having changed the playlist
  • get_medium_term_favorites_playlist(with_distance: bool, generate_csv: bool, generate_parquet: bool, build_playlist: bool)
api.get_medium_term_favorites_playlist(generate_csv=True, build_playlist=True)
# Function that returns the pandas DataFrame representing the 
# medium term top 5 recommendation playlist
# All parameters are defaulted to False
# The "distance" is a mathematical value with no explicit units, that is 
# used by te algorithm to find the closest songs
# build_playlist will change the user's library
  • get_short_term_favorites_playlist(with_distance: bool, generate_csv: bool, generate_parquet: bool, build_playlist: bool)
api.get_short_term_favorites_playlist(generate_csv=True, build_playlist=True)
# Function that returns the pandas DataFrame representing the 
# short term top 5 recommendation playlist
# All parameters are defaulted to False
# The "distance" is a mathematical value with no explicit units, that is 
# used by te algorithm to find the closest songs
# build_playlist will change the user's library
  • get_recommendations_for_song(song: str, K: int, with_distance: bool, generate_csv: bool, generate_parquet: bool, build_playlist: bool)
api.get_recommendations_for_song(song='<SONG_NAME>', K=50)
# Function that creates returns the pandas DataFrame representing the 
# given song recommendation playlist
# the 'song' and 'K' parameters are mandatory and the rest is
# defaulted to False
# The "distance" is a mathematical value with no explicit units, that is 
# used by te algorithm to find the closest songs
# build_playlist will change the user's library

OG Scripts

Context

This script, in jupyter notebook format for organization purposes, applies the technique called K Nearest Neighbors to find the 50 closest songs to either one chosen or one of the users top 5(short term), all within a specific Spotify playlist, in order to maintain the most consistency in terms of the specific chosen style, and creates a new playlist with those songs in the user's library, using their genres, artists and overall popularity as metrics to determine indexes of comparison between songs

Variations

There are also 2 variations from that, which consist of medium term favorites related top 100 and "short term top 5" related top 50 songs. They vary from OG model since the base song(s) is(are) not chosen by hand but statistically

DISCLAIMER

Not fit for direct use since some information such as client id, client secret, both of which are, now, in a hidden script on .gitignore so that it is not made public, have to be informed in order for the Spotify Web API to work properly. And also, these scripJsonts are deprecated, so they will not have any maintenance or overtime improvements

Packages used

  • Pandas
pip install pandas
  • Requests
pip install requests
  • Webbrowser
pip install webbrowser
  • Json (json)
  • Operator (operator)
  • Functools (functools)
  • Os (os)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spotify_recommender_api-2.0.1.tar.gz (14.7 kB view hashes)

Uploaded Source

Built Distribution

spotify_recommender_api-2.0.1-py3-none-any.whl (12.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page