mirror of
https://github.com/anidl/multi-downloader-nx.git
synced 2026-04-21 16:31:55 +00:00
Simplify API switching code
Should also fix API switching on GUI
This commit is contained in:
parent
27e02a8548
commit
103b17f449
3 changed files with 23 additions and 21 deletions
6
@types/crunchyTypes.d.ts
vendored
6
@types/crunchyTypes.d.ts
vendored
|
|
@ -32,16 +32,14 @@ export type CrunchyDownloadOptions = {
|
||||||
skipmux?: boolean,
|
skipmux?: boolean,
|
||||||
syncTiming: boolean,
|
syncTiming: boolean,
|
||||||
nocleanup: boolean,
|
nocleanup: boolean,
|
||||||
chapters: boolean,
|
chapters: boolean
|
||||||
apiType?: 'web' | 'android'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CrunchyMultiDownload = {
|
export type CrunchyMultiDownload = {
|
||||||
dubLang: string[],
|
dubLang: string[],
|
||||||
all?: boolean,
|
all?: boolean,
|
||||||
but?: boolean,
|
but?: boolean,
|
||||||
e?: string,
|
e?: string
|
||||||
crapi?: 'web' | 'android'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CrunchyMuxOptions = {
|
export type CrunchyMuxOptions = {
|
||||||
|
|
|
||||||
37
crunchy.ts
37
crunchy.ts
|
|
@ -52,6 +52,7 @@ export type sxItem = {
|
||||||
|
|
||||||
export default class Crunchy implements ServiceClass {
|
export default class Crunchy implements ServiceClass {
|
||||||
public cfg: yamlCfg.ConfigObject;
|
public cfg: yamlCfg.ConfigObject;
|
||||||
|
public api: 'android' | 'web';
|
||||||
private token: Record<string, any>;
|
private token: Record<string, any>;
|
||||||
private req: reqModule.Req;
|
private req: reqModule.Req;
|
||||||
private cmsToken: {
|
private cmsToken: {
|
||||||
|
|
@ -62,6 +63,7 @@ export default class Crunchy implements ServiceClass {
|
||||||
this.cfg = yamlCfg.loadCfg();
|
this.cfg = yamlCfg.loadCfg();
|
||||||
this.token = yamlCfg.loadCRToken();
|
this.token = yamlCfg.loadCRToken();
|
||||||
this.req = new reqModule.Req(domain, debug, false, 'cr');
|
this.req = new reqModule.Req(domain, debug, false, 'cr');
|
||||||
|
this.api = 'android';
|
||||||
}
|
}
|
||||||
|
|
||||||
public checkToken(): boolean {
|
public checkToken(): boolean {
|
||||||
|
|
@ -71,6 +73,7 @@ export default class Crunchy implements ServiceClass {
|
||||||
public async cli() {
|
public async cli() {
|
||||||
console.info(`\n=== Multi Downloader NX ${packageJson.version} ===\n`);
|
console.info(`\n=== Multi Downloader NX ${packageJson.version} ===\n`);
|
||||||
const argv = yargs.appArgv(this.cfg.cli);
|
const argv = yargs.appArgv(this.cfg.cli);
|
||||||
|
this.api = argv.crapi;
|
||||||
if (argv.debug)
|
if (argv.debug)
|
||||||
this.debug = true;
|
this.debug = true;
|
||||||
|
|
||||||
|
|
@ -113,7 +116,7 @@ export default class Crunchy implements ServiceClass {
|
||||||
const selected = await this.downloadFromSeriesID(argv.series, { ...argv });
|
const selected = await this.downloadFromSeriesID(argv.series, { ...argv });
|
||||||
if (selected.isOk) {
|
if (selected.isOk) {
|
||||||
for (const select of selected.value) {
|
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}`);
|
console.error(`Unable to download selected episode ${select.episodeNumber}`);
|
||||||
return false;
|
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.');
|
console.info('One show can only be downloaded with one dub. Use --srz instead.');
|
||||||
}
|
}
|
||||||
argv.dubLang = [argv.dubLang[0]];
|
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) {
|
if (selected.isOk) {
|
||||||
for (const select of selected.value) {
|
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}`);
|
console.error(`Unable to download selected episode ${select.episodeNumber}`);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -144,9 +147,9 @@ export default class Crunchy implements ServiceClass {
|
||||||
}
|
}
|
||||||
else if(argv.e){
|
else if(argv.e){
|
||||||
await this.refreshToken();
|
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<CrunchyEpMeta>[]) {
|
for (const select of selected as Partial<CrunchyEpMeta>[]) {
|
||||||
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}`);
|
console.error(`Unable to download selected episode ${select.episodeNumber}`);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -154,9 +157,9 @@ export default class Crunchy implements ServiceClass {
|
||||||
return true;
|
return true;
|
||||||
} else if (argv.extid) {
|
} else if (argv.extid) {
|
||||||
await this.refreshToken();
|
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<CrunchyEpMeta>[]) {
|
for (const select of selected as Partial<CrunchyEpMeta>[]) {
|
||||||
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}`);
|
console.error(`Unable to download selected episode ${select.episodeNumber}`);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -747,7 +750,7 @@ export default class Crunchy implements ServiceClass {
|
||||||
console.info(` Total results: ${newlyAddedResults.total} (Page: ${pageCur}/${pageMax})`);
|
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<ResponseBase<CrunchyEpMeta[]>> {
|
public async getSeasonById(id: string, numbers: number, e: string|undefined, but: boolean, all: boolean) : Promise<ResponseBase<CrunchyEpMeta[]>> {
|
||||||
if(!this.cmsToken.cms){
|
if(!this.cmsToken.cms){
|
||||||
console.error('Authentication required!');
|
console.error('Authentication required!');
|
||||||
return { isOk: false, reason: new 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;
|
let episodeList = { total: 0, data: [], meta: {} } as CrunchyEpisodeList;
|
||||||
//get episode info
|
//get episode info
|
||||||
if (apiType == 'android') {
|
if (this.api == 'android') {
|
||||||
const reqEpsListOpts = [
|
const reqEpsListOpts = [
|
||||||
api.beta_cms,
|
api.beta_cms,
|
||||||
this.cmsToken.cms.bucket,
|
this.cmsToken.cms.bucket,
|
||||||
|
|
@ -929,7 +932,7 @@ export default class Crunchy implements ServiceClass {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getObjectById(apiType: 'web' | 'android', e?: string, earlyReturn?: boolean, external_id?: boolean): Promise<ObjectInfo|Partial<CrunchyEpMeta>[]|undefined> {
|
public async getObjectById(e?: string, earlyReturn?: boolean, external_id?: boolean): Promise<ObjectInfo|Partial<CrunchyEpMeta>[]|undefined> {
|
||||||
if(!this.cmsToken.cms){
|
if(!this.cmsToken.cms){
|
||||||
console.error('Authentication required!');
|
console.error('Authentication required!');
|
||||||
return [];
|
return [];
|
||||||
|
|
@ -990,7 +993,7 @@ export default class Crunchy implements ServiceClass {
|
||||||
|
|
||||||
// reqs
|
// reqs
|
||||||
let objectInfo: ObjectInfo = { total: 0, data: [], meta: {} };
|
let objectInfo: ObjectInfo = { total: 0, data: [], meta: {} };
|
||||||
if (apiType == 'android') {
|
if (this.api == 'android') {
|
||||||
const objectReqOpts = [
|
const objectReqOpts = [
|
||||||
api.beta_cms,
|
api.beta_cms,
|
||||||
this.cmsToken.cms.bucket,
|
this.cmsToken.cms.bucket,
|
||||||
|
|
@ -1268,7 +1271,7 @@ export default class Crunchy implements ServiceClass {
|
||||||
}
|
}
|
||||||
|
|
||||||
let pbData = { total: 0, data: {}, meta: {} } as PlaybackData;
|
let pbData = { total: 0, data: {}, meta: {} } as PlaybackData;
|
||||||
if (options.apiType == 'android') {
|
if (this.api == 'android') {
|
||||||
const videoStreamsReq = [
|
const videoStreamsReq = [
|
||||||
api.beta_cms,
|
api.beta_cms,
|
||||||
`${this.cmsToken.cms.bucket}/videos/${mediaId}/streams`,
|
`${this.cmsToken.cms.bucket}/videos/${mediaId}/streams`,
|
||||||
|
|
@ -2092,7 +2095,7 @@ export default class Crunchy implements ServiceClass {
|
||||||
merger.cleanUp();
|
merger.cleanUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async listSeriesID(id: string, apiType: 'android' | 'web' = 'android'): Promise<{ list: Episode[], data: Record<string, {
|
public async listSeriesID(id: string): Promise<{ list: Episode[], data: Record<string, {
|
||||||
items: CrunchyEpisode[];
|
items: CrunchyEpisode[];
|
||||||
langs: langsData.LanguageItem[];
|
langs: langsData.LanguageItem[];
|
||||||
}>}> {
|
}>}> {
|
||||||
|
|
@ -2109,7 +2112,7 @@ export default class Crunchy implements ServiceClass {
|
||||||
for(const season of Object.keys(result) as unknown as number[]) {
|
for(const season of Object.keys(result) as unknown as number[]) {
|
||||||
for (const key of Object.keys(result[season])) {
|
for (const key of Object.keys(result[season])) {
|
||||||
const s = result[season][key];
|
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
|
//TODO: Make sure the below code is ok
|
||||||
//Prepare the episode array
|
//Prepare the episode array
|
||||||
let item;
|
let item;
|
||||||
|
|
@ -2200,7 +2203,7 @@ export default class Crunchy implements ServiceClass {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async downloadFromSeriesID(id: string, data: CrunchyMultiDownload) : Promise<ResponseBase<CrunchyEpMeta[]>> {
|
public async downloadFromSeriesID(id: string, data: CrunchyMultiDownload) : Promise<ResponseBase<CrunchyEpMeta[]>> {
|
||||||
const { data: episodes } = await this.listSeriesID(id, data.crapi);
|
const { data: episodes } = await this.listSeriesID(id);
|
||||||
console.info('');
|
console.info('');
|
||||||
console.info('-'.repeat(30));
|
console.info('-'.repeat(30));
|
||||||
console.info('');
|
console.info('');
|
||||||
|
|
@ -2351,7 +2354,7 @@ export default class Crunchy implements ServiceClass {
|
||||||
return seasonsList;
|
return seasonsList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getSeasonDataById(item: SeriesSearchItem, apiType: 'android' | 'web' = 'android', log = false){
|
public async getSeasonDataById(item: SeriesSearchItem, log = false){
|
||||||
if(!this.cmsToken.cms){
|
if(!this.cmsToken.cms){
|
||||||
console.error('Authentication required!');
|
console.error('Authentication required!');
|
||||||
return;
|
return;
|
||||||
|
|
@ -2376,7 +2379,7 @@ export default class Crunchy implements ServiceClass {
|
||||||
|
|
||||||
let episodeList = { total: 0, data: [], meta: {} } as CrunchyEpisodeList;
|
let episodeList = { total: 0, data: [], meta: {} } as CrunchyEpisodeList;
|
||||||
//get episode info
|
//get episode info
|
||||||
if (apiType == 'android') {
|
if (this.api == 'android') {
|
||||||
const reqEpsListOpts = [
|
const reqEpsListOpts = [
|
||||||
api.beta_cms,
|
api.beta_cms,
|
||||||
this.cmsToken.cms.bucket,
|
this.cmsToken.cms.bucket,
|
||||||
|
|
|
||||||
|
|
@ -91,6 +91,7 @@ class CrunchyHandler extends Base implements MessageHandler {
|
||||||
console.debug(`Got download options: ${JSON.stringify(data)}`);
|
console.debug(`Got download options: ${JSON.stringify(data)}`);
|
||||||
this.setDownloading(true);
|
this.setDownloading(true);
|
||||||
const _default = yargs.appArgv(this.crunchy.cfg.cli, true);
|
const _default = yargs.appArgv(this.crunchy.cfg.cli, true);
|
||||||
|
this.crunchy.api = _default.crapi;
|
||||||
const res = await this.crunchy.downloadFromSeriesID(data.id, {
|
const res = await this.crunchy.downloadFromSeriesID(data.id, {
|
||||||
dubLang: data.dubLang,
|
dubLang: data.dubLang,
|
||||||
e: data.e
|
e: data.e
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue