To get those CSV files you need to be authenticated. Is this part of the plan?
You can, however, get the weekly charts direct from their open API without logging in.
import requests
response = requests.get('https://charts-spotify-com-service.spotify.com/public/v0/charts')
data = response.json()
for entry in response.json()["chartEntryViewResponses"][0]["entries"]:
meta = entry["trackMetadata"]
entry = entry["chartEntryData"]
track = meta['trackName']
artists = ", ".join([artist["name"] for artist in meta["artists"]])
print(f"{entry['currentRank']:3} | {track:50} | {artists}")
Results look like this:
1 | Espresso | Sabrina Carpenter
2 | Please Please Please | Sabrina Carpenter
3 | BIRDS OF A FEATHER | Billie Eilish
4 | Houdini | Eminem
5 | MILLION DOLLAR BABY | Tommy Richman
6 | Gata Only | FloyyMenor, Cris Mj
7 | LUNCH | Billie Eilish
8 | Too Sweet | Hozier
9 | I Had Some Help (Feat. Morgan Wallen) | Post Malone, Morgan Wallen
10 | A Bar Song (Tipsy) | Shaboozey
You can do something similar with the daily charts, but that requires you to login and get an authentication token. Store that token in an environment variable, SPOTIFY_BEARER
.
import os
import requests
AUTHORIZATION = os.getenv("SPOTIFY_BEARER")
DATE = "2024-01-01"
headers = {
'authorization': f'Bearer {AUTHORIZATION}',
}
response = requests.get(
f'https://charts-spotify-com-service.spotify.com/auth/v0/charts/regional-global-daily/{DATE}',
headers=headers,
)
for entry in response.json()["entries"]:
meta = entry["trackMetadata"]
entry = entry["chartEntryData"]
track = meta['trackName']
artists = ", ".join([artist["name"] for artist in meta["artists"]])
print(f"{entry['currentRank']:3} | {track:50} | {artists}")
That gives you the chart for a specific day (specified via the DATE
variable). For example, on 1 January 2024 the top 10 songs were:
1 | La Diabla | Xavi
2 | greedy | Tate McRae
3 | Cruel Summer | Taylor Swift
4 | Lovin On Me | Jack Harlow
5 | My Love Mine All Mine | Mitski
6 | La Víctima | Xavi
7 | Seven (feat. Latto) (Explicit Ver.) | Jung Kook, Latto
8 | One Of The Girls (with JENNIE, Lily Rose Depp) | The Weeknd, JENNIE, Lily-Rose Depp
9 | BELLAKEO | Peso Pluma, Anitta
10 | Paint The Town Red | Doja Cat
Options for getting a token:
- Automate login to Spotify using something like Splash, Selenium or Playwright.
- Login to Spotify via your browser and just get a token directly. Not sure how long this will be valid though.