aboutsummaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
Diffstat (limited to 'source')
-rw-r--r--source/main.js107
1 files changed, 29 insertions, 78 deletions
diff --git a/source/main.js b/source/main.js
index 7eb433e..46fef97 100644
--- a/source/main.js
+++ b/source/main.js
@@ -165,87 +165,38 @@ export default {
})();
case "/browser-rendering/screenshot":
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",
+ const { searchParams } = new URL(request.url);
+ const url = searchParams.get("url");
+ const type = searchParams.get("type") || "webp";
+ const fullPage = searchParams.get("fullPage") !== null;
+ const width = parseInt(searchParams.get("width"), 10) || 1920;
+ const height = parseInt(searchParams.get("height"), 10) || 1080;
+
+ if (!url)
+ return router.respond({
+ error: "URL parameter is required",
});
- // check if the image was fetched successfully
- return response.ok;
- }
-
- const url = new URL(request.url);
- const channel = url.searchParams.get("channel");
-
- if (!channel)
- return new Response(
- JSON.stringify({
- error: `channel not provided`,
- }),
- {
- headers: {
- "Access-Control-Allow-Origin": "*",
- "Content-Type": "application/json",
- },
- }
- );
-
- const whitelist = [
- "zuedev",
- ...["vtsweets", "bunnibana", "yayjaybae", "justawoney", "tygiwygi"],
- ];
-
- if (!whitelist.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,
- }),
- {
- headers: {
- "Access-Control-Allow-Origin": "*",
- "Content-Type": "application/json",
- },
- }
- );
+ if (!/^https?:\/\//.test(url))
+ return router.respond({
+ error: "Invalid URL format",
+ });
- return new Response(
- JSON.stringify({
- status: "offline",
- }),
- {
- headers: {
- "Access-Control-Allow-Origin": "*",
- "Content-Type": "application/json",
- },
- }
- );
+ const browser = await puppeteer.launch(environment.MYBROWSER);
+ const page = await browser.newPage();
+ await page.setViewport({ width, height });
+ await page.goto(url);
+ const screenshot = await page.screenshot({
+ type,
+ fullPage,
+ });
+ await browser.close();
+
+ return new Response(screenshot, {
+ headers: {
+ "content-type": `image/${type}`,
+ },
+ });
})();
default:
return new Response(