diff --git a/libs/discourse_helper.py b/libs/discourse_helper.py index f27dfec..160481f 100644 --- a/libs/discourse_helper.py +++ b/libs/discourse_helper.py @@ -92,26 +92,8 @@ def make_post(self, topic_id: int, post: str) -> None: def get_all_voters(self, post_id, poll_name, option_id): results = [] - i = 1 - - # Hacky way to get voters directly - page = self._discource_client._request( - "GET", - "/polls/voters.json", - params={ - "post_id": post_id, - "poll_name": poll_name, - "option_id": option_id, - "page": i, - }, - )["voters"][option_id] - - results += page - - i += 1 - - while len(page) != 0: - page = self._discource_client._request( + for i in range(1, 11): + r = self._discource_client._request( "GET", "/polls/voters.json", params={ @@ -120,15 +102,15 @@ def get_all_voters(self, post_id, poll_name, option_id): "option_id": option_id, "page": i, }, - )["voters"][option_id] + ) - results += page + # No more pages; we're done. + if r["voters"] is None: + return results - i += 1 + results += r["voters"][option_id] - # Ugh that (^^^) was a lame way of doing this, I was tired, - # TODO: Make this cooler/cleaner for pagination and the actual request - return results + raise Exception("Received too many pages of voters") def get_all_polls(self, post_id: int, close_time_override=None) -> list: assert isinstance(post_id, int)