From 0026de73bf44e1db1bdc8f8dedea6b2f7703c7ae Mon Sep 17 00:00:00 2001 From: AnimeDL Date: Fri, 28 Jun 2024 16:52:05 -0700 Subject: [PATCH] [CR] Add device_id caching Fixes issue with creating a lot of random device IDs by saving the created device id in the config --- crunchy.ts | 16 ++++++++++++---- package.json | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/crunchy.ts b/crunchy.ts index a29a0e9..43629a1 100644 --- a/crunchy.ts +++ b/crunchy.ts @@ -226,12 +226,13 @@ export default class Crunchy implements ServiceClass { } public async doAuth(data: AuthData): Promise { + const uuid = randomUUID(); const authData = new URLSearchParams({ 'username': data.username, 'password': data.password, 'grant_type': 'password', 'scope': 'offline_access', - 'device_id': randomUUID(), + 'device_id': uuid, 'device_type': 'Chrome on Windows' }).toString(); const authReqOpts: reqModule.Params = { @@ -245,6 +246,7 @@ export default class Crunchy implements ServiceClass { return { isOk: false, reason: new Error('Authentication failed') }; } this.token = await authReq.res.json(); + this.token.device_id = uuid; this.token.expires = new Date(Date.now() + this.token.expires_in); yamlCfg.saveCRToken(this.token); await this.getProfile(); @@ -253,10 +255,11 @@ export default class Crunchy implements ServiceClass { } public async doAnonymousAuth(){ + const uuid = randomUUID(); const authData = new URLSearchParams({ 'grant_type': 'client_id', 'scope': 'offline_access', - 'device_id': randomUUID(), + 'device_id': uuid, 'device_type': 'Chrome on Windows' }).toString(); const authReqOpts: reqModule.Params = { @@ -270,6 +273,7 @@ export default class Crunchy implements ServiceClass { return; } this.token = await authReq.res.json(); + this.token.device_id = uuid; this.token.expires = new Date(Date.now() + this.token.expires_in); yamlCfg.saveCRToken(this.token); } @@ -304,12 +308,13 @@ export default class Crunchy implements ServiceClass { } public async loginWithToken(refreshToken: string) { + const uuid = randomUUID(); const authData = new URLSearchParams({ 'refresh_token': this.token.refresh_token, 'grant_type': 'refresh_token', //'grant_type': 'etp_rt_cookie', 'scope': 'offline_access', - 'device_id': randomUUID(), + 'device_id': uuid, 'device_type': 'Chrome on Windows' }).toString(); const authReqOpts: reqModule.Params = { @@ -326,6 +331,7 @@ export default class Crunchy implements ServiceClass { return; } this.token = await authReq.res.json(); + this.token.device_id = uuid; this.token.expires = new Date(Date.now() + this.token.expires_in); yamlCfg.saveCRToken(this.token); await this.getProfile(false); @@ -344,12 +350,13 @@ export default class Crunchy implements ServiceClass { } else { //console.info('[WARN] The token has expired compleatly. I will try to refresh the token anyway, but you might have to reauth.'); } + const uuid = this.token.device_id || randomUUID(); const authData = new URLSearchParams({ 'refresh_token': this.token.refresh_token, 'grant_type': 'refresh_token', //'grant_type': 'etp_rt_cookie', 'scope': 'offline_access', - 'device_id': randomUUID(), + 'device_id': uuid, 'device_type': 'Chrome on Windows' }).toString(); const authReqOpts: reqModule.Params = { @@ -366,6 +373,7 @@ export default class Crunchy implements ServiceClass { return; } this.token = await authReq.res.json(); + this.token.device_id = uuid; this.token.expires = new Date(Date.now() + this.token.expires_in); yamlCfg.saveCRToken(this.token); } diff --git a/package.json b/package.json index aef4e9e..58ad238 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "multi-downloader-nx", "short_name": "aniDL", - "version": "5.1.2", + "version": "5.1.3", "description": "Downloader for Crunchyroll, Hidive, AnimeOnegai, and AnimationDigitalNetwork with CLI and GUI", "keywords": [ "download",