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 +++++++++++++++++++++++++++++++++++++++++++++++++ distract.service | 10 ++++++++++ install.sh | 12 ++++++++++++ timeout | 3 +++ 4 files changed, 74 insertions(+) create mode 100755 distract.py create mode 100644 distract.service create mode 100755 install.sh create mode 100755 timeout 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) diff --git a/distract.service b/distract.service new file mode 100644 index 0000000..fcb3dc9 --- /dev/null +++ b/distract.service @@ -0,0 +1,10 @@ +[Unit] +Description=A script to keep you focused +After=network-online.target + +[Service] +ExecStart=/usr/local/bin/distract.py +Restart=always + +[Install] +WantedBy=default.target diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..a20832e --- /dev/null +++ b/install.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +echo "Installing for the current user" +echo "Please make sure /usr/local/bin is in your PATH" + +/usr/bin/sudo cp distract.py /usr/local/bin/ +/usr/bin/sudo cp timeout /usr/local/bin/ +cp distract.service ~/.config/systemd/user/ + +echo "Starting distract.service" +systemctl enable --now --user distract.service + diff --git a/timeout b/timeout new file mode 100755 index 0000000..5ccca7d --- /dev/null +++ b/timeout @@ -0,0 +1,3 @@ +#!/bin/sh + +echo $1 > /tmp/timeout -- cgit v1.2.3-54-g00ecf