Skip to content

Commit 350dbf2

Browse files
authored
Create AlarmClock.py
1 parent 51b8d95 commit 350dbf2

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

Basic Scripts/AlarmClock.py

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
""" Alarm Clock
2+
3+
----------------------------------------
4+
5+
"""
6+
7+
import datetime
8+
9+
import os
10+
11+
import time
12+
13+
import random
14+
15+
import webbrowser
16+
17+
# If video URL file does not exist, create one
18+
19+
if not os.path.isfile("youtube_alarm_videos.txt"):
20+
21+
print('Creating "youtube_alarm_videos.txt"...')
22+
23+
with open("youtube_alarm_videos.txt", "w") as alarm_file:
24+
25+
alarm_file.write("https://www.youtube.com/watch?v=anM6uIZvx74")
26+
27+
def check_alarm_input(alarm_time):
28+
29+
"""Checks to see if the user has entered in a valid alarm time"""
30+
31+
if len(alarm_time) == 1: # [Hour] Format
32+
33+
if alarm_time[0] < 24 and alarm_time[0] >= 0:
34+
35+
return True
36+
37+
if len(alarm_time) == 2: # [Hour:Minute] Format
38+
39+
if alarm_time[0] < 24 and alarm_time[0] >= 0 and \
40+
41+
alarm_time[1] < 60 and alarm_time[1] >= 0:
42+
43+
return True
44+
45+
elif len(alarm_time) == 3: # [Hour:Minute:Second] Format
46+
47+
if alarm_time[0] < 24 and alarm_time[0] >= 0 and \
48+
49+
alarm_time[1] < 60 and alarm_time[1] >= 0 and \
50+
51+
alarm_time[2] < 60 and alarm_time[2] >= 0:
52+
53+
return True
54+
55+
return False
56+
57+
# Get user input for the alarm time
58+
59+
print("Set a time for the alarm (Ex. 06:30 or 18:30:00)")
60+
61+
while True:
62+
63+
alarm_input = input(">> ")
64+
65+
try:
66+
67+
alarm_time = [int(n) for n in alarm_input.split(":")]
68+
69+
if check_alarm_input(alarm_time):
70+
71+
break
72+
73+
else:
74+
75+
raise ValueError
76+
77+
except ValueError:
78+
79+
print("ERROR: Enter time in HH:MM or HH:MM:SS format")
80+
81+
# Convert the alarm time from [H:M] or [H:M:S] to seconds
82+
83+
seconds_hms = [3600, 60, 1] # Number of seconds in an Hour, Minute, and Second
84+
85+
alarm_seconds = sum([a*b for a,b in zip(seconds_hms[:len(alarm_time)], alarm_time)])
86+
87+
# Get the current time of day in seconds
88+
89+
now = datetime.datetime.now()
90+
91+
current_time_seconds = sum([a*b for a,b in zip(seconds_hms, [now.hour, now.minute, now.second])])
92+
93+
# Calculate the number of seconds until alarm goes off
94+
95+
time_diff_seconds = alarm_seconds - current_time_seconds
96+
97+
# If time difference is negative, set alarm for next day
98+
99+
if time_diff_seconds < 0:
100+
101+
time_diff_seconds += 86400 # number of seconds in a day
102+
103+
# Display the amount of time until the alarm goes off
104+
105+
print("Alarm set to go off in %s" % datetime.timedelta(seconds=time_diff_seconds))
106+
107+
# Sleep until the alarm goes off
108+
109+
time.sleep(time_diff_seconds)
110+
111+
# Time for the alarm to go off
112+
113+
print("Wake Up!")
114+
115+
# Load list of possible video URLs
116+
117+
with open("youtube_alarm_videos.txt", "r") as alarm_file:
118+
119+
videos = alarm_file.readlines()
120+
121+
# Open a random video from the list
122+
123+
webbrowser.open(random.choice(videos))

0 commit comments

Comments
 (0)