How to Track Your Favorite Artist’s Most Popular Songs on Spotify with Python
The Spotify Web API is a RESTful API. With this interface, data can be retrieved from Spotify straight to your web browser or Terminal.
Prerequisites
- Some knowledge of Python
- Python3 installed on a device
- Pip installed on a device
Steps to Take
1. Log in to Spotify’s Web API portal, developer.spotify.com/dashboard
1.1. Log in with your Spotify account or a Gmail, Facebook or Apple account that is connected to your Spotify account.
1.2. Create a Spotify account, if you do not have one already.
2. Create an app in the API portal.
2.1. Click on “Create an App”.
2.2. Enter a name for the app.
2.3. Enter a description for the app.
2.4. Check the checkbox.
2.5. Click on “Create”.
2.6. Make note of the Client ID and Client Secret. These credentials are needed to create the Python script.
3. Install the Spotify Python module.
Spotipy is Spotify’s Python library for their Web API. Spotipy can retrieve data from the Spotify platform with an automated script.
3.1. Open a terminal.
3.2. Run the following command, “pip install spotipy”.
4. Create a Python script.
4.1. Open a text editor on your device.
4.2. Create a new file.
4.3. Insert the following code:
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
def main():
# authenticate to the Spotify Web API with the Client ID and Client Secret
spot = spotipy.Spotify(auth_manager=SpotifyClientCredentials(
client_id=“[SPOTIFY_CLIENT_ID]”,
client_secret=“[SPOTIFY_CLIENT_SECRET]”
))
# search for an artist
tracks = spot.search(q=“[ARTIST_NAME]”)[‘tracks’]
# loop through the tracks by track name and ‘popularity’
track = tracks[‘items’]
for i in range(len(track)):
track_name = track[i][‘name’]
track_pop = track[i][‘popularity’]
print(“Track Name: %s, Popularity: %s” % (track_name, track_pop))
if __name__ == “__main__”:
main()
4.4. Name and save the file (do not name the file, spotipy.py; otherwise, you’ll get an error after you run the script).
5. Run the Python script.
Note: The “popularity” of artists’ tracks on Spotify is a metric that is based on data that only the people over at Spotify are aware of. So, the popularity of tracks based on Spotify’s Web API are arbitrary numbers.