So i am currently writing a discord bot. This discord bot is supposed to return all the streams for a certain game. However, since the Helix API allows a maximum 100 results per page and uses pagination to “tab through”, i am kind of facing a problem here. This is what my code currently looks like (the relevant part of it):
Then do that instead of writing to the DOM (like my example does)
Just wanted to illistrate an example of how I paginate.
So you probably want something like
Pseudo javascript
let streams = [];
function getStreams(cursor) {
let url = new URL('https://api.twitch.tv/helix/streams');
let params = [
[ 'first', '100' ],
[ 'game_id', '509658' ]
];
if (cursor) {
params.push([ 'after', cursor ]);
}
url.search = new URLSearchParams(params).toString();
// make request to twitch using `url`
// Parse response
// store streams in the array
streams = streams.concat(resp.data);
// another page?
if (resp.hasOwnProperty('pagination') && resp.pagination.hasOwnProperty('cursor')) {
getStreams(resp.pagination.cursor);
} else {
// all done
processStreams();
}
}
function processStreams() {
// do something with `streams`
}
So essentially i would like to sum up the viewers of all the streams in that specific game.
However, if i process them in a function, i can still only work with each set of 100 streams right?
// store streams in the array
streams = streams.concat(resp.data);
Will put all the streams into the streams array
But if you only want the total viewers.
var total_viewers = 0;
function getStreams(cursor) {
// snip do request
resp.data.forEach(stream => {
total_viewers += stream.viewer_count
});
// snip do paginate
}
100 per page the code checks for a cursor if it has a cursor it self calls the getStreams with the cursor
let streams = []; is hoisted outside the function as a global so would collect all streams into that array. Each call merging the results from the call into that array
The thing is, if i have 20 games and i want to fetch the viewer count for all of them and count the number of streams per game, that wouldnt work anymore, would it?
Also, doesn’t let declare scoped vars? sorry i’m a bit confused
let total_viewers = [];
function getStreams(game_id, cursor) {
// snip do request
resp.data.forEach(stream => {
if (!total_viewers.hasOwnProperty(game_id)) {
total_viewers[game_id] = 0;
}
total_viewers[game_id] += stream.viewer_count
});
// snip do paginate
}
It stops the variable being used in the same scope.
So I can’t have two total_viewers in the global scope
But I could have a let total_viewers inside the function scoped to the function, but since I didn’t the global scoped one is the one affected.
let variable = 'wotsit';
function thing() {
let variable = 'somethingelse';
console.log('Function is', variable);
}
console.log('Outside of function', variable);
thing();
console.log('Outside of function', variable);
result
Outside of function wotsit
Function is somethingelse
Outside of function wotsit
Doesn’t matter what i try, the “parsed” var (see original post) will always have the same value… i tried it with the “let” in different positions… doesnt change