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
|
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}
|
#image: ghcr.io/maxwinterstein/homeassistant-addon-toogoodtogo-ha-mqtt-bridge-{arch}
|
||||||
slug: screenshotter
|
slug: screenshotter
|
||||||
description: Screenshot images for E-Paper pricetags
|
description: Screenshot images for E-Paper pricetags
|
||||||
|
|
|
@ -6,6 +6,9 @@ from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
|
||||||
import yaml
|
import yaml
|
||||||
import datetime
|
import datetime
|
||||||
import croniter
|
import croniter
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logging.basicConfig(format='%(asctime)s [%(levelname)s]:%(message)s')
|
||||||
|
|
||||||
|
|
||||||
image_path = os.environ.get("SCREEN_IMAGE_PATH", default='/tmp')
|
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')
|
'like Gecko) Chrome/68.0.3440.84 Safari/537.36')
|
||||||
chrome_options.add_experimental_option('prefs', {'intl.accept_languages': ha_language})
|
chrome_options.add_experimental_option('prefs', {'intl.accept_languages': ha_language})
|
||||||
|
|
||||||
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']}.")
|
|
||||||
tasks += [image]
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
for i, task in enumerate(tasks):
|
config_file_modified_start = os.path.getmtime(config_file)
|
||||||
if task["next_execution"] > datetime.datetime.now():
|
|
||||||
continue
|
|
||||||
print(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"):
|
logging.info(f"Reading config file {config_file}")
|
||||||
print("Doing HA Auth")
|
config = None
|
||||||
driver.execute_script(f"window.localStorage.setItem('hassTokens', JSON.stringify({{hassUrl: '{ha_url}', access_token: '{ha_token}', token_type: 'Bearer'}}));")
|
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)
|
||||||
|
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
|
||||||
|
logging.info(f"Running {task['name']}")
|
||||||
|
driver = webdriver.Chrome(options=chrome_options, desired_capabilities=d)
|
||||||
|
driver.implicitly_wait(10)
|
||||||
driver.get(task["url"])
|
driver.get(task["url"])
|
||||||
time.sleep(int(wait))
|
time.sleep(int(wait))
|
||||||
|
|
||||||
print("Making screenshot")
|
if task.get("ha_auth"):
|
||||||
|
logging.info("Doing HA Auth")
|
||||||
# save screenshot
|
driver.execute_script(f"window.localStorage.setItem('hassTokens', JSON.stringify({{hassUrl: '{ha_url}', access_token: '{ha_token}', token_type: 'Bearer'}}));")
|
||||||
file_name = f'/tmp/{task["name"]}.png'
|
driver.get(task["url"])
|
||||||
driver.save_screenshot(file_name)
|
time.sleep(int(wait))
|
||||||
# print messages
|
|
||||||
for entry in driver.get_log('browser'):
|
logging.info("Making screenshot")
|
||||||
print(entry)
|
|
||||||
image = Image.open(file_name)
|
# save screenshot
|
||||||
image = image.rotate(int(rotate), expand=True)
|
file_name = f'/tmp/{task["name"]}.png'
|
||||||
file_name = f'{image_path}/{task["name"]}.png'
|
driver.save_screenshot(file_name)
|
||||||
image.save(file_name)
|
# print messages
|
||||||
tasks[i]["next_execution"] = task["croniter"].get_next(datetime.datetime)
|
browser_out = ""
|
||||||
print(f"Task finished, next execution: {tasks[i]['next_execution']}")
|
for entry in driver.get_log('browser'):
|
||||||
time.sleep(1)
|
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)
|
||||||
|
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