yt-dlx
Version:
Effortless Audio-Video Downloader And Streamer!
54 lines • 2.3 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = relatedVideosFn;
const colors_1 = __importDefault(require("colors"));
const zod_1 = require("zod");
const youtubei_1 = require("youtubei");
const ZodSchema = zod_1.z.object({ videoId: zod_1.z.string().min(2), verbose: zod_1.z.boolean().optional() });
async function relatedVideos({ videoId }) {
try {
const youtube = new youtubei_1.Client();
const videoData = await youtube.getVideo(videoId);
if (!videoData?.related?.items) {
return [];
}
return videoData.related.items.map((item) => ({ id: item.id, title: item.title, isLive: item.isLive, duration: item.duration, uploadDate: item.uploadDate, thumbnails: item.thumbnails }));
}
catch (error) {
throw new Error(error.message);
}
}
async function relatedVideosFn({ videoId, verbose }) {
try {
ZodSchema.parse({ videoId, verbose });
const videos = await relatedVideos({ videoId });
if (!videos || videos.length === 0) {
throw new Error(`${colors_1.default.red("@error:")} No related videos found for the provided video ID.`);
}
if (verbose)
console.log(colors_1.default.green("@info:"), "❣️ Thank you for using yt-dlx. Consider 🌟starring the GitHub repo https://github.com/yt-dlx.");
return videos;
}
catch (error) {
if (error instanceof zod_1.ZodError) {
const errorMessage = `${colors_1.default.red("@error:")} Argument validation failed: ${error.errors.map(e => `${e.path.join(".")}: ${e.message}`).join(", ")}`;
console.error(errorMessage);
throw new Error(errorMessage);
}
else if (error instanceof Error) {
console.error(error.message);
throw error;
}
else {
const unexpectedError = `${colors_1.default.red("@error:")} An unexpected error occurred: ${String(error)}`;
console.error(unexpectedError);
throw new Error(unexpectedError);
}
}
finally {
}
}
//# sourceMappingURL=Related.js.map