Un peu de refactorisation (display_full)
This commit is contained in:
55
main.py
55
main.py
@@ -3,7 +3,6 @@
|
|||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
import locale
|
import locale
|
||||||
import pprint
|
|
||||||
import argparse
|
import argparse
|
||||||
import requests
|
import requests
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -32,7 +31,7 @@ def load_args():
|
|||||||
|
|
||||||
|
|
||||||
def ask_api(latitude: float, longitude: float, altitude: int) -> dict:
|
def ask_api(latitude: float, longitude: float, altitude: int) -> dict:
|
||||||
""" """
|
""" interrege l'API de https://yr.no """
|
||||||
url = f"{YR_NO_URL}compact.json?lat={latitude}&lon={longitude}&altitude={altitude}"
|
url = f"{YR_NO_URL}compact.json?lat={latitude}&lon={longitude}&altitude={altitude}"
|
||||||
headers = {"User-Agent": "DaikoMete/0.1 daiko@daiko.fr"}
|
headers = {"User-Agent": "DaikoMete/0.1 daiko@daiko.fr"}
|
||||||
response = requests.get(url, headers=headers)
|
response = requests.get(url, headers=headers)
|
||||||
@@ -44,32 +43,12 @@ def ask_api(latitude: float, longitude: float, altitude: int) -> dict:
|
|||||||
return json.loads(response.text)
|
return json.loads(response.text)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def display_full(api_response):
|
||||||
"""Fonction principale"""
|
""" Affiche les informations météo complètes """
|
||||||
|
|
||||||
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")
|
locale.setlocale(locale.LC_TIME, "fr_FR.utf8")
|
||||||
cur_date = datetime.fromisoformat("1970-01-01T00:00:00Z").date()
|
cur_date = datetime.fromisoformat("1970-01-01T00:00:00Z").date()
|
||||||
|
|
||||||
for time in response["properties"]["timeseries"]:
|
for time in api_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:
|
||||||
@@ -82,5 +61,31 @@ def main():
|
|||||||
print(f" {cur_hour} : {cur_temp}° / {cur_humidity}%")
|
print(f" {cur_hour} : {cur_temp}° / {cur_humidity}%")
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
api_response = ask_api(
|
||||||
|
latitude=location["latitude"],
|
||||||
|
longitude=location["longitude"],
|
||||||
|
altitude=location["altitude"],
|
||||||
|
)
|
||||||
|
|
||||||
|
display_full(api_response)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Reference in New Issue
Block a user