diff --git a/15_owm_get_api_data/dashboard.py b/15_owm_get_api_data/dashboard.py new file mode 100644 index 0000000..b9f16bc --- /dev/null +++ b/15_owm_get_api_data/dashboard.py @@ -0,0 +1,59 @@ +from flask import jsonify, Flask +import requests +import json + +app = Flask(__name__) + +#klíč získate na https://home.openweathermap.org/api_keys +KLIC = "f70d944f638a7ae77de89435d4d23c01" +MESTO = "Praha" +# AltGR + C = & +url1 = f"https://api.openweathermap.org/data/2.5/" +url2 = f"weather?q={MESTO}&appid={KLIC}&units=metric&lang=cz" +url = url1 + url2 + +def nacti_pocasi(): + """Získá data z OpenWeatherMap API""" + try: + response = requests.get(url) + response.raise_for_status() + if response.status_code == 200: + data = response.json() + # AltGr + B|N = {|} + results = { + "mesto":data["name"], + "teplota":data["main"]["temp"], + "vlhkost":data["main"]["humidity"], + "popis":data["weather"][0]["description"] + } + return results + else: + return {"chyba":f"{response.status_code}. Zkontroluj API klíč"} + except Exception as e: + return {"chyba":f"Chyba asi v síti: {e}"} + +@app.route("/") +def index(): + """Hlavní stránka""" + pocasi = nacti_pocasi() + + if "chyba" in pocasi: + return f"

Chyba

{pocasi['chyba']}

" + + html = f""" +

{pocasi['mesto']}

+ + """ + return html + +if __name__ == "__main__": + print("-"*50) + print("Spouštím Dashboard na počasí") + print("adresa http://localhost:8080") + print(f"Město: {MESTO}") + print("-"*50) + app.run(host="0.0.0.0",port=8080,debug=True) \ No newline at end of file diff --git a/15_owm_get_api_data/rychly_pocasi.py b/15_owm_get_api_data/rychly_pocasi.py new file mode 100644 index 0000000..df11f79 --- /dev/null +++ b/15_owm_get_api_data/rychly_pocasi.py @@ -0,0 +1,25 @@ +import requests +import json + +#klíč získate na https://home.openweathermap.org/api_keys +KLIC = "f70d944f638a7ae77de89435d4d23c01" +MESTO = input("Zadej město:") +# AltGR + C = & +url1 = f"https://api.openweathermap.org/data/2.5/" +url2 = f"weather?q={MESTO}&appid={KLIC}&units=metric&lang=cz" +url = url1 + url2 + +print(f"Otevírám odkaz: {url}") +odpoved = requests.get(url) +odpoved.raise_for_status() +data = odpoved.json() +#pro výpis celé json odpovědi +print(json.dumps(data,indent=4,ensure_ascii=False)) + +popis = data["weather"][0]["description"] +teplota = data["main"]["temp"] +owm_mesto = data["name"] + +print(f"Město: {owm_mesto} (napsáno:{MESTO})") +print(f"Teplota: {teplota}°C") +print(f"Popis: {popis}") \ No newline at end of file