簡體   English   中英

NodeJS TwitchAPI聯機/脫機狀態

[英]NodeJS TwitchAPI online/offline status

我正在嘗試檢查信息流是在線還是離線...

但是我有1個問題,並非所有用戶都需要此請求。.當1個來自在線且1個離線時,只有1個或最多2個...

這是一個示例:在MySQL中,有3個流test1 test2 test3 test4(1、2、3和4是流...)(當所有脫機時,我的輸出是...)test4脫機且這4次。

腳本:

var request = require('request');
var cheerio = require('cheerio');
var parser = require('json-parser');
var JSON = require('JSON');
var mysql  = require('mysql');

var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'status',
  password : '',
  database : 'all_channels'
});

connection.query("SELECT * FROM channels", function(error, rows, response, fields) {
    if (!!error) {
        console.log('Error in Query!');
    } else {
        for(var i in rows) {


console.log(rows[i])
                request('https://api.twitch.tv/kraken/streams/'+rows[i].channels+'?client_id=(I have removed the ID from intent)', function (error, response, html) {
        var profileyt = JSON.parse(html);


if (profileyt["stream"] !== null) {
                        console.log(profileyt["stream"]["channel"]["status"]);
                    } else {


request('https://api.twitch.tv/kraken/channels/'+rows[i].channels+'?client_id=(I have removed the ID from intent)', ', function (error, response, html) {
                            var profiletest = JSON.parse(html);


console.log(''+profiletest["display_name"]+' is offline');
})
}})}}});

控制台輸出:C:\\ Bot> node test2.js

RowDataPacket { channels: 'test1', status: 'offline' }
RowDataPacket { channels: 'test2', status: 'offline' }
RowDataPacket { channels: 'test3', status: 'offline' }
RowDataPacket { channels: 'test4', status: 'offline' }

Test4 ist offline
Test4 ist offline
Test4 ist offline
Test4 ist offline

我花了2-3天的時間修復它,但我仍然不知道...

結果是

Test4 ist offline
Test4 ist offline
Test4 ist offline
Test4 ist offline

因為您是在異步函數內調用rows[i].channels ,而異步函數也在循環內

看到這個

循環已經結束,或者i成為調用request2的回調之前的行,這就是為什么它總是僅顯示您的最后一個頻道的原因

for (var i in rows) {
        request1('blahblah...', function(error, response, html) {
            // rows[i].channels this will always displayed your last row data
            request2(rows[i].channels, function (error, response, html) {
            })
        })
    }
}

為了解決這個問題,創建一個單獨的函數

function getChannelStatus(row) {
    // now the row will be pass is the correct one istead of the last row like your previous code
    request('https://api.twitch.tv/kraken/streams/' + row.channels + '?client_id=(I have removed the ID from intent)', function(error, response, html) {
            var profileyt = JSON.parse(html);


            if (profileyt["stream"] !== null) {
                console.log(profileyt["stream"]["channel"]["status"]);
            } else {


                request('https://api.twitch.tv/kraken/channels/' + row.channels + '?client_id=(I have removed the ID from intent)', function (error, response, html) {
                    var profiletest = JSON.parse(html);


                    console.log('' + profiletest["display_name"] + ' is offline');
                })
        }
    })
}

connection.query("SELECT * FROM channels", function(error, rows, response, fields) {
    if (!!error) {
        console.log('Error in Query!');
    } else {
        for (var i in rows) {

            console.log(rows[i])
            getChannelStatus(rows[i]);
        }
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM