import http from "node:http"; import { URL } from "node:url"; import { getChannelBundle, getVideoInfo, getYt, resolveInput, searchVideos, } from "./youtube.mjs"; const HOST = process.env.YT_SVC_HOST || "127.0.0.1"; const PORT = Number(process.env.YT_SVC_PORT || 8020); function send(res, status, body) { const payload = JSON.stringify(body); res.writeHead(status, { "Content-Type": "application/json; charset=utf-8", "Cache-Control": "no-store", }); res.end(payload); } function readJson(req) { return new Promise((resolve, reject) => { const chunks = []; req.on("data", (c) => chunks.push(c)); req.on("end", () => { if (!chunks.length) return resolve({}); try { resolve(JSON.parse(Buffer.concat(chunks).toString("utf8"))); } catch (e) { reject(e); } }); req.on("error", reject); }); } async function handle(req, res) { const url = new URL(req.url || "/", `http://${HOST}:${PORT}`); const path = url.pathname.replace(/\/+$/, "") || "/"; try { if (req.method === "GET" && path === "/health") { // warm session lazily let ready = false; let error = ""; try { await getYt(); ready = true; } catch (e) { error = String(e?.message || e).slice(0, 200); } return send(res, 200, { ok: ready, service: "youtube-svc", engine: "youtubei.js", repo: "https://github.com/LuanRT/YouTube.js", time: Date.now() / 1000, error: error || undefined, }); } if (req.method === "GET" && path === "/resolve") { const q = url.searchParams.get("url") || url.searchParams.get("q") || ""; const data = await resolveInput(q); return send(res, 200, { ok: true, ...data }); } if (req.method === "GET" && path === "/channel") { const q = url.searchParams.get("url") || url.searchParams.get("id") || url.searchParams.get("q") || ""; const includeVideos = url.searchParams.get("videos") === "1"; const limit = Math.min(30, Math.max(1, Number(url.searchParams.get("limit") || 8))); const data = await getChannelBundle(q, { includeLive: true, includeVideos, videoLimit: limit }); return send(res, 200, { ok: true, channel: data }); } if (req.method === "GET" && path === "/channel/live") { const q = url.searchParams.get("url") || url.searchParams.get("id") || url.searchParams.get("q") || ""; const data = await getChannelBundle(q, { includeLive: true, includeVideos: false }); return send(res, 200, { ok: true, channel_id: data.channel_id, title: data.title, handle: data.handle, is_live: data.is_live, live: data.live, upcoming: data.upcoming, url: data.url, }); } if (req.method === "GET" && path === "/video") { const q = url.searchParams.get("url") || url.searchParams.get("id") || url.searchParams.get("q") || ""; const data = await getVideoInfo(q); return send(res, 200, { ok: true, video: data }); } if (req.method === "GET" && path === "/search") { const q = url.searchParams.get("q") || url.searchParams.get("query") || ""; const limit = Math.min(30, Math.max(1, Number(url.searchParams.get("limit") || 12))); const data = await searchVideos(q, limit); return send(res, 200, { ok: true, ...data }); } if (req.method === "POST" && path === "/batch/live") { const body = await readJson(req); const items = Array.isArray(body.items) ? body.items : Array.isArray(body.urls) ? body.urls : []; const out = []; for (const item of items.slice(0, 20)) { const key = typeof item === "string" ? item : item?.url || item?.channel_url || item?.id || ""; try { const data = await getChannelBundle(key, { includeLive: true, includeVideos: false }); out.push({ input: key, ok: true, channel_id: data.channel_id, title: data.title, handle: data.handle, is_live: data.is_live, live: data.live, url: data.url, }); } catch (e) { out.push({ input: key, ok: false, error: String(e?.message || e).slice(0, 300) }); } } return send(res, 200, { ok: true, results: out }); } return send(res, 404, { ok: false, error: "not found" }); } catch (e) { return send(res, 400, { ok: false, error: String(e?.message || e).slice(0, 500) }); } } const server = http.createServer((req, res) => { handle(req, res).catch((e) => { send(res, 500, { ok: false, error: String(e?.message || e).slice(0, 500) }); }); }); server.listen(PORT, HOST, () => { console.log(`[youtube-svc] listening on http://${HOST}:${PORT}`); });