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