diff options
| author | Donald Williams <129223418+dwilliam62@users.noreply.github.com> | 2025-12-29 16:10:33 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-12-29 16:10:33 -0500 |
| commit | 3f9d09e4a4a01e5794629766aceeec504529ec5e (patch) | |
| tree | 21fdfac3b47258ef240d26ce1b56349ec04c6fa6 | |
| parent | 059476451904e27694e88d21d793487b44a4442c (diff) | |
| parent | 44a9a5af345c93cb269d5d76e720a6db6fad1032 (diff) | |
Merge pull request #895 from Lumethra/patch-2
Update: Enhance Weather.sh to get city from IP address
| -rwxr-xr-x | config/hypr/UserScripts/Weather.sh | 46 |
1 files changed, 40 insertions, 6 deletions
diff --git a/config/hypr/UserScripts/Weather.sh b/config/hypr/UserScripts/Weather.sh index ac9abc13..4588ed1d 100755 --- a/config/hypr/UserScripts/Weather.sh +++ b/config/hypr/UserScripts/Weather.sh @@ -2,16 +2,50 @@ # /* ---- 💫 https://github.com/JaKooLit 💫 ---- */ ## # weather info from wttr. https://github.com/chubin/wttr.in # Remember to add city +# Function to get current city from IP address with fallback -city="" +# Get your current location with your IP adress +get_current_city() { + local city + + # First try: ipinfo.io + local location_data=$(curl -fsS "https://ipinfo.io/json" 2>/dev/null) + if [ $? -eq 0 ] && [ -n "$location_data" ]; then + city=$(echo "$location_data" | grep -o '"city"[[:space:]]*:[[:space:]]*"[^"]*"' | cut -d'"' -f4) + if [ -n "$city" ]; then + echo "$city" + return 0 + fi + fi + + # Fallback: ipapi.co + city=$(curl -fsS "https://ipapi.co/json" 2>/dev/null | grep -o '"city"[[:space:]]*:[[:space:]]*"[^"]*"' | cut -d'"' -f4) + if [ -n "$city" ]; then + echo "$city" + return 0 + fi + + # Last resort: ipwho.is + city=$(curl -fsS "https://ipwho.is/" 2>/dev/null | grep -o '"city"[[:space:]]*:[[:space:]]*"[^"]*"' | cut -d'"' -f4) + if [ -n "$city" ]; then + echo "$city" + return 0 + fi + + # If all fail + echo "Unknown" >&2 + return 1 +} +city=$(get_current_city) -# if city is blank, use https://ipapi.co/json to get location from IP -if [ -z "$city" ]; then - city=$(curl -fsS https://ipapi.co/json | grep city | cut -f4 -d'"') +# If city is empty, that means the IP check failed, which means, we should use manual setting +if [ -z "$city" ] || [ "$city" = "Unknown" ]; then + # SET YOUR MANUAL CITY HERE + city=" " # ← Change this to your preferred city + echo "Using manual city: $city" >&2 fi - # URL-encode city for safe use in URLs encoded_city="$city" if command -v python3 >/dev/null 2>&1; then @@ -206,4 +240,4 @@ tooltip_json=$(json_escape "${weather[0]}: $temperature $cond_disp") printf '{"text":"%s", "alt":"%s", "tooltip":"%s"}\n' "$text_json" "$alt_json" "$tooltip_json" # Write a two-line cache with an actual newline between lines -printf ' %s \n%s %s\n' "$temperature" "$condition" "${weather[1]}" > "$HOME/.cache/.weather_cache"
\ No newline at end of file +printf ' %s \n%s %s\n' "$temperature" "$condition" "${weather[1]}" > "$HOME/.cache/.weather_cache" |
