Compare commits

..

No commits in common. "0d841a0c0321e4cde952c1a93dc6af6c12758c64" and "8672155b0a092e7d26654304aa35921b50417c83" have entirely different histories.

2 changed files with 105 additions and 148 deletions

View File

@ -5,7 +5,7 @@
"exclusive": true, "exclusive": true,
"passthrough": false, "passthrough": false,
"gtk-layer-shell": true, "gtk-layer-shell": true,
"height": 45, "height": 20,
"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 || echo 'Unknown'", "exec": "powerprofilesctl get",
"interval": 5, "interval": 5,
"format": "󰈐 {}", "format": "󰈐 {}",
"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", "on-click": "pp=$(powerprofilesctl get); case \"$pp\" in performance) n=balanced;; balanced) n=powersave;; *) n=performance;; esac; powerprofilesctl set \"$n\"; pkill -SIGRTMIN+8 waybar",
"signal": 8, "signal": 8,
"return-type": "string" "return-type": "string"
}, },

View File

@ -2,25 +2,9 @@
import json import json
import requests import requests
import sys
from datetime import datetime from datetime import datetime
# Default fallback data WEATHER_CODES = {
data = {
"text": "Weather N/A",
"tooltip": "Weather data unavailable"
}
try:
# Try to get weather data
response = requests.get("https://wttr.in/?format=j1", timeout=10)
response.raise_for_status() # Raise an exception for bad status codes
# Parse JSON data
weather = response.json()
# Define weather codes mapping
WEATHER_CODES = {
'113': '☀️ ', '113': '☀️ ',
'116': '', '116': '',
'119': '☁️ ', '119': '☁️ ',
@ -69,15 +53,23 @@ try:
'389': '🌨️', '389': '🌨️',
'392': '🌧️', '392': '🌧️',
'395': '❄️ ' '395': '❄️ '
} }
def format_time(time): data = {}
return time.replace("00", "").zfill(2)
def format_temp(temp):
return (temp+"°").ljust(3)
def format_chances(hour): weather = requests.get("https://wttr.in/?format=j1").json()
#Def format_time(time):
# return time.replace("00", "").zfill(2)
def format_temp(temp):
return (hour['FeelsLikeF']+"°").ljust(3)
def format_chances(hour):
chances = { chances = {
"chanceoffog": "Fog", "chanceoffog": "Fog",
"chanceoffrost": "Frost", "chanceoffrost": "Frost",
@ -91,72 +83,37 @@ try:
conditions = [] conditions = []
for event in chances.keys(): for event in chances.keys():
if int(hour.get(event, 0)) > 0: if int(hour[event]) > 0:
conditions.append(chances[event]+" "+hour.get(event, "")+"%") conditions.append(chances[event]+" "+hour[event]+"%")
return ", ".join(conditions) return ", ".join(conditions)
# Safely extract weather data tempint = int(weather['current_condition'][0]['FeelsLikeF'])
if 'current_condition' in weather and len(weather['current_condition']) > 0: #extrachar = ''
current = weather['current_condition'][0] if tempint > 0 and tempint < 10:
extrachar = '+'
# Extract temperature safely
feels_like = current.get('FeelsLikeF', '0')
tempint = int(feels_like) if feels_like.isdigit() else 0
extrachar = '+' if 0 < tempint < 10 else ''
# Build text data['text'] = ''+WEATHER_CODES[weather['current_condition'][0]['weatherCode']] + \
weather_code = current.get('weatherCode', '113') " "+extrachar+weather['current_condition'][0]['FeelsLikeF']+"°"
weather_icon = WEATHER_CODES.get(weather_code, '')
data['text'] = '' + weather_icon + " " + extrachar + feels_like + "°" data['tooltip'] = f"<b>{weather['current_condition'][0]['weatherDesc'][0]['value']} {weather['current_condition'][0]['temp_F']}°</b>\n"
data['tooltip'] += f"Feels like: {weather['current_condition'][0]['FeelsLikeF']}°\n"
# Build tooltip data['tooltip'] += f"Wind: {weather['current_condition'][0]['windspeedKmph']}Km/h\n"
data['tooltip'] = f"<b>{current.get('weatherDesc', [{'value': 'Unknown'}])[0].get('value', 'Unknown')} {current.get('temp_F', '0')}°</b>\n" data['tooltip'] += f"Humidity: {weather['current_condition'][0]['humidity']}%\n"
data['tooltip'] += f"Feels like: {feels_like}°\n" for i, day in enumerate(weather['weather']):
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>" data['tooltip'] += f"\n<b>"
if i == 0: if i == 0:
data['tooltip'] += "Today, " data['tooltip'] += "Today, "
elif i == 1: if i == 1:
data['tooltip'] += "Tomorrow, " data['tooltip'] += "Tomorrow, "
data['tooltip'] += f"{day.get('date', '')}</b>\n" data['tooltip'] += f"{day['date']}</b>\n"
data['tooltip'] += f"⬆️ {day.get('maxtempF', '0')}° ⬇️ {day.get('mintempF', '0')}° " data['tooltip'] += f"⬆️ {day['maxtempF']}° ⬇️ {day['mintempF']}° "
data['tooltip'] += f"🌅 {day.get('astronomy', [{}])[0].get('sunrise', '')} 🌇 {day.get('astronomy', [{}])[0].get('sunset', '')}\n" data['tooltip'] += f"🌅 {day['astronomy'][0]['sunrise']} 🌇 {day['astronomy'][0]['sunset']}\n"
for hour in day['hourly']:
# Hourly forecast
hourly = day.get('hourly', [])
for hour in hourly:
if i == 0: if i == 0:
try: if int(format_time(hour['time'])) < datetime.now().hour-2:
hour_time = int(format_time(hour.get('time', '0')))
if hour_time < datetime.now().hour - 2:
continue continue
except ValueError: data['tooltip'] += f"{format_time(hour['time'])} {WEATHER_CODES[hour['weatherCode']]} {format_temp(hour['FeelsLikeF'])} {hour['weatherDesc'][0]['value']}, {format_chances(hour)}\n"
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))