TheDusty01

Twitch Stream Check

Oct 5th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Rewrite to Twitch Helix API Version, the original script is written by someone else
  2.  
  3. var apiKey = "YourApiKey";
  4. var username = "YourChannelName";
  5. var twitch_widget = $("#livestream, #livestream-button");
  6. var recntStatChange = 0;
  7. var visitOffline = 0;
  8. var long = 60000;
  9. var short = 5000;
  10.  
  11. function refresh() {
  12.     handleTwitchStream();
  13. }
  14.  
  15. function notRecnt() {
  16.     if (recntStatChange == 0) {
  17.         recntStatChange = 1;
  18.     } else {
  19.         if (recntStatChange == 1) {
  20.             recntStatChange = 0;
  21.         }
  22.     }
  23. }
  24.  
  25. function recntAndRefresh() {
  26.     handleTwitchStream();
  27.     notRecnt();
  28. }
  29.  
  30. function handleTwitchStream() {
  31.     twitch_widget.attr(`href`, `https://twitch.tv/${username}`);
  32.  
  33.     $.ajax({
  34.         url: `https://api.twitch.tv/helix/streams?user_login=${username}`,
  35.         type: `GET`,
  36.         dataType: `json`,
  37.         headers: {
  38.             "Client-ID": apiKey
  39.         },
  40.         success: (res) => {
  41.             if (res.data.length > 0) {
  42.                 if (recntStatChange == 0) {
  43.                     showStreamElements();
  44.                     visitOffline = 1;
  45.                     setTimeout(recntAndRefresh, long);
  46.                 }
  47.                 if (recntStatChange == 1) {
  48.                     setTimeout(refresh, short);
  49.                     //Refresh period set to short, streaming
  50.                 }
  51.             }
  52.             else {
  53.                 if (recntStatChange == 1) {
  54.                     hideStreamElements();
  55.                     setTimeout(recntAndRefresh, long);
  56.                 }
  57.                 if (recntStatChange == 0) {
  58.                     if (visitOffline == 0) {
  59.                         visitOffline = 1;
  60.                         setTimeout(refresh, long/2);
  61.                     }
  62.                     else {
  63.                         setTimeout(refresh, short);
  64.                     }
  65.                 }
  66.             }
  67.         },
  68.         error: hideStreamElements
  69.     });
  70. }
  71.  
  72. function hideStreamElements() {
  73.     $("#livestream, #livestream-button, #livestream iframe").addClass("hidetwitch");
  74.     $iframe = $("#livestream, #livestream-button").find("iframe");
  75.     $iframe.attr("src", $iframe.attr("src"));
  76. }
  77.  
  78. function showStreamElements() {
  79.     $("#livestream, #livestream-button, #livestream iframe").removeClass("hidetwitch");
  80. }
Add Comment
Please, Sign In to add comment