1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Pythonによる気象庁APIからの降水確率の監視と通知

Last updated at Posted at 2022-01-02

気象庁の半分公式のようなAPIを使って、気象データを監視し、降水確率が50%以上のときには通知をするというサンプルコードです。
通知の部分はLINEAPIなどの通知を使ってください。

import json
import requests
import time

def weatherMan():
    # 気象庁データの取得
    jma_url = 'https://www.jma.go.jp/bosai/forecast/data/forecast/170000.json'
    data = requests.get(jma_url).json()
    
    # 時間帯の取得
    pop_0_time = data[0]["timeSeries"][1]["timeDefines"][0]
    pop_1_time = data[0]["timeSeries"][1]["timeDefines"][1]
    pop_2_time = data[0]["timeSeries"][1]["timeDefines"][2]
    pop_3_time = data[0]["timeSeries"][1]["timeDefines"][3]
    pop_4_time = data[0]["timeSeries"][1]["timeDefines"][4]

    # 地域の取得
    pop_0_name = data[0]["timeSeries"][1]["areas"][0]["area"]["name"]
    pop_1_name = data[0]["timeSeries"][1]["areas"][0]["area"]["name"]
    pop_2_name = data[0]["timeSeries"][1]["areas"][0]["area"]["name"]
    pop_3_name = data[0]["timeSeries"][1]["areas"][0]["area"]["name"]
    pop_4_name = data[0]["timeSeries"][1]["areas"][0]["area"]["name"]

    # 降水確率の取得
    pop_0 = data[0]["timeSeries"][1]["areas"][0]["pops"][0]
    pop_1 = data[0]["timeSeries"][1]["areas"][0]["pops"][1]
    pop_2 = data[0]["timeSeries"][1]["areas"][0]["pops"][2]
    pop_3 = data[0]["timeSeries"][1]["areas"][0]["pops"][3]
    pop_4 = data[0]["timeSeries"][1]["areas"][0]["pops"][4]

    # 表示
    print(pop_0_name + ":" + str(pop_0_time) + ":" + str(pop_0))
    print(pop_1_name + ":" + str(pop_1_time) + ":" + str(pop_1))
    print(pop_2_name + ":" + str(pop_2_time) + ":" + str(pop_2))
    print(pop_3_name + ":" + str(pop_3_time) + ":" + str(pop_3))
    print(pop_4_name + ":" + str(pop_4_time) + ":" + str(pop_4))
    
    if (int(pop_0) >= 50):
        print("今雨が降っているようです" + " 降水確率:" + str(pop_0))
    if (int(pop_1) >= 50):
        print("数時間後に雨が降るようです!" + " 降水確率:" + str(pop_1))
        sendNotify()
    time.sleep(5)
    weatherMan()
    
def sendNotify():
    print("雨が降る確率が高いです")

weatherMan()
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?