Un peu de refactorisation (ask_api)
This commit is contained in:
34
main.py
34
main.py
@@ -10,7 +10,7 @@ from pathlib import Path
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
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():
|
||||||
@@ -31,6 +31,19 @@ def load_args():
|
|||||||
return vars(parser.parse_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():
|
def main():
|
||||||
"""Fonction principale"""
|
"""Fonction principale"""
|
||||||
|
|
||||||
@@ -47,21 +60,16 @@ def main():
|
|||||||
print(f"{args['location']} n'est pas configuré.")
|
print(f"{args['location']} n'est pas configuré.")
|
||||||
os._exit(1)
|
os._exit(1)
|
||||||
|
|
||||||
url = f"{yr_no_url}compact.json?lat={location['latitude']}&lon={location['longitude']}&altitude={location['altitude']}"
|
response = ask_api(
|
||||||
headers = {"User-Agent": "DaikoMete/0.1 daiko@daiko.fr"}
|
latitude = location['latitude'],
|
||||||
response = requests.get(url, headers=headers)
|
longitude = location['longitude'],
|
||||||
|
altitude = location['altitude'],
|
||||||
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()
|
|
||||||
|
|
||||||
locale.setlocale(locale.LC_TIME, "fr_FR.utf8")
|
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"])
|
new_date = datetime.fromisoformat(time["time"])
|
||||||
|
|
||||||
if new_date.date() > cur_date:
|
if new_date.date() > cur_date:
|
||||||
|
Reference in New Issue
Block a user