From a194bcfd67ac1a389f442750a78cac1ef0640adc Mon Sep 17 00:00:00 2001 From: aj Date: Wed, 27 Feb 2019 22:45:53 +0000 Subject: [PATCH] added user class and working playlist fetching --- main.py | 13 +++++++++++-- spotframework/net/auth.py | 23 ----------------------- spotframework/net/const.py | 3 +++ spotframework/net/playlist.py | 33 +++++++++++++++++++++++++++++++++ spotframework/net/user.py | 26 ++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 25 deletions(-) delete mode 100644 spotframework/net/auth.py create mode 100644 spotframework/net/const.py create mode 100644 spotframework/net/playlist.py create mode 100644 spotframework/net/user.py diff --git a/main.py b/main.py index 7628f0b..07d90fa 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,15 @@ -import spotframework.net.auth as auth +import spotframework.net.user as userclass +import spotframework.net.playlist as playlist if __name__ == '__main__': print('hello world') - auth.refreshToken() + + user = userclass.User() + user.refreshToken() + playlists = playlist.getPlaylists(user) + + for playlist in playlists: + print(playlist['name']) + + print(len(playlists)) diff --git a/spotframework/net/auth.py b/spotframework/net/auth.py deleted file mode 100644 index 3ca7b91..0000000 --- a/spotframework/net/auth.py +++ /dev/null @@ -1,23 +0,0 @@ -import os -import requests -from base64 import b64encode - -client_id = os.environ['SPOTCLIENT'] -client_secret = os.environ['SPOTSECRET'] -user_access_token = os.environ['SPOTACCESS'] -user_refresh_token = os.environ['SPOTREFRESH'] - -#This sets up the https connection -#we need to base 64 encode it -#and then decode it to acsii as python 3 stores it as a byte string -def refreshToken(): - - idsecret = b64encode(bytes(client_id + ':' + client_secret, "utf-8")).decode("ascii") - headers = { 'Authorization' : 'Basic %s' % idsecret } - - data = {"grant_type": "refresh_token", "refresh_token": user_refresh_token} - - req = requests.post('https://accounts.spotify.com/api/token', data = data, headers = headers ) - - if req.status_code == 200: - user_access_token = req.json()['access_token'] diff --git a/spotframework/net/const.py b/spotframework/net/const.py new file mode 100644 index 0000000..18decd3 --- /dev/null +++ b/spotframework/net/const.py @@ -0,0 +1,3 @@ + + +api_url = 'https://api.spotify.com/v1/' diff --git a/spotframework/net/playlist.py b/spotframework/net/playlist.py new file mode 100644 index 0000000..6016831 --- /dev/null +++ b/spotframework/net/playlist.py @@ -0,0 +1,33 @@ +import requests +from . import const + +limit = 50 + +def getPlaylists(user, offset = 0): + + headers = {'Authorization': 'Bearer ' + user.access_token} + + playlists = [] + + params = {'offset': offset, 'limit': limit} + req = requests.get(const.api_url + 'me/playlists', params = params, headers = headers) + + print(req.text) + + if req.status_code == 200: + + print(req.text) + + resp = req.json() + + playlists = playlists + resp['items'] + + if resp['next']: + playlists += getPlaylists(user, offset + limit) + + print(req.text) + + return playlists + + else: + return None diff --git a/spotframework/net/user.py b/spotframework/net/user.py new file mode 100644 index 0000000..c2db52c --- /dev/null +++ b/spotframework/net/user.py @@ -0,0 +1,26 @@ +import os +import requests +from base64 import b64encode + +client_id = os.environ['SPOTCLIENT'] +client_secret = os.environ['SPOTSECRET'] + +class User: + + def __init__(self): + self.access_token = os.environ['SPOTACCESS'] + self.refresh_token = os.environ['SPOTREFRESH'] + + def refreshToken(self): + + idsecret = b64encode(bytes(client_id + ':' + client_secret, "utf-8")).decode("ascii") + headers = { 'Authorization' : 'Basic %s' % idsecret } + + data = {"grant_type": "refresh_token", "refresh_token": self.refresh_token} + + req = requests.post('https://accounts.spotify.com/api/token', data = data, headers = headers ) + + print(req.status_code) + print(req.text) + + self.access_token = req.json()['access_token']