Repository navigation
Expand file tree
/
Copy pathlive.js
More file actions
69 lines (59 loc) · 2.03 KB
/
Copy pathlive.js
File metadata and controls
69 lines (59 loc) · 2.03 KB
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
// YouTube Live Stream Detection API
// Uses YouTube's oembed endpoint to check for live streams
export const config = {
runtime: 'edge',
};
export default async function handler(request) {
const url = new URL(request.url);
const channel = url.searchParams.get('channel');
if (!channel) {
return new Response(JSON.stringify({ error: 'Missing channel parameter' }), {
status: 400,
headers: { 'Content-Type': 'application/json' },
});
}
try {
// Try to fetch the channel's live page
const channelHandle = channel.startsWith('@') ? channel : `@${channel}`;
const liveUrl = `https://www.youtube.com/${channelHandle}/live`;
const response = await fetch(liveUrl, {
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 }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
}
const html = await response.text();
// Extract video ID from the page
const videoIdMatch = html.match(/"videoId":"([a-zA-Z0-9_-]{11})"/);
const isLiveMatch = html.match(/"isLive":\s*true/);
if (videoIdMatch && isLiveMatch) {
return new Response(JSON.stringify({ videoId: videoIdMatch[1], isLive: true }), {
status: 200,
headers: {
'Content-Type': 'application/json',
'Cache-Control': 'public, s-maxage=300', // Cache for 5 minutes
},
});
}
// Return null if no live stream found
return new Response(JSON.stringify({ videoId: null, isLive: false }), {
status: 200,
headers: {
'Content-Type': 'application/json',
'Cache-Control': 'public, s-maxage=300',
},
});
} catch (error) {
console.error('YouTube live check error:', error);
return new Response(JSON.stringify({ videoId: null, error: error.message }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
}
}