Add logging, reload config file on change
This commit is contained in:
parent
31ae148476
commit
ca0fa191ba
2 changed files with 55 additions and 41 deletions
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
name: E-Paper Screenshotter
|
||||
version: 1.0.1.17
|
||||
version: 1.0.1.18
|
||||
#image: ghcr.io/maxwinterstein/homeassistant-addon-toogoodtogo-ha-mqtt-bridge-{arch}
|
||||
slug: screenshotter
|
||||
description: Screenshot images for E-Paper pricetags
|
||||
|
|
|
@ -6,6 +6,9 @@ from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
|
|||
import yaml
|
||||
import datetime
|
||||
import croniter
|
||||
import logging
|
||||
|
||||
logging.basicConfig(format='%(asctime)s [%(levelname)s]:%(message)s')
|
||||
|
||||
|
||||
image_path = os.environ.get("SCREEN_IMAGE_PATH", default='/tmp')
|
||||
|
@ -32,47 +35,58 @@ chrome_options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64
|
|||
'like Gecko) Chrome/68.0.3440.84 Safari/537.36')
|
||||
chrome_options.add_experimental_option('prefs', {'intl.accept_languages': ha_language})
|
||||
|
||||
while True:
|
||||
config_file_modified_start = os.path.getmtime(config_file)
|
||||
|
||||
logging.info(f"Reading config file {config_file}")
|
||||
config = None
|
||||
with open(config_file, "r") as stream:
|
||||
config = yaml.safe_load(stream)
|
||||
|
||||
|
||||
tasks = []
|
||||
|
||||
for image in config["images"]:
|
||||
now = datetime.datetime.now()
|
||||
image["croniter"] = croniter.croniter(image["cron"], now)
|
||||
image["next_execution"] = image["croniter"].get_next(datetime.datetime)
|
||||
print(f"First execution of {image['name']} at {image['next_execution']}.")
|
||||
logging.info(f"First execution of {image['name']} at {image['next_execution']}.")
|
||||
tasks += [image]
|
||||
|
||||
while True:
|
||||
for i, task in enumerate(tasks):
|
||||
if task["next_execution"] > datetime.datetime.now():
|
||||
continue
|
||||
print(f"Running {task['name']}")
|
||||
logging.info(f"Running {task['name']}")
|
||||
driver = webdriver.Chrome(options=chrome_options, desired_capabilities=d)
|
||||
driver.implicitly_wait(10)
|
||||
driver.get(task["url"])
|
||||
time.sleep(int(wait))
|
||||
|
||||
if task.get("ha_auth"):
|
||||
print("Doing HA Auth")
|
||||
logging.info("Doing HA Auth")
|
||||
driver.execute_script(f"window.localStorage.setItem('hassTokens', JSON.stringify({{hassUrl: '{ha_url}', access_token: '{ha_token}', token_type: 'Bearer'}}));")
|
||||
driver.get(task["url"])
|
||||
time.sleep(int(wait))
|
||||
|
||||
print("Making screenshot")
|
||||
logging.info("Making screenshot")
|
||||
|
||||
# save screenshot
|
||||
file_name = f'/tmp/{task["name"]}.png'
|
||||
driver.save_screenshot(file_name)
|
||||
# print messages
|
||||
browser_out = ""
|
||||
for entry in driver.get_log('browser'):
|
||||
print(entry)
|
||||
browser_out += f"{entry}\n"
|
||||
logging.info(browser_out)
|
||||
image = Image.open(file_name)
|
||||
image = image.rotate(int(rotate), expand=True)
|
||||
file_name = f'{image_path}/{task["name"]}.png'
|
||||
image.save(file_name)
|
||||
tasks[i]["next_execution"] = task["croniter"].get_next(datetime.datetime)
|
||||
print(f"Task finished, next execution: {tasks[i]['next_execution']}")
|
||||
logging.info(f"Task finished, next execution: {tasks[i]['next_execution']}")
|
||||
config_file_modified = os.path.getmtime(config_file)
|
||||
if config_file_modified != config_file_modified_start:
|
||||
logging.info("reloading config file")
|
||||
break
|
||||
time.sleep(1)
|
||||
|
|
Loading…
Reference in a new issue