The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Extract playlist stats from Spotify

To extract playlist stats from Spotify, you generally need to use the Spotify Web API, which provides access to playlist metadata and track-level statistics. Here’s a step-by-step guide:


1. Set Up a Spotify Developer Account


2. Get Access Token

You’ll need to use OAuth 2.0 to authenticate. For simple data (like public playlists), you can use Client Credentials Flow:

Example Token Request (Python)

python
import requests import base64 client_id = 'your_client_id' client_secret = 'your_client_secret' auth = f"{client_id}:{client_secret}" b64_auth = base64.b64encode(auth.encode()).decode() headers = { "Authorization": f"Basic {b64_auth}", "Content-Type": "application/x-www-form-urlencoded" } data = {"grant_type": "client_credentials"} response = requests.post("https://accounts.spotify.com/api/token", headers=headers, data=data) access_token = response.json()["access_token"]

3. Access Playlist Data

Use the Playlist Endpoint to get playlist stats:

bash
GET https://api.spotify.com/v1/playlists/{playlist_id}

Example Request:

python
playlist_id = "37i9dQZF1DXcBWIGoYBM5M" # Example: Today's Top Hits headers = { "Authorization": f"Bearer {access_token}" } response = requests.get(f"https://api.spotify.com/v1/playlists/{playlist_id}", headers=headers) data = response.json()

4. Extract Key Stats

From the playlist object, you can extract:

  • Name

  • Description

  • Owner

  • Number of tracks

  • Follower count (not available directly; requires the Spotify SDK via web scraping or user login)

  • Each track’s popularity (0–100)

  • Artists & Album info

Example Code:

python
playlist_name = data["name"] description = data["description"] total_tracks = data["tracks"]["total"] owner = data["owner"]["display_name"] tracks = data["tracks"]["items"] track_stats = [] for item in tracks: track = item["track"] track_info = { "track_name": track["name"], "artist": track["artists"][0]["name"], "popularity": track["popularity"], "duration_ms": track["duration_ms"] } track_stats.append(track_info)

5. (Optional) Get Audio Features

For deeper stats (danceability, energy, tempo, etc.):

bash
GET https://api.spotify.com/v1/audio-features/{track_id}

Loop through track["id"] to pull stats for each track.


6. Format Output

You can export the results into a dataframe or JSON for analysis:

python
import pandas as pd df = pd.DataFrame(track_stats) print(df.head())

Summary of What You Can Extract:

StatAvailable?Notes
Playlist NameBasic metadata
DescriptionMay be empty
OwnerUser display name
Total Trackstracks.total
Track PopularityFrom track object
FollowersOnly available via Spotify SDK or web scraping
Audio Features (tempo, valence)Extra API calls

If you’d like a ready-to-use script or integration with Google Sheets, dashboards, or databases, let me know.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About