-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimeIsImportant.py
56 lines (42 loc) · 1.44 KB
/
timeIsImportant.py
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
import json
import time
import pygame
import os
def load_json(filename):
with open(filename, 'r') as file:
data = json.load(file)
return data
def schedule_alarms_and_timers(data):
current_time = time.time()
updated_data = []
for item in data:
timestamp, event = int(list(item.keys())[0]), list(item.values())[0]
if timestamp <= current_time:
continue
wait_time = timestamp - current_time
time.sleep(wait_time)
if event == "alarm":
play_sound("wake.mp3")
elif event == "timer":
play_sound("wake.mp3")
current_time = time.time()
updated_data.append(item)
return updated_data
def play_sound(sound_file):
pygame.mixer.init()
pygame.mixer.music.load(sound_file)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
continue
def create_json(filename):
with open(filename, 'w') as file:
json.dump([], file)
def start():
if not os.path.exists("time.json"):
create_json("time.json")
while True:
data = load_json("time.json")
updated_data = schedule_alarms_and_timers(data)
with open("time.json", "w") as file:
json.dump(updated_data, file, indent=4)
time.sleep(30) # Wait for 30 seconds before the next iteration