diff options
| author | Prabin Panta <pantaprabin30@gmail.com> | 2025-10-27 12:25:54 +0545 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-27 12:25:54 +0545 |
| commit | 163bce40d9612bbc364b8c6aea0892230e39bee7 (patch) | |
| tree | 7d420ab7f097852157220eddf88c6d7123b5ab58 /config/hypr/UserScripts | |
| parent | 957b0db7dd3e9274aa67bb747ad5a00615f6b23a (diff) | |
| parent | 2d4f945c6ffb850e21d3f2d24dc25087a207068d (diff) | |
Merge pull request #8 from prabinpanta0/development-weather
Diffstat (limited to 'config/hypr/UserScripts')
| -rwxr-xr-x | config/hypr/UserScripts/Weather.py | 40 | ||||
| -rwxr-xr-x | config/hypr/UserScripts/Weather.sh | 90 |
2 files changed, 33 insertions, 97 deletions
diff --git a/config/hypr/UserScripts/Weather.py b/config/hypr/UserScripts/Weather.py index 3c5d58f9..4c64221f 100755 --- a/config/hypr/UserScripts/Weather.py +++ b/config/hypr/UserScripts/Weather.py @@ -57,7 +57,7 @@ class WeatherData: CACHE_DIR: Path = Path.home() / ".cache" API_CACHE_PATH: Path = CACHE_DIR / "open_meteo_cache.json" SIMPLE_TEXT_CACHE_PATH: Path = CACHE_DIR / ".weather_cache" -CACHE_TTL_SECONDS = int(os.getenv("WEATHER_CACHE_TTL", "600")) # default 10 minutes +CACHE_TTL_SECONDS = int(os.getenv("WEATHER_CACHE_TTL", "300")) # default 5 minutes # Units: metric or imperial (default metric) UNITS = os.getenv("WEATHER_UNITS", "metric").strip().lower() # metric|imperial @@ -74,7 +74,7 @@ MANUAL_PLACE: Optional[str] = None # Location icon in tooltip (default to a standard emoji to avoid missing glyphs) LOC_ICON = os.getenv("WEATHER_LOC_ICON", "📍") # Enable/disable Pango markup in tooltip (1/0, true/false) -TOOLTIP_MARKUP = os.getenv("WEATHER_TOOLTIP_MARKUP", "1").lower() not in ("0", "false", "no") +TOOLTIP_MARKUP = os.getenv("WEATHER_TOOLTIP_MARKUP", "0").lower() in ("1", "true", "yes") # Optional debug logging to stderr (set WEATHER_DEBUG=1 to enable) DEBUG = os.getenv("WEATHER_DEBUG", "0").lower() not in ("0", "false", "no") @@ -430,9 +430,12 @@ def extract_place_parts_open_meteo(p: JSONDict) -> List[str]: admin1 = cast(Optional[str], p.get("admin1")) country = cast(Optional[str], p.get("country")) parts: List[str] = [] - for part in [name, admin1, country]: - if part is not None: - parts.append(part) + if name is not None: + parts.append(name) + if admin1 is not None: + parts.append(admin1) + if country is not None: + parts.append(country) return parts @@ -592,9 +595,11 @@ def build_weather_strings(cur: JSONDict, cur_units: JSONDict, daily: JSONDict, d feels_str = f"Feels like {int(round(feels_val))}{feels_unit}" if feels_val is not None else "" is_day_val = cur.get("is_day") - is_day = coerce_int(is_day_val) or 1 + is_day_int = coerce_int(is_day_val) + is_day = is_day_int if is_day_int is not None else 1 weather_code_val = cur.get("weather_code") - code = coerce_int(weather_code_val) or -1 + code_int = coerce_int(weather_code_val) + code = code_int if code_int is not None else -1 icon = wmo_to_icon(code, is_day) status = wmo_to_status(code) @@ -641,7 +646,10 @@ def build_aqi_info(aqi: Optional[Dict[str, Any]]) -> str: def build_place_str(lat: float, lon: float, place: Optional[str]) -> str: - return MANUAL_PLACE or ENV_PLACE or place or f"{lat:.3f}, {lon:.3f}" + effective_place = MANUAL_PLACE or ENV_PLACE or place + if effective_place: + return f"{effective_place} ({lat:.3f}, {lon:.3f})" + return f"{lat:.3f}, {lon:.3f}" @@ -752,11 +760,12 @@ def build_output(loc: Location, forecast: Optional[Dict[str, Any]], aqi: Optiona } simple_weather = ( + f"{place_str}\n" f"{data.icon} {data.status}\n" + f" {data.temp_str} ({data.feels_str})\n" - + (f"{data.wind_text} \n" if data.wind_text else "") - + (f"{data.humidity_text} \n" if data.humidity_text else "") - + f"{data.visibility_text} {data.aqi_text}\n" + + (f" {data.wind_text} \n" if data.wind_text else "") + + (f" {data.humidity_text} \n" if data.humidity_text else "") + + f" {data.visibility_text} {data.aqi_text}\n" ) return out_data, simple_weather @@ -769,9 +778,8 @@ def try_cached_weather(lat: float, lon: float) -> Optional[Tuple[Dict[str, str], aqi = cast(Optional[Dict[str, Any]], cached.get("aqi")) place_val = cached.get("place") cached_place = place_val if isinstance(place_val, str) else None - place_effective = MANUAL_PLACE or ENV_PLACE or cached_place try: - return build_output(Location(lat, lon, place_effective), forecast, aqi) + return build_output(Location(lat, lon, cached_place), forecast, aqi) except Exception as e: print(f"Cached data build failed, refetching: {e}", file=sys.stderr) return None @@ -781,9 +789,9 @@ def fetch_fresh_weather(lat: float, lon: float) -> Optional[Tuple[Dict[str, str] try: forecast = fetch_open_meteo(lat, lon) aqi = fetch_aqi(lat, lon) - place_effective = MANUAL_PLACE or ENV_PLACE or fetch_place(lat, lon) - write_api_cache({"forecast": forecast, "aqi": aqi, "place": place_effective}) - return build_output(Location(lat, lon, place_effective), forecast, aqi) + place = fetch_place(lat, lon) + write_api_cache({"forecast": forecast, "aqi": aqi, "place": place}) + return build_output(Location(lat, lon, place), forecast, aqi) except Exception as e: print(f"Open-Meteo fetch failed: {e}", file=sys.stderr) return None diff --git a/config/hypr/UserScripts/Weather.sh b/config/hypr/UserScripts/Weather.sh index 9bdaff4a..0540e51d 100755 --- a/config/hypr/UserScripts/Weather.sh +++ b/config/hypr/UserScripts/Weather.sh @@ -1,87 +1,15 @@ #!/bin/bash # /* ---- 💫 https://github.com/JaKooLit 💫 ---- */ ## -# weather info from wttr. https://github.com/chubin/wttr.in -# Remember to add city +# weather info using Python script with Open-Meteo APIs -city= -cachedir="$HOME/.cache/rbn" -cachefile=${0##*/}-$1 - -if [ ! -d $cachedir ]; then - mkdir -p $cachedir -fi - -if [ ! -f $cachedir/$cachefile ]; then - touch $cachedir/$cachefile +if ! command -v python3 >/dev/null 2>&1; then + echo "python3 not found in PATH" >&2 + exit 127 fi -# Save current IFS -SAVEIFS=$IFS -# Change IFS to new line. -IFS=$'\n' - -cacheage=$(($(date +%s) - $(stat -c '%Y' "$cachedir/$cachefile"))) -if [ $cacheage -gt 1740 ] || [ ! -s $cachedir/$cachefile ]; then - data=($(curl -s https://en.wttr.in/"$city"$1\?0qnT 2>&1)) - echo ${data[0]} | cut -f1 -d, > $cachedir/$cachefile - echo ${data[1]} | sed -E 's/^.{15}//' >> $cachedir/$cachefile - echo ${data[2]} | sed -E 's/^.{15}//' >> $cachedir/$cachefile +python3 "$(dirname "$0")/Weather.py" "$@" +exit_code=$? +if [ "$exit_code" -ne 0 ]; then + echo "Failed to run Weather.py" >&2 fi - -weather=($(cat $cachedir/$cachefile)) - -# Restore IFSClear -IFS=$SAVEIFS - -temperature=$(echo ${weather[2]} | sed -E 's/([[:digit:]]+)\.\./\1 to /g') - -#echo ${weather[1]##*,} - -# https://fontawesome.com/icons?s=solid&c=weather -case $(echo ${weather[1]##*,} | tr '[:upper:]' '[:lower:]') in -"clear" | "sunny") - condition="" - ;; -"partly cloudy") - condition="" - ;; -"cloudy") - condition="" - ;; -"overcast") - condition="" - ;; -"fog" | "freezing fog") - condition="" - ;; -"patchy rain possible" | "patchy light drizzle" | "light drizzle" | "patchy light rain" | "light rain" | "light rain shower" | "mist" | "rain") - condition="" - ;; -"moderate rain at times" | "moderate rain" | "heavy rain at times" | "heavy rain" | "moderate or heavy rain shower" | "torrential rain shower" | "rain shower") - condition="" - ;; -"patchy snow possible" | "patchy sleet possible" | "patchy freezing drizzle possible" | "freezing drizzle" | "heavy freezing drizzle" | "light freezing rain" | "moderate or heavy freezing rain" | "light sleet" | "ice pellets" | "light sleet showers" | "moderate or heavy sleet showers") - condition="" - ;; -"blowing snow" | "moderate or heavy sleet" | "patchy light snow" | "light snow" | "light snow showers") - condition="" - ;; -"blizzard" | "patchy moderate snow" | "moderate snow" | "patchy heavy snow" | "heavy snow" | "moderate or heavy snow with thunder" | "moderate or heavy snow showers") - condition="" - ;; -"thundery outbreaks possible" | "patchy light rain with thunder" | "moderate or heavy rain with thunder" | "patchy light snow with thunder") - condition="" - ;; -*) - condition="" - echo -e "{\"text\":\""$condition"\", \"alt\":\""${weather[0]}"\", \"tooltip\":\""${weather[0]}: $temperature ${weather[1]}"\"}" - ;; -esac - -#echo $temp $condition - -echo -e "{\"text\":\""$temperature $condition"\", \"alt\":\""${weather[0]}"\", \"tooltip\":\""${weather[0]}: $temperature ${weather[1]}"\"}" - -cached_weather=" $temperature \n$condition ${weather[1]}" - -echo -e $cached_weather > "$HOME/.cache/.weather_cache"
\ No newline at end of file +exit "$exit_code"
\ No newline at end of file |
