~cyplo/dotfiles

ref: 83ddcc09dc68389b129d598722eca9e90a6dff33 dotfiles/nixos/i3/openweathermap-fullfeatured.sh -rwxr-xr-x 2.5 KiB
83ddcc09 — Cyryl Płotnicki remove broken i3 msg call 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/sh

source $HOME/.open-secrets.sh

KEY="$OPENWEATHERMAP_KEY"
CITY=""
UNITS="metric"
SYMBOL="°"

API="https://api.openweathermap.org/data/2.5"

CURL=`which curl`
CURL="$CURL -sf"
CUT=`which cut`
DATE=`which date`
JQ=`which jq`
UNAME=`which uname`

get_icon() {
    case $1 in
        01d) icon="";;
        01n) icon="";;
        02d) icon="";;
        02n) icon="";;
        03d) icon="";;
        03n) icon="";;
        04d) icon="";;
        04n) icon="";;
        09d) icon="";;
        09n) icon="";;
        10d) icon="";;
        10n) icon="";;
        11d) icon="";;
        11n) icon="";;
        13d) icon="";;
        13n) icon="";;
        50d) icon="";;
        50n) icon="";;
        *) icon="";
    esac

    echo $icon
}

get_duration() {

    osname=$($UNAME -s)

    case $osname in
        *BSD) $DATE -r "$1" -u +%H:%M;;
        *) $DATE --date="@$1" -u +%H:%M;;
    esac

}

if [ -n "$CITY" ]; then
    if [ "$CITY" -eq "$CITY" ] 2>/dev/null; then
        CITY_PARAM="id=$CITY"
    else
        CITY_PARAM="q=$CITY"
    fi

    current=$($CURL "$API/weather?appid=$KEY&$CITY_PARAM&units=$UNITS")
    forecast=$($CURL "$API/forecast?appid=$KEY&$CITY_PARAM&units=$UNITS&cnt=1")
else
    location=$($CURL https://location.services.mozilla.com/v1/geolocate?key=geoclue)

    if [ -n "$location" ]; then
        location_lat="$(echo "$location" | $JQ '.location.lat')"
        location_lon="$(echo "$location" | $JQ '.location.lng')"

        current=$($CURL "$API/weather?appid=$KEY&lat=$location_lat&lon=$location_lon&units=$UNITS")
        forecast=$($CURL "$API/forecast?appid=$KEY&lat=$location_lat&lon=$location_lon&units=$UNITS&cnt=1")
    fi
fi

if [ -n "$current" ] && [ -n "$forecast" ]; then
    current_temp=$(echo "$current" | $JQ ".main.temp" | $CUT -d "." -f 1)
    current_feels=$(echo "$current" | $JQ ".main.feels_like" | $CUT -d "." -f 1)
    current_icon=$(echo "$current" | $JQ -r ".weather[0].icon")

    forecast_temp=$(echo "$forecast" | $JQ ".list[].main.temp" | $CUT -d "." -f 1)
    forecast_feels=$(echo "$forecast" | $JQ ".list[].main.feels_like" | $CUT -d "." -f 1)
    forecast_icon=$(echo "$forecast" | $JQ -r ".list[].weather[0].icon")


    if [ "$current_temp" -gt "$forecast_temp" ]; then
        trend=""
    elif [ "$forecast_temp" -gt "$current_temp" ]; then
        trend=""
    else
        trend=""
    fi

    echo "$(get_icon "$current_icon") $current_temp($current_feels)$SYMBOL $trend $(get_icon "$forecast_icon") $forecast_temp($forecast_feels)$SYMBOL"
fi