KISS/kiss/ssmanager.py

54 lines
1.4 KiB
Python
Raw Normal View History

2022-11-17 02:52:37 +00:00
import os
import subprocess
import tempfile
import time
from pathlib import Path
from threading import Thread
2022-11-17 14:01:38 +00:00
import yaml
2022-11-17 14:13:35 +00:00
from PIL import Image, ImageOps
2022-11-17 14:01:38 +00:00
2022-11-17 02:52:37 +00:00
class ScreenshotManager:
def __init__(self):
2022-11-17 13:03:16 +00:00
self.__config_path = os.environ.get("CONFIG_FILE", "/data/config.yml")
2022-11-17 02:52:37 +00:00
if not Path(self.__config_path).is_file():
raise ValueError("Invalid Configuration")
2022-11-17 14:01:38 +00:00
cf = open(self.__config_path, "r")
self.__config_yaml = yaml.safe_load(cf)
self.data_path = tempfile.gettempdir()
2022-11-17 02:52:37 +00:00
def stop(self):
self.__pending_shutdown = True
self.__loop_thread.join()
def start(self):
self.__pending_shutdown = False
self.__loop_thread = Thread(target=self.__loop)
self.__loop_thread.start()
2022-11-17 14:13:35 +00:00
def __process_image(self, ss):
filename = ss.get("output")
2022-11-17 14:01:38 +00:00
fp = Path(self.data_path, filename)
2022-11-17 14:13:35 +00:00
img = ImageOps.grayscale(Image.open(fp))
if ss.get("rotate", False) is True:
img = img.rotate(90, expand=True)
2022-11-17 14:01:38 +00:00
img.save(fp)
2022-11-17 02:52:37 +00:00
def __loop(self):
counter = 0
while not self.__pending_shutdown:
if counter % 900 == 0:
subprocess.run(
["shot-scraper", "multi", self.__config_path], cwd=self.data_path
)
2022-11-17 14:01:38 +00:00
for ss in self.__config_yaml:
2022-11-17 14:13:35 +00:00
self.__process_image(ss)
2022-11-17 14:01:38 +00:00
2022-11-17 02:52:37 +00:00
time.sleep(1)
counter += 1