initial commit
authorAndrew Lorimer <andrew@lorimer.id.au>
Mon, 9 Jul 2018 08:27:51 +0000 (18:27 +1000)
committerAndrew Lorimer <andrew@lorimer.id.au>
Mon, 9 Jul 2018 08:27:51 +0000 (18:27 +1000)
.gitattributes [new file with mode: 0644]
.gitignore [new file with mode: 0644]
get-access-token.py [new file with mode: 0755]
get-auth-code.py [new file with mode: 0755]
lastsong.py [new file with mode: 0755]
refresh-access-token.py [new file with mode: 0755]
diff --git a/.gitattributes b/.gitattributes
new file mode 100644 (file)
index 0000000..9c48216
--- /dev/null
@@ -0,0 +1 @@
+*.py filter=scrub
diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..cdb5f04
--- /dev/null
@@ -0,0 +1 @@
+readme.md
diff --git a/get-access-token.py b/get-access-token.py
new file mode 100755 (executable)
index 0000000..1444346
--- /dev/null
@@ -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 (executable)
index 0000000..cfde6a2
--- /dev/null
@@ -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 (executable)
index 0000000..94e43bd
--- /dev/null
@@ -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 (executable)
index 0000000..fc1e776
--- /dev/null
@@ -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);