#!/usr/bin/python3 import configparser import datetime import json import os import pathlib import subprocess import sys import persistqueue config = configparser.ConfigParser() config.read(os.getenv('CHEDDAR_KNIGHT_CONFIG', 'settings.cfg')) storage_path = config.get('General', 'storage_path') class Timer: @classmethod def schedule(cls, time, data): command = '/usr/bin/python3 {0} \'{1}\''.format(__file__, json.dumps(data)) env = ((k, v) for k, v in os.environ.items() if k.startswith('CHEDDAR_KNIGHT')) subprocess.run(( '/usr/bin/sudo', '/usr/bin/systemd-run', '--uid=nobody', '--gid=nobody', *['--setenv={0}={1}'.format(k, v) for k, v in env], '--timer-property=AccuracySec=1s', '--on-calendar={0}'.format(time.astimezone().strftime('%Y-%m-%d %H:%M:%S')), '/bin/sh', '-c', 'cd {0} && {1}'.format(pathlib.Path(__file__).parent, command) )) @classmethod def execute(cls, data): queue = persistqueue.SQLiteQueue(storage_path) queue.put(('youtube', data)) queue.task_done() def main(): Timer.execute(json.loads(sys.argv[1])) if __name__ == '__main__': main()