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