From e138012141e5cdc63e5966a9f59e8a8120d2c9f3 Mon Sep 17 00:00:00 2001 From: aka paul <50n50@noreply.localhost> Date: Sun, 2 Nov 2025 14:33:12 +0000 Subject: [PATCH] Add youtube/youtube.js --- youtube/youtube.js | 123 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 youtube/youtube.js diff --git a/youtube/youtube.js b/youtube/youtube.js new file mode 100644 index 0000000..e9d0b50 --- /dev/null +++ b/youtube/youtube.js @@ -0,0 +1,123 @@ +async function searchResults(keyword) { + const results = []; + try { + const response = await fetchv2("https://api.piped.private.coffee/search?q=" + encodeURIComponent(keyword) + "&filter=all"); + const data = await response.json(); + + if (data && Array.isArray(data.items)) { + for (const item of data.items) { + if (item.type === "stream") { + const videoId = item.url ? item.url.replace("/watch?v=", "") : ""; + results.push({ + title: item.title || "", + image: item.thumbnail || "", + href: videoId + }); + } + } + } + + let nextpage = data.nextpage; + if (nextpage) { + try { + const response2 = await fetchv2("https://api.piped.private.coffee/nextpage/search?nextpage=" + encodeURIComponent(nextpage) + "&q=" + encodeURIComponent(keyword) + "&filter=all"); + const data2 = await response2.json(); + + if (data2 && Array.isArray(data2.items)) { + for (const item of data2.items) { + if (item.type === "stream") { + const videoId = item.url ? item.url.replace("/watch?v=", "") : ""; + results.push({ + title: item.title || "", + image: item.thumbnail || "", + href: videoId + }); + } + } + } + + nextpage = data2.nextpage; + if (nextpage) { + const response3 = await fetchv2("https://api.piped.private.coffee/nextpage/search?nextpage=" + encodeURIComponent(nextpage) + "&q=" + encodeURIComponent(keyword) + "&filter=all"); + const data3 = await response3.json(); + + if (data3 && Array.isArray(data3.items)) { + for (const item of data3.items) { + if (item.type === "stream") { + const videoId = item.url ? item.url.replace("/watch?v=", "") : ""; + results.push({ + title: item.title || "", + image: item.thumbnail || "", + href: videoId + }); + } + } + } + } + } catch (err) { + console.error("Error fetching additional pages:", err); + } + } + + return JSON.stringify(results); + } catch (err) { + console.error(err); + return JSON.stringify([{ + title: "Error", + image: "Error", + href: "Error" + }]); + } +} + +async function extractDetails(ID) { + try { + const response = await fetchv2("https://invidious.nikkosphere.com/api/v1/videos/" + ID); + const data = await response.json(); + + return JSON.stringify([{ + description: data.description, + aliases: data.author, + airdate: data.publishedText + }]); + } catch (err) { + return JSON.stringify([{ + description: "Error", + aliases: "Error", + airdate: "Error" + }]); + } +} + +async function extractEpisodes(ID) { + const results = []; + try { + + results.push({ + href: ID, + number: 1 + }); + + return JSON.stringify(results); + } catch (err) { + return JSON.stringify([{ + href: "Error", + number: "Error" + }]); + } +} + +async function extractStreamUrl(ID) { + try { + const response = await fetchv2("https://invidious.nikkosphere.com/api/v1/videos/" + ID); + const data = await response.json(); + + if (data && Array.isArray(data.formatStreams) && data.formatStreams.length > 0) { + return data.formatStreams[0].url || "https://error.org/"; + } + + return "https://error.org/"; + } catch (err) { + return "https://error.org/"; + } +}