Advertisement
Guest User

Untitled

a guest
Sep 11th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. var stream = "https://wind-bow.glitch.me/twitch-api/streams/";
  2. var streamList = ['freecodecamp', 'ESL_SC2', 'test_channel'];
  3. var count = 0;
  4.  
  5. //build stream URL and populate HTML elements with stream data.
  6. function statusCheck(count) {
  7. stream = "https://wind-bow.glitch.me/twitch-api/streams/";
  8. stream = stream + streamList[count];
  9.  
  10. //check to see if stream is live
  11. $.getJSON(stream, function(data) {
  12. var status;
  13. if(data.stream === null) {
  14. populate(status);
  15. } else if(data.stream !== null){
  16. status = 'live';
  17. populate(status);
  18. }
  19. });
  20. }
  21. //pass the status to this function to populate the HTML
  22. function populate(status) {
  23.  
  24. //if the channel is not live, retrieve the data from the users URL then populate
  25. if(status === undefined) {
  26.  
  27. stream = "https://wind-bow.glitch.me/twitch-api/users/";
  28. stream = stream + streamList[count];
  29.  
  30. $.getJSON(stream, function(data) {
  31. var streamName = data.display_name;
  32. var div = document.createElement('div');
  33. var mainDiv = document.getElementById('main');
  34.  
  35. div.setAttribute('class', 'stream');
  36. div.innerHTML = "<img class='logo' src = '" + data.logo + "'>" + streamName + ' Offline';
  37. mainDiv.appendChild(div);
  38. });
  39. count++
  40. console.log(status);
  41. //if the channel is live, populate the HTML with the stream URL
  42. } else {
  43. stream = "https://wind-bow.glitch.me/twitch-api/streams/";
  44. stream = stream + streamList[count];
  45. $.getJSON(stream, function(data) {
  46. status = data.stream.stream_type;
  47. var streamName = data.display_name;
  48. var div = document.createElement('div');
  49. var mainDiv = document.getElementById('main');
  50.  
  51. div.setAttribute('class', 'stream');
  52. div.innerHTML = "<img class='logo' src = '" + data.logo + "'>" + streamName;
  53. mainDiv.appendChild(div);
  54. });
  55. count++
  56. }
  57. }
  58. //increment count to proceed to next stream
  59.  
  60.  
  61.  
  62. statusCheck();
  63. statusCheck();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement