New WeatherAlertsModule

This commit is contained in:
nemunaire 2022-08-11 16:09:01 +02:00
parent d0fcfd08d1
commit e42a4bc1d6
3 changed files with 77 additions and 0 deletions

View File

@ -67,6 +67,11 @@ def main():
from modules.weather import WeeklyWeatherModule
image.paste(WeeklyWeatherModule().draw_module(config, int(480/1.6), 275), (480-int(480/1.6), 305))
# alerts
from modules.weather import WeatherAlertsModule
mod = WeatherAlertsModule().draw_module(config, 480, 200)
image.paste(mod, (0, 580-mod.height-5), mod)
# températures
from modules.weather import WeatherTemperatureModule
image.paste(WeatherTemperatureModule().draw_module(config, 480, 200), (0, 580))

View File

@ -265,3 +265,69 @@ class WeeklyWeatherModule:
return image
class WeatherAlertsModule:
def __init__(self):
self.icon_size = 50
def draw_module(self, config, width, height):
image = Image.new('RGBA', (width, height), "#000")
draw = ImageDraw.Draw(image)
if WeatherAPI().has_alerts():
alerts = WeatherAPI().get_alerts()
fnt_R = ImageFont.truetype(config.fnt_R_path, 16)
fnt_B = ImageFont.truetype(config.fnt_RB_path, 16)
align = 7
for alert in alerts:
if alert["severity"] == "watch":
icon = "wi-small-craft-advisory.png"
elif alert["severity"] == "warning":
icon = "wi-gale-warning.png"
else:
icon = None
if icon is not None:
color_img = Image.new('RGB', (self.icon_size, self.icon_size), "#fff")
icon_img = Image.open("icons/" + icon).resize((self.icon_size, self.icon_size))
image.paste(color_img, (0, align - 5), icon_img)
draw.text(
(self.icon_size - 5, align),
alert["title"],
fill="white", anchor="lt", font=fnt_B
)
startTime = datetime.fromtimestamp(alert["time"], tz=timezone.utc)
endTime = datetime.fromtimestamp(alert["expires"], tz=timezone.utc)
# Show alert timing if under a day
if startTime.hour != endTime.hour:
draw.text(
(self.icon_size + fnt_B.getsize(alert["title"])[0], align + 3),
startTime.strftime(("%x " if startTime.day != datetime.now().day else "") + "%X") + " - " + endTime.strftime(("%x " if startTime.day != endTime.day else "") + "%X"),
fill="white", anchor="lt", font=fnt_R
)
elif startTime.day != datetime.now().day:
draw.text(
(self.icon_size + fnt_B.getsize(alert["title"])[0], align + 3),
startTime.strftime("%x"),
fill="white", anchor="lt", font=fnt_R
)
align += fnt_B.getsize(alert["title"])[1]
draw.multiline_text(
(self.icon_size - 5, align),
alert["description"],
fill="white", font=fnt_R
)
align += draw.multiline_textsize(
alert["description"],
font=fnt_R
)[1]
image = image.crop((0,0,width, align))
return image

View File

@ -145,6 +145,12 @@ class DarkSkyAPI:
def get_daily(self, *args, **kwargs):
return self.get_weather(*args, **kwargs)["daily"]
def has_alerts(self, *args, **kwargs):
return "alerts" in self.get_weather(*args, **kwargs) and len(self.get_weather(*args, **kwargs)["alerts"]) > 0
def get_alerts(self, *args, **kwargs):
return self.get_weather(*args, **kwargs)["alerts"]
WeatherAPI = DarkSkyAPI