Skip to content

Instantly share code, notes, and snippets.

@kernelhcy
Last active March 11, 2020 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kernelhcy/415a8db2bc243d41d054f62e087c9216 to your computer and use it in GitHub Desktop.
Save kernelhcy/415a8db2bc243d41d054f62e087c9216 to your computer and use it in GitHub Desktop.
get current location's current weather
#! /bin/bash
#set -x
VERSION="1.6.1"
JQ_BIN=jq
LAST_TIME_FILE='/tmp/wea_last_time'
LAST_WEATHER='/tmp/wea_last_weather'
MAX_TIME_GAP=2000
function error()
{
now=`date +"%Y/%m/%d %H:%M:%S"`
echo "${now} [ERROR] --> $1" >> /tmp/weather_log
}
function info()
{
now=`date +"%Y/%m/%d %H:%M:%S"`
echo "${now} [INFO] --> $1" >> /tmp/weather_log
}
function queryWeather()
{
local data=`https_proxy= curl -s "https://www.tianqiapi.com/api/?version=v6&appid=76961778&appsecret=K1Evb4rc"`
local moon=`http_proxy= curl -s 'http://zh.wttr.in?format=%m&period=60'`
echo "" >> /tmp/weather_log
echo "" >> /tmp/weather_log
info "$data"
info "$moon"
if [ -z "$data" ] #-o -z "$moon" -o ${#moon} -ge "10" ]
then
echo -n ""
else
extractWeather "$data" "$moon"
fi
}
function extractWeather()
{
local data=$1
local moon=$2
local city=`echo -n $data | $JQ_BIN -cMr '.city'`
if [ -z "$city" ]
then
error "extract city error. $data"
echo "$data" > /tmp/weather_data_log
exit -1
fi
info "[city] $city"
local wea=`echo -n $data | $JQ_BIN -cMr '.wea'`
local wea_img=''
case `echo -n $data | $JQ_BIN -cMr '.wea_img'` in
lei) wea_img='⚡️';;
qing) wea_img='☀️ ';;
shachen) wea_img='😷 ';;
wu) wea_img='🌫 ';;
xue) wea_img='❄️ ';;
yu) wea_img='🌧 ';;
yujiaxue) wea_img='🌨 ';;
yun) wea_img='⛅️ ';;
zhenyu) wea_img='🌧 ';;
yin) wea_img='☁️ ';;
bingbao) wea_img='🧊 ';;
*) wea_img='';;
esac
if [ -z "$wea" ]
then
error "extract weather error. $data"
exit -1
fi
info "[weather] $wea_img $wea"
local air_pm25=`echo -n $data | $JQ_BIN -cMr '.air_pm25'`
local air_level=`echo -n $data | $JQ_BIN -cMr '.air_level'`
if [ -z "$air_pm25" -o -z "$air_level" ]
then
error "extract air error. $data"
exit -1
fi
info "[air] $air_pm25 $air_level"
local humidity=`echo -n $data | $JQ_BIN -cMr '.humidity'`
if [ -z "$humidity" ]
then
error "extract humidity error. $data"
exit -1
fi
info "[humidity] $humidity"
local tem=`echo -n $data | $JQ_BIN -cMr '.tem'`
if [ -z "$tem" ]
then
error "extract temperature error. $data"
exit -1
fi
info "[temperature] $tem"
local pressure=`echo -n $data | $JQ_BIN -cMr '.pressure'`
if [ -z "$pressure" ]
then
error "extract pressure error. $data"
exit -1
fi
info "[pressure] $pressure"
local visibility=`echo -n $data | $JQ_BIN -cMr '.visibility'`
if [ -z "$visibility" ]
then
error "extract visibility error. $data"
exit -1
fi
info "[visibility] $visibility"
local win_speed=`echo -n $data | $JQ_BIN -cMr '.win_speed'`
local win_meter=`echo -n $data | $JQ_BIN -cMr '.win_meter'`
local win=`echo -n $data | $JQ_BIN -cMr '.win'`
local win_arrow=''
case $win in
东南风) win_arrow='↖';;
南风) win_arrow='↑';;
西南风) win_arrow='↗';;
东风) win_arrow='←';;
西风) win_arrow='→';;
东北风) win_arrow='↙';;
北风) win_arrow='↓';;
西北风) win_arrow='↘';;
*) win_arrow='';;
esac
if [ -z "$win_speed" -o -z "$win" ]
then
error "extract wind error. $data"
exit -1
fi
info "[wind] $win_arrow $win_speed $win_meter"
echo -n "$city: $wea_img$wea ${tem}℃ $humidity $win_arrow $win_speed [$air_pm25]$air_level $moon $visibility"
}
function queryAndUpdateCache()
{
weather=`queryWeather`
if [ -z "$weather" ]
then
rm -rf "$LAST_TIME_FILE"
rm -rf "$LAST_WEATHER"
echo -n "request error"
else
echo -n "$weather" > "$LAST_WEATHER"
echo -n "$currentTime" > "$LAST_TIME_FILE"
fi
}
if [ ! -f $LAST_TIME_FILE ]
then
echo -n "0" > "$LAST_TIME_FILE"
fi
lastTime=`cat $LAST_TIME_FILE 2>/dev/null`
currentTime=`date +%s`
gap=$[currentTime-lastTime]
case $1 in
--update-time)
lastTimeStr=`date -r $lastTime +"%Y/%m/%d %H:%M:%S"`
echo "Last Time: $lastTimeStr"
nextTimeStr=`date -r $[lastTime+MAX_TIME_GAP] +"%Y/%m/%d %H:%M:%S"`
echo "Next Time: $nextTimeStr"
leftSeconds=$[MAX_TIME_GAP-gap]
echo "Left Seconds: ${leftSeconds}s"
exit 0
;;
-f|--force)
queryAndUpdateCache
;;
-h|--help)
echo "Weather.sh - print current location's current weather."
echo "Usage: weather.sh"
echo " or: weather.sh -f"
echo ""
echo "Arguments:"
echo " --update-time show updating time of the cache"
echo " -f or --force force update weather and print (do not use cache)"
echo " -h or --help Print Help (this message) and exit"
echo " -v or --version Print version information and exit"
echo ""
exit 0
;;
-v|--version)
echo "Weather.sh v$VERSION"
exit 0
;;
*)
;;
esac
if [ $gap -ge $MAX_TIME_GAP ]
then
queryAndUpdateCache
fi
cat "$LAST_WEATHER" 2>/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment