-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsenrentokei.py
More file actions
225 lines (209 loc) · 7.69 KB
/
Copy pathsenrentokei.py
File metadata and controls
225 lines (209 loc) · 7.69 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#V1.2.0 21/08/11
from pydub import AudioSegment
from pydub.playback import play
from PIL import Image
from os import startfile
import time
import random
import pystray
import threading
import json
RESPATH="resources/"
chara_dict = {
1: "yos",
2: "mak",
3: "mur",
4: "ren"
}
index_dict = {
"AM": "01",
"PM": "02",
"h1": "03",
"h2": "04",
"h3": "05",
"h4": "06",
"h5": "07",
"h6": "08",
"h7": "09",
"h8": "10",
"h9": "11",
"h10": "12",
"h11": "13",
"h12": "14",
"10" : "15",
"20" : "16",
"30" : "17",
"40" : "18",
"50" : "19",
"m0" : "20",
"m1" : "21",
"m2" : "22",
"m3" : "23",
"m4" : "24",
"m5" : "25",
"m6" : "26",
"m7" : "27",
"m8" : "28",
"m9" : "29",
"m10" : "30",
"m20" : "31",
"m30" : "32",
"m40" : "33",
"m50" : "34",
"END" : "35"
}
def pharse_conf(conf):
try:
alarmlist = []
with open(conf,"r") as file:
config = json.load(file)
character = config["character"]
play_hours = bool(config["play_hours"])
play_halfhr = bool(config["play_halfhr"])
play_tens = bool(config["play_tens"])
play_fives = bool(config["play_fives"])
play_mins = bool(config["play_hours"])
rand_chr = bool(config["rand_chr"])
alarm_playtime = bool(config["alarm_playtime"])
alarm_start_wav = config["alarm_start_wav"]
sleeping_hours = list(config["sleeping_hours"])
for item in config["alarmlist"]:
alarmlist += item.values()
except Exception as error:
print("Error: "+str(error)+", Loading default settings")
return ("mur",True,True,True,True,False,True,True,"",[],[])
return (character,play_hours,play_halfhr,play_tens,play_fives,play_mins,rand_chr,alarm_playtime,alarm_start_wav,sleeping_hours,alarmlist)
character,play_hours,play_halfhr,play_tens,play_fives,play_mins,rand_chr,alarm_playtime,alarm_start_wav,sleeping_hours,alarmlist =\
pharse_conf(RESPATH+"config.json")
def format_cur_time(localtime,indexdict=index_dict,includemins=True):
tlist = []
index_list = []
hr = localtime.tm_hour
if hr >= 12:
tlist += ["PM"]
if hr == 12:
pass
else:
hr -= 12
hr = "h"+str(hr)
tlist += [hr]
else:
if hr == 0:
hr = 12
tlist += ["AM","h"+str(hr)]
if includemins:
minutes = localtime.tm_min
tens = minutes//10
ones = minutes%10
if (tens != 0) and (ones == 0):
tlist += ["m"+str(tens*10)]
elif (tens != 0) and (ones != 0):
tlist += [str(tens*10)]
tlist += ["m"+str(ones)]
else:
tlist += ["m"+str(ones)]
tlist += ["END"]
for item in tlist:
index_list += [indexdict.get(item)]
return [tlist,index_list]
def play_cur_time(timelist,chara="mur",middle="_watch_"):
audlist = []
print(str(chara)+str(timelist[0]))
for item in timelist[1]:
audlist += [AudioSegment.from_wav(RESPATH+chara+middle+item+".wav")]
for aud in audlist:
play(aud)
def alarm(localtime,alarmlist,indexdict=index_dict,playtime=True,alarm_start_wav=""):
try:
for item in alarmlist:
wkday = []
temp = item.get("wkday")
alarm_min = item.get("min")
alarm_hour = item.get("hour")
for num in temp:
wkday += [num-1]
if localtime.tm_min == alarm_min and\
localtime.tm_hour == alarm_hour and\
localtime.tm_wday in wkday:
if len(str(alarm_min)) == 1:
alarm_min = "0"+str(alarm_min)
icon.notify("Alarm at "+str(alarm_hour)+":"+str(alarm_min))
if playtime:
play_cur_time(format_cur_time(localtime,indexdict=index_dict,includemins=True),chara=item.get("chara"))
if alarm_start_wav != "":
play(AudioSegment.from_wav(RESPATH+alarm_start_wav))
for i in range(1,int(item.get("loop"))+1):
play(AudioSegment.from_wav(RESPATH+item.get("chara")+"_alm_"+item.get("vindex")+".wav"))
time.sleep(3)
return True
except Exception as error:
print("Alarm Error: "+str(error))
return False
return False
def main(character,play_hours,play_halfhr,play_tens,play_fives,play_mins,rand_chr,alarm_playtime,alarm_start_wav,sleeping_hours,alarmlist):
t = time.localtime()
play_cur_time(format_cur_time(t,index_dict,includemins=True),chara=character)
t = time.localtime()
print("Started, Sleep "+str(60-t.tm_sec)+"s")
time.sleep(60-t.tm_sec)
global stop_threads
while True:
if stop_threads:
break
try:
if rand_chr:
character = chara_dict.get(random.randint(1,len(chara_dict)))
t = time.localtime()
if (alarm(t,alarmlist,playtime=alarm_playtime,alarm_start_wav=alarm_start_wav) == False)\
and (t.tm_hour not in sleeping_hours) == True:
if play_hours and t.tm_min == 0:
play_cur_time(format_cur_time(t,index_dict,includemins=False),chara=character)
print("hr")
elif play_halfhr and (t.tm_min//30 != 0) and (t.tm_min%10 == 0):
play_cur_time(format_cur_time(t,index_dict,includemins=True),chara=character)
print("30")
elif play_tens and (t.tm_min//10 != 0) and (t.tm_min%10 == 0):
play_cur_time(format_cur_time(t,index_dict,includemins=True),chara=character)
print("10")
elif play_fives and (t.tm_min%5 == 0):
play_cur_time(format_cur_time(t,index_dict,includemins=True),chara=character)
print("5")
elif play_mins:
play_cur_time(format_cur_time(t,index_dict,includemins=True),chara=character)
print("min")
else:
print("Skipped")
if (t.tm_hour in sleeping_hours) == True:
print("Sleep mode, skipped")
t = time.localtime()
if not stop_threads:
print("Sleep "+str(60-t.tm_sec)+"s")
time.sleep(60-t.tm_sec)
except KeyboardInterrupt:
print("Stopped")
break
except Exception as error:
print("Error: "+str(error))
break
def run_main():
main(character,play_hours,play_halfhr,play_tens,play_fives,play_mins,rand_chr,alarm_playtime,alarm_start_wav,sleeping_hours,alarmlist)
def quit():
icon.stop()
def open_conf():
icon.notify('Changes will be applied after restart\nPlease save the config file first')
startfile("resources\\config.json")
icon_img = Image.open(RESPATH+"icon.png","r")
menu = (pystray.MenuItem('Senren Tokei',lambda icon, item: icon.notify('Python Adaptation made by Shiro7940')),\
pystray.MenuItem('Config',open_conf),pystray.MenuItem('Quit',quit))
icon = pystray.Icon('Senren Tokei')
icon.menu = menu
icon.icon = icon_img
stop_threads = False
thread_time = threading.Thread(target=run_main)
thread_time.start()
icon.run()
if rand_chr:
character = chara_dict.get(random.randint(1,len(chara_dict)))
stop_threads = True
play(AudioSegment.from_wav(RESPATH+character+"_end.wav"))
thread_time.join()