UNPKG

youtube-likes-downloader

Version:
100 lines 4.14 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.addVideo = addVideo; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const config_1 = require("./config"); require("@anmiles/prototypes"); const cli_1 = require("./utils/cli"); const formatJSON_1 = require("./utils/formatJSON"); const formatTitle_1 = require("./utils/formatTitle"); const paths_1 = require("./utils/paths"); async function addVideo(profile) { const cli = new cli_1.Cli(); const id = await getID(cli); const channel = await getChannel(cli); const title = await getTitle(cli); const duration = await getDuration(cli); const resolution = await getResolution(cli); const videoFile = await getVideoFile(cli); const imageFile = await getImageFile(cli); const descriptionFile = await getDescriptionFile(cli); cli.close(); const ext = path_1.default.extname(videoFile).slice(1); const outputDir = (0, paths_1.getOutputDir)(profile); fs_1.default.ensureDir(outputDir, { create: true }); const filename = path_1.default.join(outputDir, `${(0, formatTitle_1.formatTitle)({ id, title, channel }).toFilename()}`); const json = (0, formatJSON_1.formatJSON)({ id, title, channel, ext, resolution, duration }); await fs_1.default.promises.writeFile(`${filename}.info.json`, JSON.stringify(json)); await fs_1.default.promises.copyFile(path_1.default.resolve(videoFile), filename + path_1.default.extname(videoFile)); await fs_1.default.promises.copyFile(path_1.default.resolve(imageFile), filename + path_1.default.extname(imageFile)); await fs_1.default.promises.copyFile(path_1.default.resolve(descriptionFile), `${filename}.description`); await fs_1.default.promises.appendFile((0, paths_1.getDownloadArchive)(profile), `youtube ${id}\n`); } async function getID(cli) { const regex = `^${config_1.config.urlPrefix.regexEscape()}(.+?)(&|$)`; return cli.getAnswer('Youtube link', (answer) => { const match = answer.match(regex); if (!match) { return new Error(`Youtube link should be in the format ${config_1.config.urlPrefix}...`); } return match.toTuple(3)[1]; }); } async function getChannel(cli) { return cli.getAnswer('Channel', (answer) => answer); } async function getTitle(cli) { return cli.getAnswer('Title', (answer) => answer); } async function getDuration(cli) { const regex = /^\d\d?(:\d\d)+$/; return cli.getAnswer('Duration', (answer) => { const match = answer.match(regex); if (!match) { return new Error('Duration should be in the format <min>:<sec> or <hour>:<min>:<sec>'); } return answer; }); } async function getResolution(cli) { const regex = /^(\d+)[xх](\d+)$/; return cli.getAnswer('Resolution', (answer) => { const match = answer.match(regex); if (!match) { return new Error('Resolution should be in the format <width>x<height>'); } return match.slice(1).map((d) => parseInt(d)).toTuple(2); }); } async function getVideoFile(cli) { return cli.getAnswer('Video file', (answer) => { const { exists } = fs_1.default.ensureFile(answer, { create: false }); if (!exists) { return new Error('File not exists'); } return answer; }); } async function getImageFile(cli) { return cli.getAnswer('Image file', (answer) => { const { exists } = fs_1.default.ensureFile(answer, { create: false }); if (!exists) { return new Error('File not exists'); } return answer; }); } async function getDescriptionFile(cli) { return cli.getAnswer('Description file', (answer) => { const { exists } = fs_1.default.ensureFile(answer, { create: false }); if (!exists) { return new Error('File not exists'); } return answer; }); } //# sourceMappingURL=addVideo.js.map