-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlaram.py
More file actions
74 lines (58 loc) · 2.46 KB
/
Copy pathAlaram.py
File metadata and controls
74 lines (58 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import datetime
import os
import re
import subprocess
def rename_files_with_whitespaces(cd,files,extra_path=""):
for file in files:
if "" in file:
renamed_file = file.replace("","_")
os.rename(os.path.join(cd,extra_path,file),os.path.join(cd,extra_path,renamed_file))
def clean_filename(file):
return ''.join(map(str.capitalize,file[:-4].split('_')))
def set_alarm():
stop = False
error = True
while error:
user_set_time = ":".join(map(lambda x: str(x).zfill(2),input("\nSet the alram time(e.g.01:10):").split(":")))
if re.match(r"^[0-9]{2}:[0-9]{2}$",user_set_time):
playback_time = f"{user_set_time}:00.000000"
error = False
else:
print(">>> Error: Time format invalid! Please try again!\n")
cd = os.path.dirname(os.path.realpath(__file__))
musics_path = os.path.join(cd,"musics")
renames_files_with_whitespaces(cd,os.listdir(musics_path),"musics")
musics = os.listdir(musics_path)
if len(musics) < 1:
print(">>> Error: No music in the musics folder! Please add music first!\n")
exit()
elif len(musics) == 1:
print(">> Alarm music has been set default -->" + clean_filename(musics[0]))
selected_music = musics[0]
else:
error = True
while error:
try:
print("\n Select any alaram music:\n")
for i in range(1,len(musics)+1):
print(f"{i}.{clean_filename(musics[i-1])}")
user_input = int(input("\n Enter the index of listed music(e.g.1)"))
selected_music = musics[user_input - 1]
print(">> Alaram music has been set -->"+clean_filename(selected_music))
error=False
except:
print(">>> Error: Invalid Index! Please try again!\n")
while stop == False:
current_time = str(datetime.datetime.now().time())
if current_time >= playback_time:
stop = True
subprocess.run(('cmd','/C','start',f"{cd}\\musics\\{selected_music}"))
print(">>> Alarm ringing! Closing the program!! Bye Bye!! <<<")
def display_header(header):
print("")
print("#################".center(os.get_terminal_size().columns))
print(f"######{header}########".center(os.get_terminal_size().columns))
print("#######".center(os.get_terminal_size().columns))
if __name__ == "__main__":
display_header("Alarm Program")
set_alarm()