Skip to content

Commit

Permalink
feat(weather): enhance weather widget with additional data fields
Browse files Browse the repository at this point in the history
This commit updates the WeatherWidget class to include more comprehensive weather information, such as wind speed, pressure, precipitation, UV index, visibility, and cloud cover. The API key can now be sourced from an environment variable if specified as env.
  • Loading branch information
amnweb committed Dec 2, 2024
1 parent 185ae3e commit e428879
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions src/core/widgets/yasb/weather.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
import os
import re
import urllib.request
import urllib.parse
Expand Down Expand Up @@ -30,9 +31,9 @@ def __init__(
self._location = location
self._hide_decimal = hide_decimal
self._icons = icons
self._api_key = api_key
self._api_key = api_key if api_key != 'env' else os.getenv('YASB_WEATHER_API')
self.api_url = f"http://api.weatherapi.com/v1/forecast.json?key={self._api_key}&q={urllib.parse.quote(self._location)}&days=1&aqi=no&alerts=no"

print(self.api_url)
# Store weather data
self.weather_data = None
self._show_alt_label = False
Expand Down Expand Up @@ -115,6 +116,7 @@ def _update_label(self,update_class=True):
label_parts = [part for part in label_parts if part]

widget_index = 0

try:
for part in label_parts:
part = part.strip()
Expand All @@ -138,6 +140,7 @@ def _update_label(self,update_class=True):
self._reload_css(active_widgets[widget_index])
else:
active_widgets[widget_index].setText(part)

if not active_widgets[widget_index].isVisible():
active_widgets[widget_index].show()
widget_index += 1
Expand Down Expand Up @@ -191,7 +194,21 @@ def format_temp(temp, unit):
'{is_day}': current['is_day'],
'{icon}': icon_string[0].lower() + icon_string[1:],
'{icon_class}': icon_string[0].lower() + icon_string[1:],
'{conditions}': conditions_data
'{conditions}': conditions_data,
'{wind_mph}': current['wind_mph'],
'{wind_kph}': current['wind_kph'],
'{wind_dir}': current['wind_dir'],
'{wind_degree}': current['wind_degree'],
'{pressure_mb}': current['pressure_mb'],
'{pressure_in}': current['pressure_in'],
'{precip_mm}': current['precip_mm'],
'{precip_in}': current['precip_in'],
'{uv}': current['uv'],
'{vis_km}': current['vis_km'],
'{vis_miles}': current['vis_miles'],
'{cloud}': current['cloud'],
'{feelslike_c}': format_temp(current['feelslike_c'], 'C'),
'{feelslike_f}': format_temp(current['feelslike_f'], 'F')
}
except (urllib.error.URLError, json.JSONDecodeError) as e:
logging.error(f"Error fetching weather data: {e}")
Expand All @@ -207,5 +224,19 @@ def format_temp(temp, unit):
'{is_day}': 0,
'{icon}': 'unknown',
'{icon_class}': '',
'{conditions}': 'No Data'
'{conditions}': 'No Data',
'{wind_mph}': 'N/A',
'{wind_kph}': 'N/A',
'{wind_dir}': 'N/A',
'{wind_degree}': 'N/A',
'{pressure_mb}': 'N/A',
'{pressure_in}': 'N/A',
'{precip_mm}': 'N/A',
'{precip_in}': 'N/A',
'{uv}': 'N/A',
'{vis_km}': 'N/A',
'{vis_miles}': 'N/A',
'{cloud}': 'N/A',
'{feelslike_c}': 'N/A',
'{feelslike_f}': 'N/A'
}

0 comments on commit e428879

Please sign in to comment.