From ba0b94fa1c6ab36a29632126df8d310ac8c91d22 Mon Sep 17 00:00:00 2001 From: Andrew Lorimer Date: Mon, 9 Jul 2018 18:27:51 +1000 Subject: [PATCH] initial commit --- .gitattributes | 1 + .gitignore | 1 + get-access-token.py | 19 +++++++++++++++++++ get-auth-code.py | 12 ++++++++++++ lastsong.py | 31 +++++++++++++++++++++++++++++++ refresh-access-token.py | 16 ++++++++++++++++ 6 files changed, 80 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100755 get-access-token.py create mode 100755 get-auth-code.py create mode 100755 lastsong.py create mode 100755 refresh-access-token.py diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..9c48216 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.py filter=scrub diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cdb5f04 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +readme.md diff --git a/get-access-token.py b/get-access-token.py new file mode 100755 index 0000000..1444346 --- /dev/null +++ b/get-access-token.py @@ -0,0 +1,19 @@ +#! /bin/python + +import requests +import os + +id = input('Enter the client id: '); +secret = input('Enter the client secret: '); +code = input('Enter the auth code (obtained from get-auth-code.py): '); +callback = input('Enter the callback uri: '); + +params = {'client_id':id, + 'client_secret':secret, + 'grant_type':"authorization_code", + 'code':code, + 'redirect_uri':callback} + +r = requests.post(url = "https://accounts.spotify.com/api/token", data = params) + +print(r.text); diff --git a/get-auth-code.py b/get-auth-code.py new file mode 100755 index 0000000..cfde6a2 --- /dev/null +++ b/get-auth-code.py @@ -0,0 +1,12 @@ +#! /bin/python + +import webbrowser, os + +id = input('Enter client id: '); +secret = input('Enter client secret: '); +callback = input('Enter callback url: '); +scopes = input('Enter space-separated scopes: '); + +webbrowser.get().open("https://accounts.spotify.com/authorize/client_id=" + id + "&response_type=code&redirect_uri=" + callback + "&scope=" + scopes); + +print('A browser window has been opened asking you to log in to Spotify. Once logged in, you will be redirected to the callback uri you gave, with a parameter containing the auth code.') diff --git a/lastsong.py b/lastsong.py new file mode 100755 index 0000000..94e43bd --- /dev/null +++ b/lastsong.py @@ -0,0 +1,31 @@ +#! /bin/python + +import requests, json, collections + +id = "censored" +secret = "censored" +Token = collections.namedtuple('token', ['access', 'refresh', 'err']) + +def refresh() : + + authfile = open('./.auth', 'r+') # current refresh token is kept in .auth + rt = authfile.read().splitlines()[0]; + + params = {'client_id':id, + 'client_secret':secret, + 'grant_type':"refresh_token", + 'refresh_token':rt} + + data = json.loads(requests.post(url = "https://accounts.spotify.com/api/token", data = params).text) + try: + authfile.write(data['refresh_token']); # refresh token is not updated if delta t < 3600ms + except: + pass + # print("Refresh token unchanged") + return(data['access_token']); + +header = {'Authorization': "Authorization: Bearer " + refresh()} +response = requests.get("https://api.spotify.com/v1/me/player/recently-played", headers = header).text +data = json.loads(response) +# open('./sampleoutput.json', 'w').write(response); # for debugging +print("\"" + data['items'][0]['track']['name'] + "\" by " + data['items'][0]['track']['artists'][0]['name']); diff --git a/refresh-access-token.py b/refresh-access-token.py new file mode 100755 index 0000000..fc1e776 --- /dev/null +++ b/refresh-access-token.py @@ -0,0 +1,16 @@ +#! /bin/python + +import requests + +id = input('Enter the client id: '); +secret = input('Enter the client secret: '); +token = input('Enter the refresh token (obtained from get-access-token.py): '); + +params = {'client_id':id, + 'client_secret':secret, + 'grant_type':"refresh_token", + 'refresh_token':token} + +r = requests.post(url = "https://accounts.spotify.com/api/token", data = params) + +print(r.text); -- 2.43.2