aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDennis Eriksen <d@ennis.no>2023-11-09 14:11:35 +0100
committerDennis Eriksen <d@ennis.no>2023-11-09 14:11:35 +0100
commite8e33997b360bfee461d5939b7c9fef5f3de6e8c (patch)
tree8f61e55c69edbd6abf140aba462bf535c83e1f22
parentadded new function to get env (diff)
downloadenergyscripts-e8e33997b360bfee461d5939b7c9fef5f3de6e8c.tar.gz
optimize and simplify
- use more fstrings - use exceptions - don't print "oh lol" - drop tempfiles
-rw-r--r--scripts/elvia.py31
-rw-r--r--scripts/elvia_gridtariff.py18
-rw-r--r--scripts/entsoe.py40
-rw-r--r--scripts/mqtt_listener.py2
-rw-r--r--scripts/mqtt_watch.py2
-rw-r--r--scripts/nb.py32
-rw-r--r--scripts/queue_runner.py4
-rw-r--r--scripts/yr.py37
8 files changed, 62 insertions, 104 deletions
diff --git a/scripts/elvia.py b/scripts/elvia.py
index fffb6bf..a680104 100644
--- a/scripts/elvia.py
+++ b/scripts/elvia.py
@@ -17,42 +17,34 @@ startTime = startTime.isoformat("T")
endTime = datetime.now(get_localzone()).isoformat("T")
-
### Get the data
try:
- url = apiUrl + "?startTime=" + startTime + "&endTime=" + endTime
+ # Query parameters
+ params = {"startTime": startTime, "endTime": endTime}
# Request headers
- hdr = {
+ headers = {
"Cache-Control": "no-cache",
"Authorization": "Bearer " + apiKey,
}
- response = requests.get(url, headers=hdr, timeout=10)
- if response.status_code != 200:
- print(response.status_code)
- print("Oh shit")
- response.raise_for_status()
+ response = requests.get(apiUrl, params=params, headers=headers, timeout=10)
+ response.raise_for_status()
except requests.exceptions.RequestException as e:
- print("oh lol")
sys.exit(e)
data = response.json()
-print(
- "Got "
- + str(len(data["meteringpoints"][0]["metervalue"]["timeSeries"]))
- + " items from between "
- + startTime
- + " and "
- + endTime
-)
-### insert data into database
+num_items = len(data["meteringpoints"][0]["metervalue"]["timeSeries"])
+print(f"Got {num_items} items from between {startTime} and {endTime}")
+
+### Insert data into database
values = []
+
for item in data["meteringpoints"][0]["metervalue"]["timeSeries"]:
- # Only deal with verified items.
+ # Only append verified items
if item["verified"]:
values.append(
(
@@ -65,7 +57,6 @@ for item in data["meteringpoints"][0]["metervalue"]["timeSeries"]:
)
)
-
# SQL
sql = """INSERT INTO elvia
VALUES(%s, %s, %s, %s, %s, %s)
diff --git a/scripts/elvia_gridtariff.py b/scripts/elvia_gridtariff.py
index 4475c48..3e8ed1e 100644
--- a/scripts/elvia_gridtariff.py
+++ b/scripts/elvia_gridtariff.py
@@ -40,30 +40,20 @@ try:
}
response = requests.post(url, headers=hdr, json=body, timeout=10)
- if response.status_code != 200:
- print(response.status_code)
- print("Oh shit")
- response.raise_for_status()
+ response.raise_for_status()
except requests.exceptions.RequestException as e:
- print("oh lol")
sys.exit(e)
data = response.json()
-print(
- "Got "
- + str(len(data["gridTariffCollections"][0]["gridTariff"]["tariffPrice"]["hours"]))
- + " items from between "
- + startTime
- + " and "
- + endTime
-)
-
+num_items = len(data["gridTariffCollections"][0]["gridTariff"]["tariffPrice"]["hours"])
+print(f"Got {num_items} items from between {startTime} and {endTime}")
### insert data into database
values = []
+
for item in data["gridTariffCollections"][0]["gridTariff"]["tariffPrice"]["hours"]:
values.append(
(
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
diff --git a/scripts/mqtt_listener.py b/scripts/mqtt_listener.py
index 1ff3176..69e62ad 100644
--- a/scripts/mqtt_listener.py
+++ b/scripts/mqtt_listener.py
@@ -33,7 +33,7 @@ tempsensors = [
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
- print("Connected with result code " + str(rc))
+ print(f"Connected with result code {rc}")
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
diff --git a/scripts/mqtt_watch.py b/scripts/mqtt_watch.py
index 22105c6..f263c6d 100644
--- a/scripts/mqtt_watch.py
+++ b/scripts/mqtt_watch.py
@@ -16,7 +16,7 @@ mqtt_pass = common.env("el_mqtt_pass")
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
- print("Connected with result code " + str(rc))
+ print(f"Connected with result code {rc}")
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
diff --git a/scripts/nb.py b/scripts/nb.py
index 6afb74e..fad142a 100644
--- a/scripts/nb.py
+++ b/scripts/nb.py
@@ -5,6 +5,7 @@ import csv
import sys
import tempfile
from datetime import datetime, timedelta
+from io import StringIO
import common
import requests
@@ -27,39 +28,28 @@ temp = tempfile.NamedTemporaryFile()
### Get the data
try:
- url = apiUrl + "&startPeriod=" + startTime + "&endPeriod=" + endTime
+ url = f"{apiUrl}&startPeriod={startTime}&endPeriod={endTime}"
response = requests.get(url, timeout=10)
if response.status_code != 200:
- print(response.status_code)
- print("Oh shit")
+ print(f"Request Error, Status code: {response.status_code}")
response.raise_for_status()
- with open(temp.name, "w", encoding="utf-8") as fd:
- fd.write(response.text)
+ data = StringIO(response.text)
except requests.exceptions.RequestException as e:
- print("oh lol")
+ print("Error: %s", e)
sys.exit(e)
+values = []
+csvReader = csv.DictReader(data, delimiter=";")
+for item in csvReader:
+ values.append(
+ (item["TIME_PERIOD"], item["BASE_CUR"], item["QUOTE_CUR"], item["OBS_VALUE"])
+ )
### insert data into database
-values = []
-with open(temp.name, encoding="utf-8") as csvfile:
- csvReader = csv.DictReader(csvfile, delimiter=";")
-
- for item in csvReader:
- values.append(
- (
- item["TIME_PERIOD"],
- item["BASE_CUR"],
- item["QUOTE_CUR"],
- item["OBS_VALUE"],
- )
- )
-
-temp.close()
# SQL
sql = """INSERT INTO nbex
diff --git a/scripts/queue_runner.py b/scripts/queue_runner.py
index 0fdb80d..35cd97f 100644
--- a/scripts/queue_runner.py
+++ b/scripts/queue_runner.py
@@ -12,7 +12,9 @@ from litequeue import SQLQueue
QUEUE_DB = common.env("el_QUEUE_db", "litequeue.db")
QUEUE_DIR = common.env("el_QUEUE_dir", "queue")
QUEUE_DB = QUEUE_DIR + "/" + QUEUE_DB
-QUEUE_SLEEP = int(common.env("el_QUEUE_sleep", 15)) # Default sleep 15 seconds when queue empty
+QUEUE_SLEEP = int(
+ common.env("el_QUEUE_sleep", 15)
+) # Default sleep 15 seconds when queue empty
# Unlock all
con = sqlite3.connect(QUEUE_DB)
diff --git a/scripts/yr.py b/scripts/yr.py
index d4dc159..2fa17b1 100644
--- a/scripts/yr.py
+++ b/scripts/yr.py
@@ -1,41 +1,28 @@
#!/usr/bin/env python3
""" Get weatherdata from yr.no """
-import os
import sys
import common
import requests
-location = int(os.environ["el_location"])
-lat = str(os.environ["el_yr_lat"])
-lon = str(os.environ["el_yr_lon"])
+apiUrl = "https://api.met.no/weatherapi/locationforecast/2.0/compact?lat={}&lon={}"
+location = int(common.env("el_location"))
+lat = common.env("el_yr_lat")
+lon = common.env("el_yr_lon")
-apiUrl = (
- "https://api.met.no/weatherapi/locationforecast/2.0/compact?lat="
- + lat
- + "&lon="
- + lon
-)
+url = apiUrl.format(lat, lon)
+headers = {
+ "User-Agent": "gratis.morell@litepost.no",
+ "Cache-Control": "no-cache",
+}
### Get the data
try:
- url = apiUrl
-
- # Request headers
- hdr = {
- "User-Agent": "gratis.morell@litepost.no",
- "Cache-Control": "no-cache",
- }
-
- response = requests.get(url, headers=hdr, timeout=10)
- if response.status_code != 200:
- print(response.status_code)
- print("Oh shit")
- response.raise_for_status()
-
+ response = requests.get(url, headers=headers, timeout=10)
+ response.raise_for_status()
except requests.exceptions.RequestException as e:
- print("oh lol")
+ print("Error in request: %s", e)
sys.exit(e)
data = response.json()