added get user playlists

This commit is contained in:
aj 2019-02-27 23:05:44 +00:00
parent a194bcfd67
commit 920f2e4ed7
3 changed files with 40 additions and 10 deletions

13
main.py
View File

@ -5,11 +5,16 @@ if __name__ == '__main__':
print('hello world') print('hello world')
user = userclass.User() user = userclass.User()
user.refreshToken()
playlists = playlist.getPlaylists(user) playlists = playlist.getPlaylists(user)
for playlist in playlists: #for playlister in playlists:
print(playlist['name']) #print(playlister['name'])
print(len(playlists)) print(playlists[0])
#print(len(playlists))
print(user.username)
moreplaylists = playlist.getUserPlaylists(user)
print(len(moreplaylists))

View File

@ -12,11 +12,11 @@ def getPlaylists(user, offset = 0):
params = {'offset': offset, 'limit': limit} params = {'offset': offset, 'limit': limit}
req = requests.get(const.api_url + 'me/playlists', params = params, headers = headers) req = requests.get(const.api_url + 'me/playlists', params = params, headers = headers)
print(req.text) #print(req.text)
if req.status_code == 200: if req.status_code == 200:
print(req.text) #print(req.text)
resp = req.json() resp = req.json()
@ -25,9 +25,22 @@ def getPlaylists(user, offset = 0):
if resp['next']: if resp['next']:
playlists += getPlaylists(user, offset + limit) playlists += getPlaylists(user, offset + limit)
print(req.text) #print(req.text)
return playlists return playlists
else: else:
return None return None
def getUserPlaylists(user):
playlists = getPlaylists(user)
returnlist = []
for playlist in playlists:
if playlist['owner']['id'] == user.username:
returnlist.append(playlist)
return returnlist

View File

@ -10,6 +10,10 @@ class User:
def __init__(self): def __init__(self):
self.access_token = os.environ['SPOTACCESS'] self.access_token = os.environ['SPOTACCESS']
self.refresh_token = os.environ['SPOTREFRESH'] self.refresh_token = os.environ['SPOTREFRESH']
self.refreshToken()
self.username = self.getInfo()['id']
def refreshToken(self): def refreshToken(self):
@ -20,7 +24,15 @@ class User:
req = requests.post('https://accounts.spotify.com/api/token', data = data, headers = headers ) req = requests.post('https://accounts.spotify.com/api/token', data = data, headers = headers )
print(req.status_code) #print(req.status_code)
print(req.text) #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()