aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/tibber_prices.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/tibber_prices.py')
-rw-r--r--scripts/tibber_prices.py58
1 files changed, 32 insertions, 26 deletions
diff --git a/scripts/tibber_prices.py b/scripts/tibber_prices.py
index af839bd..53642bb 100644
--- a/scripts/tibber_prices.py
+++ b/scripts/tibber_prices.py
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
-''' import energy prices from tibber '''
+""" import energy prices from tibber """
import os
import sys
@@ -10,13 +10,13 @@ import requests
from tzlocal import get_localzone
# variables
-apiKey = os.environ['el_tibber_token']
+apiKey = os.environ["el_tibber_token"]
apiUrl = "https://api.tibber.com/v1-beta/gql"
-startTime = datetime.now(get_localzone()) - timedelta(days = 1)
-startTime = startTime.isoformat('T')
+startTime = datetime.now(get_localzone()) - timedelta(days=1)
+startTime = startTime.isoformat("T")
-endTime = datetime.now(get_localzone()).isoformat('T')
+endTime = datetime.now(get_localzone()).isoformat("T")
# Get the data
try:
@@ -24,11 +24,12 @@ try:
# Request headers
hdr = {
- 'Authorization': "Bearer " + apiKey,
- 'Content-Type': 'application/json',
+ "Authorization": "Bearer " + apiKey,
+ "Content-Type": "application/json",
}
- body = {"query":"""{
+ body = {
+ "query": """{
viewer {
homes {
currentSubscription{
@@ -38,7 +39,8 @@ try:
}
}
}
- } }"""}
+ } }"""
+ }
response = requests.post(url, headers=hdr, json=body, timeout=10)
if response.status_code != 200:
@@ -52,7 +54,11 @@ except requests.exceptions.RequestException as e:
data = response.json()
-numdata = len(data["data"]["viewer"]["homes"][0]["currentSubscription"]["priceInfo"]["today"]) + len(data["data"]["viewer"]["homes"][0]["currentSubscription"]["priceInfo"]["tomorrow"])
+numdata = len(
+ data["data"]["viewer"]["homes"][0]["currentSubscription"]["priceInfo"]["today"]
+) + len(
+ data["data"]["viewer"]["homes"][0]["currentSubscription"]["priceInfo"]["tomorrow"]
+)
print("Got " + str(numdata) + " rows from Tibber")
@@ -60,25 +66,25 @@ print("Got " + str(numdata) + " rows from Tibber")
# prices
prices = []
-for item in data["data"]["viewer"]["homes"][0]["currentSubscription"]["priceInfo"]["today"]:
- prices.append((
- item["startsAt"],
- item["total"],
- item["energy"],
- item["tax"],
- item["level"]))
-
-for item in data["data"]["viewer"]["homes"][0]["currentSubscription"]["priceInfo"]["tomorrow"]:
- prices.append((
- item["startsAt"],
- item["total"],
- item["energy"],
- item["tax"],
- item["level"]))
+for item in data["data"]["viewer"]["homes"][0]["currentSubscription"]["priceInfo"][
+ "today"
+]:
+ prices.append(
+ (item["startsAt"], item["total"], item["energy"], item["tax"], item["level"])
+ )
+
+for item in data["data"]["viewer"]["homes"][0]["currentSubscription"]["priceInfo"][
+ "tomorrow"
+]:
+ prices.append(
+ (item["startsAt"], item["total"], item["energy"], item["tax"], item["level"])
+ )
# SQL
-sql = """INSERT INTO tibber_prices
+sql = (
+ """INSERT INTO tibber_prices
VALUES(%s, %s, %s, %s, %s)
ON CONFLICT (startsat) DO NOTHING""",
+)
common.dbi(sql, prices, verbose=True)