LoginSignup
1
2

More than 5 years have passed since last update.

tmuxのステータス上に天気予報をWebAPIにやさしく表示する

Last updated at Posted at 2016-11-23

tmuxのステータス上に天気予報を小さく表示してみました。
tmuxのステータスインターバルは60秒でAPIに取得制限の項目は見当たりませんでしたが、
1時間に4回程度の取得で済むようにWebAPIにやさしく工夫してみました。
他の定期実行にも使えると思います。

環境: Centos6,7, jq インストール済み

こんな感じ

abc.png

まずは取得部分

天気情報取得のAPIを利用して、作り方は以下を参照してメッセージを "曇り" とかだけにした。

IDは以下から調べる
https://raw.githubusercontent.com/kazeburo/WebService-Livedoor-Weather/master/t/data/primary_area.xml

#270000は大阪
ABC=$(curl -s http://weather.livedoor.com/forecast/webservice/json/v1\?city\=270000 | jq -r '.forecasts[0].telop');

WebAPIにやさしく対応したもの

環境変数に保存して再取得時間まで使いまわすようにします。
ステータスのインターバルが長いとタイミングによって取り損ねるので、
取り逃さないように4で割った分の取得余裕を取りました。
!ステータスインターバルが倍の人は、倍にして余裕を狭めてください。

#!/bin/bash

if [ -z $(echo $ABC)  ];#一回で書けるんだろうなぁ。
then
        ABC=$(curl -s http://weather.livedoor.com/forecast/webservice/json/v1\?city\=270000 | jq -r '.forecasts[0].telop');
        export ABC
fi
if [ $(expr $(date +'%M') / 4) == '0'  ]; 
then
        ABC=$(curl -s http://weather.livedoor.com/forecast/webservice/json/v1\?city\=270000 | jq -r '.forecasts[0].telop');
        export ABC
fi
echo $ABC

.tmux.confに追加する。

set -g status-right ''\
'#(source $HOME/weather.sh)'\

~スクリプトは、動作、WebAPI の制限について確認した上で利用してください~。

1
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2