more error handling

This commit is contained in:
Michael Thomas 2023-03-07 07:09:31 +00:00
parent a48fcb096e
commit fd8a3404d9

View file

@ -48,8 +48,11 @@ def get_timeline(server, access_token, max):
# Paginate as needed # Paginate as needed
while len(toots) < max: while len(toots) < max:
try:
response = get_toots(response.links['next']['url'], access_token) response = get_toots(response.links['next']['url'], access_token)
toots = toots + response.json() toots = toots + response.json()
except Exception as ex:
print(f"Error getting timeline toots: {ex}")
print(f"Found {len(toots)} toots in timeline") print(f"Found {len(toots)} toots in timeline")
@ -317,13 +320,17 @@ def get_toot_context(server, toot_id, toot_url):
def add_context_urls(server, access_token, context_urls, seen_urls): def add_context_urls(server, access_token, context_urls, seen_urls):
"""add the given toot URLs to the server""" """add the given toot URLs to the server"""
count = 0 count = 0
failed = 0
for url in context_urls: for url in context_urls:
if url not in seen_urls: if url not in seen_urls:
add_context_url(url, server, access_token) added = add_context_url(url, server, access_token)
if added is True:
seen_urls.add(url) seen_urls.add(url)
count += 1 count += 1
else:
failed += 1
print(f"Added {count} new context toots") print(f"Added {count} new context toots (with {failed} failures)")
def add_context_url(url, server, access_token): def add_context_url(url, server, access_token):
@ -340,19 +347,22 @@ def add_context_url(url, server, access_token):
print( print(
f"Error adding url {search_url} to server {server}. Exception: {ex}" f"Error adding url {search_url} to server {server}. Exception: {ex}"
) )
return return False
if resp.status_code == 200: if resp.status_code == 200:
print(f"Added context url {url}") print(f"Added context url {url}")
return True
elif resp.status_code == 403: elif resp.status_code == 403:
print( print(
f"Error adding url {search_url} to server {server}. Status code: {resp.status_code}. " f"Error adding url {search_url} to server {server}. Status code: {resp.status_code}. "
"Make sure you have the read:search scope enabled for your access token." "Make sure you have the read:search scope enabled for your access token."
) )
return False
else: else:
print( print(
f"Error adding url {search_url} to server {server}. Status code: {resp.status_code}" f"Error adding url {search_url} to server {server}. Status code: {resp.status_code}"
) )
return False
class OrderedSet: class OrderedSet: