Wetter-API Integration hinzugefügt
This commit is contained in:
44
app.py
44
app.py
@@ -4,6 +4,8 @@ import os
|
||||
import logging
|
||||
import numpy as np
|
||||
from datetime import datetime
|
||||
from dotenv import load_dotenv
|
||||
import requests
|
||||
|
||||
app = Flask(__name__, static_folder='static')
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
@@ -15,6 +17,9 @@ VERSION = "1.0.1"
|
||||
# Pfad zur CSV-Datei
|
||||
CSV_FILE = "data/customers.csv"
|
||||
|
||||
# Lade Umgebungsvariablen
|
||||
load_dotenv()
|
||||
|
||||
def clean_dataframe(df):
|
||||
"""Konvertiert NaN-Werte in None für JSON-Kompatibilität"""
|
||||
return df.replace({np.nan: None})
|
||||
@@ -35,6 +40,31 @@ def load_data():
|
||||
logger.error(f"Fehler beim Laden der CSV-Datei: {str(e)}")
|
||||
return None
|
||||
|
||||
def get_weather(city):
|
||||
"""Holt Wetterinformationen für eine Stadt von der OpenWeather API"""
|
||||
api_key = os.getenv('OPENWEATHER_API_KEY')
|
||||
base_url = "http://api.openweathermap.org/data/2.5/weather"
|
||||
|
||||
try:
|
||||
params = {
|
||||
'q': city,
|
||||
'appid': api_key,
|
||||
'units': 'metric', # Temperatur in Celsius
|
||||
'lang': 'de'
|
||||
}
|
||||
response = requests.get(base_url, params=params)
|
||||
data = response.json()
|
||||
|
||||
if response.status_code == 200:
|
||||
return {
|
||||
'temperature': round(data['main']['temp']),
|
||||
'icon': data['weather'][0]['icon'],
|
||||
'description': data['weather'][0]['description']
|
||||
}
|
||||
except Exception as e:
|
||||
print(f"Fehler beim Abrufen der Wetterdaten: {e}")
|
||||
return None
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return render_template('index.html')
|
||||
@@ -95,7 +125,19 @@ def search():
|
||||
|
||||
results = df[mask].to_dict('records')
|
||||
logger.info(f"{len(results)} Ergebnisse gefunden")
|
||||
return jsonify(results)
|
||||
|
||||
# Füge Wetterinformationen für jeden Treffer hinzu
|
||||
for result in results:
|
||||
weather = get_weather(result['Ort'])
|
||||
if weather:
|
||||
result['weather'] = weather
|
||||
else:
|
||||
result['weather'] = None
|
||||
|
||||
return jsonify({
|
||||
'results': results,
|
||||
'total': len(results)
|
||||
})
|
||||
except Exception as e:
|
||||
logger.error(f"Fehler bei der Suche: {str(e)}")
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
Reference in New Issue
Block a user