Compare commits

...

2 Commits

Author SHA1 Message Date
cb610ae248 [WIP] Add weather icons 2025-09-03 20:29:39 +02:00
76aa1195ef Refactoring color_temperature function 2025-09-03 18:47:38 +02:00

66
main.py
View File

@@ -7,11 +7,12 @@ import argparse
import requests import requests
from pathlib import Path from pathlib import Path
from datetime import datetime from datetime import datetime
from sty import fg, bg, ef, rs from sty import fg
from meteo.config import Config from meteo.config import Config
YR_NO_URL = "https://api.met.no/weatherapi/locationforecast/2.0/" YR_NO_URL = "https://api.met.no/weatherapi/locationforecast/2.0/"
def load_args(): def load_args():
"""Charge l'action et les paramêtres communs a toutes les actions.""" """Charge l'action et les paramêtres communs a toutes les actions."""
parser = argparse.ArgumentParser(description="Affiche la météo") parser = argparse.ArgumentParser(description="Affiche la météo")
@@ -63,15 +64,24 @@ def display_full(data):
wind_speed = infos["data"]["instant"]["details"]["wind_speed"] wind_speed = infos["data"]["instant"]["details"]["wind_speed"]
wind_direction = diplay_wind_direction( wind_dir = display_wind_direction(
infos["data"]["instant"]["details"]["wind_from_direction"] infos["data"]["instant"]["details"]["wind_from_direction"]
) )
cur_humidity = infos["data"]["instant"]["details"]["relative_humidity"] icon_str = ''
print(f" {time} : {temperature}° / {cur_humidity}% / {wind_direction} {wind_speed} m/s") if "next_12_hours" in infos["data"]:
icon_str = infos["data"]["next_12_hours"]["summary"]["symbol_code"]
if "next_6_hours" in infos["data"]:
icon_str = infos["data"]["next_6_hours"]["summary"]["symbol_code"]
if "next_1_hours" in infos["data"]:
icon_str = infos["data"]["next_1_hours"]["summary"]["symbol_code"]
icon = display_icon(icon_str)
humidity = infos["data"]["instant"]["details"]["relative_humidity"]
print(f" {time} : {icon} {temperature}° / {humidity}% / {wind_dir} {wind_speed} m/s")
def diplay_wind_direction(degrees: float) -> str: def display_wind_direction(degrees: float) -> str:
""" Retourne une fleche indicant la direction du vent""" """ Retourne une fleche indicant la direction du vent"""
if 22.5 <= degrees < 67.5: if 22.5 <= degrees < 67.5:
return '' return ''
@@ -97,27 +107,41 @@ def diplay_wind_direction(degrees: float) -> str:
return '' return ''
def display_icon(string: str) -> str:
""" """
# https://www.nerdfonts.com/cheat-sheet
t = {
'clearsky_day': fg.yellow + '' + fg.rs,
'clearsky_night': '',
'cloudy': '',
'fog': '󰖑',
'lightrain': '',
'heavyrain': '',
'partlycloudy_night': '',
'partlycloudy_day': '',
'rain': '',
'lightrainshowers_night': '',
'lightrainshowers_day':'',
'fair_day': fg.yellow + '' + fg.rs,
'fair_night': '',
}
if string in t:
return t[string]
return string
def coloring_temperature(temperature: float) -> str: def coloring_temperature(temperature: float) -> str:
""" A partir de la température retourne une chaine de caractères colorisée """ """ A partir de la température retourne une chaine de caractères colorisée """
if temperature >= 40.0:
return fg.magenta + str(temperature) + fg.rs
if temperature >= 30.0: temp_limit = [40, 30, 25, 20, 10, 0]
return fg.red + str(temperature) + fg.rs color = [fg.magenta, fg.red, fg(255, 150, 50), fg.yellow, '', fg.cyan]
if temperature >= 25.0: for key, limit in enumerate(temp_limit):
return fg(255, 150, 50) + str(temperature) + fg.rs if temperature >= limit:
return color[key] + str(temperature) + fg.rs
if temperature >= 20.0: return fg.blue + str(temperature) + fg.rs
return fg.yellow + str(temperature) + fg.rs
if temperature >= 10.0:
return str(temperature)
if temperature >= 0.0:
return fg.cyan + str(temperature) + fg.rs
return fg.blue + str(cur_temp) + fg.rs
def main(): def main():