-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
737 lines (632 loc) · 26.5 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
import json
import os
import shutil
import subprocess
import traceback
from datetime import datetime, timedelta
from typing import Any, Union
import asyncpg
import jinja2
import jwt # PyJWT
import requests
import tornado.escape
import tornado.gen
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.websocket
from dotenv import load_dotenv
from tornado.web import Application, RequestHandler, url
import synology_uploader
load_dotenv()
loader = jinja2.FileSystemLoader("templates")
env = jinja2.Environment(loader=loader)
class Broadcast:
def __init__(
self,
host: str,
description: str,
password: str,
is_private: bool,
start_time: datetime,
):
self.host = host
self.description = description
self.password = password
self.is_private = is_private
self.start_time = start_time
active_broadcasts: dict[str, Broadcast] = {}
async def get_db_connection():
return await asyncpg.connect(
host=os.environ.get("POSTGRES_HOST"),
port=os.environ.get("POSTGRES_PORT"),
database=os.environ.get("POSTGRES_DB"),
user=os.environ.get("POSTGRES_USER"),
password=os.environ.get("POSTGRES_PASSWORD"),
)
async def fetch_audio_archives():
conn = await get_db_connection()
try:
rows = await conn.fetch("SELECT * FROM audioarchives")
return [dict(row) for row in rows]
finally:
await conn.close()
async def update_visit(file_name):
conn = await get_db_connection()
try:
await conn.execute(
"""
UPDATE audioarchives
SET visit_count = COALESCE(visit_count, 0) + 1, latest_visit = $1
WHERE filename = $2
""",
datetime.now(),
file_name,
)
finally:
await conn.close()
async def update_click(file_name):
conn = await get_db_connection()
try:
await conn.execute(
"""
UPDATE audioarchives
SET click_count = COALESCE(click_count, 0) + 1, latest_click = $1
WHERE filename = $2
""",
datetime.now(),
file_name,
)
finally:
await conn.close()
def format_length(length_in_minutes):
hours, minutes = divmod(int(length_in_minutes), 60)
hours_string = f"{hours} hour{'s' if hours > 1 else ''}" if hours > 0 else ""
minutes_string = (
f"{minutes} minute{'s' if minutes != 1 else ''}" if minutes > 0 else ""
)
if hours_string and minutes_string:
return f"{hours_string}, {minutes_string}"
return hours_string or minutes_string
def get_grouped_data(audio_data):
today = datetime.today()
current_year, current_month, current_week = (
today.year,
today.month,
today.isocalendar()[1],
)
groups = {
"Today": {},
"Yesterday": {},
"Two Days Ago": {},
"Three Days Ago": {},
"Sometime This Week": {},
"Last Week": {},
"Sometime This Month": {},
"Last Month": {},
"Two Months Ago": {},
"Three Months Ago": {},
"Sometime This Year": {},
"Last Year": {},
"Two Years Ago": {},
"Three Years Ago": {},
"Everything Else": {},
}
for row in audio_data:
itemData = dict(row)
itemData["formatted_length"] = format_length(itemData["length"])
item_date = datetime.strptime(row["date"], "%B %d %A %Y %I_%M %p")
item_week = item_date.isocalendar()[1]
diff_days = (today.date() - item_date.date()).days
item_name = row["filename"].replace("_", ":").replace(".mp3", "")
if diff_days == 0:
itemData["uploaded_days_ago"] = "Today"
elif diff_days == 1:
itemData["uploaded_days_ago"] = "Yesterday"
else:
itemData["uploaded_days_ago"] = f"{diff_days} days ago"
if item_date.year == current_year:
if item_date.month == current_month:
if item_week == current_week:
if diff_days == 0:
groups["Today"][item_name] = itemData
elif diff_days == 1:
groups["Yesterday"][item_name] = itemData
elif diff_days == 2:
groups["Two Days Ago"][item_name] = itemData
elif diff_days == 3:
groups["Three Days Ago"][item_name] = itemData
else:
groups["Sometime This Week"][item_name] = itemData
elif item_week == current_week - 1:
groups["Last Week"][item_name] = itemData
else:
groups["Sometime This Month"][item_name] = itemData
elif item_date.month == current_month - 1:
groups["Last Month"][item_name] = itemData
elif item_date.month == current_month - 2:
groups["Two Months Ago"][item_name] = itemData
elif item_date.month == current_month - 3:
groups["Three Months Ago"][item_name] = itemData
else:
groups["Sometime This Year"][item_name] = itemData
elif item_date.year == current_year - 1:
groups["Last Year"][item_name] = itemData
elif item_date.year == current_year - 2:
groups["Two Years Ago"][item_name] = itemData
elif item_date.year == current_year - 3:
groups["Three Years Ago"][item_name] = itemData
else:
groups["Everything Else"][item_name] = itemData
return {key: value for key, value in groups.items() if value}
error_messages = {
500: "Oooops! Internal Server Error. That is, something went terribly wrong.",
404: "Uh-oh! You seem to have ventured into the void. This page doesn't exist!",
403: "Hold up! You’re trying to sneak into a restricted area. Access denied!",
400: "Yikes! The server couldn't understand your request. Try being clearer!",
401: "Hey, who goes there? You need proper credentials to enter!",
405: "Oops! You knocked on the wrong door with the wrong key. Method not allowed!",
408: "Well, this is awkward. Your request took too long. Let's try again?",
502: "Looks like the gateway had a hiccup. It’s not you, it’s us!",
503: "We’re taking a quick nap. Please try again later!",
418: "I’m a teapot, not a coffee maker! Why would you ask me to do that?",
}
class BaseHandler(RequestHandler):
def write_error(self, status_code: int, stack_trace: str = "", **kwargs):
error_message = error_messages.get(status_code, "Something went majorly wrong.")
template = env.get_template("error.html")
rendered_template = template.render(
error_code=status_code, error_message=error_message, stack_trace=stack_trace
)
self.write(rendered_template)
class MainHandler(BaseHandler):
async def get(self):
try:
audio_data = await fetch_audio_archives()
grouped_data = get_grouped_data(audio_data)
try:
path = os.getenv("RECORDING_STATUS_PATH", r"\\Pinecone\web\HBNI Audio Stream Recorder\static\recording_status.json")
with open(path, "r", encoding="utf-8") as f:
recording_status = json.load(f)
except Exception as e:
recording_status = {
"ERROR": str(e)
}
template = env.get_template("index.html")
rendered_template = template.render(
downloadableRecordings=grouped_data,
recording_status=recording_status,
url_for=url_for_static,
)
self.write(rendered_template)
except Exception as e:
self.set_status(500)
self.write_error(500, stack_trace=f"{str(e)} {traceback.print_exc()}")
def url_for_static(filename):
static_recordings_path = os.getenv(
"STATIC_RECORDINGS_PATH", "/app/static/Recordings"
)
return f"{static_recordings_path}/{filename}"
class PlayRecordingHandler(BaseHandler):
async def get(self, file_name):
await update_visit(file_name) # Await the async database update
conn = await get_db_connection()
try:
result = await conn.fetchrow(
"""
SELECT visit_count, latest_visit, date, description, length
FROM audioarchives
WHERE filename = $1
""",
file_name,
)
finally:
await conn.close()
if result:
visit_count = result["visit_count"] or 0
latest_visit = result["latest_visit"] or "N/A"
date = result["date"] or "N/A"
description = result["description"] or "N/A"
length = format_length(result["length"]) or "N/A"
else:
visit_count = 0
latest_visit = "N/A"
date = "N/A"
description = "N/A"
length = format_length(0)
audio_data = await fetch_audio_archives()
grouped_data = get_grouped_data(audio_data)
template = env.get_template("play_recording.html")
rendered_template = template.render(
title=file_name.split(" - ")[0],
file_name=file_name,
visit_count=visit_count,
latest_visit=latest_visit,
date=date,
description=description,
length=length,
downloadableRecordings=grouped_data,
url_for=url_for_static,
)
self.write(rendered_template)
class ValidatePasswordHandler(RequestHandler):
async def post(self):
try:
data: dict[str, str] = tornado.escape.json_decode(self.request.body)
password = data.get("password")
correct_password = os.getenv("HBNI_STREAMING_PASSWORD")
if password == correct_password:
# Generate a token valid for a limited time
token = jwt.encode(
{
"exp": datetime.now() + timedelta(hours=1),
"iat": datetime.now(),
"scope": "broadcast",
},
os.getenv("SECRET_KEY"),
algorithm="HS256",
)
self.write({"success": True, "token": token})
else:
self.set_status(401)
self.write({"success": False, "error": "Invalid password"})
except Exception as e:
print(e)
self.set_status(500)
self.write({"success": False, "error": str(e)})
def cleanup_old_schedules():
try:
if not os.path.exists("schedule.json"):
return
# Load the existing schedule
with open("schedule.json", "r") as f:
schedule: dict[str, dict[str, str]] = json.load(f)
# Get current time
now = datetime.now()
# Remove old schedules
updated_schedule = {
key: value
for key, value in schedule.items()
if datetime.fromisoformat(value["start_time"]) > now
}
# Save the updated schedule
with open("schedule.json", "w") as f:
json.dump(updated_schedule, f, indent=4)
# print("Old schedules cleaned up successfully.")
except Exception as e:
print(f"Error during schedule cleanup: {e}")
class ScheduleBroadcastHandler(BaseHandler):
async def post(self):
try:
data: dict[str, str] = tornado.escape.json_decode(self.request.body)
host = data.get("host")
description = data.get("description")
start_time = data.get("startTime")
if not host or not description or not start_time:
self.set_status(400)
self.write({"success": False, "error": "Missing required fields"})
return
if not os.path.exists("schedule.json"):
with open("schedule.json", "w") as f:
json.dump({}, f)
with open("schedule.json", "r") as f:
schedule = json.load(f)
schedule[datetime.now().isoformat()] = {
"host": host,
"description": description,
"start_time": start_time,
}
with open("schedule.json", "w") as f:
json.dump(schedule, f, indent=4)
self.set_status(200)
self.write({"success": True})
except Exception as e:
self.set_status(500)
self.write_error(500, stack_trace=f"{str(e)} {traceback.print_exc()}")
class BroadcastWSHandler(tornado.websocket.WebSocketHandler):
def open(self):
self.host: str = ""
self.description: str = ""
self.password: str = ""
self.is_private: bool = False
self.ffmpeg_process = None
self.output_filename: str = ""
self.starting_time: datetime = None
self.ending_time: datetime = None
def on_message(self, message):
# If the received message is JSON metadata, start the ffmpeg process
if isinstance(message, str):
try:
metadata: dict[str, str] = json.loads(message)
self.host = (
metadata.get("host", "unknown")
.lower()
.strip()
.replace(" ", "_")
.replace("/", "")
)
self.description = metadata.get(
"description", "Unspecified Description"
)
self.password = metadata.get("password", "")
if self.password != os.getenv("HBNI_STREAMING_PASSWORD"):
self.write_message("Invalid password.")
return
self.is_private = metadata.get("isPrivate", False)
self.starting_time = datetime.now()
self.output_filename = f'{self.host.title()} - {self.description} - {self.starting_time.strftime("%B %d %A %Y %I_%M %p")} - BROADCAST_LENGTH.wav'
self.ffmpeg_process = subprocess.Popen(
[
"ffmpeg",
"-re",
"-i",
"-",
"-c:a",
"libmp3lame", # Use MP3 codec
"-b:a",
"128k", # Audio bitrate
"-content_type",
"audio/mpeg",
"-y", # Overwrite output file if it already exists
# Add metadata for the Icecast stream
"-metadata",
f"title={self.description}",
"-metadata",
f"artist={self.host}",
"-metadata",
"genre=various",
"-metadata",
f"comment={self.description}",
# Output 1: Icecast stream
"-f",
"mp3",
f"icecast://source:{self.password}@hbniaudio.hbni.net:8000/{self.host}",
# Output 2: Local MP3 file to save the broadcast
"-f",
"wav",
self.output_filename,
],
stdin=subprocess.PIPE, # We want to send binary audio data
)
# Check if the process started successfully
if self.ffmpeg_process.poll() is None:
print(
f"FFmpeg process started successfully for {self.output_filename}"
)
active_broadcasts[self.host] = Broadcast(
self.host,
self.description,
self.password,
self.is_private,
datetime.now(),
)
else:
print(f"FFmpeg process failed to start for {self.output_filename}")
except json.JSONDecodeError:
self.write_message("Invalid metadata format.")
except Exception as e:
print(f"Error starting FFmpeg process: {e}")
# If the received message is binary (audio data)
elif isinstance(message, bytes):
if self.ffmpeg_process and self.ffmpeg_process.stdin:
try:
# Write the audio data directly as bytes
self.ffmpeg_process.stdin.write(message)
except BrokenPipeError:
print("FFmpeg process has ended. Unable to write more data.")
except Exception as e:
print(f"Error writing to FFmpeg process stdin: {e}")
def on_close(self):
if self.ffmpeg_process:
try:
if self.ffmpeg_process.stdin:
self.ffmpeg_process.stdin.close()
except Exception as e:
print(f"Error closing stdin: {e}")
# Terminate FFmpeg process gracefully
self.ffmpeg_process.terminate()
try:
self.ffmpeg_process.wait(timeout=5)
except subprocess.TimeoutExpired:
# Kill FFmpeg process not so gracefully
self.ffmpeg_process.kill()
print("FFmpeg process has gracefully been terminated.")
self.ending_time = datetime.now()
delta = self.ending_time - self.starting_time
total_minutes = delta.total_seconds() / 60
hours, remainder = divmod(delta.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
if hours <= 0:
formated_length = f"{minutes:02d}m {seconds:02d}s"
else:
formated_length = f"{hours:02d}h {minutes:02d}m {seconds:02d}s"
new_output_filename = self.output_filename.replace(
"BROADCAST_LENGTH", formated_length
)
static_recordings_path = (
os.getenv("STATIC_RECORDINGS_PATH", "/app/static/Recordings")
.replace("\\", "/")
.replace("//", "/")
.replace("//", "\\\\")
.replace("/", "\\")
)
if total_minutes > 10.0 and not self.is_private:
shutil.move(
self.output_filename,
f"{static_recordings_path}\\{new_output_filename}",
)
synology_uploader.upload(
new_output_filename,
f"{static_recordings_path}\\{new_output_filename}",
self.host,
self.description,
self.starting_time.strftime("%B %d %A %Y %I_%M %p"),
total_minutes,
)
# else:
# shutil.move(
# self.output_filename,
# f"{static_recordings_path}\\Tests\\{new_output_filename}",
# )
class BroadcastHandler(BaseHandler):
def get(self):
template = env.get_template("broadcasting_page.html")
rendered_template = template.render()
self.write(rendered_template)
class ListenHandler(BaseHandler):
def is_broadcast_private(self, host: str) -> bool:
if broadcast := active_broadcasts.get(host):
return broadcast.is_private
return False
def get_active_broadcast_count(self, broadcast_data) -> int:
active_broadcast_count = 0
for broadcast in broadcast_data:
if not broadcast.get("is_private", False):
active_broadcast_count += 1
return active_broadcast_count
async def get_active_hbni_broadcasts(self) -> list[dict[str, Union[str, int]]]:
# URL for fetching the JSON data
status_url = "http://hbniaudio.hbni.net:8000/status-json.xsl"
# Attempt to get the JSON data from the URL
try:
response = requests.get(status_url)
if response.status_code == 200:
json_data = response.json()
else:
self.set_status(500)
self.write_error(500)
return []
except requests.exceptions.RequestException as e:
self.set_status(500)
self.write_error(
500, stack_trace=f"Error while fetching JSON data: {str(e)}"
)
return []
# Extract relevant data for rendering
icestats = json_data.get("icestats", {})
sources = icestats.get("source", {})
broadcast_data = []
# Prepare data for template rendering
if sources:
if isinstance(sources, dict): # Only one broadcast is currently online
host = sources.get("listenurl", "/").split("/")[-1]
broadcast_data.append(
{
"admin": icestats.get("admin", "N/A"),
"location": icestats.get("location", "N/A"),
"server_name": sources.get("server_name", "Unspecified name"),
"server_description": sources.get(
"server_description", "Unspecified description"
),
"genre": sources.get("genre", "N/A"),
"listeners": sources.get("listeners", 0),
"host": host,
"listener_peak": sources.get("listener_peak", 0),
"listen_url": sources.get("listenurl", "#"),
"stream_start": sources.get("stream_start", "N/A"),
"is_private": self.is_broadcast_private(host),
}
)
elif isinstance(sources, list): # Multiple broadcasts are currently online
for source in sources:
host = source.get("listenurl", "/").split("/")[-1]
broadcast_data.append(
{
"admin": icestats.get("admin", "N/A"),
"location": icestats.get("location", "N/A"),
"server_name": source.get(
"server_name", "Unspecified name"
),
"server_description": source.get(
"server_description", "Unspecified description"
),
"genre": source.get("genre", "N/A"),
"listeners": source.get("listeners", 0),
"host": host,
"listener_peak": source.get("listener_peak", 0),
"listen_url": source.get("listenurl", "#"),
"stream_start": source.get("stream_start", "N/A"),
"is_private": self.is_broadcast_private(host),
}
)
return broadcast_data
def get_scheduled_broadcast_count(self) -> int:
with open("schedule.json", "r") as f:
schedule = json.load(f)
return len(schedule)
async def get(self):
try:
broadcast_data = await self.get_active_hbni_broadcasts()
broadcast_count = self.get_active_broadcast_count(broadcast_data)
scheduled_broadcast_count = self.get_scheduled_broadcast_count()
with open("schedule.json", "r") as f:
schedule = json.load(f)
template = env.get_template("listeners_page.html")
rendered_template = template.render(
broadcast_status=broadcast_data,
schedule=schedule,
broadcast_count=broadcast_count,
scheduled_broadcast_count=scheduled_broadcast_count,
)
self.write(rendered_template)
except Exception as e:
self.set_status(500)
self.write_error(500, stack_trace=f"{str(e)} {traceback.print_exc()}")
class DownloadLinksJSONHandler(RequestHandler):
async def get(self):
audio_data = await fetch_audio_archives()
json_response = {}
for row in audio_data:
file_name = row["filename"]
file_description = row["description"]
file_date = row["date"]
file_length = row["length"]
host = row["host"]
file_id = row["id"]
download_link = row["download_link"]
json_response[file_name] = {
"date": file_date,
"description": file_description,
"downloadLink": download_link,
"length": file_length,
"host": host,
"id": file_id,
}
self.set_header("Content-Type", "application/json")
self.write(json.dumps(json_response, indent=4))
class RecordingStatusJSONHandler(RequestHandler):
def get(self):
try:
path = os.getenv("RECORDING_STATUS_PATH", r"\\Pinecone\web\HBNI Audio Stream Recorder\static\recording_status.json")
with open(path, "r", encoding="utf-8") as f:
recording_status = json.load(f)
except Exception:
recording_status = {"ERROR": "Could not load recording status JSON file."}
self.set_header("Content-Type", "application/json")
self.write(json.dumps(recording_status, indent=4))
def make_app():
return Application(
[
url(r"/", MainHandler),
url(r"/play_recording/(.*)", PlayRecordingHandler),
url(r"/broadcast_ws", BroadcastWSHandler),
url(r"/schedule_broadcast", ScheduleBroadcastHandler),
url(r"/broadcasting_page", BroadcastHandler),
url(r"/validate-password", ValidatePasswordHandler),
url(r"/listeners_page", ListenHandler),
url(r"/download_links.json", DownloadLinksJSONHandler),
url(
r"/app/static/Recordings/(.*)",
tornado.web.StaticFileHandler,
{"path": os.getenv("STATIC_RECORDINGS_PATH", "/app/static/Recordings")},
),
url(r"/recording_status.json", RecordingStatusJSONHandler),
url(r"/.*", BaseHandler),
],
static_path=os.path.join(os.path.dirname(__file__), "static"),
)
if __name__ == "__main__":
app = tornado.httpserver.HTTPServer(make_app())
app.listen(int(os.getenv("PORT", default=5053)))
tornado.ioloop.PeriodicCallback(cleanup_old_schedules, 5 * 60 * 1000).start()
tornado.ioloop.IOLoop.instance().start()