From 47e8b485a520d5f4f81f5a77481d43d3927b3a16 Mon Sep 17 00:00:00 2001 From: Timothy Quilling Date: Sun, 2 Jul 2023 23:51:46 -0400 Subject: [PATCH] fix: handle zero notifications --- find_posts.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/find_posts.py b/find_posts.py index 2fb84d6..10aebdf 100644 --- a/find_posts.py +++ b/find_posts.py @@ -795,12 +795,30 @@ def get_paginated_mastodon(url, max, headers = {}, timeout = 0, max_tries = 5): if(isinstance(max, int)): while len(result) < max and 'next' in response.links: response = get(response.links['next']['url'], headers, timeout, max_tries) - result = result + response.json() + if response.status_code != 200: + raise Exception( + f"Error getting URL {response.url}. \ + Status code: {response.status_code}" + ) + response_json = response.json() + if isinstance(response_json, list): + result += response_json + else: + break else: - while parser.parse(result[-1]['created_at']) >= max and 'next' in response.links: + while result and parser.parse(result[-1]['created_at']) >= max \ + and 'next' in response.links: response = get(response.links['next']['url'], headers, timeout, max_tries) - result = result + response.json() - + if response.status_code != 200: + raise Exception( + f"Error getting URL {response.url}. \ + Status code: {response.status_code}" + ) + response_json = response.json() + if isinstance(response_json, list): + result += response_json + else: + break return result