From f98aca05e9df1da05f7e5a1c352480247c456ad9 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Mon, 18 Jan 2021 10:01:45 +0000 Subject: [PATCH] Add translators from CAP to dict MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Other systems we’re working with won’t easily be adapted to emit JSON instead of CAP, so it’s less work for us to do that conversion. This commit adds to code to parse the XML and turn it into a dict that we can work with, including converting the polygon string into native Python lists. --- app/broadcast_message/translators.py | 31 ++++++++++++++++++++++++++++ requirements-app.txt | 1 + requirements.txt | 18 +++++++++------- 3 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 app/broadcast_message/translators.py diff --git a/app/broadcast_message/translators.py b/app/broadcast_message/translators.py new file mode 100644 index 000000000..c8fde2e58 --- /dev/null +++ b/app/broadcast_message/translators.py @@ -0,0 +1,31 @@ +from bs4 import BeautifulSoup + + +def cap_xml_to_dict(cap_xml): + # This function assumes that it’s being passed valid CAP XML + cap = BeautifulSoup(cap_xml, "xml") + return { + "reference": cap.alert.identifier.text, + "category": cap.alert.info.category.text, + "expires": cap.alert.info.expires.text, + "content": cap.alert.info.description.text, + "areas": [ + { + "name": area.areaDesc.text, + "polygons": [ + cap_xml_polygon_to_list(polygon.text) + for polygon in area.find_all('polygon') + ] + } + for area in cap.alert.info.find_all('area') + ] + } + + +def cap_xml_polygon_to_list(polygon_string): + return [ + [ + float(coordinate) for coordinate in pair.split(',') + ] + for pair in polygon_string.split(' ') + ] diff --git a/requirements-app.txt b/requirements-app.txt index a71754683..8688b3922 100644 --- a/requirements-app.txt +++ b/requirements-app.txt @@ -23,6 +23,7 @@ SQLAlchemy==1.3.22 strict-rfc3339==0.7 rfc3987==1.3.8 cachetools==4.2.0 +beautifulsoup4==4.9.3 notifications-python-client==5.7.1 diff --git a/requirements.txt b/requirements.txt index 3e1cefe8c..bb5c70107 100644 --- a/requirements.txt +++ b/requirements.txt @@ -25,6 +25,7 @@ SQLAlchemy==1.3.22 strict-rfc3339==0.7 rfc3987==1.3.8 cachetools==4.2.0 +beautifulsoup4==4.9.3 notifications-python-client==5.7.1 @@ -42,14 +43,14 @@ alembic==1.4.3 amqp==1.4.9 anyjson==0.3.3 attrs==20.3.0 -awscli==1.18.206 +awscli==1.18.215 bcrypt==3.2.0 billiard==3.3.0.23 bleach==3.2.1 blinker==1.4 boto==2.49.0 -boto3==1.16.46 -botocore==1.19.46 +boto3==1.16.55 +botocore==1.19.55 certifi==2020.12.5 chardet==4.0.0 click==7.1.2 @@ -60,19 +61,19 @@ flask-redis==0.4.0 future==0.18.2 geojson==2.5.0 govuk-bank-holidays==0.8 -greenlet==0.4.17 +greenlet==1.0.0 idna==2.10 -importlib-metadata==3.3.0 +importlib-metadata==3.4.0 Jinja2==2.11.2 jmespath==0.10.0 kombu==3.0.37 -Mako==1.1.3 +Mako==1.1.4 MarkupSafe==1.1.1 mistune==0.8.4 monotonic==1.5 orderedset==2.0.3 packaging==20.8 -phonenumbers==8.12.15 +phonenumbers==8.12.16 pyasn1==0.4.8 pycparser==2.20 pyparsing==2.4.7 @@ -86,9 +87,10 @@ PyYAML==5.3.1 redis==3.5.3 requests==2.25.1 rsa==4.5 -s3transfer==0.3.3 +s3transfer==0.3.4 six==1.15.0 smartypants==2.0.1 +soupsieve==2.1 statsd==3.3.0 typing-extensions==3.7.4.3 urllib3==1.26.2