Skip to content

Instantly share code, notes, and snippets.

@entriphy
Last active December 27, 2024 11:02
Show Gist options
  • Save entriphy/94bb06c81c1e7b64e163bdf67faabab9 to your computer and use it in GitHub Desktop.
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!

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