1
0
mirror of https://github.com/snobu/destreamer.git synced 2026-01-17 05:22:18 +00:00

Title template (#194)

* added template option and validation

* update comment link to element list

* get author info when fetching video info

* added template elements to video object

* minor function naming changes

* better exit message for template error

* changed template elements for better substitution

* implemented video title template

* removed trailing decimals on duration

* added template description

* removed hashing from uniqueId
removed debug logger.warn()

* fixed typos in default template
added elements to template fail message

* moved ffmpeg version logging to verbose
This commit is contained in:
lukaarma
2020-08-12 17:10:04 +01:00
committed by GitHub
parent ddecd9e2bc
commit 292c72aa1f
6 changed files with 166 additions and 26 deletions

View File

@@ -1,9 +1,11 @@
import { CLI_ERROR, ERROR_CODE } from './Errors';
import { checkOutDir } from './Utils';
import { logger } from './Logger';
import { templateElements } from './Types';
import fs from 'fs';
import readlineSync from 'readline-sync';
import sanitize from 'sanitize-filename';
import yargs from 'yargs';
@@ -33,6 +35,13 @@ export const argv: any = yargs.options({
default: 'videos',
demandOption: false
},
outputTemplate: {
alias: 't',
describe: 'The template for the title. See the README for more info.',
type: 'string',
default: '{title} - {publishDate} {uniqueId}',
demandOption: false
},
keepLoginCookies: {
alias: 'k',
describe: 'Let Chromium cache identity provider cookies so you can use "Remember me" during login',
@@ -102,7 +111,7 @@ export const argv: any = yargs.options({
})
.wrap(120)
.check(() => noArguments())
.check((argv: any) => inputConflicts(argv.videoUrls, argv.inputFile))
.check((argv: any) => checkInputConflicts(argv.videoUrls, argv.inputFile))
.check((argv: any) => {
if (checkOutDir(argv.outputDirectory)) {
return true;
@@ -113,6 +122,7 @@ export const argv: any = yargs.options({
throw new Error(' ');
}
})
.check((argv: any) => isOutputTemplateValid(argv))
.argv;
@@ -129,7 +139,7 @@ function noArguments(): boolean {
}
function inputConflicts(videoUrls: Array<string | number> | undefined,
function checkInputConflicts(videoUrls: Array<string | number> | undefined,
inputFile: string | undefined): boolean {
// check if both inputs are declared
if ((videoUrls !== undefined) && (inputFile !== undefined)) {
@@ -162,6 +172,39 @@ function inputConflicts(videoUrls: Array<string | number> | undefined,
}
function isOutputTemplateValid(argv: any): boolean {
let finalTemplate: string = argv.outputTemplate;
const elementRegEx = RegExp(/{(.*?)}/g);
let match = elementRegEx.exec(finalTemplate);
// if no template elements this fails
if (match) {
// keep iterating untill we find no more elements
while (match) {
if (!templateElements.includes(match[1])) {
logger.error(
`'${match[0]}' is not aviable as a template element \n` +
`Aviable templates elements: '${templateElements.join("', '")}' \n`,
{ fatal: true }
);
process.exit(1);
}
match = elementRegEx.exec(finalTemplate);
}
}
// bad template from user, switching to default
else {
logger.warn('Empty output template provided, using default one \n');
finalTemplate = '{title} - {publishDate} {uniqueId}';
}
argv.outputTemplate = sanitize(finalTemplate.trim());
return true;
}
export function promptUser(choices: Array<string>): number {
let index: number = readlineSync.keyInSelect(choices, 'Which resolution/format do you prefer?');