From e099d3e240dae327c027634fc90e1981f787e4d8 Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Wed, 31 Jan 2024 21:34:58 -0800 Subject: [PATCH] Catch MastodonNotFoundError --- scl.py | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/scl.py b/scl.py index bc17e58..61b84a1 100644 --- a/scl.py +++ b/scl.py @@ -3,6 +3,7 @@ import math from datetime import datetime from typing import Optional +import mastodon import requests import yaml from mastodon import Mastodon @@ -25,7 +26,7 @@ except requests.JSONDecodeError: config = yaml.safe_load(open("config.yml")) stadiamaps_api_key = config["stadiamaps"]["api_key"] nominatim_url = config["nominatim"]["api_base_url"] -mastodon = Mastodon( +mastodon_client = Mastodon( client_id=config["mastodon"]["client_id"], client_secret=config["mastodon"]["client_secret"], access_token=config["mastodon"]["access_token"], @@ -198,7 +199,7 @@ with Session(engine) as session: updated_entries.append("") updated_entries.append(hashtag_string) if max_event_class["is_postable"] and existing_record.initial_post_id: - post_result = mastodon.status_post( + post_result = mastodon_client.status_post( status="\n".join(updated_entries), in_reply_to_id=existing_record.most_recent_post_id, visibility="public", @@ -349,7 +350,7 @@ with Session(engine) as session: with io.BytesIO() as map_image_file: map_image.save(map_image_file, format="PNG", optimize=True) - map_media_post = mastodon.media_post( + map_media_post = mastodon_client.media_post( map_image_file.getvalue(), mime_type="image/png", description=alt_text, @@ -384,7 +385,7 @@ with Session(engine) as session: ) ) - post_result = mastodon.status_post( + post_result = mastodon_client.status_post( status=post_text, media_ids=map_media_post, visibility="public", @@ -419,14 +420,19 @@ with Session(engine) as session: ): # Event ID no longer exists in response if active_outage.most_recent_post_id: - post_result = mastodon.status_post( - status="This outage is reported to be resolved.\n\n#SeattleCityLightOutage #SCLOutage #SCLOutage{}".format( - active_outage.outage_user_id - ), - in_reply_to_id=active_outage.most_recent_post_id, - visibility="public", - language="en", - ) - active_outage.most_recent_post_id = post_result["id"] + try: + post_result = mastodon_client.status_post( + status="This outage is reported to be resolved.\n\n#SeattleCityLightOutage #SCLOutage #SCLOutage{}".format( + active_outage.outage_user_id + ), + in_reply_to_id=active_outage.most_recent_post_id, + visibility="public", + language="en", + ) + active_outage.most_recent_post_id = post_result["id"] + except mastodon.MastodonNotFoundError: + print( + "The outage post couldn't be replied to, it was externally deleted." + ) active_outage.no_longer_in_response_time = datetime.now() session.commit()