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:
Chris Hill-Scott
2021-01-18 10:01:45 +00:00
parent 7530408a21
commit f98aca05e9
3 changed files with 42 additions and 8 deletions

View File

@@ -0,0 +1,31 @@
from bs4 import BeautifulSoup
def cap_xml_to_dict(cap_xml):
# This function assumes that its 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(' ')
]

View File

@@ -23,6 +23,7 @@ SQLAlchemy==1.3.22
strict-rfc3339==0.7 strict-rfc3339==0.7
rfc3987==1.3.8 rfc3987==1.3.8
cachetools==4.2.0 cachetools==4.2.0
beautifulsoup4==4.9.3
notifications-python-client==5.7.1 notifications-python-client==5.7.1

View File

@@ -25,6 +25,7 @@ SQLAlchemy==1.3.22
strict-rfc3339==0.7 strict-rfc3339==0.7
rfc3987==1.3.8 rfc3987==1.3.8
cachetools==4.2.0 cachetools==4.2.0
beautifulsoup4==4.9.3
notifications-python-client==5.7.1 notifications-python-client==5.7.1
@@ -42,14 +43,14 @@ alembic==1.4.3
amqp==1.4.9 amqp==1.4.9
anyjson==0.3.3 anyjson==0.3.3
attrs==20.3.0 attrs==20.3.0
awscli==1.18.206 awscli==1.18.215
bcrypt==3.2.0 bcrypt==3.2.0
billiard==3.3.0.23 billiard==3.3.0.23
bleach==3.2.1 bleach==3.2.1
blinker==1.4 blinker==1.4
boto==2.49.0 boto==2.49.0
boto3==1.16.46 boto3==1.16.55
botocore==1.19.46 botocore==1.19.55
certifi==2020.12.5 certifi==2020.12.5
chardet==4.0.0 chardet==4.0.0
click==7.1.2 click==7.1.2
@@ -60,19 +61,19 @@ flask-redis==0.4.0
future==0.18.2 future==0.18.2
geojson==2.5.0 geojson==2.5.0
govuk-bank-holidays==0.8 govuk-bank-holidays==0.8
greenlet==0.4.17 greenlet==1.0.0
idna==2.10 idna==2.10
importlib-metadata==3.3.0 importlib-metadata==3.4.0
Jinja2==2.11.2 Jinja2==2.11.2
jmespath==0.10.0 jmespath==0.10.0
kombu==3.0.37 kombu==3.0.37
Mako==1.1.3 Mako==1.1.4
MarkupSafe==1.1.1 MarkupSafe==1.1.1
mistune==0.8.4 mistune==0.8.4
monotonic==1.5 monotonic==1.5
orderedset==2.0.3 orderedset==2.0.3
packaging==20.8 packaging==20.8
phonenumbers==8.12.15 phonenumbers==8.12.16
pyasn1==0.4.8 pyasn1==0.4.8
pycparser==2.20 pycparser==2.20
pyparsing==2.4.7 pyparsing==2.4.7
@@ -86,9 +87,10 @@ PyYAML==5.3.1
redis==3.5.3 redis==3.5.3
requests==2.25.1 requests==2.25.1
rsa==4.5 rsa==4.5
s3transfer==0.3.3 s3transfer==0.3.4
six==1.15.0 six==1.15.0
smartypants==2.0.1 smartypants==2.0.1
soupsieve==2.1
statsd==3.3.0 statsd==3.3.0
typing-extensions==3.7.4.3 typing-extensions==3.7.4.3
urllib3==1.26.2 urllib3==1.26.2