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 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) |
Great, thanks! Could you elaborate on the usage?
I tried with python get_track_uri_from_playlist.py spotify:user:12345:playlist:blablob on OSX and got no result:Traceback (most recent call last): File "get_track_uri_from_playlist.py", line 3, in <module> PLAYLIST_URI = os.environ['PLAYLIST_URI'] File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/os.py", line 678, in __getitem__ raise KeyError(key) from None KeyError: 'PLAYLIST_URI'
I guess I need to define the key somewhere?
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great, thanks! Could you elaborate on the usage?
I tried with python get_track_uri_from_playlist.py spotify:user:12345:playlist:blablob on OSX and got no result:
I guess I need to define the key somewhere?