[Python x Zapier] Get alert information and notify with Slack

What you want to do (BEFORE)

** Get the warning information of the Japan Meteorological Agency and notify by Slack if there is a warning in the specified area **

This article What you want to do is Don Pisha! I thought that I was proceeding according to the article, but every time the data of the Japan Meteorological Agency is updated, I am running the task of Zapier (a tool that can automate the work) My plan exceeds the maximum task ...

screencapture-zapier-app-billing-plans-2020-02-09-17_41_58.png

So I narrowed down the scope of what I wanted to do.

What you want to do (AFTER)

** Get the warning information of the Japan Meteorological Agency at "7:00 every morning" and notify you with Slack if there is a warning in the designated area ** ▼ It looks like this (it looks like I got a warning instead because there was no alarm when I moved it) I got the information only once every morning at 7 o'clock and set the number of task executions to once a day.

Zap overview

  1. Every Day: Set to run the task here every morning at 7am
  2. Run Python: Get / extract information from Japan Meteorological Agency (code below)
  3. Only continue if ...: If there is an output in 2, proceed to the next (if an alarm is issued)
  4. Send Channel Message in Slack: Notify on the channel specified by Slack

Python code

I found out how to scrape and wrote it because I thought I had to study because what I wanted to do changed a little. First Python! When I was researching, I found out that I would use something called Beautiful Soup, but Zapier Help ) Wrote something like this. スクリーンショット 2020-02-09 18.23.51.png Oh, oh, I can't use Beautiful Soup! There is a feeling of force, but I will extract only the desired part (alarm part) without using Beautiful Soup.

import re
import requests
import time

code_list = [['Sapporo', '0110000'], ['Eastern Sendai City', '0410001'], ['Saitama City', '1110000']]
output = {'text': ''}

for code in code_list:
    html = requests.get('https://www.jma.go.jp/jp/warn/f_' + code[1] + '.html').text
    data_list = re.findall('<span style="color:#FF2800">(.*?)</span>', html)

    if len(data_list) == 0:
        continue

    text = '【' + code[0] + '】'
    i = 1
    for data in data_list:
        if i == len(data_list):
            text += data
        else:
            data = re.sub('alarm', '', data)
            text  += data + ','
        i += 1
    output['text'] += text + '\n'
    time.sleep(1)

Since the font color of the alarm part is # FF2800 on the page of each region, all the parts surrounded by<span style = "color: # FF2800"> ~ </ span>are extracted. Also, since it scrapes multiple pages, rest for 1 second with time.sleep (1).

reference

Obtain information from the Japan Meteorological Agency and notify Slack of weather warnings in the 23 wards of Tokyo I was very helpful including how to use Zapier.

Recommended Posts

[Python x Zapier] Get alert information and notify with Slack
Get Alembic information with Python
[Python] Get user information and article information with Qiita API
Install Python 2.7.9 and Python 3.4.x with pip.
Get weather information with Python & scraping
Site monitoring and alert notification with AWS Lambda + Python + Slack
Get property information by scraping with python
[Python] Get Python package information with PyPI API
[Blender x Python] Let's get started with Blender Python !!
[Yahoo! Weather Replacement Version] How to get weather information with LINE Notify + Python
Get iPad maintenance by scraping and notify Slack
Get Gmail subject and body with Python and Gmail API
Python script to get note information with REAPER
[Python] Get product information such as ASIN and JAN with Amazon PA-API ver5.0
Get date with python
Get media timeline images and videos with Python + Tweepy
Get comments on youtube Live with [python] and [pytchat]!
Get mail from Gmail and label it with Python3
Get country code with python
Programming with Python and Tkinter
x86 compiler self-made with python
Encryption and decryption with Python
Word Count with Apache Spark and python (Mac OS X)
Python and hardware-Using RS232C with Python-
Compare HTTP GET / POST with cURL (command) and Python (programming)
Get Twitter timeline with python
Post to slack with Python 3
Test Python with Miniconda on OS X and Linux with travis-ci
Send experiment results (text and images) to slack with Python
Get rid of dirty data with Python and regular expressions
Streamline information gathering with the Twitter API and Slack bots
Get started with Python! ~ ② Grammar ~
python with pyenv and venv
Collecting information from Twitter with Python (MySQL and Python work together)
[python] Get quotient and remainder
Get stock price with Python
Sample of HTTP GET and JSON parsing with python of pepper
Get home directory with python
Get keyboard events with python
With skype, notify with skype from python!
Get additional data to LDAP with python (Writer and Reader)
Try Juniper JUNOS PyEz (python library) Memo 2 ~ Get information with PyEz ~
Works with Python and R
Get information from the Japan Meteorological Agency and notify Slack of weather warnings in the 23 wards of Tokyo
Get started with the Python framework Django on Mac OS X
Get conversions and revenue with Google Analytics API and report to Slack
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
Try to display google map and geospatial information authority map with python
Install lp_solve on Mac OS X and call it with python.
I tried ChatOps with Slack x API Gateway x Lambda (Python) x RDS
PhytoMine-I tried to get the genetic information of plants with Python
Communicate with FX-5204PS with Python and PyUSB
Get video file information with ffmpeg-python
Robot running with Arduino and python
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
Link to get started with python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
Scraping with Python, Selenium and Chromedriver
Get the weather with Python requests