[PYTHON] Get weather information using Yahoo! Open Local Platform (YOLP) and let Raspberry Pi talk with AquesTalkPi

For some reason, if you're looking for a simple weather information, preferably tomorrow or the day after tomorrow, but a span of about ◯◯ minutes from now, Yahoo! Open Local I found Platform (YOLP). I didn't find many use cases, but it was very convenient and interesting, so I will summarize it briefly. As usual, if I can do something that should be added, I will write it in blog.

[About YOLP]

According to the official website

Yahoo! Open Local Platform (YOLP) is an API / SDK for maps and regional information provided by Yahoo! JAPAN for developers.

(Quoted from http://developer.yahoo.co.jp/webapi/map/) And that. There is Weather Information API in this, which is super convenient. The case of commercial use is mentioned at http://www.yahoo-help.jp/app/answers/detail/p/537/a_id/43405. It is accepted. It does not prohibit all use by commercial sites and companies, and if you want to use it for corporations, please consult the inquiry window of each API. "

[Acquire weather information]

Preparation: Get the application ID

Obtain the application ID according to "User's Guide". As for the registered contents, there is nothing to worry about, so I will omit the explanation.

Try it with a python script

While reading Explanation of weather information API, try to get it with Python.

The target URL is http://weather.olp.yahooapis.jp/v1/place regardless of whether it is acquired in XML or JSON. It's a very standard flow that JSON is returned when you get the necessary information as a query and attach it to it.

For the time being, in terms of operation check, the following three are sufficient.

--appid: Application ID. Obtained by the above procedure. -coordinates: latitude and longitude. Separated by commas in the order of longitude and latitude. When searching for multiple points, separate them with a half-width space (up to 10 points) --output: Output format. Either xml / json.

Since the point data that says "it's raining now or is about to rain" is more interesting, it happens to be raining at the time of confirmation, near Shichikashuku Town, Miyagi Prefecture. Try out.

getOtenki.py


#!/usr/bin/python
# encoding:utf-8

import urllib
import pprint
import json

APP_ID = "***************************************"

BASE_URL = "http://weather.olp.yahooapis.jp/v1/place"
COORDINATES = "140.471414,38.026973"
OUTPUT="json"

url = BASE_URL + "?appid=%s&coordinates=%s&output=%s" % (APP_ID,COORDINATES,OUTPUT)
# print (url)

json_tree = json.loads( urllib.urlopen(url).read())
pprint.pprint(json_tree)

The output result looks like this.

{u'Feature': [{u'Geometry': {u'Coordinates': u'140.47141,38.026973',
                             u'Type': u'point'},
               u'Id': u'201609231900_140.47141_38.026973',
               u'Name': u'\u5730\u70b9(140.47141,38.026973)\u306e2016\u5e7409\u670823\u65e5 19\u664200\u5206\u304b\u308960\u5206\u9593\u306e\u5929\u6c17\u60c5\u5831',
               u'Property': {u'WeatherAreaCode': 3420,
                             u'WeatherList': {u'Weather': [{u'Date': u'201609231900',
                                                            u'Rainfall': 5.25,
                                                            u'Type': u'observation'},
                                                           {u'Date': u'201609231910',
                                                            u'Rainfall': 0.95,
                                                            u'Type': u'forecast'},
                                                           {u'Date': u'201609231920',
                                                            u'Rainfall': 2.13,
                                                            u'Type': u'forecast'},
                                                           {u'Date': u'201609231930',
                                                            u'Rainfall': 1.25,
                                                            u'Type': u'forecast'},
                                                           {u'Date': u'201609231940',
                                                            u'Rainfall': 0.55,
                                                            u'Type': u'forecast'},
                                                           {u'Date': u'201609231950',
                                                            u'Rainfall': 0.0,
                                                            u'Type': u'forecast'},
                                                           {u'Date': u'201609232000',
                                                            u'Rainfall': 1.75,
                                                            u'Type': u'forecast'}]}}}],
 u'ResultInfo': {u'Copyright': u'(C) Yahoo Japan Corporation.',
                 u'Count': 1,
                 u'Description': u'',
                 u'Latency': 0.004469,
                 u'Start': 1,
                 u'Status': 200,
                 u'Total': 1}}

The data you want exists in json_tree ['Feature'] [0] ['Property'] ['WeatherList'] ['Weather'], so you can get the necessary data like this.

#!/usr/bin/python
# encoding:utf-8

import urllib
import pprint
import json

APP_ID = "******************************"

BASE_URL = "http://weather.olp.yahooapis.jp/v1/place"
COORDINATES = "140.471414,38.026973"
OUTPUT="json"

url = BASE_URL + "?appid=%s&coordinates=%s&output=%s" % (APP_ID,COORDINATES,OUTPUT)
# print (url)

json_tree = json.loads( urllib.urlopen(url).read())
#pprint.pprint(json_tree)

for var in range(0,7):
    date = json_tree['Feature'][0]['Property']['WeatherList']['Weather'][var]['Date']
    rainfall = json_tree['Feature'][0]['Property']['WeatherList']['Weather'][var]['Rainfall']
    type = json_tree['Feature'][0]['Property']['WeatherList']['Weather'][var]['Type']
    print("%s,%s,%s"%(date,rainfall,type))

The output result looks like this. Looking at the Response field, in the Type field, observation is the measured value and forecast is the predicted value. Is shown. In addition, the Rainfall field is the precipitation intensity (it is not the amount of precipitation because it is the value obtained by converting the observed intensity of precipitation into mm / h). Hmmmm.

201609231945,0.85,observation
201609231955,0.0,forecast
201609232005,2.13,forecast
201609232015,1.45,forecast
201609232025,1.45,forecast
201609232035,1.95,forecast
201609232045,0.0,forecast

I want to get the latitude and longitude from the zip code and forecast the weather

I was able to meet the minimum requirements above, but it is not good to specify the coordinates one by one, so the Zip Code Search API also provided by YOLP I want to use (/webapi/map/openlocalplatform/v1/zipcodesearch.html) to "take a zip code as an argument and pull the coordinates without permission".

This is also not interesting unless it is about to get off, so I will specify the zip code for the Oi Matsuda IC that is about to get off. Also, if the zip code is not specified, the data near Hamarikyu will be displayed.

#!/usr/bin/python
# encoding:utf-8

import urllib
import pprint
import json
import sys

APP_ID = "***********************************"

BASE_URL = "http://weather.olp.yahooapis.jp/v1/place"
COORDINATES = "139.763707,35.659729"
OUTPUT="json"

if len(sys.argv) == 2:
    zip_code = sys.argv[1]
    ZIP_BASE_URL = "http://search.olp.yahooapis.jp/OpenLocalPlatform/V1/zipCodeSearch"
    zip_url = ZIP_BASE_URL + "?appid=%s&query=%s&output=%s" % (APP_ID,zip_code,OUTPUT)
    zip_json_tree = json.loads(urllib.urlopen(zip_url).read())
    #pprint.pprint(zip_json_tree['Feature'][0]['Geometry']['Coordinates'])
    COORDINATES = zip_json_tree['Feature'][0]['Geometry']['Coordinates']


url = BASE_URL + "?appid=%s&coordinates=%s&output=%s" % (APP_ID,COORDINATES,OUTPUT)
# print (url)

json_tree = json.loads( urllib.urlopen(url).read())
#pprint.pprint(json_tree)

for var in range(0,7):
    date = json_tree['Feature'][0]['Property']['WeatherList']['Weather'][var]['Date']
    rainfall = json_tree['Feature'][0]['Property']['WeatherList']['Weather'][var]['Rainfall']
    type = json_tree['Feature'][0]['Property']['WeatherList']['Weather'][var]['Type']
    print("%s,%s,%s"%(date,rainfall,type))

# python sample.py "258-0019"
201609232015,0.0,observation
201609232025,0.0,forecast
201609232035,0.85,forecast
201609232045,16.5,forecast
201609232055,4.38,forecast
201609232105,0.0,forecast
201609232115,0.0,forecast

# python sample.py
201609232015,0.0,observation
201609232025,0.0,forecast
201609232035,0.0,forecast
201609232045,0.0,forecast
201609232055,0.0,forecast
201609232105,0.0,forecast
201609232115,0.0,forecast

Umm. I was able to do what I wanted to do.

[Speak]

There is a program called AquesTalkPi that makes Raspberry Pi speak, which I haven't learned before. Let's process the previous output appropriately and let it speak. I'd really like to be a little smarter, but for now I'll do something about it.

#!/usr/bin/python
# encoding:utf-8

import urllib
import pprint
import json
import sys
import os

APP_ID = "********************************************"

BASE_URL = "http://weather.olp.yahooapis.jp/v1/place"
COORDINATES = "139.763707,35.659729"
OUTPUT="json"
ZIP_CODE = "100-0000"

if (len(sys.argv) == 2) or (len(ZIP_CODE) > 0):
    if len(sys.argv) < 2:
        zip_code = ZIP_CODE
    else:
        zip_code = sys.argv[1]
    ZIP_BASE_URL = "http://search.olp.yahooapis.jp/OpenLocalPlatform/V1/zipCodeSearch"
    zip_url = ZIP_BASE_URL + "?appid=%s&query=%s&output=%s" % (APP_ID,zip_code,OUTPUT)
    zip_json_tree = json.loads(urllib.urlopen(zip_url).read())
    #pprint.pprint(zip_json_tree['Feature'][0]['Geometry']['Coordinates'])
    COORDINATES = zip_json_tree['Feature'][0]['Geometry']['Coordinates']
    name = zip_json_tree['Feature'][0]['Property']['Address']
    print name


url = BASE_URL + "?appid=%s&coordinates=%s&output=%s" % (APP_ID,COORDINATES,OUTPUT)
# print (url)

json_tree = json.loads( urllib.urlopen(url).read())
#pprint.pprint(json_tree)

for var in range(0,7):
    date     = json_tree['Feature'][0]['Property']['WeatherList']['Weather'][var]['Date']
    rainfall = json_tree['Feature'][0]['Property']['WeatherList']['Weather'][var]['Rainfall']
    type     = json_tree['Feature'][0]['Property']['WeatherList']['Weather'][var]['Type']
    print("%s,%s,%s"%(date,rainfall,type))
    rain_level = ""
    talk = ""
    if (rainfall == 0.0):
        rain_level = "It rains"
    elif (rainfall < 5.0) :
        rain_level = "It rains a little"
    elif (rainfall < 10.0):
        rain_level = "It rains a lot"
    elif (rainfall < 20.0):
        rain_level = "It rains a little"
    elif (rainfall < 30.0):
        rain_level = "It's raining down"
    elif (rainfall < 50.0):
        rain_level = "Heavy rain"
    elif (rainfall < 80.0):
        rain_level = "Very heavy rain"
    elif (rainfall >= 80.0):
        rain_level = "Heavy rain"

    if type == "observation" :
        time = "now,"
        if rainfall == 0.0:
            suffix = "Not"
            talk = time + rain_level + suffix
        else:
            suffix = "I have"
            talk = time + rain_level + suffix
    else:
        time = str(var * 10) + "After a minute"
        if rainfall == 0.0:
            # suffix = "I don't."
            talk = ""
        else:
            suffix = "It seems to be."
            talk = time + rain_level + suffix

    print talk
    if len(talk) > 0:
        os.system('/home/pi/AquesTalkPi "' + talk + '" | aplay&')

Oh, speak with "that voice"! This is interesting.

That's it for today.

Recommended Posts

Get weather information using Yahoo! Open Local Platform (YOLP) and let Raspberry Pi talk with AquesTalkPi
Get the weather using the API and let the Raspberry Pi speak!
Get BITCOIN LTP information with Raspberry PI
Get CPU information of Raspberry Pi with Python
Get temperature and humidity with DHT11 and Raspberry Pi
YOLP Get map information XML file with Yahoo! Static Map API
Create your own IoT platform using raspberry pi and ESP32 (Part 1)
Using a webcam with Raspberry Pi
Get weather information with Python & scraping
[Yahoo! Weather Replacement Version] How to get weather information with LINE Notify + Python
Get GrovePi + sensor value with Raspberry Pi and store it in kintone
Pet monitoring with Rekognition and Raspberry pi
Connect to the console of Raspberry PI and display local IP and SD information
Ask the bot to tell you the weather (precipitation information) using the weather information API (YOLP) provided by Yahoo ~ slack bot development with python ④ ~