...little changes

This commit is contained in:
NoLogicAlan committed 2026-09-04 19:51:02 +03:00
1 parent 7bc286776f
commit bdf7221366
2 files changed
+121 -4

No files matched your search

+45 -4
View File
@@ -1474,18 +1474,59 @@ export default class Player extends EventEmitter {
events.emit("message", "Searching...");
const ytId = Utils.extractYouTubeId(query);
const canonicalYtUrl = ytId ? Utils.normalizeYouTubeUrl(query) : null;
const searchQuery = (isUrl && canonicalYtUrl) ? canonicalYtUrl : query;
let result;
try {
if (isUrl) {
result = await this._lavalink.search(query);
result = await this._lavalink.search(searchQuery);
} else {
result = await this._lavalink.search(query, { source });
result = await this._lavalink.search(searchQuery, { source });
}
} catch (searchErr) {
logger.warn("[Player] URL/primary search failed:", searchErr?.message);
result = null;
if (!isUrl) throw searchErr;
}
let lcTracks = result?.tracks ?? [];
if (isUrl && lcTracks.length === 0 && ytId) {
const directBlocked = "YouTube blocked direct loading for this video (`Sign in to confirm you're not a bot`) — trying search fallback...";
if (searchQuery !== query) {
events.emit("message", "Retrying with original URL...");
try {
const retry = await this._lavalink.search(query);
lcTracks = retry?.tracks ?? [];
} catch (_) { lcTracks = []; }
}
if (lcTracks.length === 0) {
events.emit("message", directBlocked);
const title = await Utils.fetchYouTubeOEmbedTitle(ytId);
if (title) {
logger.player(`[Player] oEmbed fallback: resolved title "${title}" for ${ytId}`);
for (const fallbackSource of ["ytsearch", "ytmsearch"]) {
try {
const fb = await this._lavalink.search(title, { source: fallbackSource });
if (fb?.tracks?.length > 0) {
lcTracks = fb.tracks;
break;
}
} catch (_) { /* try next source */ }
}
} else {
logger.warn("[Player] oEmbed fallback: could not resolve title for " + ytId);
}
}
}
const lcTracks = result?.tracks ?? [];
if (lcTracks.length === 0) {
if (!isUrl && source !== "ytmsearch") {
events.emit("message", "No results from primary source, trying YouTube Music...");
const fallback = await this._lavalink.search(query, { source: "ytmsearch" });
const fallback = await this._lavalink.search(searchQuery, { source: "ytmsearch" });
if (fallback?.tracks?.length > 0) {
const video = this._lcTrackToVideo(fallback.tracks[0], trackMeta);
if (video) {
+76
View File
@@ -204,6 +204,82 @@ export class Utils {
}
}
/**
* Extract an 11-character YouTube video ID from any common YouTube URL form
* (youtu.be short links, watch?v=, /shorts/, /embed/, /live/, music.youtube.com).
* Tracking parameters (si, feature, utm_*) are irrelevant — only the ID matters.
* @param {string} str - Candidate URL or raw ID.
* @returns {string|null} The video ID, or null if none could be extracted.
*/
static extractYouTubeId(str) {
if (!str || typeof str !== "string") return null;
const trimmed = str.trim();
if (/^[A-Za-z0-9_-]{11}$/.test(trimmed)) return trimmed;
let url;
try { url = new URL(trimmed); } catch (_) { return null; }
const host = url.hostname.replace(/^www\./i, "").toLowerCase();
if (host === "youtu.be") {
const id = url.pathname.split("/").filter(Boolean)[0] ?? "";
return /^[A-Za-z0-9_-]{11}$/.test(id) ? id : null;
}
if (host === "youtube.com" || host === "music.youtube.com" || host === "youtube-nocookie.com") {
const v = url.searchParams.get("v");
if (v && /^[A-Za-z0-9_-]{11}$/.test(v)) return v;
const parts = url.pathname.split("/").filter(Boolean);
const idx = parts.findIndex(p => ["shorts", "embed", "live", "v"].includes(p.toLowerCase()));
if (idx !== -1 && parts[idx + 1] && /^[A-Za-z0-9_-]{11}$/.test(parts[idx + 1])) {
return parts[idx + 1];
}
}
return null;
}
/**
* Build the canonical YouTube watch URL from any YouTube URL form.
* Strips share/tracking parameters (si, feature, utm_*, etc.) which some
* audio nodes mishandle and which break NodeLink's resolver cache reuse.
* @param {string} str - Candidate URL.
* @returns {string|null} Canonical URL like https://www.youtube.com/watch?v=<id>, or null.
*/
static normalizeYouTubeUrl(str) {
const id = Utils.extractYouTubeId(str);
return id ? "https://www.youtube.com/watch?v=" + id : null;
}
/**
* Fetch a YouTube video's title via the public oEmbed endpoint.
* oEmbed is served from YouTube's edge and is NOT subject to the
* "Sign in to confirm you're not a bot" gating that blocks datacenter-IP
* player/stream requests — so it works even when NodeLink's YouTube
* source is bot-blocked. Used as a last-resort fallback to search by title.
* @param {string} videoIdOrUrl - Video ID or any YouTube URL form.
* @param {number} [timeoutMs=5000] - Request timeout in milliseconds.
* @returns {Promise<string|null>} The video title, or null on failure.
*/
static async fetchYouTubeOEmbedTitle(videoIdOrUrl, timeoutMs = 5000) {
const id = Utils.extractYouTubeId(videoIdOrUrl);
if (!id) return null;
try {
const target = "https://www.youtube.com/watch?v=" + id;
const res = await fetch(
"https://www.youtube.com/oembed?url=" + encodeURIComponent(target) + "&format=json",
{
headers: { "User-Agent": "Mozilla/5.0 (compatible; RemixBot/1.0)" },
signal: AbortSignal.timeout(timeoutMs),
}
);
if (!res.ok) return null;
const data = await res.json();
const title = typeof data?.title === "string" ? data.title.trim() : "";
return title || null;
} catch (_) {
return null;
}
}
/**
* Clamp a number between a minimum and maximum value.
* @param {number} num - The number to clamp.