Split map image generation into a new function
This commit is contained in:
parent
3c40e695a7
commit
000acf19e3
1 changed files with 109 additions and 100 deletions
39
scl.py
39
scl.py
|
@ -1,7 +1,7 @@
|
||||||
import io
|
import io
|
||||||
import re
|
import re
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Optional
|
from typing import Optional, Tuple
|
||||||
|
|
||||||
import mastodon
|
import mastodon
|
||||||
import requests
|
import requests
|
||||||
|
@ -9,6 +9,7 @@ import shapely
|
||||||
import staticmap
|
import staticmap
|
||||||
import yaml
|
import yaml
|
||||||
from mastodon import Mastodon
|
from mastodon import Mastodon
|
||||||
|
from mastodon.return_types import MediaAttachment
|
||||||
from PIL import Image, ImageDraw, ImageFont
|
from PIL import Image, ImageDraw, ImageFont
|
||||||
from shapely import Geometry
|
from shapely import Geometry
|
||||||
from sqlalchemy import create_engine, select
|
from sqlalchemy import create_engine, select
|
||||||
|
@ -83,7 +84,6 @@ class AttribStaticMap(staticmap.StaticMap, object):
|
||||||
textSize = fnt.getbbox(self.attribution)
|
textSize = fnt.getbbox(self.attribution)
|
||||||
textPosition = (image.size[0] - textSize[2], image.size[1] - textSize[3])
|
textPosition = (image.size[0] - textSize[2], image.size[1] - textSize[3])
|
||||||
offset = 2
|
offset = 2
|
||||||
options = {"fill": (255, 255, 255, 180)}
|
|
||||||
d.rectangle(
|
d.rectangle(
|
||||||
[
|
[
|
||||||
(textPosition[0] - (2 * offset), textPosition[1] - (2 * offset)),
|
(textPosition[0] - (2 * offset), textPosition[1] - (2 * offset)),
|
||||||
|
@ -152,18 +152,11 @@ def get_hashtag_string(event) -> str:
|
||||||
return hashtag_string
|
return hashtag_string
|
||||||
|
|
||||||
|
|
||||||
def do_initial_post(
|
def generate_post_map_image(
|
||||||
event,
|
event, event_class, outage_geometries: shapely.MultiPolygon
|
||||||
event_class,
|
) -> Tuple[MediaAttachment, str]:
|
||||||
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
|
# Fallback location from the SCL API in case one couldn't be reverse geocoded
|
||||||
area_text = event["city"]
|
area_text = event["city"]
|
||||||
try:
|
|
||||||
map = AttribStaticMap(
|
map = AttribStaticMap(
|
||||||
1024,
|
1024,
|
||||||
1024,
|
1024,
|
||||||
|
@ -254,18 +247,34 @@ def do_initial_post(
|
||||||
with io.BytesIO() as map_image_file:
|
with io.BytesIO() as map_image_file:
|
||||||
map_image.save(map_image_file, format="WebP", method=6)
|
map_image.save(map_image_file, format="WebP", method=6)
|
||||||
map_media_post = mastodon_client.media_post(
|
map_media_post = mastodon_client.media_post(
|
||||||
map_image_file.getvalue(),
|
map_image_file,
|
||||||
mime_type="image/webp",
|
mime_type="image/webp",
|
||||||
description=alt_text,
|
description=alt_text,
|
||||||
)
|
)
|
||||||
map_media_post_id = map_media_post["id"]
|
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
|
||||||
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
print(
|
print(
|
||||||
"Ran into an issue with generating/uploading the map. Will post without it."
|
"Ran into an issue with generating/uploading the map. Will post without it."
|
||||||
)
|
)
|
||||||
map_media_post = None
|
|
||||||
hashtag_string = get_hashtag_string(event)
|
hashtag_string = get_hashtag_string(event)
|
||||||
|
|
||||||
est_restoration_post_text = str()
|
est_restoration_post_text = str()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue