#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat Apr 3 22:42:36 2021 @author: fish Try to get and parse spotify playlists spotify:playlist:7hGGMcWOPgXj2qFw5DQyXP - likes_2021 spotify:playlist:2etpNxusBBUoXoxb3BOsF3 - likes_2020 Todo: ---- - extract the wanted info (track name, artist(s), picture url, track URI, etc) more elegantly - handle multiple artists - access token timeout? check if check is needed - find alternative to the kludge to prevent skipping first list Done: ---- 2021-04-04: handle longer playlists Outline: ------- - get playlist - extract playlist metadata - create playlist dictionary (---) - extract tracks & store - check if done, else iterate over next set of tracks (---) - store completed list, clean up. Playlist format: --------------- Playlist = {'name': '', 'image': '', 'uri': '', 'tracks': [ {'artist': '', 'name': '' }, {'artist': '' 'name': '' } ] } """ import json import requests # GLOBALS # kludge to avoid storing secrets in git repo with open('spotifyids') as f: CLIENT_ID, CLIENT_SECRET = f.readline().rstrip().split(',') AUTH_URL = 'https://accounts.spotify.com/api/token' BASE_URL = 'https://api.spotify.com/v1/' # base URL of all Spotify API endpoints PLAYLIST_ID = '37i9dQZF1EMdXVibVNv347' # temp def get_access_token(): """ Get the temporary access token from the Spotify servers (POST request) """ auth_response = requests.post(AUTH_URL, { 'grant_type': 'client_credentials', 'client_id': CLIENT_ID, 'client_secret': CLIENT_SECRET, }) # convert the response to JSON auth_response_data = auth_response.json() # save the access token access_token = auth_response_data['access_token'] return access_token def main(): """ main function """ token = get_access_token() print(token) if __name__ == '__main__': main()