Skip to content

Instantly share code, notes, and snippets.

@pualien
Last active August 9, 2020 19:23
Show Gist options
  • Save pualien/09038f2cbbcee280d28af01760527d03 to your computer and use it in GitHub Desktop.
Save pualien/09038f2cbbcee280d28af01760527d03 to your computer and use it in GitHub Desktop.
List of spotify uri from given playlist URI
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)
@cukabeka
Copy link

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?

@pualien
Copy link
Author

pualien commented Jul 9, 2020

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