Last active
July 11, 2023 01:52
-
-
Save delannoy/f271bd04574826898bcff2a6f910dbef to your computer and use it in GitHub Desktop.
minimal wrapper for spotify API
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
| #!/usr/bin/env python3 | |
| from __future__ import annotations | |
| import json | |
| import awkward | |
| import pandas | |
| import requests | |
| # [Spotify Web API](https://developer.spotify.com/documentation/web-api/reference/) | |
| def token(client_id: str, client_secret: str) -> dict(str, str): | |
| # [How to access spotify's web api with client id and secret?](https://stackoverflow.com/a/56513073) | |
| url = 'https://accounts.spotify.com/api/token' | |
| response = requests.post(url, data={'grant_type': 'client_credentials'}, auth=(client_id, client_secret)) | |
| return response.json().get('access_token') | |
| client_token = token(client_id = '...', client_secret = '...') # https://developer.spotify.com/dashboard/applications | |
| # https://developer.spotify.com/console/get-users-profile/ | |
| # Required scopes for this endpoint: 'user-library-modify', 'user-library-read', 'playlist-modify-public', 'playlist-modify-private' | |
| oauth_token = '...' | |
| artist_id = '3grvcGPaLhfrD5CYsecr4j' | |
| album_id = '5hbxMCegyQPhpycfjtlW6I' | |
| track_id = '5dux3AkWPrlKFVcHDUqor2' | |
| user_id= 'andres.delannoy' | |
| def headers(token: str) -> dict(str, str): | |
| return {'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': f'Bearer {token}'} | |
| def get(endpoint: str, token: str = client_token, **kwargs) -> awkward.Record: | |
| url = f'https://api.spotify.com/v1/{endpoint}' | |
| response = requests.get(url=url, params=kwargs, headers=headers(token)) | |
| print(response.url) | |
| return awkward.from_json(response.text) | |
| def post(endpoint: str, token: str = oauth_token, **kwargs) -> awkward.Record: | |
| url = f'https://api.spotify.com/v1/{endpoint}' | |
| response = requests.post(url=url, data=json.dumps(kwargs), headers=headers(token)) # https://stackoverflow.com/a/30100530 | |
| print(response.url) | |
| if response.json().get('error'): | |
| response = requests.post(url=url, params=kwargs, headers=headers(token)) # https://stackoverflow.com/a/30100530 | |
| return response | |
| def put(endpoint: str, token: str = oauth_token, **kwargs) -> awkward.Record: | |
| url = f'https://api.spotify.com/v1/{endpoint}' | |
| response = requests.put(url=url, params=kwargs, headers=headers(token)) | |
| print(response.url) | |
| return response | |
| def main(): | |
| resp = put('me/tracks', ids=track_id) # [Save Tracks for Current User](https://developer.spotify.com/documentation/web-api/reference/#/operations/save-tracks-user) | |
| saved_tracks = get('me/tracks', token=oauth_token) # [Get User's Saved Tracks](https://developer.spotify.com/documentation/web-api/reference/#/operations/get-users-saved-tracks) | |
| print(f'saved track names: {saved_tracks.items.track.name.to_list()}') | |
| saved_track_ids = saved_tracks.items.track.id.to_list() | |
| uris = str.join(',', [f'spotify:track:{track}' for track in saved_track_ids]) | |
| new_playlist = post(f'users/{user_id}/playlists', name='test_playlist', public=True) # [Create Playlist](https://developer.spotify.com/documentation/web-api/reference/#/operations/create-playlist) | |
| playlist_id = new_playlist.json().get('id') | |
| resp = post(f'playlists/{playlist_id}/tracks', position=0, uris=uris) # [Add Items to Playlist](https://developer.spotify.com/documentation/web-api/reference/#/operations/add-tracks-to-playlist) | |
| playlist_items = get(f'playlists/{playlist_id}/tracks') # [Get Playlist Items](https://developer.spotify.com/documentation/web-api/reference/#/operations/get-playlists-tracks) | |
| print(f'track names in playlist: {playlist_items.items.track.name.to_list()}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment