From 920f2e4ed7eac13e990363bc4e0d2e46c887f4b4 Mon Sep 17 00:00:00 2001 From: aj Date: Wed, 27 Feb 2019 23:05:44 +0000 Subject: [PATCH] added get user playlists --- main.py | 13 +++++++++---- spotframework/net/playlist.py | 19 ++++++++++++++++--- spotframework/net/user.py | 18 +++++++++++++++--- 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index 07d90fa..f22d4e5 100644 --- a/main.py +++ b/main.py @@ -5,11 +5,16 @@ if __name__ == '__main__': print('hello world') user = userclass.User() - user.refreshToken() playlists = playlist.getPlaylists(user) - for playlist in playlists: - print(playlist['name']) + #for playlister in playlists: + #print(playlister['name']) - print(len(playlists)) + print(playlists[0]) + #print(len(playlists)) + + print(user.username) + + moreplaylists = playlist.getUserPlaylists(user) + print(len(moreplaylists)) diff --git a/spotframework/net/playlist.py b/spotframework/net/playlist.py index 6016831..1d976a4 100644 --- a/spotframework/net/playlist.py +++ b/spotframework/net/playlist.py @@ -12,11 +12,11 @@ def getPlaylists(user, offset = 0): params = {'offset': offset, 'limit': limit} req = requests.get(const.api_url + 'me/playlists', params = params, headers = headers) - print(req.text) + #print(req.text) if req.status_code == 200: - print(req.text) + #print(req.text) resp = req.json() @@ -25,9 +25,22 @@ def getPlaylists(user, offset = 0): if resp['next']: playlists += getPlaylists(user, offset + limit) - print(req.text) + #print(req.text) return playlists else: return None + +def getUserPlaylists(user): + + playlists = getPlaylists(user) + + returnlist = [] + + for playlist in playlists: + if playlist['owner']['id'] == user.username: + returnlist.append(playlist) + + return returnlist + diff --git a/spotframework/net/user.py b/spotframework/net/user.py index c2db52c..a50986b 100644 --- a/spotframework/net/user.py +++ b/spotframework/net/user.py @@ -10,6 +10,10 @@ class User: def __init__(self): self.access_token = os.environ['SPOTACCESS'] self.refresh_token = os.environ['SPOTREFRESH'] + + self.refreshToken() + + self.username = self.getInfo()['id'] def refreshToken(self): @@ -20,7 +24,15 @@ class User: req = requests.post('https://accounts.spotify.com/api/token', data = data, headers = headers ) - print(req.status_code) - print(req.text) + #print(req.status_code) + #print(req.text) + + if req.status_code is 200: + self.access_token = req.json()['access_token'] - self.access_token = req.json()['access_token'] + def getInfo(self): + + headers = { 'Authorization' : 'Bearer %s' % self.access_token } + + req = requests.get('https://api.spotify.com/v1/me', headers = headers) + return req.json()