diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..25bddd4 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "breakscreen" +version = "0.1.0" diff --git a/main.py b/main.py new file mode 100755 index 0000000..c92b578 --- /dev/null +++ b/main.py @@ -0,0 +1,98 @@ +#! /usr/bin/env python3 +# vim:fenc=utf-8 +# +# Copyright © 2023 gum +# +# 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) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..9f67720 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pyright