Last active
May 21, 2019 02:27
-
-
Save yuw-unknown/ffd828367305301bccd1271ec797a910 to your computer and use it in GitHub Desktop.
気象情報をYOLPから取得し、雨が振りそうだったらGoogleHomeにPOSTします。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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