-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitch2_collector.py
186 lines (159 loc) · 5.75 KB
/
switch2_collector.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.select import Select
#import chromedriver_binary ## enable this to run on windows
from bs4 import BeautifulSoup
import time
import datetime
from influxdb import InfluxDBClient
from dateutil.parser import parse
from sys import exit
from datetime import date, timedelta
######### SWITCH2.CO.UK CREDENTIALS ###################
username = ''
pwd = ''
########### GET ALL THE DATA (FIRST RUN) OR GET ONLY THE LATEST DATA (FOR DAILY POLLING)
get = "latest" # "latest" or "all"
########### InfluxDB information. In case you want to save data to influx to graph with Grafana #############
write_to_influx = True
influxdb_ip = ''
influxdb_port = ''
influxdb_username = ''
influxdb_password = ''
influxdb_database = ''
def writetoinflux(type, date, measurement, fixrate, unitcost):
if (write_to_influx):
client = InfluxDBClient(host=influxdb_ip, port=influxdb_port,
username=influxdb_username, password=influxdb_password)
client.switch_database(influxdb_database)
json_body = [
{
"measurement": "Switch2",
"tags": {
"type": type
},
"time": date,
"fields": {
"measurement": measurement,
"fix_rate": fixrate,
"unit_cost": unitcost
}
}
]
client.write_points(json_body)
def writetoinflux2(type, today, fixrate):
if (write_to_influx):
client = InfluxDBClient(host=influxdb_ip, port=influxdb_port,
username=influxdb_username, password=influxdb_password)
client.switch_database(influxdb_database)
json_body = [
{
"measurement": "Switch2",
"tags": {
"type": type
},
"time": today,
"fields": {
"fix_rate": fixrate
}
}
]
client.write_points(json_body)
today = date.today()
today = parse(today.strftime("%B %d, %Y"))
options = Options()
options.headless = True
options.add_argument("--log-level=3")
driver = webdriver.Chrome(options=options)
driver.get('https://my.switch2.co.uk/Login')
user = driver.find_element_by_name("UserName")
user.clear()
user.send_keys(username)
print("sending username: " + username)
driver.find_element_by_id("loginButton").click()
time.sleep(3)
password = driver.find_element_by_id("Password")
password.clear()
print("sending password")
password.send_keys(pwd)
driver.find_element_by_id("nextStepButton").click()
time.sleep(3)
if ("Welcome" in driver.page_source):
print("login successfull")
else:
print("login error")
exit(1)
driver.get('https://my.switch2.co.uk/MeterReadings/History')
types = ["Water", "Electricity", "Heat", "Sewerage"]
for each_type in types:
fix_rate = 0
unit_cost = 0
if each_type == "Water":
fix_rate = 0.04466
unit_cost = 1.365
if each_type == "Electricity":
fix_rate = 0.142
unit_cost = 0.135
if each_type == "Heat":
fix_rate = 0.72
unit_cost = 0.0695
if each_type == "Sewerage":
fix_rate = 0.05133
unit_cost = 0.826
print("####################### Measurements for " +
each_type + " ##################################")
el = driver.find_element_by_id('RegisterId')
for option in el.find_elements_by_tag_name('option'):
if each_type in option.text:
option.click()
break
elif each_type == "Sewerage":
if "Water" in option.text:
option.click()
break
el = driver.find_element_by_id('PageSize')
for option in el.find_elements_by_tag_name('option'):
if option.text == 'All':
option.click()
break
driver.find_element_by_id("ReloadButton").click()
if (get == "all"):
for row in driver.find_elements_by_class_name("meter-reading-history-table-data-row"):
try:
date = row.find_elements_by_class_name(
"meter-reading-history-table-data-row-item")[0]
try:
measurement = row.find_elements_by_class_name(
"meter-reading-history-table-data-row-item-right")[0]
except:
measurement = ""
measurement_int = measurement.text.replace(
' m3', "").replace(' kWh', "")
print(str(parse(date.text)) + " = " + measurement_int)
writetoinflux(each_type, parse(
date.text), int(measurement_int), fix_rate, unit_cost)
except:
pass
if (get == "latest"):
driver.find_elements_by_class_name(
"meter-reading-history-table-data-row")[0]
try:
date = driver.find_elements_by_class_name(
"meter-reading-history-table-data-row-item")[0]
try:
measurement = driver.find_elements_by_class_name(
"meter-reading-history-table-data-row-item-right")[0]
except:
measurement = ""
measurement_int = measurement.text.replace(
' m3', "").replace(' kWh', "")
print(str(parse(date.text)) + " = " + measurement_int)
writetoinflux(each_type, parse(date.text),
int(measurement_int), fix_rate, unit_cost)
except:
pass
# This Will Ensure that we get the fixed rates in, even in days where measurements failed
writetoinflux2(each_type, today, fix_rate)
driver.quit()
exit()