Hi everyone! I’m stuck with a Python script that uses the Twitch API. I need to get all the channels that popular streamers follow for my university project about social networks.
I can get the first 100 results but my pagination loop isn’t working right. The followed_channels list stops growing after the first request and my while loop runs forever. The issue seems to be with this line: followed_channels.append(item['channel']['display_name']).
I’m pretty new to Python so this might be something obvious. Any help would be great!
offset_val = 0
counter = 0
popular_streamers = ['Shroud'] # testing with one streamer
followed_channels = []
for streamer in popular_streamers:
print(streamer)
api_response = requests.get('https://api.twitch.tv/kraken/users/{}/follows/channels?client_id={}&offset=0&limit=100'.format(streamer, my_client_id))
response_data = json.loads(api_response.text)
for item in response_data['follows']:
followed_channels.append(item['channel']['display_name'])
print(len(followed_channels))
print('total count:')
print(response_data['_total'])
while len(followed_channels) < response_data['_total']:
offset_val = offset_val + 100
next_request = requests.get('https://api.twitch.tv/kraken/users/{}/follows/channels?client_id={}&offset={}&limit=100'.format(streamer, my_client_id, offset_val))
next_data = json.loads(next_request.text)
for channel in next_data['follows']:
followed_channels.append(channel['channel']['display_name']) # this doesn't seem to work
print(len(followed_channels))
print(channel['channel']['display_name'])
counter = counter + 1
print(len(followed_channels))
print(response_data['_total'])
print(len(followed_channels))
print(followed_channels)
I want to end up with a dictionary structure that maps each streamer to their followed channels.
hey, just a heads up - the Kraken API is gone. twitch switched to helix like, years ago, and it doesn’t have the follows endpoint anymore (you kno, privacy stuff). maybe check with your prof or look for another data source.
Everyone already covered the API issue, but there’s another bug in your pagination logic. You’re not checking if next_data['follows'] is empty before the loop starts, so you might be making pointless requests and processing nothing. Also, that counter variable isn’t doing anything useful.
I’ve hit similar pagination issues with APIs before. Your code isn’t handling the response structure properly - sometimes APIs return empty results or different formats when you hit limits, which breaks your loop. But there’s a bigger problem that others mentioned. Those Kraken API endpoints? Twitch deprecated and shut them down completely. Even if you fix the pagination, your requests won’t work because those URLs don’t exist anymore. For your university project, I’d pivot to publicly available data - maybe streamer collaboration networks, viewer overlap from third-party sites, or just focus on pagination itself with a different API that’s still running. GitHub’s API or Reddit’s API have similar pagination and actually work for learning this stuff.
Your infinite loop is happening because offset_val is declared outside the streamer loop. When you process multiple streamers, it keeps the previous offset value instead of resetting to 0. This breaks pagination for the next streamers. Just move offset_val = 0 inside the for loop right after for streamer in popular_streamers:. Reset your counter variable there too. But here’s the bigger issue - like henryg said, the Kraken API was killed in 2022. The new Helix API doesn’t give you user follows data anymore due to privacy changes. You’ll probably need to change your whole project approach or find different data sources like public follower lists or streamer networks. I’d check with your instructor about this limitation before you waste more time fixing the pagination.