summaryrefslogtreecommitdiff
path: root/distract.py
blob: 7a60a5b0f8fd61678774e1a8f71e887e8b212bba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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)