diff --git a/DataPoints.py b/DataPoints.py index 55d1a4e..5efb2b8 100644 --- a/DataPoints.py +++ b/DataPoints.py @@ -1,3 +1,4 @@ +from datetime import datetime from influxdb_client import Point class Pulse: label = str(__name__).lower() @@ -22,3 +23,22 @@ class Pulse: def get_datapoint(self): return Point(measurement_name=str(self.label)).from_dict(self.datapoint) +class Price: + label = str(__name__).lower() + def __init__(self,data): + measurement = data['data']['viewer']['homes'][0]['currentSubscription']['priceInfo']['current'] + current_time_utc = datetime.utcnow() + timestamp = current_time_utc.isoformat() + total = measurement.get('total',0.34) + tags = {self.label: ""} + fields = { + "total": float(total), + } + self.datapoint = {"fields": fields, + "tags": tags, + "measurement": "pulse", + "time": timestamp + } + def get_datapoint(self): + return Point(measurement_name=str(self.label)).from_dict(self.datapoint) + diff --git a/Dockerfile b/Dockerfile index 3c2824d..4d5791a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,5 +12,5 @@ ADD . / # Chmod RUN chmod 755 /pulse.py - +CMD ["python3","/get_price.py"] CMD ["python3","/pulse.py"] \ No newline at end of file diff --git a/get_price.py b/get_price.py new file mode 100644 index 0000000..a2da5c8 --- /dev/null +++ b/get_price.py @@ -0,0 +1,46 @@ +import os +import requests +from influxdb_client import InfluxDBClient +from influxdb_client.client.write_api import SYNCHRONOUS +from DataPoints import Price + + +TOKEN=os.getenv('TOKEN', '') +TIBBERTOKEN=os.getenv('TIBBERTOKEN', '') +URL = os.getenv('URL',"" ) +BUCKET = os.getenv('BUCKET',"tibber" ) +ORG = os.getenv('ORG',"Default" ) + +# URL of the Tibber API +url = 'https://api.tibber.com/v1-beta/gql' + +# Request headers +headers = { + 'authority': 'api.tibber.com', + 'accept': 'application/json', + 'authorization': f'Bearer {TIBBERTOKEN}', + 'content-type': 'application/json', + 'user-agent': 'python 3.10', +} + +# GraphQL query +query = '{"query":"{ viewer { homes { currentSubscription { priceInfo { current { total } } } } } }"}' + + + +client = InfluxDBClient(url=URL, token=TOKEN, org=ORG) + +write_api = client.write_api(write_options=SYNCHRONOUS) +query_api = client.query_api() +# Make the HTTP POST request +response = requests.post(url, headers=headers, data=query) + +# Check for successful response +if response.status_code == 200: + # Print the response content (JSON data) + print(response.json()) +else: + print(f'Error: HTTP {response.status_code}') + +p = Price(response.json()).get_datapoint() +write_api.write(record=p, bucket=BUCKET) diff --git a/pulse.py b/pulse.py index d7fe3ea..7369158 100644 --- a/pulse.py +++ b/pulse.py @@ -10,15 +10,13 @@ from influxdb_client import InfluxDBClient from influxdb_client.client.write_api import SYNCHRONOUS from DataPoints import Pulse import logging -import time + TOKEN=os.getenv('TOKEN', '') TIBBERTOKEN=os.getenv('TIBBERTOKEN', '') URL = os.getenv('URL',"" ) BUCKET = os.getenv('BUCKET',"tibber" ) ORG = os.getenv('ORG',"Default" ) - - #logging logger = logging.getLogger("TibberInflux") formatter = logging.Formatter( @@ -32,7 +30,7 @@ logger.addHandler(ch) logger.setLevel(logging.INFO) -__version__ = "v0.0.6" +__version__ = "v0.1.1" logger.info(__version__) client = InfluxDBClient(url=URL, token=TOKEN, org=ORG) @@ -70,4 +68,3 @@ async def run(): loop = asyncio.get_event_loop() loop.run_until_complete(run()) -