Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var fetch = require('node-fetch');
- var esaGameId = "";
- var bsgGameId = "";
- var esaGame = "";
- var esaTitle = "";
- var bsgTitle = "";
- var interval = "2"; //in minutes
- logic();
- setInterval(function() {
- logic();
- }, interval * 60 * 1000);
- function logic() {
- getGame();
- setTimeout(function() {
- if (esaGameId !== bsgGameId) {
- setGame();
- startAd();
- } else {
- console.log("No update");
- }
- }, (2 * 1000));
- }
- function getGame() { //scope channel_editor
- fetch('https://api.twitch.tv/helix/channels?broadcaster_id=54739364', {
- method: 'GET',
- headers: {
- 'Authorization': 'Bearer xxx',
- 'Client-Id': 'xxx'
- }
- })
- .then(res => res.json())
- .then(res => {
- esaGame = res.data[0].game_name;
- esaGameId = res.data[0].game_id;
- esaTitle = res.data[0].title;
- console.log("ESA Game saved: " + esaGameId + " " + res.data[0].game_name);
- });
- fetch('https://api.twitch.tv/helix/channels?broadcaster_id=30685577', {
- method: 'GET',
- headers: {
- 'Authorization': 'Bearer xxx',
- 'Client-Id': 'xxx'
- }
- })
- .then(res => res.json())
- .then(res => {
- bsgGameId = res.data[0].game_id;
- bsgTitle = res.data[0].title;
- console.log("BSG Game saved: " + bsgGameId + ' ' + res.data[0].game_name);
- });
- }
- function setGame() {
- postRequest('https://api.twitch.tv/helix/channels?broadcaster_id=30685577')
- .then(data => console.log(""))
- // .catch(error => console.error(error))
- .catch(error => console.error())
- function postRequest(url, data) {
- return fetch(url, {
- method: 'PATCH', // 'GET', 'PUT', 'DELETE', etc.
- body: '{"game_id": "' + esaGameId + '", "title": "' + esaTitle + '"}', // Coordinate the body type with 'Content-Type'
- headers: {
- 'Client-ID': 'xxx',
- 'Authorization': 'Bearer xxx', //scope channel:manage:broadcast
- // 'Accept': 'application/vnd.twitchtv.v5+json',
- 'Content-Type': 'application/json'
- },
- })
- .then(response => response.json())
- }
- console.log("UPDATE: Changed BSG game to " + esaGameId + ' ' + esaGame);
- }
- function startAd() {
- postRequest('https://api.twitch.tv/helix/channels/commercial')
- .then(data => console.log(""))
- .catch(error => console.error(error))
- console.log('Sent commercial of 180 seconds');
- function postRequest(url, data) {
- return fetch(url, {
- method: 'POST', // 'GET', 'PUT', 'DELETE', etc.
- body: '{"broadcaster_id": "30685577", "length": 180}', // Coordinate the body type with 'Content-Type'
- headers: {
- 'Client-ID': 'xxx',
- 'Authorization': 'Bearer xxx', //scope channel:edit:commercial
- // 'Accept': 'application/vnd.twitchtv.v5+json',
- 'Content-Type': 'application/json'
- },
- })
- .then(response => response.json())
- .then(res => {
- console.log(res)
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement