[PYTHON] Get information from the Japan Meteorological Agency and notify Slack of weather warnings in the 23 wards of Tokyo

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: image.png

The big picture of Zap

Data provided by the Japan Meteorological Agency is used to obtain weather information, and Slack is notified by filtering and parsing with Python.

image.png

Zap details

Get and filter feeds

This time, the feed is updated from time to time http://www.data.jma.go.jp/developer/xml/feed/extra.xml Is used.

Reference: Japan Meteorological Agency \ | Japan Meteorological Agency Disaster Prevention Information XML Format Telegram (PULL Type)

image.png

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. image.png

Process data with Python

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.

image.png

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. ..

Further filtering

Filter by "whether text exists ". (Because text is empty if there is no corresponding information) image.png

Post to Slack

All you have to do is post the text to the specified channel. image.png

That's it: hugging:

reference

-Japan Meteorological Agency \ | Japan Meteorological Agency Disaster Prevention Information XML Format -JMA Disaster Prevention Information XML Format | Technical Data

Recommended Posts

Get information from the Japan Meteorological Agency and notify Slack of weather warnings in the 23 wards of Tokyo
Scraping and tabulating weather warnings and warnings nationwide from the Japan Meteorological Agency
Download the wind data of the Japan Meteorological Agency
Scraping the rainfall data of the Japan Meteorological Agency and displaying it on M5Stack
Let's get notified of the weather in your favorite area from yahoo weather on LINE!
Get 1000 posts in order of posting from all Slack channels and rank emoji reactions
I want to get rid of import warnings from Pyright and pylint in VS Code
Let's get notified of the weather in your favorite area from yahoo weather on LINE! ~ PART2 ~
Read the GRIB2 file of the Japan Meteorological Agency with pygrib
[Python x Zapier] Get alert information and notify with Slack
Notify the contents of the task before and after executing the task in Fabric
Get the title and delivery date of Yahoo! News in Python
Explanation and implementation of the XMPP protocol used in Slack, HipChat, and IRC
Open an Excel file in Python and color the map of Japan
Slack notification of weather information on OpenWhisk
Get the address from latitude and longitude
Get the last element of the array by splitting the string in Python and PHP
A story about getting the Atom field (XML telegram) of the Japan Meteorological Agency with Raspberry Pi and tweeting it