Skip to content

Commit

Permalink
adds logic for gcn.notices. notices, replace healpix_file if present (#…
Browse files Browse the repository at this point in the history
…162)

* adds logic for gcn.notices. notices, replace healpix_file if present

Fixes formating and check size of all fields

Adds function to traverse dict and replace content thats too long

* whitespace

* Update gcn_email/core.py

Co-authored-by: Leo Singer <[email protected]>

* add back gcn.notices. check

---------

Co-authored-by: Leo Singer <[email protected]>
  • Loading branch information
dakota002 and lpsinger authored Jan 31, 2024
1 parent 746d552 commit d9abf9c
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions gcn_email/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from email.message import EmailMessage
import logging
import os
import json

import boto3
from boto3.dynamodb.conditions import Key
Expand All @@ -27,6 +28,26 @@
log = logging.getLogger(__name__)


REPLACEMENT_TEXT = (
"This content is too large for email. "
"To receive it, see https://gcn.nasa.gov/quickstart"
)


def replace_long_values(data, max_length):
if isinstance(data, dict):
iter = data.items()
elif isinstance(data, list):
iter = enumerate(data)
else:
return
for key, value in iter:
if isinstance(value, str) and len(value) > max_length:
data[key] = REPLACEMENT_TEXT
else:
replace_long_values(value, max_length)


def get_email_notification_subscription_table():
client = boto3.client("ssm")
result = client.get_parameter(
Expand Down Expand Up @@ -85,6 +106,10 @@ def kafka_message_to_email(message):
maintype="application",
subtype="xml",
)
elif topic.startswith("gcn.notices."):
valueJson = json.loads(message.value().decode())
replace_long_values(valueJson, 512)
email_message.set_content(json.dumps(valueJson, indent=4))
else:
email_message.add_attachment(
message.value(),
Expand Down

0 comments on commit d9abf9c

Please sign in to comment.