You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.6 KiB
82 lines
2.6 KiB
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 = "<h1> Aktuální počasí ve městech </h1>"
|
|
html += f"<p>{mesta}</p>"
|
|
html += "<hr>"
|
|
html += "<h3>Chuck Norris Qoute of the day</h3>"
|
|
html += f"{joke}"
|
|
html += "<hr>"
|
|
for vysledek in vysledky_pocasi:
|
|
if "error" in vysledek:
|
|
html += f"<h2>{vysledek['name']}</h2>"
|
|
html += f"<ul><li>Chyba:{vysledek['error']}</li></ul>"
|
|
else:
|
|
html += f"<h2>{vysledek['name']}</h2>"
|
|
html += "<ul>"
|
|
html += f"<li>Teplota: {vysledek['temp']}°C</li>"
|
|
html += f"<li>Vlhkost: {vysledek['hum']}%</li>"
|
|
html += f"<li>Popis: {vysledek['desc']}</li>"
|
|
html += f"<li> Zajímavost: {get_random_fact()}</li>"
|
|
html += "</ul>"
|
|
html += '<p><a href="/">Aktualizovat data</a></p>'
|
|
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)
|