mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-23 08:51:30 -05:00
Add translators from CAP to dict
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.
This commit is contained in:
31
app/broadcast_message/translators.py
Normal file
31
app/broadcast_message/translators.py
Normal file
@@ -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(' ')
|
||||
]
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user