UNPKG

yt-dlx

Version:

Effortless Audio-Video Downloader And Streamer!

110 lines 5.34 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 = watch_history; 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 watch_history(options) { try { ZodSchema.parse(options); const { verbose = false, cookies, sort } = options; if (verbose) console.log(colors_1.default.green("@info:"), "Starting watch history fetch..."); 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 history = await client.getHistory(); if (!history) { throw new Error(`${colors_1.default.red("@error:")} Failed to fetch watch history.`); } const result = { status: "success", data: { Shorts: [], Videos: [] } }; history.sections?.forEach(section => { section.contents?.forEach(content => { const sanitized = (0, sanitizeContentItem_1.default)(content); if (sanitized?.type === "ReelShelf") { const shorts = sanitized.items?.map((item) => ({ title: item?.accessibility_text, videoId: item?.on_tap_endpoint?.payload?.videoId, thumbnails: item?.thumbnail })) || []; if (result.data?.Shorts) result.data.Shorts.push(...shorts); } else if (sanitized?.type === "Video") { const video = { title: sanitized?.title?.text, videoId: sanitized?.videoId, thumbnails: sanitized?.thumbnails, description: sanitized?.description || "" }; if (result.data?.Videos) result.data.Videos.push(video); } }); }); switch (sort) { case "oldest": if (result.data?.Shorts && result.data.Shorts.length > 0) result.data.Shorts.splice(0, result.data.Shorts.length - 1); if (result.data?.Videos && result.data.Videos.length > 0) result.data.Videos.splice(0, result.data.Videos.length - 1); break; case "newest": if (result.data?.Shorts && result.data.Shorts.length > 1) result.data.Shorts.splice(1); if (result.data?.Videos && result.data.Videos.length > 1) 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) => { if (!a?.videoId || !b?.videoId) return 0; return 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) => { if (!a?.videoId || !b?.videoId) return 0; return 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=History.js.map