fixed some issues and added a fallback for errors

This commit is contained in:
nicholai 2025-09-08 16:11:45 -06:00
parent 0d381d9c27
commit 0d841a0c03
3 changed files with 153 additions and 109 deletions

View File

@ -5,7 +5,7 @@
"exclusive": true, "exclusive": true,
"passthrough": false, "passthrough": false,
"gtk-layer-shell": true, "gtk-layer-shell": true,
"height": 18, "height": 45,
"modules-left": ["clock","custom/weather","custom/wallpaper","hyprland/window"], "modules-left": ["clock","custom/weather","custom/wallpaper","hyprland/window"],
"modules-center": ["hyprland/workspaces"], "modules-center": ["hyprland/workspaces"],
"modules-right": ["network", "bluetooth", "temperature","custom/power_profile","battery","backlight","pulseaudio","pulseaudio#microphone","tray","idle_inhibitor"], "modules-right": ["network", "bluetooth", "temperature","custom/power_profile","battery","backlight","pulseaudio","pulseaudio#microphone","tray","idle_inhibitor"],
@ -43,10 +43,10 @@
"custom/power_profile": { "custom/power_profile": {
"exec": "powerprofilesctl get", "exec": "powerprofilesctl get || echo 'Unknown'",
"interval": 5, "interval": 5,
"format": "󰈐 {}", "format": "󰈐 {}",
"on-click": "pp=$(powerprofilesctl get); case \"$pp\" in performance) n=balanced;; balanced) n=powersave;; *) n=performance;; esac; powerprofilesctl set \"$n\"; pkill -SIGRTMIN+8 waybar", "on-click": "pp=$(powerprofilesctl get 2>/dev/null || echo 'balanced'); case \"$pp\" in performance) n=balanced;; balanced) n=powersave;; *) n=performance;; esac; powerprofilesctl set \"$n\" 2>/dev/null || echo 'powerprofilesctl not available'; pkill -SIGRTMIN+8 waybar",
"signal": 8, "signal": 8,
"return-type": "string" "return-type": "string"
}, },

View File

@ -2,118 +2,161 @@
import json import json
import requests import requests
import sys
from datetime import datetime from datetime import datetime
WEATHER_CODES = { # Default fallback data
'113': '☀️ ', data = {
'116': '', "text": "Weather N/A",
'119': '☁️ ', "tooltip": "Weather data unavailable"
'122': '☁️ ',
'143': '☁️ ',
'176': '🌧️',
'179': '🌧️',
'182': '🌧️',
'185': '🌧️',
'200': '⛈️ ',
'227': '🌨️',
'230': '🌨️',
'248': '☁️ ',
'260': '☁️ ',
'263': '🌧️',
'266': '🌧️',
'281': '🌧️',
'284': '🌧️',
'293': '🌧️',
'296': '🌧️',
'299': '🌧️',
'302': '🌧️',
'305': '🌧️',
'308': '🌧️',
'311': '🌧️',
'314': '🌧️',
'317': '🌧️',
'320': '🌨️',
'323': '🌨️',
'326': '🌨️',
'329': '❄️ ',
'332': '❄️ ',
'335': '❄️ ',
'338': '❄️ ',
'350': '🌧️',
'353': '🌧️',
'356': '🌧️',
'359': '🌧️',
'362': '🌧️',
'365': '🌧️',
'368': '🌧️',
'371': '❄️',
'374': '🌨️',
'377': '🌨️',
'386': '🌨️',
'389': '🌨️',
'392': '🌧️',
'395': '❄️ '
} }
data = {} try:
# Try to get weather data
response = requests.get("https://wttr.in/?format=j1", timeout=10)
weather = requests.get("https://wttr.in/?format=j1").json() response.raise_for_status() # Raise an exception for bad status codes
# Parse JSON data
#Def format_time(time): weather = response.json()
# return time.replace("00", "").zfill(2)
# Define weather codes mapping
WEATHER_CODES = {
def format_temp(temp): '113': '☀️ ',
return (hour['FeelsLikeF']+"°").ljust(3) '116': '',
'119': '☁️ ',
'122': '☁️ ',
def format_chances(hour): '143': '☁️ ',
chances = { '176': '🌧️',
"chanceoffog": "Fog", '179': '🌧️',
"chanceoffrost": "Frost", '182': '🌧️',
"chanceofovercast": "Overcast", '185': '🌧️',
"chanceofrain": "Rain", '200': '⛈️ ',
"chanceofsnow": "Snow", '227': '🌨️',
"chanceofsunshine": "Sunshine", '230': '🌨️',
"chanceofthunder": "Thunder", '248': '☁️ ',
"chanceofwindy": "Wind" '260': '☁️ ',
'263': '🌧️',
'266': '🌧️',
'281': '🌧️',
'284': '🌧️',
'293': '🌧️',
'296': '🌧️',
'299': '🌧️',
'302': '🌧️',
'305': '🌧️',
'308': '🌧️',
'311': '🌧️',
'314': '🌧️',
'317': '🌧️',
'320': '🌨️',
'323': '🌨️',
'326': '🌨️',
'329': '❄️ ',
'332': '❄️ ',
'335': '❄️ ',
'338': '❄️ ',
'350': '🌧️',
'353': '🌧️',
'356': '🌧️',
'359': '🌧️',
'362': '🌧️',
'365': '🌧️',
'368': '🌧️',
'371': '❄️',
'374': '🌨️',
'377': '🌨️',
'386': '🌨️',
'389': '🌨️',
'392': '🌧️',
'395': '❄️ '
} }
conditions = [] def format_time(time):
for event in chances.keys(): return time.replace("00", "").zfill(2)
if int(hour[event]) > 0:
conditions.append(chances[event]+" "+hour[event]+"%")
return ", ".join(conditions)
tempint = int(weather['current_condition'][0]['FeelsLikeF']) def format_temp(temp):
#extrachar = '' return (temp+"°").ljust(3)
if tempint > 0 and tempint < 10:
extrachar = '+'
def format_chances(hour):
chances = {
"chanceoffog": "Fog",
"chanceoffrost": "Frost",
"chanceofovercast": "Overcast",
"chanceofrain": "Rain",
"chanceofsnow": "Snow",
"chanceofsunshine": "Sunshine",
"chanceofthunder": "Thunder",
"chanceofwindy": "Wind"
}
data['text'] = ''+WEATHER_CODES[weather['current_condition'][0]['weatherCode']] + \ conditions = []
" "+extrachar+weather['current_condition'][0]['FeelsLikeF']+"°" for event in chances.keys():
if int(hour.get(event, 0)) > 0:
conditions.append(chances[event]+" "+hour.get(event, "")+"%")
return ", ".join(conditions)
data['tooltip'] = f"<b>{weather['current_condition'][0]['weatherDesc'][0]['value']} {weather['current_condition'][0]['temp_F']}°</b>\n" # Safely extract weather data
data['tooltip'] += f"Feels like: {weather['current_condition'][0]['FeelsLikeF']}°\n" if 'current_condition' in weather and len(weather['current_condition']) > 0:
data['tooltip'] += f"Wind: {weather['current_condition'][0]['windspeedKmph']}Km/h\n" current = weather['current_condition'][0]
data['tooltip'] += f"Humidity: {weather['current_condition'][0]['humidity']}%\n"
for i, day in enumerate(weather['weather']): # Extract temperature safely
data['tooltip'] += f"\n<b>" feels_like = current.get('FeelsLikeF', '0')
if i == 0: tempint = int(feels_like) if feels_like.isdigit() else 0
data['tooltip'] += "Today, " extrachar = '+' if 0 < tempint < 10 else ''
if i == 1:
data['tooltip'] += "Tomorrow, " # Build text
data['tooltip'] += f"{day['date']}</b>\n" weather_code = current.get('weatherCode', '113')
data['tooltip'] += f"⬆️ {day['maxtempF']}° ⬇️ {day['mintempF']}° " weather_icon = WEATHER_CODES.get(weather_code, '')
data['tooltip'] += f"🌅 {day['astronomy'][0]['sunrise']} 🌇 {day['astronomy'][0]['sunset']}\n"
for hour in day['hourly']: data['text'] = '' + weather_icon + " " + extrachar + feels_like + "°"
if i == 0:
if int(format_time(hour['time'])) < datetime.now().hour-2: # Build tooltip
continue data['tooltip'] = f"<b>{current.get('weatherDesc', [{'value': 'Unknown'}])[0].get('value', 'Unknown')} {current.get('temp_F', '0')}°</b>\n"
data['tooltip'] += f"{format_time(hour['time'])} {WEATHER_CODES[hour['weatherCode']]} {format_temp(hour['FeelsLikeF'])} {hour['weatherDesc'][0]['value']}, {format_chances(hour)}\n" data['tooltip'] += f"Feels like: {feels_like}°\n"
data['tooltip'] += f"Wind: {current.get('windspeedKmph', '0')}Km/h\n"
data['tooltip'] += f"Humidity: {current.get('humidity', '0')}%\n"
# Add forecast data if available
if 'weather' in weather:
for i, day in enumerate(weather['weather']):
data['tooltip'] += f"\n<b>"
if i == 0:
data['tooltip'] += "Today, "
elif i == 1:
data['tooltip'] += "Tomorrow, "
data['tooltip'] += f"{day.get('date', '')}</b>\n"
data['tooltip'] += f"⬆️ {day.get('maxtempF', '0')}° ⬇️ {day.get('mintempF', '0')}° "
data['tooltip'] += f"🌅 {day.get('astronomy', [{}])[0].get('sunrise', '')} 🌇 {day.get('astronomy', [{}])[0].get('sunset', '')}\n"
# Hourly forecast
hourly = day.get('hourly', [])
for hour in hourly:
if i == 0:
try:
hour_time = int(format_time(hour.get('time', '0')))
if hour_time < datetime.now().hour - 2:
continue
except ValueError:
continue
data['tooltip'] += f"{format_time(hour.get('time', ''))} {WEATHER_CODES.get(hour.get('weatherCode', ''), '')} {format_temp(hour.get('FeelsLikeF', ''))} {hour.get('weatherDesc', [{'value': ''}])[0].get('value', '')}, {format_chances(hour)}\n"
else:
data['text'] = "Weather N/A"
data['tooltip'] = "Weather data unavailable"
except requests.exceptions.RequestException as e:
# Network error
data['text'] = "Weather N/A"
data['tooltip'] = f"Weather error: Network issue ({str(e)})"
except json.JSONDecodeError as e:
# JSON parsing error
data['text'] = "Weather N/A"
data['tooltip'] = f"Weather error: Invalid data ({str(e)})"
except Exception as e:
# Other errors
data['text'] = "Weather N/A"
data['tooltip'] = f"Weather error: {str(e)}"
# Output the result
print(json.dumps(data)) print(json.dumps(data))

View File

@ -8,7 +8,6 @@
font-family: "JetBrainsMono Nerd Font"; font-family: "JetBrainsMono Nerd Font";
font-weight: bold; font-weight: bold;
font-size: 14px; font-size: 14px;
line-height: 16px;
min-height: 0; min-height: 0;
} }
@ -20,7 +19,7 @@ window#waybar {
#workspaces button { #workspaces button {
padding: 2px 6px; padding: 5px;
color: @color4; color: @color4;
margin-right: 5px; margin-right: 5px;
} }
@ -45,6 +44,7 @@ window#waybar {
background: @background; background: @background;
color: @color12; color: @color12;
border-radius: 10px; border-radius: 10px;
font-size: 18px;
} }
#custom-power_profile, #custom-power_profile,
@ -63,8 +63,9 @@ window#waybar {
background: @background; background: @background;
color: @color12; color: @color12;
opacity: 0.8; opacity: 0.8;
padding: 0px 6px; padding: 0px 10px;
margin: 1px 0px; margin: 3px 0px;
margin-top: 10px;
border: 1px solid #181825; border: 1px solid #181825;
} }