S
S
Sergey Starozhuk2017-09-03 04:03:22
Node.js
Sergey Starozhuk, 2017-09-03 04:03:22

How to get broadcast status via requestify?

I tried to get it this way, but it doesn't even get to the point of checking.
Tried requestify through the moluot, but I guess I made a mistake somewhere

const requestify = require('requestify'); 
var STREAMER_ID = 's1rozha';
var checkStreamUrl = 'https://api.twitch.tv/kraken/streams/'+STREAMER_ID+'?client_id=ott641ae4i3mq3vz86vkwm9qzj3wobs';

function checkStream() {
    requestify.get(checkStreamUrl).then(function(response) {     
        if(response.getBody().stream === null) {
            isStreamOnline = false;
      console.log("Status: Offline");
        }
        else {
            if(typeof(isStreamOnline) !== 'undefined' && !isStreamOnline) {
                console.log("Status: Online");
            }
            isStreamOnline = true;
        }     
    });
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2017-09-03
@S1ROZHA

Your code works great. In sense - there are no errors, the request is successfully fulfilled.
"Does not reach the check" - do you mean that none of the console.log does not work? Well, this is not surprising - I do not see that the isStreamOnline variable is declared.
It turns out like this: the data was successfully received, the condition stream === null turns out to be false (we passed by the first console.log) - go to else, where the check for typeof isStreamOnline !== 'undefined' also turns out to be false (we drove past the second console.log) - because the variable is not declared, so it is still undefined. Well, after that isStreamOnline is set to true.
When checkStream is called again - again stream is not null, then the typeof check finally gives a positive result, but the second part of the condition (!isStreamOnline) turns out to be false, because isStreamOnline is already true - passing console.log again.
In short, add a variable declaration, something like , and remove the typeof check for 'undefined', one !isStreamOnline is enough. var isStreamOnline = false;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question