Domo, good evening!
This is the article on the 5th day of Zapier Advent Calendar 2019.
This time,
――I want to detect the weather warning in the 23 wards as soon as possible! ――I want to refer to more probable information instead of a third-party API!
Based on the request, I made a little Zap, so I will introduce it.
I'm sending this to Slack: point_down:
Data provided by the Japan Meteorological Agency is used to obtain weather information, and Slack is notified by filtering and parsing with Python.
This time, the feed is updated from time to time http://www.data.jma.go.jp/developer/xml/feed/extra.xml Is used.
However, if this is left as it is, various information other than disaster information is included, so filter it. Here, only when the title is "Weather Special Warning / Warning / Warning", the next step is performed.
Zapier can execute JavaScript or Python code, but it seemed better to use Python for xml parsing (or rather, it seems to be strict with JavaScript), so I wrote it for the first time.
from xml.etree import ElementTree
response = requests.get(input_data['url'])
response.encoding = 'utf-8'
root = ElementTree.fromstring(response.text)
output = {'text': ''}
for item in root.findall('./{http://xml.kishou.go.jp/jmaxml1/body/meteorology1/}Body/{http://xml.kishou.go.jp/jmaxml1/body/meteorology1/}Warning/{http://xml.kishou.go.jp/jmaxml1/body/meteorology1/}Item'):
code = item.find('./{http://xml.kishou.go.jp/jmaxml1/body/meteorology1/}Area/{http://xml.kishou.go.jp/jmaxml1/body/meteorology1/}Code')
if code.text == '130011' or code.text == '130012':
area_name = item.find('./{http://xml.kishou.go.jp/jmaxml1/body/meteorology1/}Area/{http://xml.kishou.go.jp/jmaxml1/body/meteorology1/}Name')
list = []
for kind in item.findall('./{http://xml.kishou.go.jp/jmaxml1/body/meteorology1/}Kind'):
kind_text = ''
kind_name = kind.find('./{http://xml.kishou.go.jp/jmaxml1/body/meteorology1/}Name')
if kind_name is None:
continue
if not 'alarm' in kind_name.text:
continue
kind_text += kind_name.text + ':'
kind_status = kind.find('./{http://xml.kishou.go.jp/jmaxml1/body/meteorology1/}Status')
kind_text += kind_status.text
list.append(kind_text)
if not list:
continue
text = '【' + area_name.text + '】' + '、'.join(list)
print(text)
output['text'] = output['text'] + text + "\n"
What you are doing
--Process only information in the 23 wards
--Region code is 130011
or 130012
--Process only alarm information
--Put the formatted one for slack posting into text
is. I struggled with the xml namespace rather than Python. ..
Filter by "whether text
exists ".
(Because text
is empty if there is no corresponding information)
All you have to do is post the text
to the specified channel.
That's it: hugging:
-Japan Meteorological Agency \ | Japan Meteorological Agency Disaster Prevention Information XML Format -JMA Disaster Prevention Information XML Format | Technical Data
Recommended Posts