Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
気象情報をYOLPから取得し、雨が振りそうだったらGoogleHomeにPOSTします。
import requests
import urllib
import pprint
import json
import struct
import subprocess
def main():
url = makeUrl()
json_data = requestJson(url)
current_rainfall = currentRainfall(json_data)
one_hour_rainfall = oneHourRainfall(json_data)
if speakGoogleHomeIfNeeded(current_rainfall, one_hour_rainfall):
message = ("雨が振りそうです。1時間後の降水確率は、%sパーセントです" % (one_hour_rainfall))
speak_google_home(message)
def makeUrl():
APP_ID = "(YOLP(Yahoo Developer)のAppID)"
BASE_URL = "http://weather.olp.yahooapis.jp/v1/place"
COORDINATES = "(軽度,緯度の順に記載)"
OUTPUT = "json"
yolp_url = BASE_URL + "?appid=%s&coordinates=%s&output=%s" % (APP_ID,COORDINATES,OUTPUT)
return yolp_url
def requestJson(url):
req = requests.get(url)
json_data = req.json()
return json_data
# YOLPの結果から天気情報が入っている箇所の抜き出し
def wetherData(json_data):
return json_data['Feature'][0]['Property']['WeatherList']['Weather']
# 現在の降水確率
def currentRainfall(json_data):
weather = wetherData(json_data)
return weather[0]['Rainfall']
# 1時間後の降水確率
def oneHourRainfall(json_data):
weather = wetherData(json_data)
return weather[6]['Rainfall']
# GoogleHomeに喋らせるか判断させます。ここでは急に雨になりそうな時を検知します
# 条件は以下の通り
# - 1時間後の降水確率の方が、現在より高い
# - 1時間後の降水確率が、今より30%高い
def speakGoogleHomeIfNeeded(current_rainfall, one_hour_rainfall):
if one_hour_rainfall > current_rainfall:
diff = one_hour_rainfall - current_rainfall
if one_hour_rainfall > 50 and diff > 30:
return True
return False
def speak_google_home(message):
url = "http://(google-home-notifierを動かしているIP or ドメイン):(ポート番号)/google-home-notifier"
data = [("text",message),]
req = requests.post(url, data=data)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment