UNPKG

youtube-likes-downloader

Version:
104 lines 3.99 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.dataSchema = exports.inputDataSchema = exports.fileSchema = exports.epochSchema = exports.resolutionSchema = exports.durationSchema = exports.youtubeIdSchema = exports.urlSchema = exports.numberSchema = exports.stringSchema = exports.errorSchema = exports.commonErrorSchema = exports.videoJSONSchema = exports.videoInfoSchema = void 0; const fs_1 = __importDefault(require("fs")); const zod_1 = __importDefault(require("zod")); require("@anmiles/prototypes"); const config_1 = require("../config"); exports.videoInfoSchema = zod_1.default.object({ id: zod_1.default.string(), title: zod_1.default.string(), channel: zod_1.default.string(), ext: zod_1.default.string(), epoch: zod_1.default.number(), }); exports.videoJSONSchema = exports.videoInfoSchema.extend({ width: zod_1.default.number(), height: zod_1.default.number(), resolution: zod_1.default.string(), duration_string: zod_1.default.string(), // eslint-disable-line camelcase epoch: zod_1.default.number(), }); exports.commonErrorSchema = zod_1.default.object({ errors: zod_1.default.null().or(zod_1.default.array(zod_1.default.unknown())), }); exports.errorSchema = zod_1.default.object({ errors: zod_1.default.array(zod_1.default.object({ reason: zod_1.default.string().optional(), message: zod_1.default.string().optional(), })), }); const urlRegex = /^https:\/\//; const youtubeRegex = new RegExp(`^${config_1.config.urlPrefix.regexEscape()}(.+?)(&|$)`); const durationRegex = /^\d\d?(:\d\d)+$/; const resolutionRegex = /^(\d+)[xх](\d+)$/; const dateRegex = /^(\d{4})-(\d{2})-(\d{2})$/; exports.stringSchema = zod_1.default.string(); exports.numberSchema = zod_1.default.number(); exports.urlSchema = zod_1.default .string() .regex(urlRegex, { message: 'URL should be in the format https://...', }); exports.youtubeIdSchema = zod_1.default .string() .regex(youtubeRegex, { message: `Youtube link should be in the format ${config_1.config.urlPrefix}...`, }) .transform((value) => { const match = value.match(youtubeRegex); return match.toTuple(3)[1]; }); exports.durationSchema = zod_1.default .string() .regex(durationRegex, { message: 'Duration should be in the format <min>:<sec> or <hour>:<min>:<sec>', }); exports.resolutionSchema = zod_1.default .string() .regex(resolutionRegex, { message: 'Resolution should be in the format <width>x<height>', }) .transform((value) => { const match = value.match(resolutionRegex); return match.slice(1).map((d) => parseInt(d)).toTuple(2); }); exports.epochSchema = zod_1.default .string() .regex(dateRegex, { message: 'Date should be in the format YYYY-MM-dd', }) .transform((value) => { const match = value.match(dateRegex); return Math.round(new Date(match[1]).getTime() / 1000); }); exports.fileSchema = zod_1.default .string() .refine((path) => fs_1.default.ensureFile(path, { create: false }).exists, { message: 'File not exists', }); exports.inputDataSchema = zod_1.default.object({ id: exports.stringSchema, title: exports.stringSchema, channel: exports.stringSchema, duration: exports.durationSchema, resolution: exports.resolutionSchema, epoch: exports.numberSchema, thumbnail: exports.urlSchema, description: exports.stringSchema, }); exports.dataSchema = zod_1.default.object({ id: exports.youtubeIdSchema, title: exports.stringSchema, channel: exports.stringSchema, duration: exports.durationSchema, resolution: exports.resolutionSchema, epoch: exports.epochSchema, videoFile: exports.fileSchema, imageFile: exports.fileSchema, descriptionFile: exports.fileSchema, }); //# sourceMappingURL=schema.js.map