From 710c12a9514a4ea4ac74f800b17a6c88b45960c6 Mon Sep 17 00:00:00 2001 From: RemixDev Date: Wed, 22 Dec 2021 14:04:59 +0100 Subject: [PATCH] reuse api token until invalid --- deezer/gw.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/deezer/gw.js b/deezer/gw.js index 474df34..5293e3f 100644 --- a/deezer/gw.js +++ b/deezer/gw.js @@ -38,14 +38,16 @@ class GW{ constructor(cookie_jar, headers){ this.http_headers = headers this.cookie_jar = cookie_jar + this.api_token = null } async api_call(method, args, params){ if (typeof args === undefined) args = {} if (typeof params === undefined) params = {} + if (!this.api_token && method != 'deezer.getUserData') this.api_token = await this._get_token() let p = { api_version: "1.0", - api_token: method == 'deezer.getUserData' ? 'null' : await this._get_token(), + api_token: method == 'deezer.getUserData' ? 'null' : this.api_token, input: '3', method: method, ...params @@ -70,7 +72,15 @@ class GW{ } throw new GWAPIError(`${method} ${args}:: ${e.name}: ${e.message}`) } - if (result_json.error.length || result_json.error.DATA_ERROR) { + if (result_json.error.length || Object.keys(result_json.error).length) { + if ( + JSON.stringify(result_json.error) == '{"GATEWAY_ERROR":"invalid api token"}' || + JSON.stringify(result_json.error) == '{"VALID_TOKEN_REQUIRED":"Invalid CSRF token"}' + ){ + console.log("invalid") + this.api_token = await this._get_token() + return this.api_call(method, args, params) + } if (result_json.payload && result_json.payload.FALLBACK){ Object.keys(result_json.payload.FALLBACK).forEach(key => { args[key] = result_json.payload.FALLBACK[key]