speedwm-personal/modules/module_weather

91 lines
2 KiB
Bash
Executable file

#!/bin/sh
# weather module for status/stellar
# load config
. $HOME/.config/speedwm-de/statusrc
BINDIR="$(dirname $(command -v speedwm_status))/"
# argument 1
ARG1="$1"
# clicking
CLICK() {
# values
WEATHER_TERM="speedwm-core -curl-weather"
ICON="$ITEM5_ICON"
BUTTON="$(cat /tmp/speedwm-button)"
case "$TERMINAL" in
"") TERMINAL=st
esac
case "$BUTTON" in
"3") $TERMINAL speedwm-core -curl-weather ; exit ;;
esac
# check if a forecast exists
# status script should create this
if [ -e "/tmp/wstatus" ]; then
FORECAST="$(cat /tmp/wstatus)"
else
echo "FATAL: No weather status available!"
exit
fi
grep -q "Unknown location" /tmp/wstatus && exit
# no spacing if there's no icon
if [ "$ICON" != "" ]; then
ICONSPACING=" " # one character spacing
fi
# send the notification
SEND_NOTIF() {
FULL_WEATHER="${ICON}${ICONSPACING}$FORECAST"
notify-send "$FULL_WEATHER"
echo "Notification sent successfully!"
echo "INFO: $FULL_WEATHER"
exit 0
}
command -v notify-send > /dev/null && SEND_NOTIF
echo "FATAL: libnotify not installed, can't send notification."
exit 1
}
# information itself
PRINT() {
if [ "$ENABLE_ITEM5" = "true" ]; then
if [ -e "${BINDIR}curl" ]; then
if [ -e "/tmp/itest" ]; then
if [ -e "/tmp/wstatus" ]; then
grep -q "Unknown location" /tmp/wstatus && exit
grep -q "<html>" /tmp/wstatus || exit
echo "$SEPARATOR $ITEM5_ICON $(cat /tmp/wstatus)"
else
echo "$(curl -s wttr.in/?format="%C"), $(curl -s wttr.in/?format=3 | sed 's/.* //; s/.*\(.....\)/\1/')" > /tmp/wstatus
echo "$SEPARATOR $ITEM5_ICON $(cat /tmp/wstatus)"
fi
else
curl -so /tmp/itest wttr.in && hasinternet=true
if [ "$hasinternet" != "true" ]; then
rm -f /tmp/itest
rm -f /tmp/wstatus
itemcmd5=""
fi
fi
fi
fi
}
# argument 1
case "$ARG1" in
"") CLICK ;;
"--print") PRINT > /tmp/module_weather ;;
"--click") CLICK ;;
esac