dotfiles/.config/hypr/UserScripts/WeatherWrap.sh
2026-05-13 21:22:17 +02:00

62 lines
1.6 KiB
Bash
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# ==================================================
# KoolDots (2026)
# Project URL: https://github.com/LinuxBeginnings
# License: GNU GPLv3
# SPDX-License-Identifier: GPL-3.0-or-later
# ==================================================
# Weather entrypoint: prefer Python (OpenMeteo), fallback to legacy Bash (wttr.in)
SCRIPT_DIR="$(dirname "$0")"
PY_SCRIPT="$SCRIPT_DIR/Weather.py"
BASH_FALLBACK="$SCRIPT_DIR/Weather.sh"
# Function to check network connectivity
check_network() {
# Try multiple methods to check network
if ping -c1 -W1 8.8.8.8 >/dev/null 2>&1; then
return 0
fi
if ping -c1 -W1 1.1.1.1 >/dev/null 2>&1; then
return 0
fi
if curl -s --connect-timeout 2 "https://ipinfo.io" >/dev/null 2>&1; then
return 0
fi
return 1
}
# If no network, return offline status immediately
if ! check_network; then
echo '{"text":"󰖪", "alt":"Offline", "tooltip":"No network connection"}'
exit 0
fi
run_fallback() {
if [ -f "$BASH_FALLBACK" ]; then
# Invoke via bash to avoid requiring +x and ensure consistent shell
bash "$BASH_FALLBACK" "$@"
return $?
else
echo "Weather fallback not found: $BASH_FALLBACK" >&2
return 127
fi
}
if command -v python3 >/dev/null 2>&1; then
python3 "$PY_SCRIPT" "$@"
exit_code=$?
if [ "$exit_code" -eq 0 ]; then
exit 0
fi
echo "Weather.py failed with code $exit_code — falling back to Weather.sh" >&2
run_fallback "$@"
exit $?
else
echo "python3 not found in PATH — falling back to Weather.sh" >&2
run_fallback "$@"
exit $?
fi