Last active
August 19, 2019 14:00
-
-
Save badalnabizade/fdbfe6165579a6fb7358da1f12759d0a to your computer and use it in GitHub Desktop.
Extracting user's playlist data from Spotify API.
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 spotipy | |
from spotipy.oauth2 import SpotifyClientCredentials | |
with open('app_token.txt', 'r') as file: | |
tokens = file.read() | |
token_dict = json.loads(tokens) | |
client_credentials_manager = \ | |
SpotifyClientCredentials(client_id=token_dict['client_id'], | |
client_secret=token_dict['client_secret']) | |
# Spotify API object | |
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) | |
def find_user_playlists(user_name): | |
""" | |
Returns dictionary object. | |
playlist names as keys, playlist ids as values. | |
""" | |
playlists = sp.user_playlists(user_name) | |
playlists_dict = dict() # Dictionary --> {playlist_name: playlist_id} | |
while playlists: | |
for i, playlist in enumerate(playlists['items']): | |
playlists_dict[playlist['name']] = playlist['uri'].split(':')[-1] # playlist id. | |
if playlists['next']: # if there is data more than currently obtained. | |
playlists = sp.next(playlists) | |
else: | |
playlists = None # breaks wile loop. | |
return playlists_dict |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment