From 464a3317d5293bf1424a147ef6ef856bedeb8b4d Mon Sep 17 00:00:00 2001 From: Michael Thomas Date: Thu, 9 Mar 2023 07:18:07 +0000 Subject: [PATCH] better error handling when getting timeline toots --- get_context.py | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/get_context.py b/get_context.py index 7eebb34..ef507c9 100644 --- a/get_context.py +++ b/get_context.py @@ -45,17 +45,35 @@ def get_timeline(server, access_token, max): """Get all post in the user's home timeline""" url = f"https://{server}/api/v1/timelines/home" - - response = get_toots(url, access_token) - toots = response.json() - # Paginate as needed - while len(toots) < max: - try: + try: + + response = get_toots(url, access_token) + + if response.status_code == 200: + toots = response.json() + elif response.status_code == 401: + raise Exception( + f"Error getting URL {url}. Status code: {response.status_code}. " + "Ensure your access token is correct" + ) + elif response.status_code == 403: + raise Exception( + f"Error getting URL {url}. Status code: {response.status_code}. " + "Make sure you have the read:statuses scope enabled for your access token." + ) + else: + raise Exception( + f"Error getting URL {url}. Status code: {response.status_code}" + ) + + # Paginate as needed + while len(toots) < max: response = get_toots(response.links['next']['url'], access_token) toots = toots + response.json() - except Exception as ex: - print(f"Error getting timeline toots: {ex}") + except Exception as ex: + print(f"Error getting timeline toots: {ex}") + sys.exit(1) print(f"Found {len(toots)} toots in timeline") @@ -68,10 +86,15 @@ def get_toots(url, access_token): if response.status_code == 200: return response + elif response.status_code == 401: + raise Exception( + f"Error getting URL {url}. Status code: {response.status_code}. " + "It looks like your access token is incorrect." + ) elif response.status_code == 403: raise Exception( f"Error getting URL {url}. Status code: {response.status_code}. " - "Make sure you have the admin:read:accounts scope enabled for your access token." + "Make sure you have the read:statuses scope enabled for your access token." ) else: raise Exception( @@ -94,6 +117,11 @@ def get_active_user_ids(server, access_token, reply_interval_hours): if last_active > since: print(f"Found active user: {user['username']}") yield user["id"] + elif resp.status_code == 401: + raise Exception( + f"Error getting user IDs on server {server}. Status code: {resp.status_code}. " + "Ensure your access token is correct" + ) elif resp.status_code == 403: raise Exception( f"Error getting user IDs on server {server}. Status code: {resp.status_code}. "