This repository was archived by the owner on Feb 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.py
More file actions
67 lines (56 loc) · 2.14 KB
/
api.py
File metadata and controls
67 lines (56 loc) · 2.14 KB
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
import requests
import json
from pathlib import Path
AUTH_URL = "https://smartmetertexas.com/api/user/authenticate"
DAILY_URL = "https://smartmetertexas.com/api/usage/daily"
TIMEOUT = 30
# pinned certificate trust
CERT = "www-smartmetertexas-com-chain.pem"
class MeterReader:
logged_in = False
def __init__(self, auth_url=AUTH_URL, daily_url=DAILY_URL, timeout=TIMEOUT):
self.session = requests.Session()
self.auth_url = auth_url
self.daily_url = daily_url
self.timeout = timeout
self.certpath = f"{Path(__file__).parent}/{CERT}"
# self.session.headers["referrer"] = "https://www.smartmetertexas.com/home"
# self.session.headers["origin"] = "https://www.smartmetertexas.com"
# self.session.headers["user-agent"] = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.142 Safari/537.36"
def api_call(self, url, json):
try:
return self.session.post(url=url, json=json, timeout=self.timeout, verify=self.certpath)
except Exception as ex:
print(repr(ex))
raise
def login(self, username, password):
creds = {
"username": username,
"password": password
}
r = self.api_call(self.auth_url, json=creds)
if (r.status_code != 200):
print("Login failed.")
return False
else:
self.token = r.json()['token']
print("Login successful!")
self.session.headers["Authorization"] = f"Bearer {self.token}"
self.logged_in = True
return r
def get_daily_read(self, esiid, start_date, end_date):
if self.logged_in == False:
print("You must login first.")
return False
json = {
"esiid": esiid,
"endDate": end_date,
"startDate": start_date,
}
r = self.api_call(self.daily_url, json=json)
if (r.status_code != 200 or "error" in r.text.lower()):
print("Error fetching daily read.")
print(r.text)
return False
else:
return r.json()