#!/usr/bin/env python3 import os import json import locale import pprint import argparse import requests from pathlib import Path from datetime import datetime 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: """ """ 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 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) response = ask_api( latitude = location['latitude'], longitude = location['longitude'], altitude = location['altitude'], ) locale.setlocale(locale.LC_TIME, "fr_FR.utf8") cur_date = datetime.fromisoformat("1970-01-01T00:00:00Z").date() for time in response["properties"]["timeseries"]: new_date = datetime.fromisoformat(time["time"]) if new_date.date() > cur_date: print(new_date.strftime("%A, %d. %B %Y")) cur_date = new_date.date() cur_hour = new_date.strftime("%H:%M") cur_temp = time["data"]["instant"]["details"]["air_temperature"] cur_humidity = time["data"]["instant"]["details"]["relative_humidity"] print(f" {cur_hour} : {cur_temp}° / {cur_humidity}%") if __name__ == "__main__": main()