adding weather to swaybar

This commit is contained in:
2025-04-17 19:51:47 -07:00
parent f18f11279f
commit ac2ad7961f
4 changed files with 87 additions and 3 deletions

View File

@@ -157,6 +157,7 @@ settings:
'*://wiki.alpinelinux.org/*': true
'*://www.duplicati.com/*': true
'*://www.fidelity.com/*': true
'*://www.instagram.com/*': true
'*://www.newegg.com/*': true
'*://www.vitamix.com/*': true
'*://www.youtube.com/*': true

View File

@@ -303,8 +303,8 @@ bindsym $mod+Shift+0 move container to workspace $ws7
bar {
position top
font "Monospace 17"
height 40
font "Monospace 15"
height 35
strip_workspace_numbers yes
status_command ~/.config/sway/scripts/bar.sh

View File

@@ -4,6 +4,29 @@ get_date() {
date '+%H:%M %a %d/%m/%y'
}
get_weather() {
CACHE_FILE="$HOME/.cache/weather.txt"
# 10 minutes
CACHE_DURATION=600
mkdir -p "$(dirname "$CACHE_FILE")"
if [ -f "$CACHE_FILE" ]; then
last_modified=$(stat -c %Y "$CACHE_FILE")
current_time=$(date +%s)
age=$((current_time - last_modified))
else
age=$((CACHE_DURATION + 1))
fi
if [ "$age" -gt "$CACHE_DURATION" ]; then
weather_output=$(python3 "$HOME/.config/sway/scripts/weather.py")
echo "$weather_output" > "$CACHE_FILE"
fi
cat "$CACHE_FILE"
}
get_battery() {
BATTERY_PATH="/sys/class/power_supply/BAT0"
@@ -31,7 +54,7 @@ get_volume() {
}
while true; do
printf "V: %s | %s | B: %s\n" "$(get_volume)" "$(get_date)" "$(get_battery)"
printf "V: %s | %s | %s | B: %s\n" "$(get_volume)" "$(get_date)" "$(get_weather)" "$(get_battery)"
sleep 1
done

60
.config/sway/scripts/weather.py Executable file
View File

@@ -0,0 +1,60 @@
#!/usr/bin/env python3
import requests
import json
import sys
API_KEY = "99631af2d6db903d1f689c7d2cb13764"
CITY_ID = "5809844"
UNITS = "metric"
def celsius_to_fahrenheit(celsius):
return (celsius * 9 / 5) + 32
def get_weather_icon(description):
icons = {
"clear sky": "☀️",
"few clouds": "🌤️",
"scattered clouds": "🌥️",
"broken clouds": "☁️",
"overcast clouds": "☁️",
"shower rain": "🌦️",
"light rain": "🌧️",
"light intensity drizzle": "🌧️",
"moderate rain": "🌧️",
"rain": "🌧️",
"thunderstorm": "⛈️",
"snow": "❄️",
"mist": "🌫️",
"haze": "🌫️",
"smoke": "🌫️",
"fog": "🌫️",
}
return icons.get(description, "")
try:
response = requests.get(
f"http://api.openweathermap.org/data/2.5/weather?id={CITY_ID}&units={UNITS}&appid={API_KEY}"
)
response.raise_for_status()
weather_data = response.json()
#print(weather_data)
weather_desc = weather_data["weather"][0]["description"]
(weather_desc)
temp_c = round(weather_data["main"]["temp"])
temp_f = round(celsius_to_fahrenheit(temp_c))
weather_icon = get_weather_icon(weather_desc)
print(f"{weather_icon} {temp_c}°C / {temp_f}°F")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
print(json.dumps({"text": "", "tooltip": "Could not retrieve weather data"}))
sys.exit(1)
except json.JSONDecodeError:
print("Error: Failed to parse JSON response")
print(response.content)
sys.exit(1)