2021-01-18 10:01:45 +00:00
|
|
|
|
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 {
|
2021-02-15 09:32:33 +00:00
|
|
|
|
"msgType": cap.alert.msgType.text,
|
2021-01-18 10:01:45 +00:00
|
|
|
|
"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(' ')
|
|
|
|
|
|
]
|