import fs from 'node:fs'; import fetch from 'node-fetch'; const botBearerFile = process.env.BOT_BEARER_FILE; const userBearerFile = process.env.USER_BEARER_FILE; const instance = process.env.INSTANCE; const videoId = process.env.VIDEO_ID; const botBearer = fs.readFileSync(botBearerFile, 'utf-8'); const userBearer = fs.readFileSync(userBearerFile, 'utf-8'); const globalState = { exit: false, currentlyLive: false, }; const headers = (bearer) => ({ accept: '*/*', authorization: `Bearer ${bearer}`, 'cache-control': 'no-cache', 'content-type': 'application/json', pragma: 'no-cache', }); const post = ({ status, spoiler_text, visibility = 'direct', content_type = 'text/plain', }) => fetch(`https://${instance}/api/v1/statuses`, { headers: headers(botBearer), referrerPolicy: 'same-origin', body: JSON.stringify({ spoiler_text, status, source: "HJ's twitch bot", visibility, content_type, }), method: 'POST', mode: 'cors', credentials: 'include', }); const scrobble = ({ title, external_link }) => fetch(`https://${instance}/api/v1/pleroma/scrobble`, { headers: headers(userBearer), referrerPolicy: 'same-origin', body: JSON.stringify({ title, external_link, }), method: 'POST', mode: 'cors', credentials: 'include', }); // await post({ // spoiler_text: 'Debug', // status: '@hj twitch bot initialized', // }); process.on('SIGINT', () => { globalState.exit = true; process.exit(); }); const loop = async () => { const response = await fetch( `https://tube.ebin.club/api/v1/videos/${videoId}`, ); const { state } = await response.json(); const isLive = state.id === 1; // published console.info('Live: ', isLive, globalState.currentlyLive); const stateChange = isLive !== globalState.currentlyLive; console.info('State change', stateChange); globalState.currentlyLive = isLive; console.info('State: ', state); if (stateChange) { if (isLive) { console.info('Went live!'); console.info(process.env); const postResult = await post({ spoiler_text: 'Stream announcment', status: [ 'hj just went live on peertube', '', `https://tube.ebin.club/w/${videoId}`, ].join('\n'), visibility: 'public', }); console.info(postResult); const scrobbleResult = await scrobble({ title: 'Streaming on PeerTube', external_link: `https://tube.ebin.club/w/${videoId}`, }); console.info(scrobbleResult); } else { console.info('Went offline...'); await scrobble({ title: 'Stream over', }); } } if (!globalState.exit) setTimeout(loop, 30 * 1000); }; loop(); console.info('Ready!');