weather/weather.sh

82 lines
1.7 KiB
Bash
Raw Permalink Normal View History

2017-12-15 23:45:29 +01:00
#!/bin/bash
. ./.env
LOCATION="Poznan"
IS_CELSIUS=true
get_data() {
local tmp_file="/tmp/weather_$LOCATION.json"
local last_changed=0;
if [ -e "$tmp_file" ]
then
local last_changed=$(stat -c "%Y" $tmp_file)
fi
local current_time=$(date +%s)
local should_request=$((($current_time - $last_changed) > 300))
if (($should_request))
then
curl -s "http://api.apixu.com/v1/current.json?key=$API_KEY&q=$LOCATION" > $tmp_file; #API_KEY comes from .env
fi
WEATHER=$(cat $tmp_file)
}
get_weather() {
local presenter=()
local temp="c"
if [ "$IS_CELSIUS" == false ]
then
temp="f"
fi
clear
get_data
presenter+=("City: $(echo $WEATHER | jq -r '.location.name')")
presenter+=("Country: $(echo $WEATHER | jq -r '.location.country')")
presenter+=("Temperature: $(echo $WEATHER | jq -r '.current.temp_'$temp) ${temp^}")
presenter+=("Feels like: $(echo $WEATHER | jq -r '.current.feelslike_'$temp) ${temp^}")
presenter+=("Wind: $(echo $WEATHER | jq -r '.current.wind_kph')")
presenter+=("Presure: $(echo $WEATHER | jq -r '.current.pressure_mb')")
presenter+=("Humidity: $(echo $WEATHER | jq -r '.current.humidity')")
presenter+=("Cloudy: $(echo $WEATHER | jq -r '.current.cloud')")
echo "$(printf '%s\n' "${presenter[@]}")"
}
while getopts "ldf" opt; do
case $opt in
l)
OPTARG=${!OPTIND}
if [ "$#" -lt "$OPTIND" ]; then
OPTARG=$LOCATION
else
OPTIND=$(( $OPTIND + 1 ))
fi
LOCATION="$OPTARG"
;;
d)
IS_DYNAMIC=true
;;
f)
IS_CELSIUS=false
;;
esac
done
if [ "$IS_DYNAMIC" == true ]
then
while [ true ]
do
get_weather
sleep 300;
done
else
get_weather
fi