From f00f674da007932ae16123305160399c3c448789 Mon Sep 17 00:00:00 2001 From: Florestan Bredow Date: Thu, 14 Aug 2025 13:40:04 +0200 Subject: [PATCH] Un peu de refactorisation (ask_api) --- main.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/main.py b/main.py index ad51964..4b18009 100755 --- a/main.py +++ b/main.py @@ -10,7 +10,7 @@ from pathlib import Path from datetime import datetime 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(): @@ -31,6 +31,19 @@ def load_args(): 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""" @@ -47,21 +60,16 @@ def main(): print(f"{args['location']} n'est pas configuré.") os._exit(1) - url = f"{yr_no_url}compact.json?lat={location['latitude']}&lon={location['longitude']}&altitude={location['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) - - j = json.loads(response.text) - - cur_date = datetime.fromisoformat("1970-01-01T00:00:00Z").date() + 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 j["properties"]["timeseries"]: + for time in response["properties"]["timeseries"]: new_date = datetime.fromisoformat(time["time"]) if new_date.date() > cur_date: