Catch MastodonNotFoundError

This commit is contained in:
Liam Steckler 2024-01-31 21:34:58 -08:00
parent b3474e24c9
commit e099d3e240

16
scl.py
View file

@ -3,6 +3,7 @@ import math
from datetime import datetime from datetime import datetime
from typing import Optional from typing import Optional
import mastodon
import requests import requests
import yaml import yaml
from mastodon import Mastodon from mastodon import Mastodon
@ -25,7 +26,7 @@ except requests.JSONDecodeError:
config = yaml.safe_load(open("config.yml")) config = yaml.safe_load(open("config.yml"))
stadiamaps_api_key = config["stadiamaps"]["api_key"] stadiamaps_api_key = config["stadiamaps"]["api_key"]
nominatim_url = config["nominatim"]["api_base_url"] nominatim_url = config["nominatim"]["api_base_url"]
mastodon = Mastodon( mastodon_client = Mastodon(
client_id=config["mastodon"]["client_id"], client_id=config["mastodon"]["client_id"],
client_secret=config["mastodon"]["client_secret"], client_secret=config["mastodon"]["client_secret"],
access_token=config["mastodon"]["access_token"], access_token=config["mastodon"]["access_token"],
@ -198,7 +199,7 @@ with Session(engine) as session:
updated_entries.append("") updated_entries.append("")
updated_entries.append(hashtag_string) updated_entries.append(hashtag_string)
if max_event_class["is_postable"] and existing_record.initial_post_id: 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), status="\n".join(updated_entries),
in_reply_to_id=existing_record.most_recent_post_id, in_reply_to_id=existing_record.most_recent_post_id,
visibility="public", visibility="public",
@ -349,7 +350,7 @@ with Session(engine) as session:
with io.BytesIO() as map_image_file: with io.BytesIO() as map_image_file:
map_image.save(map_image_file, format="PNG", optimize=True) 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(), map_image_file.getvalue(),
mime_type="image/png", mime_type="image/png",
description=alt_text, 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, status=post_text,
media_ids=map_media_post, media_ids=map_media_post,
visibility="public", visibility="public",
@ -419,7 +420,8 @@ with Session(engine) as session:
): ):
# Event ID no longer exists in response # Event ID no longer exists in response
if active_outage.most_recent_post_id: if active_outage.most_recent_post_id:
post_result = mastodon.status_post( try:
post_result = mastodon_client.status_post(
status="This outage is reported to be resolved.\n\n#SeattleCityLightOutage #SCLOutage #SCLOutage{}".format( status="This outage is reported to be resolved.\n\n#SeattleCityLightOutage #SCLOutage #SCLOutage{}".format(
active_outage.outage_user_id active_outage.outage_user_id
), ),
@ -428,5 +430,9 @@ with Session(engine) as session:
language="en", language="en",
) )
active_outage.most_recent_post_id = post_result["id"] 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() active_outage.no_longer_in_response_time = datetime.now()
session.commit() session.commit()