Skip to content

Commit

Permalink
Detect if no OpenWeatherMap API key provided
Browse files Browse the repository at this point in the history
  • Loading branch information
mathoudebine committed Dec 21, 2024
1 parent d8ef41a commit 07b2de1
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions library/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,6 @@ def stats():
if activate:
temp = None
feel = None
desc = None
time = None
humidity = None
if HW_SENSORS in ["STATIC", "STUB"]:
Expand All @@ -877,26 +876,26 @@ def stats():
try:
response = requests.get(url)
if response.status_code == 200:
try:
data = response.json()
temp = f"{data['current']['temp']:.1f}{deg}"
feel = f"({data['current']['feels_like']:.1f}{deg})"
desc = data['current']['weather'][0]['description'].capitalize()
if wdescription_theme_data['CENTER_LENGTH']:
desc = desc.center(center_description_length)
humidity = f"{data['current']['humidity']:.0f}%"
now = datetime.datetime.now()
time = f"@{now.hour:02d}:{now.minute:02d}"
except Exception as e:
logger.error(str(e))
desc = "Error fetching weather"
data = response.json()
temp = f"{data['current']['temp']:.1f}{deg}"
feel = f"({data['current']['feels_like']:.1f}{deg})"
desc = data['current']['weather'][0]['description'].capitalize()
if wdescription_theme_data['CENTER_LENGTH']:
desc = desc.center(center_description_length)
humidity = f"{data['current']['humidity']:.0f}%"
now = datetime.datetime.now()
time = f"@{now.hour:02d}:{now.minute:02d}"
else:
print(f"Failed to fetch weather data. Status code: {response.status_code}")
print(f"Response content: {response.content}")
logger.error(f"Error {response.status_code} fetching OpenWeatherMap API:")
# logger.debug(f"Response content: {response.content}")
logger.error(response.text)

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

This expression logs
sensitive data (private)
as clear text.
This expression logs
sensitive data (private)
as clear text.
desc = response.json().get('message')
except:
desc = "Connection error"
except Exception as e:
logger.error(f"Error fetching OpenWeatherMap API: {str(e)}")
desc = "Error fetching OpenWeatherMap API"
else:
logger.warning("No OpenWeatherMap API key provided in config.yaml")
desc = "No OpenWeatherMap API key"

if activate:
# Display Temperature
Expand All @@ -907,7 +906,7 @@ def stats():
display_themed_value(theme_data=wupdatetime_theme_data, value=time)
# Display Humidity
display_themed_value(theme_data=whumidity_theme_data, value=humidity)
# Display Weather Description
# Display Weather Description (or error message)
display_themed_value(theme_data=wdescription_theme_data, value=desc)


Expand Down

0 comments on commit 07b2de1

Please sign in to comment.