hj-stream-bot/index.js

107 lines
2.4 KiB
JavaScript
Raw Normal View History

2021-06-09 13:22:07 +03:00
import fetch from 'node-fetch';
2026-01-14 15:24:40 +02:00
const botBearer = process.env.BOT_BEARER;
2026-01-13 22:16:12 +02:00
const userBearer = process.env.USER_BEARER;
const instance = process.env.INSTANCE;
const videoId = process.env.VIDEO_ID;
2021-06-09 13:22:07 +03:00
2026-01-14 15:14:14 +02:00
const globalState = {
exit: false,
currentlyLive: false,
};
2021-06-09 13:22:07 +03:00
2026-01-13 22:16:12 +02:00
const headers = (bearer) => ({
accept: '*/*',
authorization: `Bearer ${bearer}`,
'cache-control': 'no-cache',
'content-type': 'application/json',
pragma: 'no-cache',
2021-06-09 13:22:07 +03:00
});
2026-01-13 22:16:12 +02:00
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',
});
2021-06-09 13:22:07 +03:00
2026-01-13 22:16:12 +02:00
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',
});
2021-06-09 13:22:07 +03:00
2026-01-13 22:16:12 +02:00
// await post({
// spoiler_text: 'Debug',
// status: '@hj twitch bot initialized',
// });
2021-06-09 13:22:07 +03:00
2026-01-13 22:16:12 +02:00
process.on('SIGINT', () => {
2026-01-14 15:14:14 +02:00
globalState.exit = true;
2026-01-13 22:16:12 +02:00
process.exit();
});
2021-06-09 14:19:48 +03:00
2026-01-13 22:16:12 +02:00
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
2026-01-14 15:40:26 +02:00
constole.info('Live: ', isLive, currentlyLive)
2026-01-13 22:16:12 +02:00
const stateChange = isLive !== globalState.currentlyLive;
2026-01-14 15:40:26 +02:00
constole.info('State change', stateChange)
2026-01-13 22:16:12 +02:00
globalState.currentlyLive = isLive;
2021-06-09 13:22:07 +03:00
2026-01-14 15:40:26 +02:00
constole.info('State: ', state)
2026-01-13 22:16:12 +02:00
if (stateChange) {
if (isLive) {
2026-01-14 15:40:26 +02:00
constole.info('Went live!')
2021-06-09 13:22:07 +03:00
await post({
spoiler_text: 'Stream announcment',
2026-01-13 22:16:12 +02:00
status: [
2026-01-13 22:16:52 +02:00
'hj just went live on peertube',
2026-01-13 22:16:12 +02:00
'',
`https://tube.ebin.club/w/${videoId}`,
].join('\n'),
visibility: 'public',
});
await scrobble({
title: 'Streaming on PeerTube',
external_link: `https://tube.ebin.club/w/${videoId}`,
});
} else {
2026-01-14 15:40:26 +02:00
constole.info('Went offline...')
2026-01-13 22:16:12 +02:00
await scrobble({
title: 'Stream over',
});
2021-06-09 13:22:07 +03:00
}
}
2026-01-14 15:14:14 +02:00
if (!globalState.exit) setTimeout(loop, 30 * 1000);
2026-01-13 22:16:12 +02:00
};
loop();
console.info('Ready!');