commit 6feb2471edd2f0dd0508a82d1798cba98ac3f0d9 Author: cheesus_crust Date: Tue Feb 14 21:30:55 2023 +0300 lets diff --git a/README.md b/README.md new file mode 100644 index 0000000..471c628 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +Shitty code in Python + +*sleeptimer.py* +usage +``` +sleeptimer.py [time in minutes] +example +sleeptimer.py 20 +``` diff --git a/mpdalarm.py b/mpdalarm.py new file mode 100644 index 0000000..c461477 --- /dev/null +++ b/mpdalarm.py @@ -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 diff --git a/sleeptimer.py b/sleeptimer.py new file mode 100644 index 0000000..edb8ac6 --- /dev/null +++ b/sleeptimer.py @@ -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