Skip to content

Commit

Permalink
Bad login is not detected #8 /n Log file will grow indefinitely #7
Browse files Browse the repository at this point in the history
  • Loading branch information
Scrat committed Dec 4, 2021
1 parent b5742f7 commit 15db90a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ v1.2.0 - Scrat
- Feature : Only use Python for the config + SQL insertion -> Change cfg file and crontab
v1.2.1 - Scrat
- Fix : Fix issue in _domoticz_gazpar.cfg #5
- Fix : DB path should be configurable #6
- Fix : DB path should be configurable #6
v1.2.2 - Scrat
- Fix : Bad login is not detected #8
- Fix : Log file will grow indefinitely #7
30 changes: 25 additions & 5 deletions gazpar.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# (C) v1.0 2021-11-29 Scrat
# (C) v1.2.2 2021-12-04 Scrat
"""Generates energy consumption JSON files from GRDf consumption data
collected via their website (API).
"""
Expand Down Expand Up @@ -68,6 +68,14 @@ def login():
#print (resp1.text)
if resp1.status_code != requests.codes.ok:
print("Login call - error status :"+resp1.status_code+'\n');
logging.error("Login call - error status :"+resp1.status_code+'\n')
exit()

j = json.loads(resp1.text)
if j['state'] != "SUCCESS":
print("Login call - error status :"+j['state']+'\n');
logging.error("Login call - error status :"+j['state']+'\n')
exit()

#2nd request
headers = {
Expand All @@ -77,6 +85,8 @@ def login():
resp2 = session.get(API_BASE_URI, allow_redirects=True)
if resp2.status_code != requests.codes.ok:
print("Login 2nd call - error status :"+resp2.status_code+'\n');
logging.error("Login 2nd call - error status :"+resp2.status_code+'\n')
exit()

return session

Expand All @@ -89,6 +99,8 @@ def generate_db_script(session, start_date, end_date):
resp3 = session.get('https://monespace.grdf.fr/api/e-connexion/users/pce/historique-consultation')
if resp3.status_code != requests.codes.ok:
print("Get NumPce call - error status :",resp3.status_code, '\n');
logging.error("Get NumPce call - error status :",resp3.status_code, '\n')
exit()
#print(resp3.text)

j = json.loads(resp3.text)
Expand Down Expand Up @@ -131,15 +143,23 @@ def update_db():
sql_as_string = open(script_dir +"/req.sql", "r").read()
conn = sqlite3.connect(dbPath + "/domoticz.db")
c = conn.cursor()
c.executescript(sql_as_string)
conn.commit()
try:
c.executescript(sql_as_string)
conn.commit()
except sqlite3.Error as er:
print('SQLite error: %s' % (' '.join(er.args)))
print("Exception class is: ", er.__class__)
logging.error("Error during the database update")
exit()
c.close()
conn.close()

def get_data_with_interval(session, resource_id, numPce, start_date=None, end_date=None):
r=session.get('https://monespace.grdf.fr/api/e-conso/pce/consommation/informatives?dateDebut='+ start_date + '&dateFin=' + end_date + '&pceList[]=' + str(numPce))
if r.status_code != requests.codes.ok:
print("error status :"+r.status_code+'\n');
print("Get data - error status :"+r.status_code+'\n');
logging.error("Get data - error status :",r.status_code, '\n')
exit()
return r.text

def get_config():
Expand All @@ -164,7 +184,7 @@ def get_config():

# Main script
def main():
logging.basicConfig(filename=script_dir + '/domoticz_gazpar.log', format='%(asctime)s %(message)s', level=logging.INFO)
logging.basicConfig(filename=script_dir + '/domoticz_gazpar.log', format='%(asctime)s %(message)s', filemode='w', level=logging.INFO)

try:
logging.info("Get configuration")
Expand Down

1 comment on commit 15db90a

@Ericd80
Copy link

@Ericd80 Ericd80 commented on 15db90a Dec 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bonjour
Pour info
J'ai découvert vos modifs et fait un essai d'intégration sur mon PI
Pour info pour le moment mon GAZPAR.PY me renvoie des erreurs
Faut il mettre le login "" et password comme dans l'autre version ?
J'ai essayé sans ou avec " ou ' m^me pb
Merci
Bonne soirée

pi@raspberrypi:~/domoticz/domoticz_gazpar $ ./gazpar.py
Traceback (most recent call last):
File "./gazpar.py", line 213, in
main()
File "./gazpar.py", line 191, in main
get_config()
File "./gazpar.py", line 177, in get_config
password = config['GRDF']['GAZPAR_PASSWORD']
File "/usr/lib/python3.7/configparser.py", line 1252, in getitem
return self._parser.get(self._name, key)
File "/usr/lib/python3.7/configparser.py", line 799, in get
d)
File "/usr/lib/python3.7/configparser.py", line 394, in before_get
self._interpolate_some(parser, option, L, value, section, defaults, 1)
File "/usr/lib/python3.7/configparser.py", line 444, in _interpolate_some
"found: %r" % (rest,))
configparser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found: '%'

et le log

pi@raspberrypi:~/domoticz/domoticz_gazpar $ cat domoticz_gazpar.log
2021-12-05 18:49:41,909 Get configuration

Please sign in to comment.