「天気予報」タグアーカイブ

Web APIをShellで使おう!(jqをCentOS6.5にインストール編)1

みなさん、こんにちは。
Arakawaです。最近CUIがシンプルで心地よく感じる時があり、できるならば
CUIで過ごせないかと時々画策しています。
small__5450429373
photo credit: blakespot via photopin cc

さて、CUIで過ごしていると、どうしてもいろんな情報をCUIで取得したくなります。
天気予報であったり、ニュースであったり、技術情報であったり・・。
そーいうのをLynxでみるのも悪くはないですが、もっとシンプルに
取得したい・・・。思いついたのがWeb APIでした。

Web APIは、XMLやJSONで提供されていることが多いと思います。そこでそれらを
簡単に処理できるアプリがないかなぁ~と思いいろいろググっていますと、
jqというアプリが便利だということがわかってきました。

jqを一度インストールしてみて、いろいろ画策(笑)していきたいと思います。

——————–
インストール実施した内容ログ

1.http://stedolan.github.io/jq/download/からバイナリを適当なところにダウンロードします。

[shell] #cd /tmp
#wget http://stedolan.github.io/jq/download/linux64/jq
[/shell]

2.実行権限を与えました。755にします。

[shell] #chmod 755 jq
[/shell]

3.では、動くかどうか、試してみたいと思います。Web APIでまずテストといえば
天気予報が簡単でよさそうな気がしています。Livedoorさんのサービスをお借りしたいと思います。
http://weather.livedoor.com/weather_hacks/webservice
から京都のデータをゲットしたいともいます。2014/11現在このアドレスでデータが取得できるようです。
http://weather.livedoor.com/forecast/webservice/json/v1?city=260010

では、実際に試してみます。

[shell] #wget -q -O – http://weather.livedoor.com/forecast/webservice/json/v1?city=260010 | ./jq ‘.’
[/shell]

こんな感じで表示されました。

[shell] {
"pinpointLocations": [
{
"link": "http://weather.livedoor.com/area/forecast/2610000",
"name": "京都市"
},
{
"link": "http://weather.livedoor.com/area/forecast/2620400",
"name": "宇治市"
},
{
"link": "http://weather.livedoor.com/area/forecast/2620600",
"name": "亀岡市"
},
{
"link": "http://weather.livedoor.com/area/forecast/2620700",
"name": "城陽市"
},
{
"link": "http://weather.livedoor.com/area/forecast/2620800",
"name": "向日市"
},
{
"link": "http://weather.livedoor.com/area/forecast/2620900",
"name": "長岡京市"
},
{
"link": "http://weather.livedoor.com/area/forecast/2621000",
"name": "八幡市"
},
{
"link": "http://weather.livedoor.com/area/forecast/2621100",
"name": "京田辺市"
},
{
"link": "http://weather.livedoor.com/area/forecast/2621300",
"name": "南丹市"
},
{
"link": "http://weather.livedoor.com/area/forecast/2621400",
"name": "木津川市"
},
{
"link": "http://weather.livedoor.com/area/forecast/2630300",
"name": "大山崎町"
},
{
"link": "http://weather.livedoor.com/area/forecast/2632200",
"name": "久御山町"
},
{
"link": "http://weather.livedoor.com/area/forecast/2634300",
"name": "井手町"
},
{
"link": "http://weather.livedoor.com/area/forecast/2634400",
"name": "宇治田原町"
},
{
"link": "http://weather.livedoor.com/area/forecast/2636400",
"name": "笠置町"
},
{
"link": "http://weather.livedoor.com/area/forecast/2636500",
"name": "和束町"
},
{
"link": "http://weather.livedoor.com/area/forecast/2636600",
"name": "精華町"
},
{
"link": "http://weather.livedoor.com/area/forecast/2636700",
"name": "南山城村"
},
{
"link": "http://weather.livedoor.com/area/forecast/2640700",
"name": "京丹波町"
}
],
"link": "http://weather.livedoor.com/area/forecast/260010",
"forecasts": [
{
"dateLabel": "今日",
"telop": "晴のち曇",
"date": "2014-11-12",
"temperature": {
"min": null,
"max": {
"celsius": "21",
"fahrenheit": "69.8"
}
},
"image": {
"width": 50,
"url": "http://weather.livedoor.com/img/icon/5.gif",
"title": "晴のち曇",
"height": 31
}
},
{
"dateLabel": "明日",
"telop": "曇時々晴",
"date": "2014-11-13",
"temperature": {
"min": {
"celsius": "9",
"fahrenheit": "48.2"
},
"max": {
"celsius": "13",
"fahrenheit": "55.4"
}
},
"image": {
"width": 50,
"url": "http://weather.livedoor.com/img/icon/9.gif",
"title": "曇時々晴",
"height": 31
}
},
{
"dateLabel": "明後日",
"telop": "曇時々晴",
"date": "2014-11-14",
"temperature": {
"min": null,
"max": null
},
"image": {
"width": 50,
"url": "http://weather.livedoor.com/img/icon/9.gif",
"title": "曇時々晴",
"height": 31
}
}
],
"location": {
"city": "京都",
"area": "近畿",
"prefecture": "京都府"
},
"publicTime": "2014-11-12T11:00:00+0900",
"copyright": {
"provider": [
{
"link": "http://tenki.jp/",
"name": "日本気象協会"
}
],
"link": "http://weather.livedoor.com/",
"title": "(C) LINE Corporation",
"image": {
"width": 118,
"link": "http://weather.livedoor.com/",
"url": "http://weather.livedoor.com/img/cmn/livedoor.gif",
"title": "livedoor 天気情報",
"height": 26
}
},
"title": "京都府 京都 の天気",
"description": {
"text": " 近畿地方は、湿った空気の影響で雲が広がり、弱い雨の降っているところ\nがあります。\n\n 今日の京都府は、湿った空気の影響で雲が広がりやすく、午後は日本海か\nら気圧の谷が南下するため、北部では夕方から雨の降るところがあるでしょ\nう。\n\n 明日の京都府は、気圧の谷や寒気の影響で北部を中心に雲が広がり、雨や\n雷雨となるところがある見込みです。",
"publicTime": "2014-11-12T10:36:00+0900"
}
}
[/shell]

ちゃんと整形されていますね。めでたしめでたし。
では、せっかくなので、どこからでもアクセスできるようにしておきましょうかね。
4. 設定

[shell] #mv jq /usr/local/bin/
#wget -q -O – http://weather.livedoor.com/forecast/webservice/json/v1?city=260010 | jq ‘.’
[/shell]

皆さん動きましたか?./ がついていないことを確認してくださいね。
動かない人は、どこにパスが通っているか確認してください。

[shell] #echo $PATH
[/shell]

では、次は、これらを使ってもう少し、面白そうなことをしていこうと思います。
では。($・・)/~~~