break-timer/main.py

99 lines
2.4 KiB
Python
Raw Normal View History

#! /usr/bin/env python3
# vim:fenc=utf-8
#
# Copyright © 2023 gum <gum@gums-lappy>
#
# Distributed under terms of the MIT license.
"""
Play my playlist every few minutes.
"""
import sys
import threading
from argparse import ArgumentParser
from select import select
from subprocess import DEVNULL, Popen, run
from termios import TCIOFLUSH, tcflush
from time import sleep, time
class StoppableThread(threading.Thread):
"""Thread class with a stop() method. The thread itself has to check
regularly for the stopped() condition."""
def __init__(self, *args, **kwargs):
super(StoppableThread, self).__init__(*args, **kwargs)
self._stop_event = threading.Event()
def stop(self):
self._stop_event.set()
def stopped(self):
return self._stop_event.is_set()
def generate_interval(interval: int):
"""
Generate the interval between plays in seconds.
"""
if interval:
while True:
yield int(interval)
else:
# pomodoro scheme
while True:
yield 60 * 25
for _ in range(3):
yield 60 * 5
yield 60 * 25
yield 60 * 30
def play_playlist(playlist):
"""
Play the playlist.
"""
return Popen(
["mpv", "--shuffle", "--no-video", playlist], stdout=DEVNULL, stderr=DEVNULL
)
def wait_or_enter(duration, i3_focus=False):
stop = ""
start = time()
while not stop and (time() - start < duration): # Run until flag is True
if i3_focus:
run(["i3-msg", '[title=\"break\"] focus'])
stop, _, _ = select([sys.stdin], [], [], 1)
tcflush(sys.stdin, TCIOFLUSH)
def main(playlist, interval):
for i in generate_interval(interval):
play = play_playlist(playlist)
print(f"playlist playing, press enter to start {i // 60} minute interval")
wait_or_enter(float("inf"), i3_focus=True)
play.terminate()
print("...")
wait_or_enter(i)
print("next")
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument(
"--playlist",
default="/mnt/nas/m/m/anime-muziek",
help="path to the folder with the playlist to play",
)
parser.add_argument(
"--interval",
default=None,
type=str,
help="Interval between plays in minutes, if empty I use the pomodoro scheme.",
)
args = parser.parse_args()
main(args.playlist, args.interval)