I’m struggling to find a one-pass method to distinguish between:
- Active account and streaming
- Active account but not currently streaming
- Not active account
The three calls I have tried are the endpoint + /streams/, endpoint + /users/ and the endpoint + /channels/
In the end I peeked at the example FreeCodeCamp give on CodePen but noticed their code doesn’t work either.
Specifically this:
if (data.stream === null) {
game = “Offline - undefined e.g. comster404 shouldn’t appear”;
status = “offline”;
} else if (data.stream === undefined) {
game = “Account Closed”;
status = “offline”;
} else {
game = data.stream.game;
status = “online”;
};
Problem is the ‘stream’ parameter does not return ‘undefined’ when the “Account Closed” - it returns ‘null’.
e.g.
https://wind-bow.glitch.me/twitch-api/streams/brunofin
returns
{“stream”:null,"_links":{“self”:“https://api.twitch.tv/kraken/streams/brunofin",“channel”:"https://api.twitch.tv/kraken/channels/brunofin”}}
Perhaps this is due to the change to the endpoint from …kraken… to …wind-blow…?
Just as:
https://wind-bow.glitch.me/twitch-api/streams/freecodecamp
returns
{“stream”:null,"_links":{“self”:“https://api.twitch.tv/kraken/streams/freecodecamp",“channel”:"https://api.twitch.tv/kraken/channels/freecodecamp”}}
So in summary:
- if you use /streams/ --> Active and broadcasting returns a stream, not broadcasting and non-active return null
- if you use /channels/ --> Active (Broadcasting and non-broadcasting) both return some info (non of which indicates they are actively streaming) and non-active returns error == Not Found.
- Similarly, if you use /users/ --> Active (Broadcasting and non-broadcasting) both return some info (non of which indicates they are actively streaming) and non-active returns error == Unprocessable Entity
Clearly you need two calls but currently struggling (even using ‘promises’) to reconcile the two pieces of information asynchronously.