Advertisement
Riekelt

copycat

Mar 5th, 2022
927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var fetch = require('node-fetch');
  2. var esaGameId = "";
  3. var bsgGameId = "";
  4. var esaGame = "";
  5. var esaTitle = "";
  6. var bsgTitle = "";
  7. var interval = "2"; //in minutes
  8.  
  9.  
  10. logic();
  11. setInterval(function() {
  12.   logic();
  13. }, interval * 60 * 1000);
  14.  
  15.  
  16. function logic() {
  17.   getGame();
  18.   setTimeout(function() {
  19.     if (esaGameId !== bsgGameId) {
  20.       setGame();
  21.       startAd();
  22.     } else {
  23.       console.log("No update");
  24.     }
  25.   }, (2 * 1000));
  26. }
  27.  
  28. function getGame() { //scope channel_editor
  29.   fetch('https://api.twitch.tv/helix/channels?broadcaster_id=54739364', {
  30.       method: 'GET',
  31.       headers: {
  32.         'Authorization': 'Bearer xxx',
  33.         'Client-Id': 'xxx'
  34.       }
  35.     })
  36.     .then(res => res.json())
  37.     .then(res => {
  38.       esaGame = res.data[0].game_name;
  39.       esaGameId = res.data[0].game_id;
  40.       esaTitle = res.data[0].title;
  41.       console.log("ESA Game saved: " + esaGameId + " " + res.data[0].game_name);
  42.     });
  43.  
  44.   fetch('https://api.twitch.tv/helix/channels?broadcaster_id=30685577', {
  45.       method: 'GET',
  46.       headers: {
  47.         'Authorization': 'Bearer xxx',
  48.         'Client-Id': 'xxx'
  49.       }
  50.     })
  51.     .then(res => res.json())
  52.     .then(res => {
  53.       bsgGameId = res.data[0].game_id;
  54.       bsgTitle = res.data[0].title;
  55.       console.log("BSG Game saved: " + bsgGameId + ' ' + res.data[0].game_name);
  56.     });
  57. }
  58.  
  59. function setGame() {
  60.   postRequest('https://api.twitch.tv/helix/channels?broadcaster_id=30685577')
  61.     .then(data => console.log(""))
  62.     // .catch(error => console.error(error))
  63.     .catch(error => console.error())
  64.  
  65.   function postRequest(url, data) {
  66.     return fetch(url, {
  67.         method: 'PATCH', // 'GET', 'PUT', 'DELETE', etc.
  68.         body: '{"game_id": "' + esaGameId + '", "title": "' + esaTitle + '"}', // Coordinate the body type with 'Content-Type'
  69.         headers: {
  70.           'Client-ID': 'xxx',
  71.           'Authorization': 'Bearer xxx', //scope channel:manage:broadcast
  72.           // 'Accept': 'application/vnd.twitchtv.v5+json',
  73.           'Content-Type': 'application/json'
  74.         },
  75.       })
  76.       .then(response => response.json())
  77.   }
  78.   console.log("UPDATE: Changed BSG game to " + esaGameId + ' ' + esaGame);
  79. }
  80.  
  81. function startAd() {
  82.   postRequest('https://api.twitch.tv/helix/channels/commercial')
  83.     .then(data => console.log(""))
  84.     .catch(error => console.error(error))
  85.     console.log('Sent commercial of 180 seconds');
  86.  
  87.   function postRequest(url, data) {
  88.     return fetch(url, {
  89.         method: 'POST', // 'GET', 'PUT', 'DELETE', etc.
  90.         body: '{"broadcaster_id": "30685577", "length": 180}', // Coordinate the body type with 'Content-Type'
  91.         headers: {
  92.           'Client-ID': 'xxx',
  93.           'Authorization': 'Bearer xxx', //scope channel:edit:commercial
  94.           // 'Accept': 'application/vnd.twitchtv.v5+json',
  95.           'Content-Type': 'application/json'
  96.         },
  97.       })
  98.       .then(response => response.json())
  99.       .then(res => {
  100.         console.log(res)
  101.       });
  102.   }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement