himorogiの日記

主にプログラミングに関することなど。少々ハード(電子工作)についても。

【macOS】PowerShell から WebAPI を呼ぶ

Web Access

PowerShellmacOS にやってきて、.Net 周りの機能はいろいろ入ってなかったので、てっきり WebClient (というか DownloadFile/DownloadString)は使えないと思っていたのだけど、確認したら既に使えるようになってたし、Invoke-RestMethod や Invoke-WebRequest も使えた。

Yahoo! Open Local Platform

以前 GoogleMap の StreetView をダウンロードする script を公開したけど、今は無償利用でも GoogleMap では決済情報の登録が必須になったようなので、今回は Yahoo! の地図・地域情報を利用した。

YOLP(地図):YOLP(地図) - Yahoo!デベロッパーネットワークdeveloper.yahoo.co.jp

事前準備

Yahoo! の WebAPI は決済情報の登録は不要だけど、WebAPI の呼びしに Client ID が必要になるので予め YahooID を作成し、それに紐付いたアプリケーション毎の Client ID を取得する必要がある。

ご利用ガイド - Yahoo!デベロッパーネットワークdeveloper.yahoo.co.jp

早速やってみる

Yahoo!ID を作成し、Client ID を取得したら、実際にやってみよう。
今回は、気象情報API で特定の場所の降水強度を実測値・予測値として受け取る。

YOLP(地図):気象情報API - Yahoo!デベロッパーネットワークdeveloper.yahoo.co.jp

気象情報API では、場所の指定に経度・緯度を渡す必要がある。
そこで、住所文字列からジオコーダAPI で経度・緯度を取得しておく。

YOLP(地図):Yahoo!ジオコーダAPI - Yahoo!デベロッパーネットワークdeveloper.yahoo.co.jp

Yahoo!ジオコーダAPI を呼ぶ

Client ID は 変数 $CID に格納しておく。
PowerShell から WebAPI を呼び出す方法はいくつかあるけど、ここでは WebClient の DownloadString を使う。
Yahoo! WebAPI の戻り値のデータ・フォーマットは XML が既定値だけど JSONP も選択できる。
PowerShell では XML を簡単に Custom Object へ展開できるので、ここはデフォルトで。

リクエストURL+パラメータ文字列
https://map.yahooapis.jp/geocode/V1/geoCoder?appid=<Client ID>&query=<住所文字列>

手順は以下の通り。

PS /Users/hoge/devPwsh> $CID="<取得した Clinent ID>"
PS /Users/hoge/devPwsh> $yolp = "https://map.yahooapis.jp"        
PS /Users/hoge/devPwsh> $wc = New-Object System.Net.WebClient
PS /Users/hoge/devPwsh> $x = [xml]$($wc.DownloadString( "$yolp/geocode/V1/geoCoder?appid=$CID&query=" + "東京都港区虎ノ門1丁目23"))

というわけで、展開してみる。

PS /Users/hoge/devPwsh> $x

xml                            YDF #comment
---                            --- --------
version="1.0" encoding="utf-8" YDF  xxxcache nohit 0.084, 0.001, 0.001 

PS /Users/hoge/devPwsh> $x.YDF        

xmlns                 : http://olp.yahooapis.jp/ydf/1.0
totalResultsReturned  : 5
totalResultsAvailable : 5
firstResultPosition   : 1
ResultInfo            : ResultInfo
Feature               : {東京都港区虎ノ門1丁目23, 東京都港区虎ノ門1丁目23-3, 東京都港区虎ノ門1丁目23-1, 東京都港区虎ノ門1丁目23-4…}

PS /Users/hoge/devPwsh>

Yahoo!ジオコーダAPI は、該当する住所がデータベースになかったり、また複数の住所がヒット(ここでは枝番の存在)したときに、それらを含めて最大10件の住所情報を返す。(REST request のパラメータ、results(=表示件数)のデフォルトが10件なので。これは最大100件まで指定可能)
今回、最初にヒットした住所がそまま使えるので…

PS /Users/hoge/devPwsh> $x.YDF.Feature[0]

Id          : 13103.18.1.23
Gid         : 
Name        : 東京都港区虎ノ門1丁目23
Geometry    : Geometry
Category    : 
Description : 
Style       : 
Property    : Property

PS /Users/hoge/devPwsh> $x.YDF.feature[0].Geometry                 

Type  Coordinates              BoundingBox
----  -----------              -----------
point 139.75022366,35.66905771 139.74074600,35.66094700 139.75178800,35.67094600

PS /Users/hoge/devPwsh> $x.YDF.feature[0].Geometry.Coordinates
139.75022366,35.66905771
PS /Users/hoge/devPwsh> 

これで、東京都港区虎ノ門1丁目23 の経度・緯度が取得できた。

Yahoo!気象情報API を呼ぶ

PowerShell 3.0 から、Invoke-RestMethod と Invoke-WebRequet が使えるようになったので、今度は Invoke-RestMethod を使ってみる。

リクエストURL+パラメータ文字列
https://map.yahooapis.jpweather/V1/place?appid=<Client ID>&coordinates=<経度>,<緯度>

手順は以下の通り。
Yahoo! の WebAPI を利用するなら Invoke-RestMethod が一番手間がかからない。

PS /Users/hoge/devPwsh> $yolp = "https://map.yahooapis.jp" 
PS /Users/hoge/devPwsh> $y = Invoke-RestMethod -Uri $("$yolp/weather/V1/place?appid=$CID&coordinates=" + "139.75001544,35.66663019")
PS /Users/hoge/devPwsh> $y

xml                            YDF
---                            ---
version="1.0" encoding="UTF-8" YDF

PS /Users/hoge/devPwsh> $y.YDF

xmlns                 : http://olp.yahooapis.jp/ydf/1.0
firstResultPosition   : 1
totalResultsAvailable : 1
totalResultsReturned  : 1
ResultInfo            : ResultInfo
Feature               : Feature


PS /Users/hoge/devPwsh> $y.YDF.Feature

Id                              Name                                                                  Geometry Property
--                              ----                                                                  -------- --------
202102071115_139.75002_35.66663 地点(139.75002,35.66663)の2021年02月07日 11時15分から60分間の天気情報 Geometry Property

PS /Users/hoge/devPwsh>

正常に取得できてる模様なので、早速展開してみる。

PS /Users/hoge/devPwsh> $y.YDF.feature.Property

WeatherAreaCode WeatherList
--------------- -----------
4410            WeatherList

PS /Users/hoge/devPwsh> $y.YDF.feature.Property.WeatherList

Weather
-------
{Weather, Weather, Weather, Weather…}

PS /Users/hoge/devPwsh> $y.YDF.feature.Property.WeatherList.Weather

Type        Date         Rainfall
----        ----         --------
observation 202102071115 0.00
forecast    202102071125 0.00
forecast    202102071135 0.00
forecast    202102071145 0.00
forecast    202102071155 0.00
forecast    202102071205 0.00
forecast    202102071215 0.00

PS /Users/hoge/devPwsh>

降水強度(気象レーダーからの換算値)が獲得できた。