Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
interface Platform {
id: string;
isLive: (username: string) => Promise<boolean>;
getChannelLink: (username: string) => string;
}
const platforms: Platform[] = [
{
id: "twitch",
getChannelLink: (username: string) => `https://twitch.tv/${username}`,
isLive: async (username: string) => {
const response = await fetch(`https://twitch.tv/${username}`);
const sourceCode = await response.text();
return sourceCode.includes("isLiveBroadcast");
},
},
{
id: "youtube",
getChannelLink: (username: string) => `https://youtube.com/c/${username}/live`,
isLive: async (username: string) => {
const livePageResponse = await fetch(
`https://www.youtube.com/c/${username}/live`,
);
return livePageResponse.status === 200;
},
},
];
export default async function(req: Request): Promise<Response> {
const url = new URL(req.url);
const platformQuery = url.searchParams.get("platform");
const userQuery = url.searchParams.get("username");
if (!platformQuery) {
return Response.json({ error: "`platform` query parameter is required" }, {
status: 400,
});
}
if (!userQuery) {
return Response.json({ error: "`username` query parameter is required" }, {
status: 400,
});
}
const platform = platforms.find((p) => p.id === platformQuery);
if (!platform) {
return Response.json({ error: `${platformQuery} is not a supported platform` }, {
status: 400,
});
}
const link = platform.getChannelLink(userQuery);
const isLive = await platform.isLive(userQuery);
return Response.json({ link, isLive });
}
triozer-livestatus.web.val.run
July 19, 2024