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')
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))

View File

@ -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

View File

@ -11,6 +11,10 @@ class User:
self.access_token = os.environ['SPOTACCESS']
self.refresh_token = os.environ['SPOTREFRESH']
self.refreshToken()
self.username = self.getInfo()['id']
def refreshToken(self):
idsecret = b64encode(bytes(client_id + ':' + client_secret, "utf-8")).decode("ascii")
@ -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']
def getInfo(self):
headers = { 'Authorization' : 'Bearer %s' % self.access_token }
req = requests.get('https://api.spotify.com/v1/me', headers = headers)
return req.json()