Fetch all the viewers of a game

Hi, i’m trying to fetch all the viewers of the most viewed game, but it takes long time to perform all the requests, and when are finished, the number of viewers isn’t correct, it’s about the half of the correct number. I’m using Dart.

This is the code:
// id = most watched game broadcast_id
final url = ‘https://api.twitch.tv/helix/streams?game_id=$id’;

Response response;
try {
  response = await dio.get(url);
} catch (e) {
  print('${e.toString()} tot viewers');
}

String cursor = response.data['pagination']['cursor'];
int total = 0;

while (cursor != null) {
  final newUrl =
      'https://api.twitch.tv/helix/streams?game_id=$id&after=$cursor';

  final resp = await dio.get(newUrl);
  for (int i = 0; i < resp.data['data'].length; i++) {
    total += resp.data['data'][i]['viewer_count'];
  }

  cursor = resp.data['pagination']['cursor'];
}

In my own tests running similar code I don’t get much of a discrepency, definetly not “half of te correct number”

Top Directory: Twitch
Bottom Output/Code used: Browser Categories | Twitch API Example

So unless you code or network translation is slow, I’m not sure why you see such a discrepency in your tests

1 Like

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