forked from empierre/domoticz_gaspar-deprecated
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgazpar.py
121 lines (103 loc) · 3.96 KB
/
gazpar.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
# adapted from cyprieng / gazpar-home-assistant
# and yukulehe / gazpar2mqtt (many thanks !)
# adapted by: frtz13
# - removed retries (done by Automations in H.A.)
# - changed return value
# - added login success check and exception handling
# - added simple check if we got some meaningful data
import datetime
import json
import requests
VERSION = "2022.07.09"
class GazparLoginException(Exception):
"""Thrown if a login error was encountered"""
class GazparInvalidDataException(Exception):
"""Thrown if a invalid data was encountered"""
class Gazpar:
def __init__(self, username, password, pce):
"""Init gazpar class
Args:
username: username
password: password
pce: Pce identifier
"""
self.username = username
self.password = password
self.pce = pce
def get_consumption(self):
session = requests.Session()
session.headers.update(
{
"User-Agent": "Mozilla/5.0"
" (Linux; Android 6.0; Nexus 5 Build/MRA58N)"
" AppleWebKit/537.36 (KHTML, like Gecko)"
" Chrome/61.0.3163.100 Mobile Safari/537.36",
"Accept-Encoding": "gzip, deflate, br",
"Accept": "application/json, */*",
"Connection": "keep-alive",
"domain": "grdf.fr",
}
)
# get nonce
_ = session.get("https://monespace.grdf.fr/client/particulier/accueil")
if "auth_nonce" not in session.cookies:
raise GazparLoginException("Cannot get auth_nonce.")
auth_nonce = session.cookies.get("auth_nonce")
# Login
login_response = session.post(
"https://login.monespace.grdf.fr/sofit-account-api/api/v1/auth",
data={
"email": self.username,
"password": self.password,
"capp": "meg",
"goto": "https://sofa-connexion.grdf.fr:443"
"/openam/oauth2/externeGrdf/authorize"
"?response_type=code"
"&scope=openid%20profile%20email%20infotravaux%20%2Fv1"
"%2Faccreditation%20%2Fv1%2Faccreditations%20%2F"
"digiconso%2Fv1%20%2Fdigiconso%2Fv1"
"%2Fconsommations%20new_meg"
"&client_id=prod_espaceclient&state=0"
"&redirect_uri=https%3A%2F%2Fmonespace.grdf.fr%2F_codexch"
f"&nonce={auth_nonce}&by_pass_okta=1&capp=meg",
},
)
# check login success
login_result = json.loads(login_response.text)
if (
("status" in login_result)
and ("error" in login_result)
and (login_result["status"] >= 400)
):
raise GazparLoginException(
f"{login_result['error']} ({login_result['status']})"
)
if ("state" in login_result) and (login_result["state"] != "SUCCESS"):
raise GazparLoginException(login_result["error"])
url = (
"https://monespace.grdf.fr/api"
"/e-conso/pce/consommation/informatives"
"?dateDebut={}&dateFin={}&pceList%5B%5D={}"
).format(
(datetime.datetime.now() - datetime.timedelta(days=7)).strftime(
"%Y-%m-%d"
),
datetime.datetime.now().strftime("%Y-%m-%d"),
self.pce,
)
session.get(url) # first try, does not return data
# now get data
reponse = session.get(url)
try:
resp_json = reponse.json()
if self.pce in resp_json:
return reponse.json()[self.pce]
else:
raise Exception("No Relevé in response")
except Exception:
if reponse is None:
raise GazparInvalidDataException("")
else:
raise GazparInvalidDataException(
f"[{reponse.status_code}]\n{reponse.text}"
)