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.
107 lines
3.4 KiB
107 lines
3.4 KiB
import requests
|
|
import datetime
|
|
import mysql.connector
|
|
from dotenv import load_dotenv
|
|
import os
|
|
|
|
# Laad de .env bestand
|
|
print("Loading environment variables from .env...")
|
|
load_dotenv()
|
|
|
|
# Haal de API-sleutel uit de omgevingsvariabelen
|
|
api_key = os.getenv('OPENWEATHERMAP_API_KEY')
|
|
# Haal de MySQL-gegevens uit de omgevingsvariabelen
|
|
db_host = os.getenv('DB_HOST')
|
|
db_user = os.getenv('DB_USER')
|
|
db_password = os.getenv('DB_PASSWORD')
|
|
db_database = os.getenv('DB_DATABASE')
|
|
# Haal de locatie uit de omgevingsvariabelen
|
|
location = os.getenv('LOCATION')
|
|
|
|
print(f"Using OpenWeatherMap API key: {api_key}")
|
|
print(f"Using location: {location}")
|
|
|
|
# URL van de OpenWeatherMap API voor de forecast
|
|
url = f'http://api.openweathermap.org/data/2.5/forecast?q={location}&units=metric&cnt=7&appid={api_key}'
|
|
|
|
print(f"Fetching weather forecast from OpenWeatherMap for {location}...")
|
|
|
|
# Haal de data op
|
|
response = requests.get(url)
|
|
if response.status_code == 200:
|
|
print("Data successfully fetched from OpenWeatherMap API.")
|
|
data = response.json()
|
|
else:
|
|
print(f"Error fetching data: {response.status_code}")
|
|
data = {}
|
|
|
|
# Verbinding maken met MySQL-database
|
|
print("Connecting to MySQL database...")
|
|
db_connection = mysql.connector.connect(
|
|
host=db_host,
|
|
user=db_user,
|
|
password=db_password,
|
|
database=db_database
|
|
)
|
|
cursor = db_connection.cursor()
|
|
|
|
# Maak de tabel aan als deze nog niet bestaat
|
|
print("Ensuring the forecast_data table exists...")
|
|
create_table_query = """
|
|
CREATE TABLE IF NOT EXISTS forecast_data (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
utc_datetime DATETIME UNIQUE,
|
|
temperature FLOAT,
|
|
weather_description VARCHAR(255),
|
|
wind_speed FLOAT,
|
|
rain FLOAT
|
|
);
|
|
"""
|
|
cursor.execute(create_table_query)
|
|
print("Table `forecast_data` is ready.")
|
|
|
|
# Toon de forecast en sla deze op in de database
|
|
print(f"Processing forecast data for {len(data.get('list', []))} entries...")
|
|
|
|
for entry in data.get('list', []):
|
|
# Converteer de tijd naar UTC
|
|
utc_dt = datetime.datetime.utcfromtimestamp(entry['dt'])
|
|
temp = entry['main']['temp']
|
|
weather = entry['weather'][0]['description']
|
|
wind_speed = entry['wind']['speed']
|
|
# Regensomstandigheden (indien aanwezig)
|
|
rain = entry.get('rain', {}).get('3h', 0)
|
|
|
|
# Print de data
|
|
print(f"\nProcessing data for {utc_dt.strftime('%Y-%m-%d %H:%M:%S')} UTC:")
|
|
print(f"Temperature: {temp}°C")
|
|
print(f"Weather: {weather.capitalize()}")
|
|
print(f"Wind Speed: {wind_speed} m/s")
|
|
print(f"Rain: {rain} mm")
|
|
|
|
# Voeg de data toe aan de database of update als het record al bestaat
|
|
query = """
|
|
INSERT INTO forecast_data (utc_datetime, temperature, weather_description, wind_speed, rain)
|
|
VALUES (%s, %s, %s, %s, %s)
|
|
ON DUPLICATE KEY UPDATE
|
|
temperature = VALUES(temperature),
|
|
weather_description = VALUES(weather_description),
|
|
wind_speed = VALUES(wind_speed),
|
|
rain = VALUES(rain);
|
|
"""
|
|
|
|
print(
|
|
f"Executing query to insert/update data for {utc_dt.strftime('%Y-%m-%d %H:%M:%S')}...")
|
|
cursor.execute(query, (utc_dt, temp, weather, wind_speed, rain))
|
|
print(
|
|
f"Data for {utc_dt.strftime('%Y-%m-%d %H:%M:%S')} successfully inserted/updated.")
|
|
|
|
# Commit de veranderingen en sluit de verbinding
|
|
print("Committing changes to the database...")
|
|
db_connection.commit()
|
|
print("Changes committed successfully.")
|
|
|
|
cursor.close()
|
|
db_connection.close()
|
|
print("Database connection closed.")
|