【问题标题】:Using the Twitch API to display offline/online streamers使用 Twitch API 显示离线/在线流媒体
【发布时间】:2016-08-27 13:26:46
【问题描述】:

我已经尝试让这个项目运行好几天了,无论我遵循了多少教程或查看了多少示例,我总是疯狂地卡在不起作用的代码中。

我正在使用 Twitch API 来显示所有在线和离线流媒体。我开始关注the code here (his finished project) 并将其应用到我的项目中……但我又被卡住了。我感到非常困惑。

这里是JSFiddle,这里是JS(我知道这是一团糟!):

window.onload = function() {
    var streamers = ['freecodecamp', 'syndicate', 'riotgames', 'esl csgo', 'Nightblue3', 'summit1g', 'LIRIK', 'PhantomL0rd', 'imaqtpie', 'captainsparklez', 'sodapoppin', 'goldglove', 'tsm bjergsen', 'Joshdog', 'Tsm dyrus', 'mushisgoshu', 'trick2g', 'comster404', 'brunofin'];
    var status, url, picture, x = 0;

    function updateHTML(section) {
        $(section).append('<div class="row"><div class="image-holder" id="user-image-' + x + '"></div></div><div class="two-thirds column"><span class="status-message">' + status + '</span></div></div></div>');
        if (section == ".online" || section == ".offline") {
            $("#user-image-" + x).css({
                background: picture,
                'background-size': '55px'
            });
        }
        x++;
    }
    
        function showOnline () { //Show only online users
        $(".offline-users, .all-users").removeClass('focus');
        $(".online-users").addClass('focus');
        $(".offline, .unavailable").addClass('hidden');
        $(".online").removeClass('hidden');
    }

    function showOffline () { //Show only offline users
        $(".online-users, .all-users").removeClass('focus');
        $(".offline-users").addClass('focus');
        $(".offline, .unavailable").removeClass('hidden');
        $(".online").addClass('hidden');
    }

    function showAll () { //Show all users
        $(".offline-users, .online-users").removeClass('focus');
        $(".all-users").addClass('focus');
        $(".online, .offline, .unavailable").removeClass('hidden'); 
    }



    //fetch the data for each streamer using ajax requests
    for (var i = 0; i < streamers.length; i++) {
        ajax();
    }

    function ajax() {
        $.ajax({
            url: "https://api.twitch.tv/kraken/streams/" + streamers[i] + "?callback=?",
            dataType: "jsonp",
            data: {
                format: "json"
            },

            success: function(data) {
                fetchData(data);
            },

            error: function() {
                console.log("unable to access json");
            }
        })
    }

    function fetchData(data) {
        if (data.stream === null) {
            url = data._links.channel.substr(38);
            updateOfflineUsers();
        } else if (data.status == 422 || data.status == 404) {
            status = data.message;
            updateHTML(".unavailable");
        } else {
            if (data.stream.channel.logo !== null) {
                picture = 'url("' + data.stream.channel.logo + '")';
            } else {
                picture = 'url("http://seeklogo.com/images/T/twitch-logo-4931D91F85-seeklogo.com.png")';
            }
            url = data._links.channel.substr(38);
            status = "<a href='https://twitch.tv/" + url + "' target='_blank'" + "'>" + data.stream.channel.display_name + "</a>" + " is currently streaming" + data.stream.game;
            updateHTML(".online");
        }
    }

    //another API call for more info on the offline users
    function updateOfflineUsers() {
        $.ajax({
            url: "https://api.twitch.tv/kraken/channels/" + url,
            dataType: "jsonp",
            data: {format: "json"},
            success: function(json) {
                status = "'<a href='" + json.url + "' target='_blank'" + "'>" + json.display_name + "</a>'" + " is currently offline";
                if (json.logo !== null) {
                    picture = 'url("' + json.logo + '")';
                } else {
                    picture = 'url("http://seeklogo.com/images/T/twitch-logo-4931D91F85-seeklogo.com.png")';
                }
                updateHTML(".offline");
            }
        });
    }


}

$('[data-toggle="tooltip"]').tooltip();

HMTL:

<html>

<head>
  <meta charset=utf-8/>

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css">
  <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css">
  <link href='https://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" href="finaltwitch.css" />


  <title>Twitch Streamers</title>

</head>


<body>
    
    <div class="content">
        
      <div class="row" id="header">
          
    <div class="row"><h1>Twitch Streamers</h1></div>
        <div class="options row">
          <div id="all">
              <button class="btn selector active" data-toggle="tooltip" data-placement="bottom" title="All"><i class="fa fa-user"></i></button>
          </div>
          <div id="online">
            <button class="btn selector" data-toggle="tooltip" data-placement="bottom" title="Online"><i class="fa fa-toggle-on"></i></button>
          </div>
          <div id="offline">
            <button class="btn selector" data-toggle="tooltip" data-placement="bottom" title="Offline"><i class="fa fa-toggle-off"></i></button>
          </div>
        </div>
      </div>
        
      <!--<ul id="streamers"></ul>-->
        <section class="online"></section>
        <section class="offline"></section>
        <section class="unavailable"></section>
      
        
      <div class="row" id="footer">
      </div>
        
    </div>
    


  <!-- jQuery & Boostrap files -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <script src="twitch.js"></script>
</body>

</html>

欢迎任何帮助或任何方向,只要任何能让我再次走上正确道路的事情。

谢谢!

【问题讨论】:

    标签: javascript html css twitch


    【解决方案1】:

    除非我遗漏了什么,一旦你将小提琴中的 JavaScript 选项中的 loadtype 设置为除 onLoad 之外的任何内容,你的初始加载代码似乎在你的小提琴中运行良好。

    如果您需要按钮工作,只需在您的 onload 回调中将 click 事件附加到它们:

      $('#all button').on('click', function() {
        showAll();
      });
    
      $('#online button').on('click', function() {
        showOnline();
      });
    
      $('#offline button').on('click', function() {
        showOffline();
      });
    

    更新小提琴:https://jsfiddle.net/un5wdnqn/4/

    【讨论】:

    • 哦,这很有趣;在我的括号编辑器中它不起作用,但它在 JSFiddle 中起作用:/
    • 不幸的是,我不熟悉括号的工作原理。由于您使用的是 jQuery,是否将 window.onload 更改为 $(document).ready(function() { /* the rest of your function */}) 在括号中修复它?也许它正在做类似于默认小提琴设置的事情。这是一个这样做的小提琴,并将小提琴设置为 onload:jsfiddle.net/un5wdnqn/6
    • 很奇怪。我刚刚安装了 Brackets 并在本地保存了您的代码,实时预览对我有用。我必须先关闭现有的 Chrome 窗口,才能使用远程调试功能。
    • 嗯,head标签和script标签里有什么代码?这可能会导致我的问题。
    • 样式表在头部,外部脚本(jquery,bootstrap)然后你的脚本在正文的末尾内联。见:pastebin.com/GHH2Qz5U(忽略末尾多余的

    猜你喜欢
    • 1970-01-01
    • 2013-09-24
    • 2014-02-05
    • 2011-10-20
    • 1970-01-01
    • 2017-06-18
    • 2021-07-03
    • 1970-01-01
    • 2013-09-21
    相关资源
    最近更新 更多