-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
347 lines (301 loc) · 8.99 KB
/
main.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import asyncio
import functools
import json
import shutil
import tkinter
from tkinter import messagebox
from concurrent.futures import ThreadPoolExecutor
import time
import pygame
import pyttsx3
from pynput import keyboard
import requests
import os
from aiohttp.web import Application, RouteTableDef, Request, Response, run_app
import filelock
from tkinter import *
import sys
from yt_dlp import YoutubeDL
from tkinter import ttk
# Setup
done = False
last_exit_press = time.time()
exit_presses = 1
all_ = False
last_reset = time.time()
enabled = True
app = Application()
shutdown = False
progress: ttk.Progressbar
routes = RouteTableDef()
# Config
gender = 0
rate = 150
channels = 8
yt_update = False
exit_key = "esc"
port = 6238
keys = {}
if sys.platform.startswith("win"):
lock = filelock.WindowsFileLock(lock_file="lock", timeout=1)
else:
lock = filelock.UnixFileLock(lock_file="lock", timeout=1)
def disable_event():
pass
def show_error_popup(message):
r = tkinter.Tk()
r.withdraw() # Hide the root window
messagebox.showerror("Error", message) # Show the error popup
r.destroy() # Close the root window after the popup
def download_audio_as_mp3(key, url):
options = {
"format": "bestaudio/best",
"postprocessors": [
{
"key": "FFmpegExtractAudio",
"preferredcodec": "mp3",
}
],
"outtmpl": f"{key}",
"quiet": True,
}
with YoutubeDL(options) as ydl:
ydl.download([url])
def connected() -> bool:
try:
with requests.Session() as session:
with session.get("http://google.com", timeout=5):
pass
except Exception:
return False
return True
def setup():
global gender, rate, channels, yt_update, exit_key, port, keys
try:
with open("config.json", "r") as f:
data = json.load(f)
gender = data.get("gender") or "male"
rate = data.get("rate") or 170
if gender.lower() == "male":
gender = 0
elif gender.lower() == "female":
gender = 1
else:
gender = 0
channels = data.get("channels") or 8
yt_update = data.get("yt_update") or False
exit_key = data.get("exit_key") or "esc"
port = data.get("port") or 6238
except Exception:
pass
try:
with open("keys.json") as f:
keys = json.load(f)
except Exception:
return
def start_pb():
global progress
root = Tk()
progress = ttk.Progressbar(root, orient=HORIZONTAL, length=100, mode="determinate")
root.protocol("WM_DELETE_WINDOW", disable_event)
root.title("KeyboardSoundPlayer")
root.resizable(False, False)
root.geometry("250x75")
progress.pack(pady=20)
root.attributes("-topmost", 1)
def pb_loop():
if done:
root.destroy()
else:
root.after(100, pb_loop)
root.after(100, pb_loop)
root.mainloop()
return progress
def save_to_file():
global all_, done, progress
try:
with open("all.mp3"):
pass
all_ = True
except FileNotFoundError:
pass
engine = pyttsx3.init()
engine.setProperty("rate", rate)
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[gender].id)
for key in keys:
if key == "all":
all_ = True
if keys[key].startswith("https://"):
if not connected():
progress["value"] += float(100) / (len(keys) + 1)
continue
if os.path.exists(f"{key}.mp3") and not yt_update:
progress["value"] += float(100) / (len(keys) + 1)
continue
try:
download_audio_as_mp3(key, keys[key])
except Exception as e:
show_error_popup(
f"Error downloading audio for key: {key}\nContact us at jgltechnologies.com/contact"
)
continue
progress["value"] += float(100) / (len(keys) + 1)
continue
if keys[key].endswith("()"):
progress["value"] += float(100) / (len(keys) + 1)
continue
if keys[key].endswith(".mp3"):
shutil.copyfile(keys[key], f"{key}.mp3")
progress["value"] += float(100) / (len(keys) + 1)
continue
engine.save_to_file(keys[key], f"{key}.mp3")
progress["value"] += float(100) / (len(keys) + 1)
engine.runAndWait()
progress["value"] += float(100) / (len(keys) + 1)
time.sleep(1)
done = True
def quit_app(pool: ThreadPoolExecutor):
global shutdown
shutdown = True
pool.submit(pygame.quit)
os._exit(1)
def play(file: str):
try:
pygame.mixer.Sound(file).play()
except Exception:
return
def reset():
global last_reset
pygame.mixer.stop()
last_reset = time.time()
def on_press(pool: ThreadPoolExecutor, key_):
global enabled, exit_presses, last_exit_press
if all_:
try:
if enabled:
pool.submit(play, "all.mp3")
except Exception:
pass
try:
k = key_.name
except AttributeError:
k = key_.char
k = str(k).lower()
if k == exit_key:
if last_exit_press + 2 <= time.time():
exit_presses = 1
if exit_presses >= 5:
quit_app(app.pool)
elif exit_presses == 1:
last_exit_press = time.time()
exit_presses += 1
if k in keys:
if keys[k] == "reset()":
if enabled:
if last_reset + 1 <= time.time():
pool.submit(reset)
else:
return
elif keys[k] == "toggle()":
enabled = not enabled
return
elif keys[k] == "pause()":
if enabled:
pool.submit(pygame.mixer.pause)
elif keys[k] == "unpause()":
if enabled:
pool.submit(pygame.mixer.unpause)
try:
if enabled:
file = f"{k}.mp3"
pool.submit(play, file)
except Exception:
return
@routes.get("/online")
async def online_endpoint(request: Request):
return Response(text="True", status=200)
@routes.get("/stop")
async def stop_endpoint(request: Request):
quit_app(app.pool)
return Response(text="ok", status=200)
@routes.get("/play")
async def play_endpoint(request: Request):
global enabled, exit_presses, last_exit_press
if all_:
try:
if enabled:
app.pool.submit(play, "all.mp3")
except Exception:
pass
k = request.query.get("key")
if k is None:
return Response(text="Key is required", status=400)
k = k.lower()
if k not in keys:
return Response(text="Key is invalid", status=400)
if k == exit_key:
if last_exit_press + 2 <= time.time():
exit_presses = 1
if exit_presses >= 5:
quit_app(app.pool)
elif exit_presses == 1:
last_exit_press = time.time()
exit_presses += 1
if k in keys:
if keys[k] == "reset()":
if enabled:
if last_reset + 1 <= time.time():
app.pool.submit(reset)
return Response(text="ok", status=200)
else:
return Response(text="ok", status=200)
elif keys[k] == "toggle()":
enabled = not enabled
return Response(text="ok", status=200)
elif keys[k] == "pause()":
if enabled:
app.pool.submit(pygame.mixer.pause)
return Response(text="ok", status=200)
elif keys[k] == "unpause()":
if enabled:
app.pool.submit(pygame.mixer.unpause)
return Response(text="ok", status=200)
try:
if enabled:
file = f"{k}.mp3"
app.pool.submit(play, file)
return Response(text="ok", status=200)
except Exception as e:
return Response(text=str(e), status=500)
async def loop():
while True:
if shutdown:
await app.shutdown()
await app.cleanup()
sys.exit()
await asyncio.sleep(0.5)
async def startup(app: Application):
asyncio.get_event_loop().create_task(loop())
def run_server(pool: ThreadPoolExecutor):
app.pool = pool
run_app(app, port=port)
def main():
setup()
with ThreadPoolExecutor(1) as pool:
aiohttp_pool = ThreadPoolExecutor(1)
app.on_startup.append(startup)
app.add_routes(routes)
aiohttp_pool.submit(run_server, pool)
listener = keyboard.Listener(on_press=functools.partial(on_press, pool))
pool.submit(save_to_file)
start_pb()
pool.submit(pygame.init)
pool.submit(pygame.mixer.set_num_channels, channels)
listener.run()
try:
with lock.acquire():
if __name__ == "__main__":
main()
except filelock.Timeout:
sys.exit()