def get_token(): if not filesys.does_exist('.token'): init_token() elif filesys.does_exist('.refreshtime'): cur_time = datetime.datetime.now() expiry_time_str = filesys.read('.refreshtime') expiry_time = datetime.datetime.strptime(expiry_time_str,'%Y%m%d %H:%M:%S') if cur_time > expiry_time: refresh_token = filesys.read('.refresh') params = { 'grant_type': 'refresh_token', 'refresh_token': refresh_token, 'client_id': client_key, 'client_secret': secret_key } result = requests.post('https://accounts.spotify.com/api/token',data=params) res_dict = json.loads(result.text) token = res_dict['access_token'] seconds_til_expiry = res_dict['expires_in'] refresh_time = datetime.datetime.strftime(datetime.datetime.now() + datetime.timedelta(seconds=int(seconds_til_expiry)),'%Y%m%d %H:%M:%S') filesys.write('.refreshtime',refresh_time,overwrite=True) filesys.write('.token',token,overwrite=True) if 'refresh_token' in res_dict.keys(): filesys.write('.refresh', res_dict['refresh_token'],overwrite=True) return filesys.read('.token')
def do_GET(request, client_address, self): data = request.recv(1024).strip() data_str = str(data) data_str = data_str[data_str.find('code=')+5:] user_code = data_str[:data_str.find('HTTP/1.1')-1] params = { 'grant_type': 'authorization_code', 'code': code, 'redirect_uri': url, 'client_id': client_key, 'client_secret': secret_key } result = requests.post('https://accounts.spotify.com/api/token',data=params) res_dict = json.loads(result.text) print(res_dict) token = res_dict['access_token'] refresh_token = res_dict['refresh_token'] seconds_til_expiry = res_dict['expires_in'] refresh_time = datetime.datetime.strftime(datetime.datetime.now() + datetime.timedelta(seconds=int(seconds_til_expiry)),'%Y%m%d %H:%M:%S') filesys.write('.token',token,overwrite=True) filesys.write('.refresh',refresh_token,overwrite=True) filesys.write('.refreshtime',refresh_time,overwrite=True) sys.exit()
def send_message_to(process, message, shouldAlert=True): pid = get_process_info(process)[0] filesys.write(process.get_input_file(), message, overwrite=False) if shouldAlert: send_signal(pid, process.signal)