From c48f0d1aff455a8cb1bbbee6576532ecb8032eed Mon Sep 17 00:00:00 2001 From: Willem Renes Date: Sun, 2 May 2021 12:26:27 +0200 Subject: branch to work on auth code --- .gitignore | 3 +++ getspot.py | 85 +++++--------------------------------------------------------- 2 files changed, 9 insertions(+), 79 deletions(-) diff --git a/.gitignore b/.gitignore index 32c1167..31fd665 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ # don't store the secrets in the repo spotifyids + +# don't spam the repo with temporary working files +*.json diff --git a/getspot.py b/getspot.py index 1978ffa..40012a5 100644 --- a/getspot.py +++ b/getspot.py @@ -11,7 +11,6 @@ spotify:playlist:7hGGMcWOPgXj2qFw5DQyXP - likes_2021 spotify:playlist:2etpNxusBBUoXoxb3BOsF3 - likes_2020 - Todo: ---- - extract the wanted info (track name, artist(s), picture url, @@ -61,6 +60,7 @@ import requests # 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 @@ -88,84 +88,11 @@ def get_access_token(): return access_token -def get_playlist(access_token): - """ - Get playlist from the servers with this request: - GET https://api.spotify.com/v1/playlists/{playlist_id} - """ - headers = {'Authorization': 'Bearer {token}'.format(token=access_token)} - # actual GET request with proper header - playlist = requests.get(BASE_URL + 'playlists/' - + PLAYLIST_ID, headers=headers) - playlist = playlist.json() - return playlist - - -# playlist ID from the URI -# playlist_id = '7hGGMcWOPgXj2qFw5DQyXP' # 2021 - shorter than 100 (for now) -# playlist_id = '2etpNxusBBUoXoxb3BOsF3' # 2020 - longer than 100 - - -# %% create playlist dict - - -playlist = get_playlist(get_access_token()) -my_playlist = {'name': playlist['name'], - 'uri': playlist['uri'], - 'image': playlist['images'][0]['url'], - 'tracks': [] - } - - -# %% strip down to the tracks - -work_playlist = playlist['tracks'] -work_playlist['next'] = BASE_URL + 'playlists/' + PLAYLIST_ID + '/tracks' -# kludge - - -def do_work(access_token): - """ extract info from playlist """ - headers = {'Authorization': 'Bearer {token}'.format(token=access_token)} - while True: - work_playlist = requests.get(work_playlist['next'], headers=headers) - work_playlist = work_playlist.json() - print('url is: ', work_playlist['next']) - - # Extract and store playlist items - playlist_tracks = [] - - for track in work_playlist['items']: - playlist_tracks.append({ - 'artist': track['track']['artists'][0]['name'], - 'name': track['track']['name']}) - - playlist['tracks'] = playlist['tracks'] + playlist_tracks - - # break loop if no next url - if not work_playlist['next']: - break - return - -# %% store list & clean up -def cleanup(): - """ store list to file and cleanup etc """ - playlist_name = playlist['name'] + '-' + playlist_id + '.json' - - with open(playlist_name, 'w') as outfile: - json.dump(playlist, outfile, indent=4) - return - - -def setup(): - pass - def main(): - - setup() - do_work(access_token) - cleanup() - return + """ main function """ + token = get_access_token() + print(token) -main() +if __name__ == '__main__': + main() -- cgit v1.2.3-70-g09d2