Anyone have any advice on how to get all followers of a streamer without manually writing new requests

I can’t seem to figure out in NodeJS how to dynamically request the next page of a streamers followers.

For example, if the streamer has 800 followers, I have to manually write 8 different requests.

When I try to do it dynamically, i can’t find a way around NodeJS and how it runs asynchronously. That is, i can never get the pagination before a loop runs the same request again, giving me the exact same results.

Don’t run the request inside a loop.

Instead, create a function that will make 1 request, and take an optional pagination parameter.

This way you call the function once, it makes the request and once it gets the results you can check if there are more results to get and if so you have the function call itself but this time pass it the pagination cursor. This way the function will keep being called and keep passing itself the next cursor each time.

What you do with the results is up to you, as an example for followers, you could concatentate the results in an array outside the function, that way each time the function gets called it adds 100 (or whatever limit you set) followers to the array, and keeps calling itself until it has all of the followers, at which point it stops calling itself and does something else with your now complete results.

1 Like

Ill give this a try today and let you know/post the code of how ti goes. Thanks Dist.

Below code worked, thank you

     var results = 0

    let paginateRequest = (streamer_id, pagination)=>{

request({
url: `https://api.twitch.tv/helix/users/follows?to_id=${streamer_id}&first=100&after=${pagination}`, 
json: true,
headers: {
	'Client-ID': 'hidden'
}

}, (error, response, body)=>{
	console.log(body)
	for(var i =0; i < body.data.length; i++){
		results += 1
	}


	if(body.pagination.cursor){
		paginateRequest(streamer_id, body.pagination.cursor)
		console.log('function was called for pagination')
		console.log('-----------------------------------')
		
	} else{
		console.log('Paginate request returned ' + results + ' followers for this user')
	}
	
	
});

};
paginateRequest(46458435, '')

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.