Skip to content

Instantly share code, notes, and snippets.

@entriphy
Last active October 31, 2025 21:41
Show Gist options
  • Select an option

  • Save entriphy/94bb06c81c1e7b64e163bdf67faabab9 to your computer and use it in GitHub Desktop.

Select an option

Save entriphy/94bb06c81c1e7b64e163bdf67faabab9 to your computer and use it in GitHub Desktop.
import requests, json
from datetime import datetime
# Get access token
r_token = requests.get("https://open.spotify.com/get_access_token?reason=transport&productType=web_player").json()
token = r_token["accessToken"]
token_expiration = datetime.fromtimestamp(r_token["accessTokenExpirationTimestampMs"] / 1000)
# Get album playcount
def get_playcount(album_id):
headers = {
"Authorization": "Bearer " + token
}
params = {
"operationName": "queryAlbumTracks",
"variables": json.dumps({
"uri": "spotify:album:" + album_id,
"offset": 0,
"limit": 300
}),
"extensions": json.dumps({
"persistedQuery": {
"version": 1,
"sha256Hash": "3ea563e1d68f486d8df30f69de9dcedae74c77e684b889ba7408c589d30f7f2e"
}
})
}
r_playcount = requests.get("https://api-partner.spotify.com/pathfinder/v1/query", headers=headers, params=params).json()
for track in r_playcount["data"]["album"]["tracks"]["items"]:
print("%s - %s" % (track["track"]["name"], track["track"]["playcount"]))
get_playcount("04Duapg2mNlVykd895xcfZ")
@speedx77
Copy link

Thank you for this!! You are the best person in the world for figuring out how to use this API!!!! I was webscraping data off of the physical search page with playwright but timeouts were crashing my server. This solution is WAYYY better!

@kapellmeister84
Copy link

Bro! That's amazing! Is it possible to modify the script so it works with playlists, instead of albums? I'm also trying to find a way to search for a track in a set of playlists. Any idea how that could work? Spotify closed the endpoint for editorial playlists.. Thank you so much!

@PatMyron
Copy link

relevant /get_access_token changes:
entriphy/sp-playcount@c24149e
entriphy/sp-playcount@a7a3e0d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment