Introducing --override

- Override OR set any filename variables
 - Use `toOverride='Override'`. Please note that the ' are required
This commit is contained in:
Izuco 2022-03-01 16:32:14 +01:00
parent 25079a2f79
commit 620e2085e2
No known key found for this signature in database
GPG key ID: E9CBE9E4EF3A1BFA
7 changed files with 70 additions and 11 deletions

View file

@ -17,7 +17,8 @@ export type CrunchyDownloadOptions = {
fsRetryTime: number,
dlsubs: string[],
skipsubs: boolean,
mp4: boolean
mp4: boolean,
override: string[]
}
export type CurnchyMultiDownload = {

View file

@ -83,7 +83,7 @@ export type FuniGetShowData = { id: number, e?: string, but: boolean, all: boole
export type FuniGetEpisodeData = { subs: FuniSubsData, fnSlug: FuniEpisodeData, simul?: boolean; dubLang: string[], s: string }
export type FuniStreamData = { callbackMaker?: (data: DownloadInfo) => HLSCallback, q: number, x: number, fileName: string, numbers: number, novids?: boolean,
timeout: number, partsize: number, fsRetryTime: number, noaudio?: boolean, mp4: boolean, ass: boolean, fontSize: number, fontName?: string, skipmux?: boolean,
forceMuxer: AvailableMuxer | undefined, simul: boolean, skipSubMux: boolean, nocleanup: boolean }
forceMuxer: AvailableMuxer | undefined, simul: boolean, skipSubMux: boolean, nocleanup: boolean, override: string[] }
export type FuniSubsData = { nosubs?: boolean, sub: boolean, dlsubs: string[] }
export type DownloadData = { id: string, e: string, dubLang: string[], fileName: string, q: number }

View file

@ -1112,8 +1112,8 @@ export default class Crunchy implements ServiceClass {
console.log(`[INFO] Selected quality: ${Object.keys(plSelectedList).find(a => plSelectedList[a] === selPlUrl)} @ ${plSelectedServer}`);
console.log('[INFO] Stream URL:', selPlUrl);
// TODO check filename
fileName = parseFileName(options.fileName, variables, options.numbers).join(path.sep);
const outFile = parseFileName(options.fileName + '.' + (mMeta.lang?.name || lang.name), variables, options.numbers).join(path.sep);
fileName = parseFileName(options.fileName, variables, options.numbers, options.override).join(path.sep);
const outFile = parseFileName(options.fileName + '.' + (mMeta.lang?.name || lang.name), variables, options.numbers, options.override).join(path.sep);
console.log(`[INFO] Output filename: ${outFile}`);
const chunkPage = await this.req.getData(selPlUrl);
if(!chunkPage.ok || !chunkPage.res){
@ -1167,7 +1167,7 @@ export default class Crunchy implements ServiceClass {
}
}
else if(options.novids){
fileName = parseFileName(options.fileName, variables, options.numbers).join(path.sep);
fileName = parseFileName(options.fileName, variables, options.numbers, options.override).join(path.sep);
console.log('[INFO] Downloading skipped!');
}

View file

@ -652,7 +652,7 @@ export default class Funi implements ServiceClass {
replaceWith: a[1],
type: typeof a[1],
} as Variable;
}), data.numbers);
}), data.numbers, data.override);
if (fnOutput.length < 1)
throw new Error(`Invalid path generated for input ${data.fileName}`);
if (log)

View file

@ -1,7 +1,7 @@
import yargs, { Choices } from 'yargs';
import { args, AvailableMuxer, groups } from './module.args';
let argvC: { [x: string]: unknown; fsRetryTime: number, forceMuxer: AvailableMuxer|undefined; username: string|undefined, password: string|undefined, silentAuth: boolean, skipSubMux: boolean, downloadArchive: boolean, addArchive: boolean, but: boolean, auth: boolean | undefined; dlFonts: boolean | undefined; search: string | undefined; 'search-type': string; page: number | undefined; 'search-locale': string; new: boolean | undefined; 'movie-listing': string | undefined; series: string | undefined; s: string | undefined; e: string | undefined; q: number; x: number; kstream: number; partsize: number; hslang: string; dlsubs: string[]; novids: boolean | undefined; noaudio: boolean | undefined; nosubs: boolean | undefined; dubLang: string[]; all: boolean; fontSize: number; allDubs: boolean; timeout: number; simul: boolean; mp4: boolean; skipmux: boolean | undefined; fileName: string; numbers: number; nosess: string; debug: boolean | undefined; nocleanup: boolean; help: boolean | undefined; service: 'funi' | 'crunchy'; update: boolean; fontName: string | undefined; _: (string | number)[]; $0: string; };
let argvC: { [x: string]: unknown; override: string[], fsRetryTime: number, forceMuxer: AvailableMuxer|undefined; username: string|undefined, password: string|undefined, silentAuth: boolean, skipSubMux: boolean, downloadArchive: boolean, addArchive: boolean, but: boolean, auth: boolean | undefined; dlFonts: boolean | undefined; search: string | undefined; 'search-type': string; page: number | undefined; 'search-locale': string; new: boolean | undefined; 'movie-listing': string | undefined; series: string | undefined; s: string | undefined; e: string | undefined; q: number; x: number; kstream: number; partsize: number; hslang: string; dlsubs: string[]; novids: boolean | undefined; noaudio: boolean | undefined; nosubs: boolean | undefined; dubLang: string[]; all: boolean; fontSize: number; allDubs: boolean; timeout: number; simul: boolean; mp4: boolean; skipmux: boolean | undefined; fileName: string; numbers: number; nosess: string; debug: boolean | undefined; nocleanup: boolean; help: boolean | undefined; service: 'funi' | 'crunchy'; update: boolean; fontName: string | undefined; _: (string | number)[]; $0: string; };
export type ArgvType = typeof argvC;

View file

@ -566,6 +566,18 @@ const args: TAppArg<boolean|number|string|unknown[]>[] = [
default: {
default: 5
},
},
{
name: 'override',
describe: 'Override a template variable',
docDescribe: true,
group: 'fileName',
service: 'both',
type: 'array',
usage: '--override "toOverride=\'VALUE\'"',
default: {
default: [ ]
}
}
];

View file

@ -3,25 +3,26 @@ import path from 'path';
import { AvailableFilenameVars } from './module.args';
export type Variable = ({
export type Variable<T extends string = AvailableFilenameVars> = ({
type: 'number',
replaceWith: number
} | {
type: 'string',
replaceWith: string
}) & {
name: AvailableFilenameVars
name: T
}
const parseFileName = (input: string, variables: Variable[], numbers: number): string[] => {
const parseFileName = (input: string, variables: Variable[], numbers: number, override: string[]): string[] => {
const varRegex = /\${[A-Za-z1-9]+}/g;
const vars = input.match(varRegex);
const overridenVars = parseOverride(variables, override);
if (!vars)
return [input];
for (let i = 0; i < vars.length; i++) {
const type = vars[i];
const varName = type.slice(2, -1);
const use = variables.find(a => a.name === varName);
const use = overridenVars.find(a => a.name === varName);
if (use === undefined) {
console.log(`[ERROR] Found variable '${type}' in fileName but no values was internally found!`);
continue;
@ -38,4 +39,49 @@ const parseFileName = (input: string, variables: Variable[], numbers: number): s
return input.split(path.sep).map(a => shlp.cleanupFilename(a));
};
const parseOverride = (variables: Variable[], override: string[]): Variable<string>[] => {
const vars: Variable<string>[] = variables;
override.forEach(item => {
console.log(item);
let index = item.indexOf('=');
if (index === -1)
return logError(item, 'invalid');
let parts = [ item.slice(0, index), item.slice(index + 1) ];
if (!(parts[1].startsWith('\'') && parts[1].endsWith('\'') && parts[1].length >= 2))
return logError(item, 'invalid');
parts[1] = parts[1].slice(1, -1);
let already = vars.findIndex(a => a.name === parts[0]);
console.log(parts);
if (already > -1) {
if (vars[already].type === 'number') {
if (isNaN(parseFloat(parts[1])))
return logError(item, 'wrongType');
vars[already].replaceWith = parseFloat(parts[1]);
} else {
vars[already].replaceWith = parts[1];
}
} else {
let isNumber = !isNaN(parseFloat(parts[1]));
vars.push({
name: parts[0],
replaceWith: isNumber ? parseFloat(parts[1]) : parts[1],
type: isNumber ? 'number' : 'string'
} as Variable<string>)
}
})
return variables;
}
const logError = (override: string, reason: 'invalid'|'wrongType') => {
switch (reason) {
case 'wrongType':
console.error(`[ERROR] Invalid type on \`${override}\`. Expected number but found string. It has been ignored`);
break;
case 'invalid':
default:
console.error(`[ERROR] Invalid override \`${override}\`. It has been ignored`);
}
}
export default parseFileName;