aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Pooley (@zuedev) <zuedev@gmail.com>2025-06-23 21:55:15 +0100
committerAlex Pooley (@zuedev) <zuedev@gmail.com>2025-06-23 21:55:15 +0100
commitcfed7ff345511002d2154debeff5c2c5cc53a7d6 (patch)
tree22a5cd3f4079595192a6814e272c6d05434c4ee8
parent931d18bbae8982403415a01af16908e70390b106 (diff)
downloadzue.dev-cfed7ff345511002d2154debeff5c2c5cc53a7d6.tar
zue.dev-cfed7ff345511002d2154debeff5c2c5cc53a7d6.tar.gz
zue.dev-cfed7ff345511002d2154debeff5c2c5cc53a7d6.tar.bz2
zue.dev-cfed7ff345511002d2154debeff5c2c5cc53a7d6.tar.xz
zue.dev-cfed7ff345511002d2154debeff5c2c5cc53a7d6.zip
add function to check if a Twitch channel is live
-rw-r--r--source/index.js40
1 files changed, 20 insertions, 20 deletions
diff --git a/source/index.js b/source/index.js
index 2fbb0d5..ad3472a 100644
--- a/source/index.js
+++ b/source/index.js
@@ -56,6 +56,26 @@ export default {
});
router.add("/96/twitch/streaming", async (request) => {
+ /*
+ 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;
+ }
+
const url = new URL(request.url);
const channel = url.searchParams.get("channel");
@@ -114,23 +134,3 @@ export default {
console.log("Scheduled event triggered!");
},
};
-
-/*
- 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;
-}