#!/usr/bin/env python3 import os import json import locale import argparse import requests from pathlib import Path from datetime import datetime from sty import fg, bg, ef, rs from meteo.config import Config YR_NO_URL = "https://api.met.no/weatherapi/locationforecast/2.0/" def load_args(): """Charge l'action et les paramêtres communs a toutes les actions.""" parser = argparse.ArgumentParser(description="Affiche la météo") parser.add_argument( "-c", "--config", default=Path.home() / ".config" / "meteo.yaml", help="Chemin vers le fichier de configuration", ) parser.add_argument( "-l", "--location", default="home", help="", ) return vars(parser.parse_args()) def ask_api(latitude: float, longitude: float, altitude: int) -> dict: """ interrege l'API de https://yr.no """ url = f"{YR_NO_URL}compact.json?lat={latitude}&lon={longitude}&altitude={altitude}" headers = {"User-Agent": "DaikoMete/0.1 daiko@daiko.fr"} response = requests.get(url, headers=headers) if response.status_code != 200: print("Error contacting API.") os._exit(1) return json.loads(response.text) def display_full(data): """ Affiche les informations météo complètes """ locale.setlocale(locale.LC_TIME, "fr_FR.utf8") cur_date = datetime.fromisoformat("1970-01-01T00:00:00Z").date() for infos in data["properties"]["timeseries"]: new_date = datetime.fromisoformat(infos["time"]) if new_date.date() > cur_date: print(new_date.strftime("%A, %d. %B %Y")) cur_date = new_date.date() time = new_date.strftime("%H:%M") temperature = coloring_temperature( infos["data"]["instant"]["details"]["air_temperature"] ) wind_speed = infos["data"]["instant"]["details"]["wind_speed"] wind_direction = diplay_wind_direction( infos["data"]["instant"]["details"]["wind_from_direction"] ) cur_humidity = infos["data"]["instant"]["details"]["relative_humidity"] print(f" {time} : {temperature}° / {cur_humidity}% / {wind_direction} {wind_speed} m/s") def diplay_wind_direction(degrees: float) -> str: """ Retourne une fleche indicant la direction du vent""" if 22.5 <= degrees < 67.5: return '↗' if 67.5 <= degrees < 112.5: return '→' if 112.5 <= degrees < 157.5: return '↘' if 157.5 <= degrees < 202.5: return '↓' if 202.5 <= degrees < 247.5: return '↙' if 247.5 <= degrees < 292.5: return '←' if 292.5 <= degrees < 337.5: return '↖' return '↑' def coloring_temperature(temperature: float) -> str: """ 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: return fg.red + str(temperature) + fg.rs if temperature >= 25.0: return fg(255, 150, 50) + str(temperature) + fg.rs if temperature >= 20.0: 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(): """Fonction principale""" args = load_args() config_file = Path(args["config"]) if not config_file.is_file(): print(f"{config_file} n'est pas un fichier.") os._exit(1) conf = Config(args["config"]) try: location = conf.get_location(args["location"]) except AttributeError: print(f"{args['location']} n'est pas configuré.") os._exit(1) api_response = ask_api( latitude=location["latitude"], longitude=location["longitude"], altitude=location["altitude"], ) display_full(api_response) if __name__ == "__main__": main()