From 3b69922a58e7ce4f992042090111c46e820ace5e Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Mon, 25 Jan 2021 18:41:18 +0100 Subject: Initial commit --- distract.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 distract.py (limited to 'distract.py') diff --git a/distract.py b/distract.py new file mode 100755 index 0000000..7a60a5b --- /dev/null +++ b/distract.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 + +import datetime +import os +import subprocess +import time + +programs = ["firefox-bin", "Discord"] +timeout_file = "/tmp/timeout" +max_timeout = 60 +time_format = "%H:%M" +work_hours = [("00:15", "11:00")] +refresh_interval = 15 + +skipped_until = datetime.datetime.now() + + +def kill(): + for program in programs: + try: + subprocess.run(["killall", program], check=True, stderr=subprocess.DEVNULL) + print(f"killed {program}") + except subprocess.CalledProcessError as e: + if e.returncode != 1: + print(f"failed to kill {program}: {e}") + + +def in_shift(shift, time): + start = datetime.datetime.strptime(shift[0], time_format) + end = datetime.datetime.strptime(shift[1], time_format) + return start.time() <= time <= end.time() + + +while True: + current_time = datetime.datetime.now() + try: + with open(timeout_file) as f: + x = min(abs(int(f.readline().strip())), max_timeout) + skipped_until = current_time + datetime.timedelta(minutes=x) + print(f"Recieved timeout request, sleeping until: {skipped_until}") + os.remove(timeout_file) + except FileNotFoundError: + pass + if current_time > skipped_until: + for shift in work_hours: + if in_shift(shift, current_time.time()): + kill() + break + time.sleep(refresh_interval) -- cgit v1.2.3-54-g00ecf