python/sleeptimer.py
2023-03-16 15:46:02 +03:00

40 lines
1.3 KiB
Python

#!/usr/bin/env python3
# Argument is minutes
import os
import sys
import time
import subprocess
import re
for time_arg in sys.argv: # defined in commandline arguments
print(time_arg, 'minutes')
time_mins = int(time_arg) # convert str to int
time_secs = time_mins * 60 # convert to seconds for time.sleep
mpc_vol_take = subprocess.check_output(['mpc', 'volume']) # take volume
mpc_vol_str = str(mpc_vol_take) # make string
mpc_vol_int = re.sub( r'b\'volume: ', '', mpc_vol_str ) # regex 1/2
mpc_vol_int = int(re.sub( r'%\\n\'', '', mpc_vol_int)) # regex 2/2 and take int
time_step = int(time_secs / 12) # time in seconds of volume reducing
vol_step = int(mpc_vol_int / 12) # volume in procents
mpc_vol_cmd = "mpc volume -{0}".format(vol_step) # reduse volume
mpc_stop = "mpc stop" # stop MPD
mpc_vol_replace = "mpc volume {0}".format(mpc_vol_int) # replace start volume
while True:
if time_secs > 0:
time.sleep(time_step)
os.system(mpc_vol_cmd)
time_secs = int(time_secs - time_step)
if time_secs == 0:
os.system(mpc_stop)
os.system(mpc_vol_replace)
break