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
Fixes formating and check size of all fields

Adds function to traverse dict and replace content thats too long
  • Loading branch information
dakota002 committed Jan 31, 2024
1 parent beaa77c commit df2cc95
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions gcn_email/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from email.message import EmailMessage
import logging
import os
import json
import sys

import boto3
from boto3.dynamodb.conditions import Key
Expand All @@ -27,10 +29,32 @@
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):
for key, value in data.items():
if isinstance(value, (str, bytes)) and len(value) > max_length:
data[key] = REPLACEMENT_TEXT
else:
replace_long_values(value, max_length)
elif isinstance(data, list):
for index, item in enumerate(data):
if isinstance(item, (str, bytes)) and len(item) > max_length:
data[index] = REPLACEMENT_TEXT
else:
replace_long_values(item, max_length)


def get_email_notification_subscription_table():
client = boto3.client('ssm')
result = client.get_parameter(
Name='/RemixGcnProduction/tables/email_notification_subscription')
Name='/RemixGcnProduction/tables/email_notification_subscription'
)
table_name = result['Parameter']['Value']
return boto3.resource('dynamodb').Table(table_name)

Expand Down Expand Up @@ -65,7 +89,7 @@ def subscribe_to_topics(consumer: Consumer):
# This may need to be updated if new topics have a format different than
# 'gcn.classic.[text | voevent | binary].[topic]'
topics = [
topic for topic in consumer.list_topics().topics
topic for topic in consumer.list_topics().topics
if topic.startswith('gcn.')]
log.info('Subscribing to topics: %r', topics)
consumer.subscribe(topics)
Expand All @@ -80,6 +104,10 @@ def kafka_message_to_email(message):
email_message.add_attachment(
message.value(), filename='notice.xml',
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(), filename='notice.bin',
Expand Down

0 comments on commit df2cc95

Please sign in to comment.