forked from Dan1001/Anglian-Water-usage-scraper-for-InfluxDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
water-usage.py
92 lines (58 loc) · 2.12 KB
/
water-usage.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
#!/usr/bin/env python3
from influxdb import InfluxDBClient
from datetime import datetime
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
import time
import json
# you will of course need to customise usernames/passwords
# not very secure at present
anglia_username = "someone@someone.com"
anglia_password = "password123"
influxdb_username = "openhab"
influxdb_password = "password123"
influxdb_url = "127.0.0.1"
influxdb_port = 8086
# if you set debug_mode to True then chrome runs in a window, so you can see what the script is doing
# and it sends text updates to the terminal
# if you send False then it's silent, save for sending "Done!" if the script succeeds
debug_mode = False
webpage = r"https://my.anglianwater.co.uk/"
opts = Options()
opts.headless = not debug_mode
def PrintDebug(text):
if debug_mode is True:
print(text)
driver = Chrome(options=opts)
driver.set_window_size(1600, 1200)
driver.get(webpage)
PrintDebug("logging in")
sbox = driver.find_element_by_id("existUser")
sbox.send_keys(anglia_username)
sbox = driver.find_element_by_id("existPass")
sbox.send_keys(anglia_password)
button = driver.find_element_by_id("existingLogIn")
button.click()
PrintDebug("Selecting usage")
button = driver.find_element_by_id("btnViewUsage")
button.click()
PrintDebug("selecting hourly")
button = driver.find_element_by_xpath('//*[@id="dbserialnumber"]/div/div[1]/div[1]/label/span')
button.click()
time.sleep(2)
PrintDebug("saving page")
html = driver.page_source
driver.quit()
startdata = html.find("var myUsageDetails_days = ") + 26
enddata = html.find(";;", startdata)
result = html[startdata: enddata]
data = json.loads(result.replace("'", '"'))
json_body = []
for x in data:
date_object = datetime.strptime(x['groupDate'] + " " + x['time'], "%d-%b-%Y %H:%M")
item = {"measurement": "anglia_water_usage", "time": date_object, "fields": {"value": x["usage"] * 1000}}
json_body.append(item)
# print(json_body)
client = InfluxDBClient(influxdb_url, influxdb_port, influxdb_username, influxdb_password, 'openhab_db')
client.write_points(json_body)
print("Done!")