import express from "express"; import dotenv from "dotenv"; import path from "path"; import morgan from "morgan"; import { createStream } from "rotating-file-stream"; const usernames = [ {name: "rubylium__", status: "off", id: ""}, {name: "lukkstv", status: "off", id: ""}, {name: "havvenfr", status: "off", id: ""}, {name: "olmanntv", status: "off", id: ""}, {name: "nina_asmr", status: "off", id: ""}, {name: "krw_aka", status: "off", id: ""}, {name: "leetgamer54", status: "off", id: ""}, {name: "coshiho", status: "off", id: ""}, {name: "soramicia", status: "off", id: ""}, {name: "entocraft", status: "off", id: ""}, {name: "steakfritees_", status: "off", id: ""}, {name: "dancyy_", status: "off", id: ""}, {name: "tchoupyy_", status: "off", id: ""}, {name: "thesollann", status: "off", id: ""}, ]; dotenv.config(); const app = express(); const accessLogStream = createStream("access.log", { interval: "1d", path: path.join(path.resolve(), "log"), }); app.use(morgan("combined", { stream: accessLogStream })); const port = process.env.PORT || 3000; let token = ""; let config = {}; const checkEnvVars = () => { const requiredEnvVars = ["CLIENT_ID", "CLIENT_SECRET"]; let error = false; requiredEnvVars.forEach((envVar) => { if (process.env[envVar] === undefined) { error = true; console.log(`${envVar} is undefined`); } else { config[envVar] = process.env[envVar]; } }); if (error) { process.exit(1); } }; checkEnvVars(); function init() { const url = `https://id.twitch.tv/oauth2/token?client_id=${config.CLIENT_ID}&client_secret=${config.CLIENT_SECRET}&grant_type=client_credentials`; fetch(url, { method: "POST" }) .then((response) => response.json()) .then((data) => { token = data.access_token; getAllUsersStatus(); setInterval(getAllUsersStatus, 60000); }); } init(); async function getAllUsersStatus() { for (let i = 0; i < usernames.length; i++) { const userData = await getUserStatus(usernames[i].name, usernames[i].id); usernames[i] = { ...usernames[i], ...userData }; if (i === usernames.length - 1) { console.log("Successfully updated all users"); } } } app.use("/api/status", api); function api(req, res, next) { res.json(usernames); } async function getUserStatus(username, id) { const userData = {}; if (id === "") { const userUrl = `https://api.twitch.tv/helix/users?login=${username}`; const response = await fetch(userUrl, { headers: { Authorization: `Bearer ${token}`, "Client-ID": config.CLIENT_ID, }, }); const data = await response.json(); userData.id = data.data[0].id; userData.profilePicture = data.data[0].profile_image_url; id = data.data[0].id; } const streamUrl = `https://api.twitch.tv/helix/streams?user_id=${id}`; const streamResponse = await fetch(streamUrl, { headers: { Authorization: `Bearer ${token}`, "Client-ID": config.CLIENT_ID, }, }); const streamData = await streamResponse.json(); if (streamData.data.length > 0) { userData.status = "live"; } else { userData.status = "offline"; } return userData; } const staticDist = express.static(path.join(path.resolve(), "./public")); app.use(staticDist); const server = app.listen(port, () => { console.log(`Server listening on port ${port}`); }); server.on("error", (err) => { console.error(err); });