-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoteborgenergi.py
212 lines (170 loc) · 6.44 KB
/
goteborgenergi.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python3
# Copyright (c) 2016 Erik Johansson <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
from datetime import date
import http.client as http
import http.cookies as cookies
import json
import logging
import re
import urllib.parse
class MonthEnergy:
def __init__(self, data):
self.data = data
def get_day_energy(self, date):
total = None
hours = 24 * [None]
for entry in self.data["series"][0]["data"]:
if entry and date.day == int(entry["name"]):
total = int(entry["y"] * 1000)
break
if total is None:
return (total, hours)
for entry in self.data["drilldown"]["series"]:
if entry and date.day == int(entry["name"]):
for h in entry["data"]:
hour = int(h["name"])
assert hour >= 0 and hour <= 23
assert h["y"] >= 0
hours[hour] = int(h["y"] * 1000)
return (total, hours)
class GoteborgEnergi:
SERVER = "elavtal.goteborgenergi.se"
LOGIN_PATH = "/din-sida-info/logga-in/"
LOGOUT_PATH = "/din-sida/elforbrukning-och-elavtal/Logout/"
ENERGY_PATH = "/din-sida/elforbrukning-och-elavtal/"
BY_HOUR_PATH = "/din-sida/elforbrukning-och-elavtal/GetConsumptionByHour/"
def __init__(self, username, password):
self.username = username
self.password = password
self.cookies = None
self.headers = {"Accept": "*/*"}
def _set_cookies(self, response):
if not self.cookies:
self.cookies = cookies.SimpleCookie()
for c in response.getheader("Set-Cookie", "").split(", "):
self.cookies.load(c)
cookie = self.cookies.output(attrs={}, header="", sep=";")
self.headers["Cookie"] = cookie.lstrip()
def log_in(self):
conn = http.HTTPSConnection(GoteborgEnergi.SERVER)
conn.request("GET", GoteborgEnergi.LOGIN_PATH)
response = conn.getresponse()
logging.debug(
"Get %s: %d (%s)",
GoteborgEnergi.LOGIN_PATH,
response.status,
response.reason,
)
if response.status != http.OK:
raise Exception("Failed to get login page")
self._set_cookies(response)
match = re.search(
r'name="__RequestVerificationToken" type="hidden" value="([^"]+)',
response.read().decode("utf-8"),
)
if not match:
raise Exception("Failed to detect login token")
params = urllib.parse.urlencode(
{
"ReturnUrl": "",
"KeepMeLoggedIn": "false",
"Username": self.username,
"Password": self.password,
"__RequestVerificationToken": match.group(1),
}
)
headers = self.headers.copy()
headers["Content-type"] = "application/x-www-form-urlencoded"
conn.request("POST", GoteborgEnergi.LOGIN_PATH, params, headers)
response = conn.getresponse()
logging.debug(
"Post %s: %d (%s)",
GoteborgEnergi.LOGIN_PATH,
response.status,
response.reason,
)
if response.status != http.FOUND:
raise Exception("Failed to log in")
self._set_cookies(response)
conn.close()
def log_out(self):
conn = http.HTTPSConnection(GoteborgEnergi.SERVER)
conn.request("GET", GoteborgEnergi.LOGOUT_PATH, headers=self.headers)
response = conn.getresponse()
logging.debug(
"Get %s: %d (%s)",
GoteborgEnergi.LOGOUT_PATH,
response.status,
response.reason,
)
if response.status != http.FOUND:
raise Exception("Faild to log out")
self.cookies = None
conn.close()
def get_month_energy(self, podid, date):
conn = http.HTTPSConnection(GoteborgEnergi.SERVER)
path = "%s?%s" % (
GoteborgEnergi.ENERGY_PATH,
urllib.parse.urlencode({"podid": podid}),
)
conn.request("GET", path, headers=self.headers)
response = conn.getresponse()
logging.debug("Get %s: %d (%s)", path, response.status, response.reason)
if response.status != http.OK:
raise Exception("Faild to load energy page")
match = re.search(
r'<form.*? id="get-consumption-form" .*?'
'<input name="__RequestVerificationToken" .*? value="([^"]+)"',
response.read().decode("utf-8"),
re.S,
)
if not match:
raise Exception("Failed to detect energy token")
params = urllib.parse.urlencode(
{
"PodId": podid,
"year": date.year,
"month": date.month,
"__RequestVerificationToken": match.group(1),
}
)
headers = self.headers.copy()
headers["Content-type"] = "application/x-www-form-urlencoded"
conn.request("POST", GoteborgEnergi.BY_HOUR_PATH, params, headers)
response = conn.getresponse()
logging.debug(
"Post %s: %d (%s)",
GoteborgEnergi.BY_HOUR_PATH,
response.status,
response.reason,
)
if response.status != http.OK:
raise Exception("Failed to get by hour energy")
data = json.loads(response.read().decode("utf-8"))
conn.close()
return MonthEnergy(data)
if __name__ == "__main__":
logging.basicConfig(
format="%(asctime)s %(levelname)s: %(message)s", level=logging.DEBUG
)
import sys
ge = GoteborgEnergi(sys.argv[1], sys.argv[2])
ge.log_in()
energy = ge.get_month_energy(sys.argv[3], date.today())
print(energy.get_day_energy(date.today()))
ge.log_out()