initial commit
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
venv/
|
||||
89
copy_energy_prices.py
Normal file
89
copy_energy_prices.py
Normal file
@@ -0,0 +1,89 @@
|
||||
# copy_to_energy_prices.py
|
||||
import mysql.connector
|
||||
import sys
|
||||
from mysql.connector import Error
|
||||
|
||||
def copy_data():
|
||||
source_conn = None
|
||||
dest_conn = None
|
||||
|
||||
# ---- Gegevens voor de BRON-database (alfen) ----
|
||||
source_db_config = {
|
||||
'host': '127.0.0.1',
|
||||
'database': 'alfen',
|
||||
'user': 'alfen_user',
|
||||
'password': '5uVgr%f%s2P5GR@3q!',
|
||||
'port': 3307
|
||||
}
|
||||
|
||||
# ---- Gegevens voor de DOEL-database (energy_prices) ----
|
||||
# VUL DEZE GEGEVENS ZELF IN
|
||||
dest_db_config = {
|
||||
'host': '127.0.0.1',
|
||||
'database': 'energy_prices',
|
||||
'user': 'energy_prices_user',
|
||||
'password': 'kS9R*xp17ZwCD@CV&E^N',
|
||||
'port': 3307
|
||||
}
|
||||
|
||||
try:
|
||||
# 1. Maak verbinding met de bron-database (alfen)
|
||||
source_conn = mysql.connector.connect(**source_db_config)
|
||||
|
||||
# 2. Maak verbinding met de doel-database (energy_prices)
|
||||
dest_conn = mysql.connector.connect(**dest_db_config)
|
||||
|
||||
if source_conn.is_connected() and dest_conn.is_connected():
|
||||
source_cursor = source_conn.cursor(dictionary=True) # dictionary=True maakt 'row['column_name']' mogelijk
|
||||
dest_cursor = dest_conn.cursor()
|
||||
|
||||
# 3. Selecteer de data van VANDAAG uit de 'alfen' database
|
||||
# We gaan ervan uit dat de API (stroomprijs_vandaag) data voor vandaag levert
|
||||
print("Data selecteren uit 'alfen' database...")
|
||||
select_query = "SELECT datetime, provider_code, price FROM dynamic_price_data WHERE DATE(datetime) = CURDATE()"
|
||||
source_cursor.execute(select_query)
|
||||
|
||||
rows_to_copy = source_cursor.fetchall()
|
||||
|
||||
if not rows_to_copy:
|
||||
print("Geen data van vandaag gevonden in 'alfen' om te kopiëren.")
|
||||
return
|
||||
|
||||
print(f"{len(rows_to_copy)} rijen gevonden om te kopiëren.")
|
||||
|
||||
# 4. Voer de data in in de 'energy_prices' database
|
||||
insert_query = """
|
||||
INSERT INTO dynamic_price_data (datetime, provider_code, price)
|
||||
VALUES (%s, %s, %s)
|
||||
ON DUPLICATE KEY UPDATE price = %s
|
||||
"""
|
||||
|
||||
copied_count = 0
|
||||
for row in rows_to_copy:
|
||||
try:
|
||||
data_tuple = (row['datetime'], row['provider_code'], row['price'], row['price'])
|
||||
dest_cursor.execute(insert_query, data_tuple)
|
||||
copied_count += 1
|
||||
except Error as e:
|
||||
print(f"Fout bij invoeren rij {row}: {e}")
|
||||
|
||||
# Commit de transactie naar de doel-database
|
||||
dest_conn.commit()
|
||||
print(f"Succesvol {copied_count} rijen gekopieerd naar 'energy_prices'.")
|
||||
|
||||
except Error as e:
|
||||
# Stuur de foutmelding naar stderr zodat het hoofdscript dit kan opvangen
|
||||
print(f"Fout tijdens kopieerproces: {e}", file=sys.stderr)
|
||||
sys.exit(1) # Sluit af met een foutcode
|
||||
|
||||
finally:
|
||||
# Sluit alle verbindingen en cursors
|
||||
if source_conn and source_conn.is_connected():
|
||||
source_cursor.close()
|
||||
source_conn.close()
|
||||
if dest_conn and dest_conn.is_connected():
|
||||
dest_cursor.close()
|
||||
dest_conn.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
copy_data()
|
||||
80
dynamic.py
Normal file
80
dynamic.py
Normal file
@@ -0,0 +1,80 @@
|
||||
import requests
|
||||
import mysql.connector
|
||||
from mysql.connector import Error
|
||||
|
||||
def insert_dynamic_price_data():
|
||||
connection = None # Connection buiten de try initialiseren
|
||||
|
||||
try:
|
||||
url = "https://enever.nl/api/stroomprijs_vandaag.php?token=5a7d9b371fe147cfc8100bcf6d9ebd55"
|
||||
response = requests.get(url)
|
||||
|
||||
if response.status_code != 200:
|
||||
raise Exception('Fout bij het ophalen van de data.')
|
||||
|
||||
data = response.json()
|
||||
|
||||
if data is None:
|
||||
raise Exception('Fout bij het decoderen van de JSON-data.')
|
||||
|
||||
connection = mysql.connector.connect(
|
||||
host='127.0.0.1', # Gebruik 127.0.0.1 ipv localhost
|
||||
database='alfen',
|
||||
user='alfen_user',
|
||||
password='5uVgr%f%s2P5GR@3q!',
|
||||
port=3307
|
||||
)
|
||||
|
||||
if connection.is_connected():
|
||||
cursor = connection.cursor()
|
||||
|
||||
query = """
|
||||
INSERT INTO dynamic_price_data (datetime, provider_code, price)
|
||||
VALUES (%s, %s, %s)
|
||||
ON DUPLICATE KEY UPDATE price = %s
|
||||
"""
|
||||
|
||||
providers = {
|
||||
'AA': 'prijsAA',
|
||||
'AIP': 'prijsAIP',
|
||||
'ANWB': 'prijsANWB',
|
||||
'BE': 'prijsBE',
|
||||
'EE': 'prijsEE',
|
||||
'EN': 'prijsEN',
|
||||
'EVO': 'prijsEVO',
|
||||
'EZ': 'prijsEZ',
|
||||
'FR': 'prijsFR',
|
||||
'GSL': 'prijsGSL',
|
||||
'MDE': 'prijsMDE',
|
||||
'NE': 'prijsNE',
|
||||
'TI': 'prijsTI',
|
||||
'VDB': 'prijsVDB',
|
||||
'VON': 'prijsVON',
|
||||
'WE': 'prijsWE',
|
||||
'ZG': 'prijsZG',
|
||||
'ZP': 'prijsZP'
|
||||
}
|
||||
|
||||
for entry in data['data']:
|
||||
datetime = entry['datum']
|
||||
for code, price_field in providers.items():
|
||||
if price_field in entry:
|
||||
price = entry[price_field]
|
||||
try:
|
||||
cursor.execute(query, (datetime, code, price, price))
|
||||
connection.commit()
|
||||
except Error as e:
|
||||
print(f"Fout bij invoeren van data voor provider {code}: {e}")
|
||||
continue
|
||||
|
||||
except Error as e:
|
||||
print(f"Fout bij databaseverbinding: {e}")
|
||||
except Exception as e:
|
||||
print(f"Algemene fout: {e}")
|
||||
finally:
|
||||
if connection is not None and connection.is_connected():
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
insert_dynamic_price_data()
|
||||
78
gas.py
Normal file
78
gas.py
Normal file
@@ -0,0 +1,78 @@
|
||||
import requests
|
||||
import mysql.connector
|
||||
from mysql.connector import Error
|
||||
|
||||
def insert_gas_price_data():
|
||||
connection = None
|
||||
|
||||
try:
|
||||
url = "https://enever.nl/api/gasprijs_vandaag.php?token=5a7d9b371fe147cfc8100bcf6d9ebd55"
|
||||
response = requests.get(url)
|
||||
|
||||
if response.status_code != 200:
|
||||
raise Exception('Fout bij het ophalen van de gasdata.')
|
||||
|
||||
data = response.json()
|
||||
|
||||
if data is None or 'data' not in data:
|
||||
raise Exception('Fout bij het decoderen van de JSON-data.')
|
||||
|
||||
connection = mysql.connector.connect(
|
||||
host='127.0.0.1',
|
||||
database='alfen',
|
||||
user='alfen_user',
|
||||
password='5uVgr%f%s2P5GR@3q!',
|
||||
port=3307
|
||||
)
|
||||
|
||||
if connection.is_connected():
|
||||
cursor = connection.cursor()
|
||||
|
||||
query = """
|
||||
INSERT INTO gas_price_data (datetime, provider_code, price)
|
||||
VALUES (%s, %s, %s)
|
||||
ON DUPLICATE KEY UPDATE price = %s
|
||||
"""
|
||||
|
||||
providers = {
|
||||
'EGSI': 'prijsEGSI',
|
||||
'EOD': 'prijsEOD',
|
||||
'ANWB': 'prijsANWB',
|
||||
'BE': 'prijsBE',
|
||||
'EE': 'prijsEE',
|
||||
'EN': 'prijsEN',
|
||||
'EVO': 'prijsEVO',
|
||||
'EZ': 'prijsEZ',
|
||||
'FR': 'prijsFR',
|
||||
'GSL': 'prijsGSL',
|
||||
'MDE': 'prijsMDE',
|
||||
'NE': 'prijsNE',
|
||||
'VDB': 'prijsVDB',
|
||||
'VON': 'prijsVON',
|
||||
'WE': 'prijsWE',
|
||||
'ZP': 'prijsZP'
|
||||
}
|
||||
|
||||
for entry in data['data']:
|
||||
datetime = entry['datum']
|
||||
for code, price_field in providers.items():
|
||||
if price_field in entry:
|
||||
price = entry[price_field]
|
||||
try:
|
||||
cursor.execute(query, (datetime, code, price, price))
|
||||
connection.commit()
|
||||
except Error as e:
|
||||
print(f"Fout bij invoeren van gasdata voor provider {code}: {e}")
|
||||
continue
|
||||
|
||||
except Error as e:
|
||||
print(f"Fout bij databaseverbinding: {e}")
|
||||
except Exception as e:
|
||||
print(f"Algemene fout: {e}")
|
||||
finally:
|
||||
if connection is not None and connection.is_connected():
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
insert_gas_price_data()
|
||||
84
tommorow.py
Normal file
84
tommorow.py
Normal file
@@ -0,0 +1,84 @@
|
||||
import requests
|
||||
import mysql.connector
|
||||
from mysql.connector import Error
|
||||
|
||||
def insert_dynamic_price_data_tomorrow():
|
||||
connection = None # Connection buiten de try initialiseren
|
||||
|
||||
try:
|
||||
# URL van de API
|
||||
url = "https://enever.nl/api/stroomprijs_morgen.php?token=5a7d9b371fe147cfc8100bcf6d9ebd55"
|
||||
response = requests.get(url)
|
||||
|
||||
if response.status_code != 200:
|
||||
raise Exception('Fout bij het ophalen van de data.')
|
||||
|
||||
data = response.json()
|
||||
|
||||
if data is None or 'data' not in data:
|
||||
raise Exception('Fout bij het decoderen van de JSON-data.')
|
||||
|
||||
# Verbinden met de MySQL-database
|
||||
connection = mysql.connector.connect(
|
||||
host='127.0.0.1', # Gebruik 127.0.0.1 ipv localhost
|
||||
database='alfen',
|
||||
user='alfen_user',
|
||||
password='5uVgr%f%s2P5GR@3q!',
|
||||
port=3307
|
||||
)
|
||||
|
||||
if connection.is_connected():
|
||||
cursor = connection.cursor()
|
||||
|
||||
# Verwijder bestaande records uit de tabel
|
||||
cursor.execute("DELETE FROM dynamic_price_data_tommorow")
|
||||
connection.commit()
|
||||
|
||||
# Voorbereiding van de query
|
||||
query = """
|
||||
INSERT INTO dynamic_price_data_tommorow (datetime, price)
|
||||
VALUES (%s, %s)
|
||||
"""
|
||||
|
||||
# Sleutelwoorden voor prijsvelden
|
||||
price_fields = [
|
||||
'prijsAA', 'prijsAIP', 'prijsANWB', 'prijsBE', 'prijsEE',
|
||||
'prijsEN', 'prijsEVO', 'prijsEZ', 'prijsFR', 'prijsGSL',
|
||||
'prijsMDE', 'prijsNE', 'prijsTI', 'prijsVDB', 'prijsVON',
|
||||
'prijsWE', 'prijsZG', 'prijsZP'
|
||||
]
|
||||
|
||||
# Data invoegen
|
||||
for entry in data['data']:
|
||||
datetime = entry['datum']
|
||||
|
||||
# Gemiddelde prijs berekenen (exclusief het veld 'prijs')
|
||||
prices = [
|
||||
float(entry[field]) for field in price_fields if field in entry and entry[field] != ''
|
||||
]
|
||||
if prices:
|
||||
avg_price = sum(prices) / len(prices)
|
||||
else:
|
||||
avg_price = None # Geen prijzen beschikbaar
|
||||
|
||||
# Data invoegen in de tabel
|
||||
if avg_price is not None:
|
||||
try:
|
||||
cursor.execute(query, (datetime, avg_price))
|
||||
connection.commit()
|
||||
except Error as e:
|
||||
print(f"Fout bij invoeren van data voor datetime {datetime}: {e}")
|
||||
continue
|
||||
|
||||
except Error as e:
|
||||
print(f"Fout bij databaseverbinding: {e}")
|
||||
except Exception as e:
|
||||
print(f"Algemene fout: {e}")
|
||||
finally:
|
||||
# Sluit de verbinding
|
||||
if connection is not None and connection.is_connected():
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
insert_dynamic_price_data_tomorrow()
|
||||
Reference in New Issue
Block a user