6
9

More than 1 year has passed since last update.

天気予報APIで翌朝の降水確率を取得してLineで通知する

Posted at

動機と目的

私は朝、会社に行くのが苦痛である。
理由は、早起きが苦手だからである。
そして通勤時の天候で(傘を持っていく/持っていかない)、(早く起きる/起きない)を決めなければならない。
例えば
(a)雨が降っているならば、傘を持って5:00に家を出て徒歩で駅に向かう。
(b)そうでないならば、傘を持たずに5:30に家を出て自転車で駅に向かう。

なので、前日20:00の天気予報で翌日の朝の天気が雨か確認し30分早く起きるかどうかを決定する。
これを自動化すること目的とする。

詳細

処理の流れは以下の通り。
(1) 天気予報APIで翌日の朝6-12の降水確率を取得する。
天気予報API
(2) もし降水確率が40%以上なら、Line notifyで自分に通知する。
PythonでLINE Notifyへ通知を送る

getWeatherForecastAndWarning.py
import requests
import json
import re

def rainWarning(msg):
  line_notify_token = "line notify のアクセストークンを入力"
  line_notify_api = 'https://notify-api.line.me/api/notify'
  headers = {'Authorization': f'Bearer {line_notify_token}'}
  data = {'message': f'message: {msg}'}
  requests.post(line_notify_api, headers = headers, data = data)
# city=の部分に該当する都市のコードを挿入する(下記表参照)
# https://weather.tsukumijima.net/primary_area.xml
url = "https://weather.tsukumijima.net/api/forecast?city=110010"

payload={}
headers = {}

response = requests.request("GET", url, headers=headers, data=payload)
res = response.json()

tomorrow = {}
message = ""
message = res["title"] + "\n"
goingToBeRain = False
possibility = 0
for item in res["forecasts"]:
  if item["dateLabel"] == "明日":
    tomorrow = item
    str = tomorrow["chanceOfRain"]["T06_12"]
    message += "明日の6時から12時の降水確率は" + str
    possibility = int(re.sub('%','',str))
    print(message)
possibility = 50
if possibility >= 40 :
  rainWarning(message)

あとはこのプログラムをwindows タスクスケジューラに登録する。
windows タスクスケジューラの登録方法

これでぎりぎりまで眠れますね。
誰か私の人生も自動化してください。

6
9
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
6
9