ansiweather

d
Nombre: ansiweather
Autor: Frederic Cambus
Más información del script en fcambus
#!/usr/bin/env bash

###############################################################################
#                                                                             #
# AnsiWeather 1.00 (c) by Frederic Cambus 2013                                #
# https://github.com/fcambus/ansiweather                                      #
#                                                                             #
# Created: 2013/08/29                                                         #
# Last Updated: 2013/10/19                                                    #
#                                                                             #
# AnsiWeather is released under the BSD 3-Clause license.                     #
# See LICENSE file for details.                                               #
#                                                                             #
###############################################################################



###[ Configuration options ]###################################################

config_file=~/.ansiweatherrc

function get_config {
        ret=""
        if [ -f $config_file ]
        then
                ret=$(grep $1 $config_file | awk -F\: '{print $2}')
        fi

        if [ "X$ret" = "X" ]
        then
                return 1
        else
                echo $ret
        fi
}

# Location : example "Moscow,RU"
if [ ! -z "$1" ]
then
        location=$1;
else
        location=$(get_config "location" || echo "Moscow,RU")
fi

# System of Units : "metric" or "imperial"
if [ ! -z "$2" ]
then
        units=$2
else
        units=$(get_config "units" || echo "metric")
fi

# Display symbols : "true" or "false" (requires an Unicode capable display)
symbols=$(get_config "symbols" || echo true)


#### [ Colors and characters ]#################################################

background=$(get_config "background" || echo "\033[44m")
text=$(get_config "text" || echo "\033[36;1m")
data=$(get_config "data" || echo "\033[33;1m")
delimiter=$(get_config "delimiter" || echo "\033[35m=>")
dashes=$(get_config "dashes" || echo "\033[34m-")



###[ Unicode Symbols for icons ]###############################################

sun=$(get_config "sun" || echo "\033[33;1m\xe2\x98\x80")
moon=$(get_config "moon" || echo "\033[36m\xe2\x98\xbd")
clouds=$(get_config "clouds" || echo "\033[37;1m\xe2\x98\x81")
rain=$(get_config "rain" || echo "\xe2\x98\x94")
fog=$(get_config "fog" || echo "\xe2\x96\x92")



###[ Fetch Weather data ]######################################################

fetch_cmd=$(get_config "fetch_cmd" || echo "curl -s")

weather=$($fetch_cmd "http://api.openweathermap.org/data/2.5/weather?q=$location\&units=$units")



###[ Process Weather data ]####################################################

city=$(echo $weather | jq -r '.name')
temperature=$(printf '%.0f' $(echo $weather | jq '.main.temp'))
humidity=$(echo $weather | jq '.main.humidity')
pressure=$(echo $weather | jq '.main.pressure')
sky=$(echo $weather | jq -r '.weather[0].main')
sunrise=$(echo $weather | jq '.sys.sunrise')
sunset=$(echo $weather | jq '.sys.sunset')
wind=$(echo $weather | jq '.wind.speed')



###[ Set the period ]##########################################################

now=$(date +%s)

if [ $now -ge $sunset ] || [ $now -le $sunrise ]
then
        period="night"
else
        period="day"
fi



###[ Set the scale ]###########################################################

case $units in
        metric)
                scale="°C"
                speed_unit="m/s"
                pressureUnit="hPa"
                pressure=$(printf '%.0f' $pressure)
                ;;
        imperial)
                scale="°F"
                speed_unit="mph"
                pressureUnit="inHg"
                pressure=$(printf '%.2f' $(echo "$pressure*0.0295" | bc))
                ;;
esac



###[ Set icons ]###############################################################

if [ $symbols = true ]
then
        case $sky in
                Clear)
                        if [ $period = "night" ]
                        then
                                icon="$moon "
                        else
                                icon="$sun  "
                        fi
                        ;;
                Clouds)
                        icon="$clouds  "
                        ;;
                Rain)
                        icon="$rain  "
                        ;;
                Fog)
                        icon="$fog  "
                        ;;
        esac
fi



###[ Display current Weather ]#################################################

echo -e "$background$text Current weather in $city $delimiter$data $temperature $scale $icon$dashes$text Humidity $delimiter$data $humidity % $dashes$text Wind $delimiter$data $wind $speed_unit $dashes$text Pressure $delimiter$data $pressure $pressureUnit \033[0m"

0 comentarios: