Skip to content

Commit bd2d791

Browse files
committed
Fixed infinite loop on some endpoints with a cursor
1 parent 3eb8ab6 commit bd2d791

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

twitch/helix/base.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def __init__(
8989
self._oauth_token = oauth_token
9090
self._params = params
9191
self._total = None
92+
self._requests_count = 0
9293

9394
# Pre-fetch the first page as soon as cursor is instantiated
9495
self.next_page()
@@ -115,11 +116,17 @@ def __getitem__(self, index):
115116
return self._queue[index]
116117

117118
def next_page(self):
119+
# Twitch stops returning a cursor when you're on the last page. So if we've made
120+
# more than 1 request to their API and we don't get a cursor back, it means
121+
# we're on the last page, so return whatever's left in the queue.
122+
if self._requests_count > 0 and not self._cursor:
123+
return self._queue
124+
118125
if self._cursor:
119126
self._params["after"] = self._cursor
120127

121128
response = self._request_get(self._path, params=self._params)
122-
129+
self._requests_count += 1
123130
self._queue = [self._resource.construct_from(data) for data in response["data"]]
124131
self._cursor = response["pagination"].get("cursor")
125132
self._total = response.get("total")

0 commit comments

Comments
 (0)