UNPKG

yt-dlx

Version:

Effortless Audio-Video Downloader And Streamer!

116 lines 5.76 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = home_feed; const colors_1 = __importDefault(require("colors")); const zod_1 = require("zod"); const TubeLogin_1 = __importDefault(require("../../utils/TubeLogin")); const sanitizeContentItem_1 = __importDefault(require("../../utils/sanitizeContentItem")); const ZodSchema = zod_1.z.object({ cookies: zod_1.z.string(), verbose: zod_1.z.boolean().optional(), sort: zod_1.z.enum(["oldest", "newest", "old-to-new", "new-to-old"]).optional() }); async function home_feed(options) { try { ZodSchema.parse(options); const { verbose, cookies, sort } = options; if (verbose) console.log(colors_1.default.green("@info:"), "Fetching home feed..."); if (!cookies) { throw new Error(`${colors_1.default.red("@error:")} Cookies not provided!`); } const client = await (0, TubeLogin_1.default)(cookies); if (!client) { throw new Error(`${colors_1.default.red("@error:")} Could not initialize Tube client.`); } const homeFeed = await client.getHomeFeed(); if (!homeFeed) { throw new Error(`${colors_1.default.red("@error:")} Failed to fetch home feed.`); } const result = { status: "success", data: { Shorts: [], Videos: [] } }; homeFeed.contents?.contents?.forEach((section) => { if (section?.type === "RichItem" && section?.content?.type === "Video") { const sanitized = (0, sanitizeContentItem_1.default)(section); if (sanitized?.content) { result.data?.Videos.push({ type: sanitized.content.type || "", title: sanitized.content.title?.text || "", videoId: sanitized.content.video_id || "", description: sanitized.content.description_snippet?.text || "", thumbnails: sanitized.content.thumbnails || [], authorId: sanitized.content.author?.id || "", authorName: sanitized.content.author?.name || "", authorThumbnails: sanitized.content.author.thumbnails || [], authorBadges: sanitized.content.author.badges || [], authorUrl: sanitized.content.author.url || "", viewCount: sanitized.content.view_count?.text || "", shortViewCount: sanitized.content.short_view_count?.text || "", }); } } else if (section?.type === "RichSection" && section?.content?.type === "RichShelf") { section.content.contents?.forEach((item) => { if (item?.content?.type === "ShortsLockupView") { const short = { title: item.content.accessibility_text || "", videoId: item.content.on_tap_endpoint?.payload?.videoId, thumbnails: item.content.thumbnail || [] }; result.data?.Shorts.push(short); } }); } }); switch (sort) { case "oldest": if (result.data?.Shorts) result.data.Shorts.splice(0, result.data.Shorts.length - 1); if (result.data?.Videos) result.data.Videos.splice(0, result.data.Videos.length - 1); break; case "newest": if (result.data?.Shorts) result.data.Shorts.splice(1); if (result.data?.Videos) result.data.Videos.splice(1); break; case "old-to-new": if (result.data?.Shorts) result.data.Shorts.sort((a, b) => { if (!a.videoId || !b.videoId) return 0; return a.videoId.localeCompare(b.videoId); }); if (result.data?.Videos) result.data.Videos.sort((a, b) => a.videoId.localeCompare(b.videoId)); break; case "new-to-old": if (result.data?.Shorts) result.data.Shorts.sort((a, b) => { if (!a.videoId || !b.videoId) return 0; return b.videoId.localeCompare(a.videoId); }); if (result.data?.Videos) result.data.Videos.sort((a, b) => b.videoId.localeCompare(a.videoId)); break; } 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 result; } 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=HomeFeed.js.map