Last active
August 9, 2020 19:23
-
-
Save pualien/09038f2cbbcee280d28af01760527d03 to your computer and use it in GitHub Desktop.
List of spotify uri from given playlist URI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import requests | |
PLAYLIST_URI = os.environ['PLAYLIST_URI'] | |
SPOTIFY_TOKEN = os.environ['SPOTIFY_TOKEN'] # from default api login or more easily from chrome network inspecting calls directed to api.spotify.com | |
url = "https://api.spotify.com/v1/playlists/{}".format(PLAYLIST_URI) | |
querystring = {"type": "track,episode", "market": "from_token"} | |
headers = { | |
'accept': "application/json", | |
'origin': "https://open.spotify.com", | |
'authorization': "Bearer {}".format(SPOTIFY_TOKEN), | |
'accept-language': "en", | |
'user-agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36", | |
'accept-encoding': "gzip, deflate, br", | |
'cache-control': "no-cache", | |
} | |
response = requests.request("GET", url, headers=headers, params=querystring) | |
uri_list = [] | |
json_response = response.json() | |
while True: | |
if len(json_response.get('tracks', {}).get('items', [])): | |
tracks = json_response.get('tracks', {}).get('items', []) | |
else: | |
tracks = json_response.get('items', []) | |
for track in tracks: | |
if isinstance(track, dict) and track.get('track', {}).get('uri') is not None: | |
uri_list.append(track.get('track', {}).get('uri')) | |
if json_response.get('tracks', {}).get('next') or json_response.get('next', None): | |
if json_response.get('tracks', {}).get('next'): | |
nxt = json_response.get('tracks', {}).get('next') | |
else: | |
nxt = json_response.get('next', {}) | |
json_response = requests.request("GET", nxt, headers=headers).json() | |
else: | |
break | |
if len(uri_list): | |
with open('uri_list.txt', 'w') as f: | |
for item in uri_list: | |
f.write("%s\n" % item) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you only have to take the last part, the spotify playlist id for ex: spotify:playlist:$$SPOTIFY_PLAYLISYT_ID$$
and of course yes you need to define it in a environment variable or if you prefer you can replace hardcoding your token and spotify playlist id