Skip to content

Commit

Permalink
Veolia/Handle changed filename
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeweerd committed Dec 16, 2023
1 parent 4fcf3ce commit 29fe79b
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions apps/meters_to_ha/meters_to_ha.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import argparse
import csv
import datetime as dt
import glob
import inspect
import json
import logging
Expand Down Expand Up @@ -418,6 +419,7 @@ class ServiceCrawler(Worker): # pylint:disable=too-many-instance-attributes
# Go to login page directly only when not logged in
site_url = "https://espace-client.vedif.eau.veolia.fr/s/"
download_veolia_filename = "historique_jours_litres.csv"
glob_download_veolia_filename = "historique_jours_litres*.csv"
site_grdf_url = "https://monespace.grdf.fr/client/particulier/consommation"
# site_grdf_url = "ttps://login.monespace.grdf.fr/mire/connexion"
download_grdf_filename = "historique_gazpar.json"
Expand Down Expand Up @@ -488,6 +490,10 @@ def __init__(
self.configuration[PARAM_DOWNLOAD_FOLDER],
self.download_veolia_filename,
)
self.__glob_path_download_veolia_idf_file = os.path.join(
self.configuration[PARAM_DOWNLOAD_FOLDER],
self.glob_download_veolia_filename,
)
self.__full_path_download_grdf_file = os.path.join(
self.configuration[PARAM_DOWNLOAD_FOLDER],
self.download_grdf_filename,
Expand Down Expand Up @@ -1380,7 +1386,10 @@ def get_veolia_idf_file(self):
"""
Get Veolia IDF water consumption 'interactively'
"""
# pylint: disable=too-many-locals

v_file = self.__full_path_download_veolia_idf_file
v_file_glob = self.__glob_path_download_veolia_idf_file

if self.configuration[PARAM_SKIP_DOWNLOAD]:
return v_file
Expand Down Expand Up @@ -1479,10 +1488,13 @@ def get_veolia_idf_file(self):

# Should be logged in here

if os.path.exists(v_file):
# for filename in glob.glob(v_file_glob):
for filename in (v_file,): # Loop to replace the above alternative
try:
self.mylog("Remove temporary download file. ", end="")
os.remove(v_file)
self.mylog(
f"Remove temporary download file '{filename}'. ", end=""
)
os.remove(filename)
except Exception:
raise
else:
Expand Down Expand Up @@ -1581,7 +1593,7 @@ def get_veolia_idf_file(self):
click_message="Click on Alertes de consommation",
delay=2,
)
time.sleep(5)
time.sleep(2)
self.click_in_view(
By.XPATH,
r"//a[contains(@class,'cICL_Tab')]"
Expand All @@ -1590,6 +1602,14 @@ def get_veolia_idf_file(self):
click_message="Click on Historique",
delay=2,
)
time.sleep(2)
self.click_in_view(
By.XPATH,
r"//span[contains(text(), 'Litres')]/parent::node()",
wait_message="Wait for button Litres",
click_message="Click on button Litres",
delay=2,
)

time.sleep(2)

Expand All @@ -1613,13 +1633,19 @@ def get_veolia_idf_file(self):
)

self.mylog(
f"Wait for end of download to {v_file}",
f"Wait for end of download to '{v_file_glob}'",
end="",
)

t = int(str(self.configuration[PARAM_TIMEOUT]))
while t > 0 and not os.path.exists(v_file):
elapsed = 0
while t > 0 and not glob.glob(v_file_glob):
time.sleep(1)
t -= 1
elapsed += 1
if elapsed < 2:
# Do not try alternative until some time passed
continue
try:
# For some reason (possibly Security setting),
# the CSV file is not written to disk in Chrome 110.
Expand Down Expand Up @@ -1648,8 +1674,13 @@ def get_veolia_idf_file(self):
except Exception:
pass

if os.path.exists(v_file):
found = glob.glob(v_file_glob)
# Most recent first in list
found.sort(key=os.path.getmtime, reverse=True)
if found and os.path.exists(found[0]):
self.mylog(st="OK")
if found[0] != v_file:
os.rename(found[0], v_file)
else:
self.get_screenshot("error.png")
raise RuntimeError("File download timeout")
Expand Down

0 comments on commit 29fe79b

Please sign in to comment.