From 103b17f4498a79826d0fa7d309a69abd7b6432ca Mon Sep 17 00:00:00 2001 From: AnimeDL Date: Mon, 5 Feb 2024 10:12:47 -0800 Subject: [PATCH] Simplify API switching code Should also fix API switching on GUI --- @types/crunchyTypes.d.ts | 6 ++--- crunchy.ts | 37 ++++++++++++++++-------------- gui/server/services/crunchyroll.ts | 1 + 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/@types/crunchyTypes.d.ts b/@types/crunchyTypes.d.ts index a7c5a7a..13ceee4 100644 --- a/@types/crunchyTypes.d.ts +++ b/@types/crunchyTypes.d.ts @@ -32,16 +32,14 @@ export type CrunchyDownloadOptions = { skipmux?: boolean, syncTiming: boolean, nocleanup: boolean, - chapters: boolean, - apiType?: 'web' | 'android' + chapters: boolean } export type CrunchyMultiDownload = { dubLang: string[], all?: boolean, but?: boolean, - e?: string, - crapi?: 'web' | 'android' + e?: string } export type CrunchyMuxOptions = { diff --git a/crunchy.ts b/crunchy.ts index f836398..7805640 100644 --- a/crunchy.ts +++ b/crunchy.ts @@ -52,6 +52,7 @@ export type sxItem = { export default class Crunchy implements ServiceClass { public cfg: yamlCfg.ConfigObject; + public api: 'android' | 'web'; private token: Record; private req: reqModule.Req; private cmsToken: { @@ -62,6 +63,7 @@ export default class Crunchy implements ServiceClass { this.cfg = yamlCfg.loadCfg(); this.token = yamlCfg.loadCRToken(); this.req = new reqModule.Req(domain, debug, false, 'cr'); + this.api = 'android'; } public checkToken(): boolean { @@ -71,6 +73,7 @@ export default class Crunchy implements ServiceClass { public async cli() { console.info(`\n=== Multi Downloader NX ${packageJson.version} ===\n`); const argv = yargs.appArgv(this.cfg.cli); + this.api = argv.crapi; if (argv.debug) this.debug = true; @@ -113,7 +116,7 @@ export default class Crunchy implements ServiceClass { const selected = await this.downloadFromSeriesID(argv.series, { ...argv }); if (selected.isOk) { for (const select of selected.value) { - if (!(await this.downloadEpisode(select, {...argv, skipsubs: false, apiType: argv.crapi }, true))) { + if (!(await this.downloadEpisode(select, {...argv, skipsubs: false}, true))) { console.error(`Unable to download selected episode ${select.episodeNumber}`); return false; } @@ -131,10 +134,10 @@ export default class Crunchy implements ServiceClass { console.info('One show can only be downloaded with one dub. Use --srz instead.'); } argv.dubLang = [argv.dubLang[0]]; - const selected = await this.getSeasonById(argv.s, argv.numbers, argv.e, argv.but, argv.all, argv.crapi); + const selected = await this.getSeasonById(argv.s, argv.numbers, argv.e, argv.but, argv.all); if (selected.isOk) { for (const select of selected.value) { - if (!(await this.downloadEpisode(select, {...argv, skipsubs: false, apiType: argv.crapi }))) { + if (!(await this.downloadEpisode(select, {...argv, skipsubs: false }))) { console.error(`Unable to download selected episode ${select.episodeNumber}`); return false; } @@ -144,9 +147,9 @@ export default class Crunchy implements ServiceClass { } else if(argv.e){ await this.refreshToken(); - const selected = await this.getObjectById(argv.crapi, argv.e, false); + const selected = await this.getObjectById(argv.e, false); for (const select of selected as Partial[]) { - if (!(await this.downloadEpisode(select as CrunchyEpMeta, {...argv, skipsubs: false, apiType: argv.crapi }))) { + if (!(await this.downloadEpisode(select as CrunchyEpMeta, {...argv, skipsubs: false}))) { console.error(`Unable to download selected episode ${select.episodeNumber}`); return false; } @@ -154,9 +157,9 @@ export default class Crunchy implements ServiceClass { return true; } else if (argv.extid) { await this.refreshToken(); - const selected = await this.getObjectById(argv.crapi, argv.extid, false, true); + const selected = await this.getObjectById(argv.extid, false, true); for (const select of selected as Partial[]) { - if (!(await this.downloadEpisode(select as CrunchyEpMeta, {...argv, skipsubs: false, apiType: argv.crapi }))) { + if (!(await this.downloadEpisode(select as CrunchyEpMeta, {...argv, skipsubs: false}))) { console.error(`Unable to download selected episode ${select.episodeNumber}`); return false; } @@ -747,7 +750,7 @@ export default class Crunchy implements ServiceClass { console.info(` Total results: ${newlyAddedResults.total} (Page: ${pageCur}/${pageMax})`); } - public async getSeasonById(id: string, numbers: number, e: string|undefined, but: boolean, all: boolean, apiType: 'web' | 'android') : Promise> { + public async getSeasonById(id: string, numbers: number, e: string|undefined, but: boolean, all: boolean) : Promise> { if(!this.cmsToken.cms){ console.error('Authentication required!'); return { isOk: false, reason: new Error('Authentication required') }; @@ -772,7 +775,7 @@ export default class Crunchy implements ServiceClass { let episodeList = { total: 0, data: [], meta: {} } as CrunchyEpisodeList; //get episode info - if (apiType == 'android') { + if (this.api == 'android') { const reqEpsListOpts = [ api.beta_cms, this.cmsToken.cms.bucket, @@ -929,7 +932,7 @@ export default class Crunchy implements ServiceClass { return true; } - public async getObjectById(apiType: 'web' | 'android', e?: string, earlyReturn?: boolean, external_id?: boolean): Promise[]|undefined> { + public async getObjectById(e?: string, earlyReturn?: boolean, external_id?: boolean): Promise[]|undefined> { if(!this.cmsToken.cms){ console.error('Authentication required!'); return []; @@ -990,7 +993,7 @@ export default class Crunchy implements ServiceClass { // reqs let objectInfo: ObjectInfo = { total: 0, data: [], meta: {} }; - if (apiType == 'android') { + if (this.api == 'android') { const objectReqOpts = [ api.beta_cms, this.cmsToken.cms.bucket, @@ -1268,7 +1271,7 @@ export default class Crunchy implements ServiceClass { } let pbData = { total: 0, data: {}, meta: {} } as PlaybackData; - if (options.apiType == 'android') { + if (this.api == 'android') { const videoStreamsReq = [ api.beta_cms, `${this.cmsToken.cms.bucket}/videos/${mediaId}/streams`, @@ -2092,7 +2095,7 @@ export default class Crunchy implements ServiceClass { merger.cleanUp(); } - public async listSeriesID(id: string, apiType: 'android' | 'web' = 'android'): Promise<{ list: Episode[], data: Record}> { @@ -2109,7 +2112,7 @@ export default class Crunchy implements ServiceClass { for(const season of Object.keys(result) as unknown as number[]) { for (const key of Object.keys(result[season])) { const s = result[season][key]; - (await this.getSeasonDataById(s, apiType))?.data?.forEach(episode => { + (await this.getSeasonDataById(s))?.data?.forEach(episode => { //TODO: Make sure the below code is ok //Prepare the episode array let item; @@ -2200,7 +2203,7 @@ export default class Crunchy implements ServiceClass { } public async downloadFromSeriesID(id: string, data: CrunchyMultiDownload) : Promise> { - const { data: episodes } = await this.listSeriesID(id, data.crapi); + const { data: episodes } = await this.listSeriesID(id); console.info(''); console.info('-'.repeat(30)); console.info(''); @@ -2351,7 +2354,7 @@ export default class Crunchy implements ServiceClass { return seasonsList; } - public async getSeasonDataById(item: SeriesSearchItem, apiType: 'android' | 'web' = 'android', log = false){ + public async getSeasonDataById(item: SeriesSearchItem, log = false){ if(!this.cmsToken.cms){ console.error('Authentication required!'); return; @@ -2376,7 +2379,7 @@ export default class Crunchy implements ServiceClass { let episodeList = { total: 0, data: [], meta: {} } as CrunchyEpisodeList; //get episode info - if (apiType == 'android') { + if (this.api == 'android') { const reqEpsListOpts = [ api.beta_cms, this.cmsToken.cms.bucket, diff --git a/gui/server/services/crunchyroll.ts b/gui/server/services/crunchyroll.ts index 9bc2777..1b9f931 100644 --- a/gui/server/services/crunchyroll.ts +++ b/gui/server/services/crunchyroll.ts @@ -91,6 +91,7 @@ class CrunchyHandler extends Base implements MessageHandler { console.debug(`Got download options: ${JSON.stringify(data)}`); this.setDownloading(true); const _default = yargs.appArgv(this.crunchy.cfg.cli, true); + this.crunchy.api = _default.crapi; const res = await this.crunchy.downloadFromSeriesID(data.id, { dubLang: data.dubLang, e: data.e