aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/entsoe.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/entsoe.py')
-rw-r--r--scripts/entsoe.py40
1 files changed, 19 insertions, 21 deletions
diff --git a/scripts/entsoe.py b/scripts/entsoe.py
index 2c825c2..1836229 100644
--- a/scripts/entsoe.py
+++ b/scripts/entsoe.py
@@ -24,7 +24,7 @@ from tzlocal import get_localzone
apiKey = common.env("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
+apiUrl = "https://web-api.tp.entsoe.eu/api"
startTime = datetime.now(get_localzone()) - timedelta(days=7)
startTime = startTime.strftime("%Y%m%d")
@@ -33,8 +33,6 @@ startTime = startTime.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"},
@@ -47,34 +45,34 @@ areas = [
UTC = tz.gettz("UTC")
CET = tz.gettz("Europe/Oslo")
+# Create a session object for making requests
+session = requests.Session()
# Get the data
values = []
for area in areas:
try:
url = (
- apiUrl
- + "&documentType=A44&in_Domain="
- + area["code"]
- + "&out_Domain="
- + area["code"]
- + "&periodStart="
- + startTime
- + "0000&periodEnd="
- + endTime
- + "0000"
+ f"{apiUrl}"
+ f"?securityToken={apiKey}"
+ f"&documentType=A44"
+ f"&in_Domain={area['code']}"
+ f"&out_Domain={area['code']}"
+ f"&periodStart={startTime}0000"
+ f"&periodEnd={endTime}0000"
)
print("Getting data for " + area["code"])
- response = requests.get(url, timeout=10)
- if response.status_code != 200:
- print(response.status_code)
- print("Oh shit")
- response.raise_for_status()
+
+ # Use the session object to make the request
+ response = session.get(url, timeout=10)
+
+ # Handle any exceptions or non-200 status code
+ response.raise_for_status()
except requests.exceptions.RequestException as e:
- print("oh lol")
- sys.exit(e)
+ print(f"Error: {e}")
+ sys.exit(1)
data_dict = xmltodict.parse(response.content)
@@ -95,7 +93,7 @@ for area in areas:
# append values
values.append((time, area["name"], item["price.amount"]))
- print("Got " + str(items) + " records")
+ print(f"Got {items} records")
# SQL
sql = """ INSERT INTO entsoe