diff options
| author | Alex Pooley (@zuedev) <zuedev@gmail.com> | 2025-07-04 02:42:29 +0100 |
|---|---|---|
| committer | Alex Pooley (@zuedev) <zuedev@gmail.com> | 2025-07-04 02:42:29 +0100 |
| commit | d020a5ca4e616012c8744ab0e12c65ca729dafe9 (patch) | |
| tree | 055af576e6b9e23847522325d318145ceb567349 /source/main.js | |
| parent | 673a2805cd8f5a0ed365b20fe1624bc7979f1953 (diff) | |
| download | zue.dev-d020a5ca4e616012c8744ab0e12c65ca729dafe9.tar zue.dev-d020a5ca4e616012c8744ab0e12c65ca729dafe9.tar.gz zue.dev-d020a5ca4e616012c8744ab0e12c65ca729dafe9.tar.bz2 zue.dev-d020a5ca4e616012c8744ab0e12c65ca729dafe9.tar.xz zue.dev-d020a5ca4e616012c8744ab0e12c65ca729dafe9.zip | |
refactor: remove Twitch channel live check and update response handling
Diffstat (limited to 'source/main.js')
| -rw-r--r-- | source/main.js | 55 |
1 files changed, 11 insertions, 44 deletions
diff --git a/source/main.js b/source/main.js index 46fef97..1eb55d3 100644 --- a/source/main.js +++ b/source/main.js @@ -81,26 +81,6 @@ export default { })(); case "/96/twitch/streaming": return (async () => { - /* - 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"); @@ -117,31 +97,12 @@ export default { } ); - const whitelist = [ - "zuedev", - ...["vtsweets", "bunnibana", "yayjaybae", "justawoney", "tygiwygi"], - ]; + const channelWhitelist = ["@vtsweets"]; - if (!whitelist.includes(channel)) + if (!channelWhitelist.includes(channel)) return new Response( JSON.stringify({ - error: `channel not whitelisted`, - }), - { - headers: { - "Access-Control-Allow-Origin": "*", - "Content-Type": "application/json", - }, - } - ); - - const channelLive = await isTwitchChannelLive(channel); - - if (channelLive) - return new Response( - JSON.stringify({ - status: "live", - channel, + error: `channel not allowed`, }), { headers: { @@ -153,7 +114,7 @@ export default { return new Response( JSON.stringify({ - status: "offline", + latestVideoId, }), { headers: { @@ -171,6 +132,7 @@ export default { const fullPage = searchParams.get("fullPage") !== null; const width = parseInt(searchParams.get("width"), 10) || 1920; const height = parseInt(searchParams.get("height"), 10) || 1080; + const delay = parseInt(searchParams.get("delay"), 10) || 0; if (!url) return router.respond({ @@ -185,7 +147,12 @@ export default { const browser = await puppeteer.launch(environment.MYBROWSER); const page = await browser.newPage(); await page.setViewport({ width, height }); - await page.goto(url); + await page.goto(url, { + waitUntil: "networkidle2", + }); + if (delay > 0) { + await new Promise((resolve) => setTimeout(resolve, delay)); + } const screenshot = await page.screenshot({ type, fullPage, |
