aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/entsoe.py
diff options
context:
space:
mode:
authorDennis Eriksen <d@ennis.no>2023-11-02 12:49:02 +0100
committerDennis Eriksen <d@ennis.no>2023-11-02 12:49:02 +0100
commitb322330b000a6d566e99cde1c29429a23b5f2cb3 (patch)
treee6c6b646869e853de4dd9179d5c6f5a5ce5bc153 /scripts/entsoe.py
parentadding linting with ruff (diff)
downloadenergyscripts-b322330b000a6d566e99cde1c29429a23b5f2cb3.tar.gz
adding formatting with ruff
Diffstat (limited to 'scripts/entsoe.py')
-rw-r--r--scripts/entsoe.py59
1 files changed, 35 insertions, 24 deletions
diff --git a/scripts/entsoe.py b/scripts/entsoe.py
index 70a1f19..fc34274 100644
--- a/scripts/entsoe.py
+++ b/scripts/entsoe.py
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
-''' Get energyprices from Entsoe '''
+""" Get energyprices from Entsoe """
import os
import sys
@@ -22,36 +22,49 @@ from tzlocal import get_localzone
# > respond to your request.
# 1: https://transparency.entsoe.eu/content/static_content/Static%20content/web%20api/Guide.html#_authentication_and_authorisation
# 2: https://transparency.entsoe.eu/content/static_content/download?path=/Static%20content/API-Token-Management.pdf
-apiKey = os.environ['el_entsoe_token']
+apiKey = os.environ["el_entsoe_token"]
# https://transparency.entsoe.eu/content/static_content/Static%20content/web%20api/Guide.html
apiUrl = "https://web-api.tp.entsoe.eu/api?securityToken=" + apiKey
-startTime = datetime.now(get_localzone()) - timedelta(days = 7)
-startTime = startTime.strftime('%Y%m%d')
-#startTime = '20230523' # <- edit for manual starttime. Like when filling in missing info.
+startTime = datetime.now(get_localzone()) - timedelta(days=7)
+startTime = startTime.strftime("%Y%m%d")
+# startTime = '20230523' # <- edit for manual starttime. Like when filling in missing info.
-endTime = datetime.now(get_localzone()) + timedelta(days = 1)
-endTime = endTime.strftime('%Y%m%d')
+endTime = datetime.now(get_localzone()) + timedelta(days=1)
+endTime = endTime.strftime("%Y%m%d")
jobname = os.path.splitext(os.path.basename(__file__))[0]
# https://transparency.entsoe.eu/content/static_content/Static%20content/web%20api/Guide.html#_areas
-areas = [ {"name": "NO-0", "code": "10YNO-0--------C"},
- {"name": "NO-1", "code": "10YNO-1--------2"},
- {"name": "NO-2", "code": "10YNO-2--------T"},
- {"name": "NO-3", "code": "10YNO-3--------J"},
- {"name": "NO-4", "code": "10YNO-4--------9"} ]
+areas = [
+ {"name": "NO-0", "code": "10YNO-0--------C"},
+ {"name": "NO-1", "code": "10YNO-1--------2"},
+ {"name": "NO-2", "code": "10YNO-2--------T"},
+ {"name": "NO-3", "code": "10YNO-3--------J"},
+ {"name": "NO-4", "code": "10YNO-4--------9"},
+]
-UTC = tz.gettz('UTC')
-CET = tz.gettz('Europe/Oslo')
+UTC = tz.gettz("UTC")
+CET = tz.gettz("Europe/Oslo")
# Get the data
-values=[]
+values = []
for area in areas:
try:
- url = apiUrl + "&documentType=A44&in_Domain=" + area["code"] + "&out_Domain=" + area["code"] + "&periodStart=" + startTime + "0000&periodEnd=" + endTime + "0000"
+ url = (
+ apiUrl
+ + "&documentType=A44&in_Domain="
+ + area["code"]
+ + "&out_Domain="
+ + area["code"]
+ + "&periodStart="
+ + startTime
+ + "0000&periodEnd="
+ + endTime
+ + "0000"
+ )
print("Getting data for " + area["code"])
response = requests.get(url, timeout=10)
@@ -69,20 +82,19 @@ for area in areas:
items = 0
if "Publication_MarketDocument" in data_dict:
for lista in data_dict["Publication_MarketDocument"]["TimeSeries"]:
- utctime = datetime.strptime(lista["Period"]["timeInterval"]["start"], "%Y-%m-%dT%H:%MZ")
- utctime = utctime.replace(tzinfo = UTC)
+ utctime = datetime.strptime(
+ lista["Period"]["timeInterval"]["start"], "%Y-%m-%dT%H:%MZ"
+ )
+ utctime = utctime.replace(tzinfo=UTC)
cettime = utctime.astimezone(CET)
items += len(lista["Period"]["Point"])
for item in lista["Period"]["Point"]:
# the response contains timerange, but not timestamp for every price, so we must calculate it
- time = str(cettime + timedelta(hours = int(item["position"]) - 1))
+ time = str(cettime + timedelta(hours=int(item["position"]) - 1))
# append values
- values.append((
- time,
- area["name"],
- item["price.amount"]))
+ values.append((time, area["name"], item["price.amount"]))
print("Got " + str(items) + " records")
@@ -92,5 +104,4 @@ sql = """ INSERT INTO entsoe
ON CONFLICT (starttime, zone) DO NOTHING"""
-
common.dbi(sql, values, verbose=True)