diff options
| author | Alex Pooley <zuedev@gmail.com> | 2025-03-21 22:07:38 +0000 |
|---|---|---|
| committer | Alex Pooley <zuedev@gmail.com> | 2025-03-21 22:07:38 +0000 |
| commit | c77ec36641828e0b8699b60cc21fd12f5e1a6212 (patch) | |
| tree | 525325b9860df6638544316f280f644e0c4c0538 /source/index.js | |
| parent | 4c6ce43e59056aee6a51ba821c9d0d8d2f03af1f (diff) | |
| download | zue.dev-c77ec36641828e0b8699b60cc21fd12f5e1a6212.tar zue.dev-c77ec36641828e0b8699b60cc21fd12f5e1a6212.tar.gz zue.dev-c77ec36641828e0b8699b60cc21fd12f5e1a6212.tar.bz2 zue.dev-c77ec36641828e0b8699b60cc21fd12f5e1a6212.tar.xz zue.dev-c77ec36641828e0b8699b60cc21fd12f5e1a6212.zip | |
add `96/twitch/streaming` endpoint
Diffstat (limited to 'source/index.js')
| -rw-r--r-- | source/index.js | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/source/index.js b/source/index.js index 9d4a9e2..a33092a 100644 --- a/source/index.js +++ b/source/index.js @@ -1,3 +1,5 @@ +import talent96 from "./data/talent96.js"; + export default { async fetch(request, environment, context) { const url = new URL(request.url); @@ -41,6 +43,33 @@ export default { error: `service not found`, }); + // check if twitch user is streaming from a whitelist + case "/96/twitch/streaming": + const channel = url.searchParams.get("channel"); + + if (!channel) + return Respond({ + error: `channel not provided`, + }); + + const whitelist = ["zuedev", ...talent96]; + + if (!whitelist.includes(channel)) + return Respond({ + error: `channel not whitelisted`, + }); + + const channelLive = await isTwitchChannelLive(channel); + + if (channelLive) + return Respond({ + status: "live", + }); + + return Respond({ + status: "offline", + }); + // default case default: return Respond({ @@ -58,3 +87,23 @@ function Respond(body) { }, }); } + +/* + Checks if a twitch channel is live by fetching the "live" preview image of the channel, + if the image is fetched successfully, then the channel is live, otherwise it's offline. + + @param {string} channel - the twitch channel name + @returns {boolean} true if the channel is live, false otherwise +*/ +async function isTwitchChannelLive(channel) { + // construct preview image url with channel name + const livePreviewUrl = `https://static-cdn.jtvnw.net/previews-ttv/live_user_${channel}-320x180.jpg`; + + // fetch the preview image, don't follow redirects + const response = await fetch(livePreviewUrl, { + redirect: "manual", + }); + + // check if the image was fetched successfully + return response.ok; +} |
