Initial Commit
This commit is contained in:
51
kiss/__init__.py
Normal file
51
kiss/__init__.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import sys
|
||||
|
||||
import click
|
||||
from flask.cli import FlaskGroup
|
||||
|
||||
|
||||
def init():
|
||||
global __version__
|
||||
global app
|
||||
global ssmanager
|
||||
|
||||
import signal
|
||||
from importlib.metadata import version
|
||||
|
||||
from flask import Flask
|
||||
|
||||
from kiss.ssmanager import ScreenshotManager
|
||||
|
||||
__version__ = version("kiss")
|
||||
|
||||
# Initialize App
|
||||
app = Flask(__name__)
|
||||
|
||||
# Screenshot Manager
|
||||
ssmanager = ScreenshotManager()
|
||||
|
||||
# Handle SIGINT
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
|
||||
|
||||
def signal_handler(sig, frame):
|
||||
if ssmanager:
|
||||
ssmanager.stop()
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
def create_app():
|
||||
init()
|
||||
|
||||
import kiss.api as api_common
|
||||
|
||||
app.register_blueprint(api_common.bp)
|
||||
|
||||
ssmanager.start()
|
||||
|
||||
return app
|
||||
|
||||
|
||||
@click.group(cls=FlaskGroup, create_app=create_app)
|
||||
def cli():
|
||||
"""Management script for the application."""
|
||||
12
kiss/api.py
Normal file
12
kiss/api.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from flask import Blueprint, send_from_directory
|
||||
|
||||
import kiss
|
||||
|
||||
# Setup blueprint
|
||||
bp = Blueprint("common", __name__)
|
||||
|
||||
|
||||
# Serve Screenshots
|
||||
@bp.route("/<path:path>")
|
||||
def assets(path):
|
||||
return send_from_directory(kiss.ssmanager.data_path, path)
|
||||
34
kiss/ssmanager.py
Normal file
34
kiss/ssmanager.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
from pathlib import Path
|
||||
from threading import Thread
|
||||
|
||||
|
||||
class ScreenshotManager:
|
||||
def __init__(self):
|
||||
self.__config_path = os.environ.get("CONFIG_FILE", "/data/config.conf")
|
||||
self.data_path = tempfile.gettempdir()
|
||||
|
||||
if not Path(self.__config_path).is_file():
|
||||
raise ValueError("Invalid Configuration")
|
||||
|
||||
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()
|
||||
|
||||
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
|
||||
)
|
||||
time.sleep(1)
|
||||
counter += 1
|
||||
Reference in New Issue
Block a user