From 9c3a8c097c12b3156c0740847549854964af3994 Mon Sep 17 00:00:00 2001 From: KubMakCZ Date: Wed, 29 Apr 2026 09:37:52 +0200 Subject: [PATCH] multiapi dashboard --- 17_owm_multicity/multiapi.py | 82 ++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 17_owm_multicity/multiapi.py diff --git a/17_owm_multicity/multiapi.py b/17_owm_multicity/multiapi.py new file mode 100644 index 0000000..d6d5f5f --- /dev/null +++ b/17_owm_multicity/multiapi.py @@ -0,0 +1,82 @@ +from flask import Flask, jsonify +import requests + +app = Flask(__name__) + +API_KEY = "f70d944f638a7ae77de89435d4d23c01" +mesta = ["Praha","Štětí","Hněvice","Ústí nad Labem"] +url1 = f"http://api.openweathermap.org/data/2.5/weather?" +url2 = f"appid={API_KEY}&units=metric&lang=cz&q=" +url_base = url1+url2 + +def get_chuck_joke(): + try: + res = requests.get("https://api.chucknorris.io/jokes/random") + if res.status_code == 200: + return res.json()["value"] + except: + return "Chuck Norris dnes nemá náladu na vtipy. ☺" + +def get_random_fact(): + try: + res = requests.get("https://uselessfacts.jsph.pl/api/v2/facts/random") + if res.status_code == 200: + return res.json()["text"] + except: + return "Věděli jste, že se nepodařilo načíst žádný fact? ☺" + +def nacti_pocasi(mesto): + try: + response = requests.get(url_base+mesto) + if response.status_code == 200: + data = response.json() + return { + "name": data["name"], + "temp": data["main"]["temp"], + "hum": data["main"]["humidity"], + "desc": data["weather"][0]["description"] + } + else: + return { + "name": mesto, + "error":f"API Chyba: {response.status_code}" + } + except Exception as e: + return { + "name": mesto, + "error": f"Chyba připojení: {e}" + } + +@app.route("/") +def index(): + vysledky_pocasi = [] + + for kontkretni_mesto in mesta: + vysledky_pocasi.append(nacti_pocasi(kontkretni_mesto)) + joke = get_chuck_joke() + html = "

Aktuální počasí ve městech

" + html += f"

{mesta}

" + html += "
" + html += "

Chuck Norris Qoute of the day

" + html += f"{joke}" + html += "
" + for vysledek in vysledky_pocasi: + if "error" in vysledek: + html += f"

{vysledek['name']}

" + html += f"" + else: + html += f"

{vysledek['name']}

" + html += "" + html += '

Aktualizovat data

' + return html + +if __name__ == "__main__": + print("Multi town Weather Dashboard") + print("Link: http://localhost:8080") + print(f"Města: {', '.join(mesta)}") + app.run(host="0.0.0.0",port="8080",debug=True) \ No newline at end of file