#! /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']);