diff --git a/17_owm_multicity/main.py b/17_owm_multicity/main.py new file mode 100644 index 0000000..fff3113 --- /dev/null +++ b/17_owm_multicity/main.py @@ -0,0 +1,61 @@ +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 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)) + + html = "

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

" + html += f"

{mesta}

" + 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