I created this script because the Spotify desktop app doesn't have a built in sleep timer, and I needed one. I made it as easy to use as possible, and it can close other programs because it asks for the package name at the beginning of the script.
import time
import sys
import subprocess
pkgname = input("Please set the package name: ")
tillclose = float(input("\nPlease choose the amount of time before it closes:\n\n[1.] 10 Minutes\n[2.] 30 Minutes\n[3.] 1 Hour\n[4.] 2 Hours\n[5.] 3 Hours\n[6.] 4 Hours\n[7.] 5 Hours\n"))
if tillclose == 1:
print("Confirmed. Set to close in 10 Minutes.")
tillclose = 600
for remaining in range(int(tillclose), 0, -1):
sys.stdout.write("\r")
sys.stdout.write("{:2d} seconds remaining.".format(remaining))
sys.stdout.flush()
time.sleep(1)
subprocess.call(["taskkill", "/F", "/IM", pkgname])
elif tillclose == 2:
print("Confirmed. Set to close in 30 Minutes.")
tillclose = 1800
for remaining in range(int(tillclose), 0, -1):
sys.stdout.write("\r")
sys.stdout.write("{:2d} seconds remaining.".format(remaining))
sys.stdout.flush()
time.sleep(1)
subprocess.call(["taskkill", "/F", "/IM", pkgname])
elif tillclose == 3:
print("Confirmed. Set to close in 1 Hour.")
tillclose = 3600
for remaining in range(int(tillclose), 0, -1):
sys.stdout.write("\r")
sys.stdout.write("{:2d} seconds remaining.".format(remaining))
sys.stdout.flush()
time.sleep(1)
subprocess.call(["taskkill", "/F", "/IM", pkgname])
elif tillclose == 4:
print("Confirmed. Set to close in 2 Hours.")
tillclose = 7200
for remaining in range(int(tillclose), 0, -1):
sys.stdout.write("\r")
sys.stdout.write("{:2d} seconds remaining.".format(remaining))
sys.stdout.flush()
time.sleep(1)
subprocess.call(["taskkill", "/F", "/IM", pkgname])
elif tillclose == 5:
print("Confirmed. Set to close in 3 Hours")
tillclose = 10800
for remaining in range(int(tillclose), 0, -1):
sys.stdout.write("\r")
sys.stdout.write("{:2d} seconds remaining.".format(remaining))
sys.stdout.flush()
time.sleep(1)
subprocess.call(["taskkill", "/F", "/IM", pkgname])
elif tillclose == 6:
print("Confirmed. Set to close in 4 Hours")
tillclose = 14400
for remaining in range(int(tillclose), 0, -1):
sys.stdout.write("\r")
sys.stdout.write("{:2d} seconds remaining.".format(remaining))
sys.stdout.flush()
time.sleep(1)
subprocess.call(["taskkill", "/F", "/IM", pkgname])
elif tillclose == 7:
print("Confirmed. Set to close in 5 Hours")
tillclose = 18000
for remaining in range(int(tillclose), 0, -1):
sys.stdout.write("\r")
sys.stdout.write("{:2d} seconds remaining.".format(remaining))
sys.stdout.flush()
time.sleep(1)
subprocess.call(["taskkill", "/F", "/IM", pkgname])
else:
print("Invalid option, please run again.")
It works flawlessly, but I'm wondering if there's a shorter way to make it.
tillclose
as the parameter and then call it. \$\endgroup\$for remaining in range(int(tillclose), 0, -1):
casting tillclose to an int seems to be redundant as it is already an int. I see you originally cast this to a float instead of an integer, there doesn't seem to be much point in that either so you might want to cast it originally to an int asint(input("\nPlease choose the amount of time before it closes:\n\n[1.] 10 Minutes\n[2.] 30 Minutes\n[3.] 1 Hour\n[4.] 2 Hours\n[5.] 3 Hours\n[6.] 4 Hours\n[7.] 5 Hours\n"))
. \$\endgroup\$options = {1: 600, 2: 1800, ...}
etc, usingdict.get()
you can rewrite this code to be really clean. \$\endgroup\$