diff --git a/gcn_email/core.py b/gcn_email/core.py index 7537575..5b96d22 100644 --- a/gcn_email/core.py +++ b/gcn_email/core.py @@ -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 @@ -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) @@ -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) @@ -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',