Compare commits

..

No commits in common. "6ca6a2dbd1075d5a73fd60acbd7286256d5ae640" and "54fed2f87ffdd8cea289d558e2b1063cdb929d36" have entirely different histories.

2 changed files with 110 additions and 127 deletions

View file

@ -1,10 +1,8 @@
osm:
url_template: https://tile.openstreetmap.org/{z}/{x}/{y}.png
attribution: © OpenStreetMap
health_check_url:
nominatim:
api_base_url: https://nominatim.openstreetmap.org
health_check_url: https://nominatim.openstreetmap.org/status
api_base_url:
mastodon:
client_id:
client_secret:

55
scl.py
View file

@ -1,7 +1,7 @@
import io
import re
from datetime import datetime
from typing import Optional, Tuple
from typing import Optional
import mastodon
import requests
@ -9,7 +9,6 @@ import shapely
import staticmap
import yaml
from mastodon import Mastodon
from mastodon.return_types import MediaAttachment
from PIL import Image, ImageDraw, ImageFont
from shapely import Geometry
from sqlalchemy import create_engine, select
@ -19,7 +18,7 @@ from sqlalchemy.orm import DeclarativeBase, Mapped, Session, mapped_column
from geospatial import DBGeometry, convert_outage_geometry
REQUESTS_HEADERS = {"User-Agent": "seattlecitylight-mastodon-bot"}
POST_DATETIME_FORMAT = "%b %e %l:%M %p"
post_datetime_format = "%b %e %l:%M %p"
scl_events_url = "https://utilisocial.io/datacapable/v2/p/scl/map/events"
scl_events_response = requests.get(scl_events_url)
@ -84,6 +83,7 @@ class AttribStaticMap(staticmap.StaticMap, object):
textSize = fnt.getbbox(self.attribution)
textPosition = (image.size[0] - textSize[2], image.size[1] - textSize[3])
offset = 2
options = {"fill": (255, 255, 255, 180)}
d.rectangle(
[
(textPosition[0] - (2 * offset), textPosition[1] - (2 * offset)),
@ -92,7 +92,7 @@ class AttribStaticMap(staticmap.StaticMap, object):
textSize[3] + textPosition[1] + (2 * offset),
),
],
fill=(255, 255, 255, 180),
**options,
)
# draw text, full opacity
@ -152,11 +152,18 @@ def get_hashtag_string(event) -> str:
return hashtag_string
def generate_post_map_image(
event, event_class, outage_geometries: shapely.MultiPolygon
) -> Tuple[MediaAttachment, str]:
def do_initial_post(
event,
event_class,
start_time: datetime,
estimated_restoration_time: datetime,
outage_geometries: shapely.MultiPolygon,
) -> dict[str, str | None]:
post_id = None
map_media_post_id = None
# Fallback location from the SCL API in case one couldn't be reverse geocoded
area_text = event["city"]
try:
map = AttribStaticMap(
1024,
1024,
@ -247,40 +254,24 @@ def generate_post_map_image(
with io.BytesIO() as map_image_file:
map_image.save(map_image_file, format="WebP", method=6)
map_media_post = mastodon_client.media_post(
map_image_file,
map_image_file.getvalue(),
mime_type="image/webp",
description=alt_text,
)
return (map_media_post, area_text)
def do_initial_post(
event,
event_class,
start_time: datetime,
estimated_restoration_time: datetime,
outage_geometries: shapely.MultiPolygon,
) -> dict[str, str | None]:
post_id = None
map_media_post = None
area_text = str()
try:
map_media_post, area_text = generate_post_map_image(
event, event_class, outage_geometries
)
map_media_post_id = map_media_post["id"]
except Exception as e:
print(e)
print(
"Ran into an issue with generating/uploading the map. Will post without it."
)
map_media_post = None
hashtag_string = get_hashtag_string(event)
est_restoration_post_text = str()
if estimated_restoration_time > datetime.now():
est_restoration_post_text = "\nEst. Restoration: {}\n".format(
estimated_restoration_time.strftime(POST_DATETIME_FORMAT)
estimated_restoration_time.strftime(post_datetime_format)
)
post_text = """Seattle City Light is reporting a {} outage in {}.
@ -291,7 +282,7 @@ Cause: {}
{}""".format(
event_class["size"].lower(),
area_text,
start_time.strftime(POST_DATETIME_FORMAT),
start_time.strftime(post_datetime_format),
est_restoration_post_text,
event["cause"],
hashtag_string,
@ -373,7 +364,6 @@ with Session(engine) as session:
existing_record = lookup_result.one()
updated_properties = []
updated_entries = []
map_media_post = None
est_restoration_diff_mins = (
abs(
@ -392,7 +382,7 @@ with Session(engine) as session:
updated_properties.append("estimated restoration")
updated_entries.append(
"Est. Restoration: {}".format(
estimated_restoration_time.strftime(POST_DATETIME_FORMAT)
estimated_restoration_time.strftime(post_datetime_format)
)
)
if event["cause"] != existing_record.cause:
@ -416,12 +406,8 @@ with Session(engine) as session:
max_event_class = classify_event_size(existing_record.max_num_people)
if existing_record.outage_geometries != outage_geometries:
print("Geometries modified")
updated_properties.append("area")
existing_record.outage_geometries = outage_geometries
existing_record.geometries_modified = True
map_media_post, _ = generate_post_map_image(
event, event_class, outage_geometries
)
if updated_properties:
updated_properties.sort()
@ -446,7 +432,6 @@ with Session(engine) as session:
post_result = mastodon_client.status_post(
status="\n".join(updated_entries),
in_reply_to_id=existing_record.most_recent_post_id,
media_ids=map_media_post,
visibility="public",
language="en",
)