79 lines
2.1 KiB
Python
Executable File
79 lines
2.1 KiB
Python
Executable File
#!/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 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)
|
|
|
|
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()
|
|
|
|
locale.setlocale(locale.LC_TIME, "fr_FR.utf8")
|
|
|
|
for time in j["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()
|