From 59b838e957f90f6f017dff64e008c84855f4bfb8 Mon Sep 17 00:00:00 2001 From: Frodd Date: Wed, 3 Sep 2025 22:38:59 +0100 Subject: [PATCH] add weather, cpu and memory to waybar --- waybar/.config/waybar/config.jsonc | 18 +- waybar/.config/waybar/style.css | 18 +- waybar/.config/waybar/weather.py | 86 +++++ waybar/.config/waybar/weather_icons.json | 342 ++++++++++++++++++++ waybar/.config/waybar/weather_settings.json | 5 + 5 files changed, 467 insertions(+), 2 deletions(-) create mode 100644 waybar/.config/waybar/weather.py create mode 100644 waybar/.config/waybar/weather_icons.json create mode 100644 waybar/.config/waybar/weather_settings.json diff --git a/waybar/.config/waybar/config.jsonc b/waybar/.config/waybar/config.jsonc index 911c2c7..b485621 100644 --- a/waybar/.config/waybar/config.jsonc +++ b/waybar/.config/waybar/config.jsonc @@ -2,7 +2,23 @@ "position": "top", "modules-left": ["hyprland/workspaces"], "modules-center": ["hyprland/window"], - "modules-right": ["network", "pulseaudio", "battery", "clock"], + "modules-right": ["custom/weather", "network", "cpu", "memory", "pulseaudio", "battery", "clock"], + "custom/weather": { + "interval": 600, + "exec": "python -O /home/frodd/.config/waybar/weather.py", + "format": "{icon} {text}", + "format_icons": { + "default": "󰖨" + } + }, + "cpu": { + "interval": 10, + "format": " {usage}%" + }, + "memory": { + "interval": 30, + "format": " {used:0.1f}/{total:0.1f} ({percentage}%)" + }, "clock": { "format": "{:%a %d %H:%M}", "tooltip-format": "{:%Y %B}\n{calendar}" diff --git a/waybar/.config/waybar/style.css b/waybar/.config/waybar/style.css index 2f630d9..a34b062 100644 --- a/waybar/.config/waybar/style.css +++ b/waybar/.config/waybar/style.css @@ -55,6 +55,7 @@ button:hover { background-color: #eb4d4b; } +#custom-weather, #pulseaudio, #clock, #battery, @@ -66,7 +67,7 @@ button:hover { #wireplumber, #tray, #network, -#mode, +#mode #scratchpad { margin-top: 2px; margin-bottom: 2px; @@ -76,6 +77,21 @@ button:hover { padding-right: 4px; } +#custom-weather { + color: @red; + border-bottom: 2px solid @red; +} + +#cpu { + color: @mauve; + border-bottom: 2px solid @mauve; +} + +#memory { + color: @peach; + border-bottom: 2px solid @peach; +} + #clock { color: @maroon; border-bottom: 2px solid @maroon; diff --git a/waybar/.config/waybar/weather.py b/waybar/.config/waybar/weather.py new file mode 100644 index 0000000..dd33b3a --- /dev/null +++ b/waybar/.config/waybar/weather.py @@ -0,0 +1,86 @@ +"""Small script to generate weather data.""" +import json +import os +import time + +import requests + + +class Weather: + """Weather data is managed here.""" + + error_time = 0 + data = {} + script_path = '' + + def __init__(self): + self.script_path = os.path.dirname(os.path.realpath(__file__)) + self.get_weather() + + def get_weather(self): + """Retrieve current weather from weatherapi.com.""" + try: + # retrieve data from json file + with open( + f"{self.script_path}/weather_settings.json", + "r", + encoding="UTF-8") as read_file: + self.data = json.load(read_file) + url = "api.weatherapi.com/v1/current.json" + key = self.data['key'] + unit = "°C" if self.data['unit'] == "Celsius" else "°F" + parameters = self.data['parameters'] + icon_pos = self.icon_position() + + # retrieve weather from weatherapi.com + request = f"http://{url}?key={key}&q={parameters}" + response = requests.get(request) + self.data = json.loads(response.content) + temp = self.data['current']['temp_c'] if unit == "°C" \ + else self.data['current']['temp_f'] + # determine the icon + icon = self.get_icon() + + # display weather + if icon_pos == "left": + print(f"{icon} {int(round(temp))}{unit}") + else: + print(f"{int(round(temp))}{unit} {icon} ") + except requests.ConnectionError: + self.error_handling() + except json.JSONDecodeError: + print("error in weather_settings.json file") + + def error_handling(self): + """Handle errors.""" + time.sleep(self.error_time) + self.error_time += 10 + self.get_weather() + + def get_icon(self): + """Determine weather icon in function of the current\ + weather conditions and hour.""" + # retrieve data from json file + with open(f"{self.script_path}/weather_icons.json", + "r", + encoding="UTF-8") as read_file: + data = json.load(read_file) + # get icon + icon = '' + for item in data: + if self.data['current']['condition']['text'].lower() in (item["night"].lower(), + item["day"].lower()): + if self.data['current']['is_day'] == 1: + icon = item["icon"] + else: + icon = item["icon-night"] + return icon + + def icon_position(self): + """Determine icon position.""" + if "icon-position" in self.data: + return self.data["icon-position"] + return "right" + + +Weather() diff --git a/waybar/.config/waybar/weather_icons.json b/waybar/.config/waybar/weather_icons.json new file mode 100644 index 0000000..3e24d72 --- /dev/null +++ b/waybar/.config/waybar/weather_icons.json @@ -0,0 +1,342 @@ +[ + { + "code" : 1000, + "day" : "Sunny", + "night" : "Clear", + "icon" : "滛", + "icon-night" : "" + }, + { + "code" : 1003, + "day" : "Partly Cloudy", + "night" : "Partly Cloudy", + "icon" : "杖", + "icon-night" : "" + }, + { + "code" : 1006, + "day" : "Cloudy", + "night" : "Cloudy", + "icon" : "", + "icon-night" :"" + }, + { + "code" : 1009, + "day" : "Overcast", + "night" : "Overcast", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1030, + "day" : "Mist", + "night" : "Mist", + "icon" : "敖", + "icon-night" : "" + }, + { + "code" : 1063, + "day" : "Patchy rain possible", + "night" : "Patchy rain possible", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1066, + "day" : "Patchy snow possible", + "night" : "Patchy snow possible", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1069, + "day" : "Patchy sleet possible", + "night" : "Patchy sleet possible", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1072, + "day" : "Patchy freezing drizzle possible", + "night" : "Patchy freezing drizzle possible", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1087, + "day" : "Thundery outbreaks possible", + "night" : "Thundery outbreaks possible", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1114, + "day" : "Blowing snow", + "night" : "Blowing snow", + "icon" : "ﭽ", + "icon-night" : "ﭽ" + }, + { + "code" : 1117, + "day" : "Blizzard", + "night" : "Blizzard", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1135, + "day" : "Fog", + "night" : "Fog", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1147, + "day" : "Freezing fog", + "night" : "Freezing fog", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1150, + "day" : "Patchy light drizzle", + "night" : "Patchy light drizzle", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1153, + "day" : "Light drizzle", + "night" : "Light drizzle", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1168, + "day" : "Freezing drizzle", + "night" : "Freezing drizzle", + "icon" : "ﭽ", + "icon-night" : "ﭽ" + }, + { + "code" : 1171, + "day" : "Heavy freezing drizzle", + "night" : "Heavy freezing drizzle", + "icon" : "ﭽ", + "icon-night" : "ﭽ" + + }, + { + "code" : 1180, + "day" : "Patchy light rain", + "night" : "Patchy light rain", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1183, + "day" : "Light rain", + "night" : "Light rain", + "icon" : "", + "icon-night" : "" + + }, + { + "code" : 1186, + "day" : "Moderate rain at times", + "night" : "Moderate rain at times", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1189, + "day" : "Moderate rain", + "night" : "Moderate rain", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1192, + "day" : "Heavy rain at times", + "night" : "Heavy rain at times", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1195, + "day" : "Heavy rain", + "night" : "Heavy rain", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1198, + "day" : "Light freezing rain", + "night" : "Light freezing rain", + "icon" : "ﭽ", + "icon-night" : "ﭽ" + }, + { + "code" : 1201, + "day" : "Moderate or heavy freezing rain", + "night" : "Moderate or heavy freezing rain", + "icon" : "ﭽ", + "icon-night" : "ﭽ" + }, + { + "code" : 1204, + "day" : "Light sleet", + "night" : "Light sleet", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1207, + "day" : "Moderate or heavy sleet", + "night" : "Moderate or heavy sleet", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1210, + "day" : "Patchy light snow", + "night" : "Patchy light snow", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1213, + "day" : "Light snow", + "night" : "Light snow", + "icon" : "", + "icon-night" : "" + + }, + { + "code" : 1216, + "day" : "Patchy moderate snow", + "night" : "Patchy moderate snow", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1219, + "day" : "Moderate snow", + "night" : "Moderate snow", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1222, + "day" : "Patchy heavy snow", + "night" : "Patchy heavy snow", + "icon" : "", + "icon-night" : "" + + }, + { + "code" : 1225, + "day" : "Heavy snow", + "night" : "Heavy snow", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1237, + "day" : "Ice pellets", + "night" : "Ice pellets", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1240, + "day" : "Light rain shower", + "night" : "Light rain shower", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1243, + "day" : "Moderate or heavy rain shower", + "night" : "Moderate or heavy rain shower", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1246, + "day" : "Torrential rain shower", + "night" : "Torrential rain shower", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1249, + "day" : "Light sleet showers", + "night" : "Light sleet showers", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1252, + "day" : "Moderate or heavy sleet showers", + "night" : "Moderate or heavy sleet showers", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1255, + "day" : "Light snow showers", + "night" : "Light snow showers", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1258, + "day" : "Moderate or heavy snow showers", + "night" : "Moderate or heavy snow showers", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1261, + "day" : "Light showers of ice pellets", + "night" : "Light showers of ice pellets", + "icon" : "晴", + "icon-night" : "晴" + }, + { + "code" : 1264, + "day" : "Moderate or heavy showers of ice pellets", + "night" : "Moderate or heavy showers of ice pellets", + "icon" : "晴", + "icon-night" : "晴" + }, + { + "code" : 1273, + "day" : "Patchy light rain with thunder", + "night" : "Patchy light rain with thunder", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1276, + "day" : "Moderate or heavy rain with thunder", + "night" : "Moderate or heavy rain with thunder", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1279, + "day" : "Patchy light snow with thunder", + "night" : "Patchy light snow with thunder", + "icon" : "", + "icon-night" : "" + }, + { + "code" : 1282, + "day" : "Moderate or heavy snow with thunder", + "night" : "Moderate or heavy snow with thunder", + "icon" : "", + "icon-night" :"" + } +] diff --git a/waybar/.config/waybar/weather_settings.json b/waybar/.config/waybar/weather_settings.json new file mode 100644 index 0000000..58088dd --- /dev/null +++ b/waybar/.config/waybar/weather_settings.json @@ -0,0 +1,5 @@ +{ + "key": "ca9f34fa929b4079850204607250309", + "parameters": "Lewisham,London", + "unit": "Celsius" +}