File size: 5,157 Bytes
3464008 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | // YouTube Live Stream Detection API
// Proxies to Railway relay which uses residential proxy for YouTube scraping
import { getCorsHeaders, isDisallowedOrigin } from '../_cors.js';
import { getRelayBaseUrl, getRelayHeaders } from '../_relay.js';
export const config = { runtime: 'edge' };
export default async function handler(request) {
const cors = getCorsHeaders(request);
if (request.method === 'OPTIONS') return new Response(null, { status: 204, headers: cors });
if (isDisallowedOrigin(request)) {
return new Response(JSON.stringify({ error: 'Origin not allowed' }), { status: 403, headers: cors });
}
const url = new URL(request.url);
const channel = url.searchParams.get('channel');
const videoIdParam = url.searchParams.get('videoId');
const params = new URLSearchParams();
if (channel) params.set('channel', channel);
if (videoIdParam) params.set('videoId', videoIdParam);
const qs = params.toString();
if (!qs) {
return new Response(JSON.stringify({ error: 'Missing channel or videoId parameter' }), {
status: 400,
headers: { ...cors, 'Content-Type': 'application/json' },
});
}
// Proxy to Railway relay
const relayBase = getRelayBaseUrl();
if (relayBase) {
try {
const relayHeaders = getRelayHeaders({ 'User-Agent': 'WorldMonitor-Edge/1.0' });
const relayRes = await fetch(`${relayBase}/youtube-live?${qs}`, { headers: relayHeaders });
if (relayRes.ok) {
const data = await relayRes.json();
const cacheTime = videoIdParam ? 3600 : 600;
return new Response(JSON.stringify(data), {
status: 200,
headers: {
...cors,
'Content-Type': 'application/json',
'Cache-Control': `public, max-age=${cacheTime}, s-maxage=${cacheTime}, stale-while-revalidate=60`,
},
});
}
} catch { /* relay unavailable — fall through to direct fetch */ }
}
// Fallback: direct fetch (works for oembed, limited for live detection from datacenter IPs)
if (videoIdParam && /^[A-Za-z0-9_-]{11}$/.test(videoIdParam)) {
try {
const oembedRes = await fetch(
`https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=${videoIdParam}&format=json`,
{ headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' } },
);
if (oembedRes.ok) {
const data = await oembedRes.json();
return new Response(JSON.stringify({ channelName: data.author_name || null, title: data.title || null, videoId: videoIdParam }), {
status: 200,
headers: { ...cors, 'Content-Type': 'application/json', 'Cache-Control': 'public, max-age=3600, s-maxage=3600' },
});
}
} catch { /* oembed failed — return minimal response */ }
return new Response(JSON.stringify({ channelName: null, title: null, videoId: videoIdParam }), {
status: 200,
headers: { ...cors, 'Content-Type': 'application/json' },
});
}
if (!channel) {
return new Response(JSON.stringify({ error: 'Missing channel parameter' }), {
status: 400,
headers: { ...cors, 'Content-Type': 'application/json' },
});
}
// Fallback: direct scrape (limited from datacenter IPs)
try {
const channelHandle = channel.startsWith('@') ? channel : `@${channel}`;
const response = await fetch(`https://www.youtube.com/${channelHandle}/live`, {
headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' },
redirect: 'follow',
});
if (!response.ok) {
return new Response(JSON.stringify({ videoId: null, channelExists: false }), {
status: 200, headers: { ...cors, 'Content-Type': 'application/json' },
});
}
const html = await response.text();
const channelExists = html.includes('"channelId"') || html.includes('og:url');
let channelName = null;
const ownerMatch = html.match(/"ownerChannelName"\s*:\s*"([^"]+)"/);
if (ownerMatch) channelName = ownerMatch[1];
else { const am = html.match(/"author"\s*:\s*"([^"]+)"/); if (am) channelName = am[1]; }
let videoId = null;
const detailsIdx = html.indexOf('"videoDetails"');
if (detailsIdx !== -1) {
const block = html.substring(detailsIdx, detailsIdx + 5000);
const vidMatch = block.match(/"videoId":"([a-zA-Z0-9_-]{11})"/);
const liveMatch = block.match(/"isLive"\s*:\s*true/);
if (vidMatch && liveMatch) videoId = vidMatch[1];
}
let hlsUrl = null;
const hlsMatch = html.match(/"hlsManifestUrl"\s*:\s*"([^"]+)"/);
if (hlsMatch && videoId) hlsUrl = hlsMatch[1].replace(/\\u0026/g, '&');
return new Response(JSON.stringify({ videoId, isLive: videoId !== null, channelExists, channelName, hlsUrl }), {
status: 200,
headers: { ...cors, 'Content-Type': 'application/json', 'Cache-Control': 'public, max-age=300, s-maxage=600, stale-while-revalidate=120' },
});
} catch {
return new Response(JSON.stringify({ videoId: null, error: 'Failed to fetch channel data' }), {
status: 200, headers: { ...cors, 'Content-Type': 'application/json' },
});
}
}
|