From 9443dc212470d7f80e55d5e43f5131b40f4caf8f Mon Sep 17 00:00:00 2001 From: thopic Date: Fri, 19 Nov 2021 18:02:40 +0100 Subject: [PATCH] Adapt script to cron use : be verbose only on failure --- print_stats.py | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/print_stats.py b/print_stats.py index 27d012f..6c4b932 100755 --- a/print_stats.py +++ b/print_stats.py @@ -1,33 +1,36 @@ #!/usr/bin/python3 import os +import traceback +import logging from time import sleep from dotenv import load_dotenv from seleniumwire import webdriver from seleniumwire.webdriver import FirefoxOptions -os.chdir(os.path.dirname(os.path.abspath(__file__))) -load_dotenv() +try: + os.chdir(os.path.dirname(os.path.abspath(__file__))) + load_dotenv() -opts = FirefoxOptions() -opts.add_argument("--headless") -browser = webdriver.Firefox(options=opts,executable_path="/usr/local/bin/geckodriver") -browser.set_window_size(3000, 1120) + opts = FirefoxOptions() + opts.add_argument("--headless") + browser = webdriver.Firefox(options=opts,executable_path="/usr/local/bin/geckodriver") + browser.set_window_size(3000, 1120) -def interceptor(request): - request.headers['Authorization'] = 'Basic '+os.environ["ES_TOKEN"] -browser.request_interceptor = interceptor + def interceptor(request): + request.headers['Authorization'] = 'Basic '+os.environ["ES_TOKEN"] + browser.request_interceptor = interceptor -for duration in ["24h", "7d", "1M", "1y"]: - browser.get("https://mon.gnous.fr/app/dashboards#/view/380099d0-4944-11ec-81a9-95d7ffa23468?embed=true&_g=(filters%3A!()%2CrefreshInterval%3A(pause%3A!t%2Cvalue%3A0)%2Ctime%3A(from%3Anow-"+duration+"%2Cto%3Anow))&hide-filter-bar=true") - sleep(30) - if duration == "24h": - sleep(15) - browser.get_screenshot_as_file("html/ressources/stats/mm"+duration+".png") + for duration in ["24h", "7d", "1M", "1y"]: + browser.get("https://mon.gnous.fr/app/dashboards#/view/380099d0-4944-11ec-81a9-95d7ffa23468?embed=true&_g=(filters%3A!()%2CrefreshInterval%3A(pause%3A!t%2Cvalue%3A0)%2Ctime%3A(from%3Anow-"+duration+"%2Cto%3Anow))&hide-filter-bar=true") + sleep(30) + if duration == "24h": + sleep(15) + browser.get_screenshot_as_file("html/ressources/stats/mm"+duration+".png") -for duration in ["24h", "7d", "1M", "1y"]: - browser.get("https://mon.gnous.fr/app/dashboards#/view/ef70de60-4947-11ec-81a9-95d7ffa23468?embed=true&_g=(filters%3A!()%2CrefreshInterval%3A(pause%3A!t%2Cvalue%3A0)%2Ctime%3A(from%3Anow-"+duration+"%2Cto%3Anow))&hide-filter-bar=true") - sleep(30) - browser.get_screenshot_as_file("html/ressources/stats/nc"+duration+".png") - -print('done') + for duration in ["24h", "7d", "1M", "1y"]: + browser.get("https://mon.gnous.fr/app/dashboards#/view/ef70de60-4947-11ec-81a9-95d7ffa23468?embed=true&_g=(filters%3A!()%2CrefreshInterval%3A(pause%3A!t%2Cvalue%3A0)%2Ctime%3A(from%3Anow-"+duration+"%2Cto%3Anow))&hide-filter-bar=true") + sleep(30) + browser.get_screenshot_as_file("html/ressources/stats/nc"+duration+".png") +except Exception as e: + logging.error(traceback.format_exc())