This commit is contained in:
cheesus_crust 2023-02-14 21:30:55 +03:00
commit 6feb2471ed
3 changed files with 80 additions and 0 deletions

9
README.md Normal file
View File

@ -0,0 +1,9 @@
Shitty code in Python
*sleeptimer.py*
usage
```
sleeptimer.py [time in minutes]
example
sleeptimer.py 20
```

33
mpdalarm.py Normal file
View File

@ -0,0 +1,33 @@
#!/usr/bin/env python3
import datetime
import os
import time
alarm_hour = int(input("Set hour: "))
alarm_minutes = int(input("Set minutes: "))
print('Waiting for', alarm_hour,':', alarm_minutes)
mpc_set_vol = "mpc volume 20"
mpc_inc_vol = "mpc volume +10"
mpc_play = "mpc play"
while True:
if alarm_hour == datetime.datetime.now().hour and alarm_minutes == datetime.datetime.now().minute:
print("ALARM!")
os.system(mpc_set_vol)
os.system(mpc_play)
time.sleep(10)
os.system(mpc_inc_vol)
time.sleep(20)
os.system(mpc_inc_vol)
time.sleep(30)
os.system(mpc_inc_vol)
time.sleep(30)
os.system(mpc_inc_vol)
time.sleep(20)
os.system(mpc_inc_vol)
time.sleep(10)
os.system(mpc_inc_vol)
break

38
sleeptimer.py Normal file
View File

@ -0,0 +1,38 @@
#!/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