Update pagebot.py

This commit is contained in:
majmongoose
2026-08-23 08:43:48 -04:00
parent b2fb924450
commit 239f432fb1
+35 -26
View File
@@ -8,6 +8,7 @@ import secrets_file
import threading
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import shutil
import speech_recognition as sr
def wait_for_file_ready(filepath, stable_seconds=1, timeout=60):
@@ -62,31 +63,32 @@ class MyHandler(FileSystemEventHandler):
try:
print("New MP3!")
wait_for_file_ready(filepath)
text = ""
if secrets_file.speech_to_text:
print("Converting To Text and MP4")
text_result = [""]
def do_text():
text_result[0] = convert_to_text(filepath, filename)
text_thread = threading.Thread(target=do_text)
text_thread.start()
mp4_file = convert_to_mp4(filepath, delete_source=False)
text_thread.join()
text = text_result[0]
try:
os.remove(filepath)
except OSError:
pass
else:
print("Converting to MP4")
mp4_file = convert_to_mp4(filepath)
mp3_copy = f"{filename}_stt.mp3"
shutil.copy2(filepath, mp3_copy)
print("Converting to MP4")
mp4_file = convert_to_mp4(filepath)
print("Sending to Discord")
asyncio.run_coroutine_threadsafe(upload_to_discord(mp4_file, text), client.loop)
asyncio.run_coroutine_threadsafe(upload_to_discord(mp4_file), client.loop)
if secrets_file.speech_to_text:
print("Transcribing in background")
mp4_basename = os.path.basename(mp4_file) if mp4_file else os.path.basename(filepath)
def do_transcribe():
text = convert_to_text(mp3_copy, filename)
try:
os.remove(mp3_copy)
except OSError:
pass
if text:
asyncio.run_coroutine_threadsafe(
send_transcript(mp4_basename, text), client.loop
)
threading.Thread(target=do_transcribe, daemon=True).start()
finally:
with _processing_lock:
_processing_files.discard(filepath)
def convert_to_mp4(mp3_file, delete_source=True):
def convert_to_mp4(mp3_file):
try:
mp4_file = os.path.splitext(mp3_file)[0] + '.mp4'
subprocess.run([
@@ -97,8 +99,7 @@ def convert_to_mp4(mp3_file, delete_source=True):
'-c:v', 'libx264', '-preset', 'ultrafast', '-tune', 'stillimage', '-pix_fmt', 'yuv420p',
'-shortest', mp4_file
])
if delete_source:
os.remove(mp3_file)
os.remove(mp3_file)
return mp4_file
except Exception as e:
print(f"Error during conversion: {e}")
@@ -133,7 +134,7 @@ def parse_dispatch_filename(filename):
timestamp = f"{month}/{day}/{year} {hour}:{minute}:{second}"
return station_number, station_name, timestamp
async def upload_to_discord(mp4_file, text):
async def upload_to_discord(mp4_file):
if mp4_file is None:
print("Conversion failed. Skipping upload.")
return
@@ -142,12 +143,9 @@ async def upload_to_discord(mp4_file, text):
filename = os.path.basename(mp4_file)
station_number, station_name, timestamp = parse_dispatch_filename(filename)
description = timestamp if timestamp else ""
if text:
description += f"\n\n{text}" if description else text
embed = discord.Embed(
title=f"Station {station_number} - {station_name}",
description=description,
description=timestamp,
color=0xFF0000
)
@@ -169,6 +167,17 @@ async def upload_to_discord(mp4_file, text):
else:
print(f"Could not find channel with ID {secrets_file.channel_id}")
async def send_transcript(filename, text):
channel = client.get_channel(secrets_file.channel_id)
if channel:
station_number, station_name, timestamp = parse_dispatch_filename(filename)
embed = discord.Embed(
title=f"Transcript - Station {station_number} - {station_name}",
description=text,
color=0x3498db
)
await channel.send(embed=embed)
TTD_TIMEOUT = getattr(secrets_file, 'ttd_timeout', 900)
def kill_process_tree(pid):