UNPKG

yt-dlx

Version:

Effortless Audio-Video Downloader And Streamer!

104 lines 4.91 kB
import colors from "colors"; import { z, ZodError } from "zod"; import TubeLogin from "../../utils/TubeLogin"; import sanitizeContentItem from "../../utils/sanitizeContentItem"; const ZodSchema = z.object({ cookies: z.string(), verbose: z.boolean().optional(), sort: z.enum(["oldest", "newest", "old-to-new", "new-to-old"]).optional() }); export default async function watch_history(options) { try { ZodSchema.parse(options); const { verbose = false, cookies, sort } = options; if (verbose) console.log(colors.green("@info:"), "Starting watch history fetch..."); if (!cookies) { throw new Error(`${colors.red("@error:")} Cookies not provided!`); } const client = await TubeLogin(cookies); if (!client) { throw new Error(`${colors.red("@error:")} Could not initialize Tube client.`); } const history = await client.getHistory(); if (!history) { throw new Error(`${colors.red("@error:")} Failed to fetch watch history.`); } const result = { status: "success", data: { Shorts: [], Videos: [] } }; history.sections?.forEach(section => { section.contents?.forEach(content => { const sanitized = sanitizeContentItem(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.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 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=History.js.map