Un peu de refactorisation (ask_api)

This commit is contained in:
2025-08-14 13:40:04 +02:00
parent d5ca160949
commit f00f674da0

34
main.py
View File

@@ -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: