summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWillem Renes <wrenes@gmail.com>2021-05-02 12:26:27 +0200
committerWillem Renes <wrenes@gmail.com>2021-05-02 12:26:27 +0200
commitc48f0d1aff455a8cb1bbbee6576532ecb8032eed (patch)
treeb6898902add7fbfb9530dbaab5d0502bba4ab0ce
parentddceb510a3940d81e6a857382ab19a4b02cf0903 (diff)
downloadGetSpot-c48f0d1aff455a8cb1bbbee6576532ecb8032eed.tar.gz
GetSpot-c48f0d1aff455a8cb1bbbee6576532ecb8032eed.zip
branch to work on auth code
-rw-r--r--.gitignore3
-rw-r--r--getspot.py85
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()