UNPKG

@ernestoyoofi/yt.loader-to

Version:

An unofficial, promise-based API wrapper for `loader.to` to fetch YouTube video information and generate download links. Written in TypeScript, it's fully typed and easy to use in any Node.js project.

37 lines (36 loc) 1.26 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 = getYoutubeVideoID; const default_1 = __importDefault(require("../default")); function getYoutubeVideoID(url) { const parsedUrl = new URL(url); const host = parsedUrl.hostname; const hostIndex = default_1.default.yt_host.indexOf(host); const pathSegments = parsedUrl.pathname.split("/").filter(segment => segment !== ""); let videoId = undefined; if (pathSegments.length > 0) { const firstSegment = pathSegments[0]; switch (firstSegment) { case "watch": videoId = pathSegments[1] || new URLSearchParams(parsedUrl.search).get("v"); break; case "shorts": case "short": case "embed": case "short": case "live": videoId = pathSegments[1]; break; default: videoId = firstSegment; break; } } return { isValid: hostIndex !== -1 && typeof videoId === "string", id: videoId }; }