-
Notifications
You must be signed in to change notification settings - Fork 0
/
edistribucion.py
executable file
·73 lines (64 loc) · 2.54 KB
/
edistribucion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import time
import requests
from datetime import datetime
from prometheus_client import start_http_server, Gauge
from backend.EdistribucionAPI import Edistribucion
import pytz
USER = '77626485j'
PASSWORD = 'XXXX'
edis = Edistribucion(USER,PASSWORD)
edis.login()
r = edis.get_cups()
cups = r['data']['lstCups'][0]['Id']
print('Cups: ',cups)
meter = edis.get_meter(cups)
print('Meter: ',meter)
def get_endesa_price(url):
try:
r = requests.get(url)
html = r.text.splitlines()
i = 0
if r.status_code == 200:
tz = pytz.timezone('Europe/Berlin')
hora = str(datetime.now(tz).hour)
if len(hora) < 2:
hora = '0' + hora
formathora = 'itemprop="description">{}h - '.format(hora)
while i < len(html):
if formathora in html[i]:
for elemnt in html[i].split(" "):
if 'itemprop="price">' in elemnt:
tipus = url.split('/')[-1]
if tipus == '':
tipus = 'Normal'
else:
tipus = tipus.split('=')[1]
kw.labels(tipus).set(elemnt.split('>')[1])
i = len(html)
i += 1
except Exception as e:
print(e)
def get_edistribucion(curl):
try:
edis = Edistribucion(USER, PASSWORD)
edis.login()
meter = edis.get_meter(cups)
print('Meter: ', meter)
potenciaActual.labels('edistribucion').set(meter['data']['potenciaActual'])
totalizador.labels('edistribucion').set(meter['data']['totalizador'])
estadoICP.labels('edistribucion').set(meter['data']['estadoICP'] == "Abierto")
potenciaContratada.labels('edistribucion').set(meter['data']['potenciaContratada'])
except Exception as e:
print(e)
kw = Gauge('preuKW_normal', 'preu €/KW tarifa ', ['tarifa'])
potenciaActual = Gauge('potenciaActual', 'potencia Actual', ['job'])
totalizador = Gauge('totalizador', 'total enegira consumida per perioda de facturacio', ['job'])
estadoICP = Gauge('estadoICP', 'estat del ICP del contador ', ['job'])
potenciaContratada = Gauge('potenciaContratada', 'KW de potencia contractada', ['job'])
start_http_server(9092)
while True:
get_endesa_price("https://tarifaluzhora.es/")
get_endesa_price("https://tarifaluzhora.es/?tarifa=discriminacion")
get_endesa_price("https://tarifaluzhora.es/?tarifa=coche_electrico")
get_edistribucion()
time.sleep(60)