added regex to strip 'feat. *' from title, made into module
authorAndrew Lorimer <andrew@lorimer.id.au>
Tue, 17 Jul 2018 12:05:44 +0000 (22:05 +1000)
committerAndrew Lorimer <andrew@lorimer.id.au>
Tue, 17 Jul 2018 12:05:44 +0000 (22:05 +1000)
lastsong.py
index 94e43bd2c83b5cee930a2d6976222696060a510a..6201884e7f9c82d2924046e42c05d9aa2dabd1f5 100755 (executable)
@@ -1,14 +1,16 @@
 #! /bin/python
 
 #! /bin/python
 
-import requests, json, collections
+import requests, json, collections, sys, re
 
 id = "censored"
 secret = "censored"
 Token = collections.namedtuple('token', ['access', 'refresh', 'err'])
 
 
 id = "censored"
 secret = "censored"
 Token = collections.namedtuple('token', ['access', 'refresh', 'err'])
 
+
+
 def refresh() :
 
 def refresh() :
 
-    authfile = open('./.auth', 'r+')        # current refresh token is kept in .auth
+    authfile = open("/mnt/andrew/code/lastsong" + '/.auth', 'r+')        # current refresh token is kept in .auth
     rt = authfile.read().splitlines()[0];
 
     params = {'client_id':id,
     rt = authfile.read().splitlines()[0];
 
     params = {'client_id':id,
@@ -24,8 +26,12 @@ def refresh() :
         # print("Refresh token unchanged")
     return(data['access_token']);
 
         # 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']);
+def getsong() :
+    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
+    return("\"" + re.sub('\s[\(][F|f][t|e][a][t][\.].*', '', data['items'][0]['track']['name']) + "\" by " + data['items'][0]['track']['artists'][0]['name']);
+
+if __name__ == '__getsong__' or '__main__':
+    getsong()