UNPKG

yt-dlx

Version:

Effortless Audio-Video Downloader And Streamer!

63 lines 2.49 kB
import colors from "colors"; import { z, ZodError } from "zod"; import { Client } from "youtubei"; import YouTubeID from "../../../utils/YouTubeId"; const ZodSchema = z.object({ videoLink: z.string().min(2), verbose: z.boolean().optional() }); async function singleVideo({ videoId }) { try { const youtube = new Client(); const singleVideoData = await youtube.getVideo(videoId); if (!singleVideoData) { throw new Error(`${colors.red("@error:")} Unable to fetch video data.`); } return { id: singleVideoData.id, title: singleVideoData.title, thumbnails: singleVideoData.thumbnails, uploadDate: singleVideoData.uploadDate, description: singleVideoData.description, duration: singleVideoData.duration, isLive: singleVideoData.isLiveContent, viewCount: singleVideoData.viewCount, channelid: singleVideoData.channel?.id, channelname: singleVideoData.channel?.name, tags: singleVideoData.tags, likeCount: singleVideoData.likeCount, }; } catch (error) { throw new Error(error.message); } } export default async function videoData({ videoLink, verbose }) { try { ZodSchema.parse({ videoLink, verbose }); const vId = await YouTubeID(videoLink); if (!vId) { throw new Error(`${colors.red("@error:")} Incorrect video link provided.`); } const metaData = await singleVideo({ videoId: vId }); if (verbose) console.log(colors.green("@info:"), "❣️ Thank you for using yt-dlx. Consider 🌟starring the GitHub repo https://github.com/yt-dlx."); return metaData; } catch (error) { if (error instanceof ZodError) { const errorMessage = `${colors.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.red("@error:")} An unexpected error occurred: ${String(error)}`; console.error(unexpectedError); throw new Error(unexpectedError); } } finally { } } //# sourceMappingURL=Single.js.map