2024-03-25 14:28:34 -04:00
|
|
|
#!/usr/bin/python3
|
2024-02-10 09:48:35 -05:00
|
|
|
import discord
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import time
|
|
|
|
import secrets_file
|
2024-02-15 14:06:49 -05:00
|
|
|
import threading
|
2024-10-25 10:02:45 -04:00
|
|
|
from datetime import datetime
|
2024-02-10 09:48:35 -05:00
|
|
|
from watchdog.observers import Observer
|
|
|
|
from watchdog.events import FileSystemEventHandler
|
2024-02-12 12:01:13 -05:00
|
|
|
import speech_recognition as sr
|
2024-02-10 09:48:35 -05:00
|
|
|
|
2024-02-15 15:29:49 -05:00
|
|
|
## New File Handler
|
2024-02-10 09:48:35 -05:00
|
|
|
class MyHandler(FileSystemEventHandler):
|
|
|
|
def on_created(self, event):
|
|
|
|
if event.is_directory:
|
|
|
|
return
|
|
|
|
filepath = event.src_path
|
|
|
|
filename, file_extension = os.path.splitext(filepath)
|
2024-02-12 12:01:13 -05:00
|
|
|
if file_extension.lower() == '.amr':
|
|
|
|
time.sleep(10)
|
|
|
|
os.remove(filepath)
|
|
|
|
print("Removing AMR.")
|
2024-02-10 09:48:35 -05:00
|
|
|
if file_extension.lower() == '.mp3':
|
2024-02-10 09:50:24 -05:00
|
|
|
print("New MP3!")
|
2024-02-12 12:01:13 -05:00
|
|
|
text = ""
|
|
|
|
if secrets_file.speech_to_text:
|
2024-02-12 13:57:42 -05:00
|
|
|
print("Converting To Text")
|
2024-02-12 14:05:40 -05:00
|
|
|
text = convert_to_text(filepath,filename)
|
2024-02-12 13:57:42 -05:00
|
|
|
print("Converting to MP4")
|
2024-02-10 09:48:35 -05:00
|
|
|
mp4_file = convert_to_mp4(filepath)
|
2024-02-12 13:57:42 -05:00
|
|
|
print("Sending to Discord")
|
2024-03-25 14:28:34 -04:00
|
|
|
client.loop.create_task(upload_to_discord(mp4_file,text))
|
2024-02-10 09:48:35 -05:00
|
|
|
|
2024-02-15 15:29:49 -05:00
|
|
|
## Convert MP3 to MP4
|
2024-02-10 09:48:35 -05:00
|
|
|
def convert_to_mp4(mp3_file):
|
|
|
|
try:
|
2024-02-12 12:01:13 -05:00
|
|
|
time.sleep(10)
|
2024-02-10 09:48:35 -05:00
|
|
|
mp4_file = os.path.splitext(mp3_file)[0] + '.mp4'
|
2024-03-02 11:52:02 -05:00
|
|
|
command = f'ffmpeg -loop 1 -i "{secrets_file.image_path}" -i "{mp3_file}" -c:a aac -b:a 192k -c:v libx264 -pix_fmt yuv420p -shortest "{mp4_file}"'
|
2024-02-10 09:48:35 -05:00
|
|
|
subprocess.run(command, shell=True)
|
2024-02-15 15:29:49 -05:00
|
|
|
os.remove(mp3_file)
|
2024-02-10 09:48:35 -05:00
|
|
|
return mp4_file
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Error during conversion: {e}")
|
|
|
|
return None
|
2024-02-15 15:29:49 -05:00
|
|
|
|
|
|
|
## Send audio to interpreter
|
2024-02-12 14:05:40 -05:00
|
|
|
def convert_to_text(mp3_path,mp3_name):
|
|
|
|
print(mp3_path)
|
2024-02-12 13:57:42 -05:00
|
|
|
try:
|
2024-02-13 07:03:42 -05:00
|
|
|
command = f'ffmpeg -i "{mp3_path}" "{mp3_name}".wav'
|
2024-02-12 13:57:42 -05:00
|
|
|
subprocess.run(command, shell=True)
|
|
|
|
r = sr.Recognizer()
|
|
|
|
# Load the audio file
|
2024-02-12 14:05:40 -05:00
|
|
|
with sr.AudioFile(f"{mp3_name}.wav") as source:
|
2024-02-12 13:57:42 -05:00
|
|
|
data = r.record(source)
|
|
|
|
# Convert speech to text
|
|
|
|
text = r.recognize_google(data)
|
2024-02-13 07:03:42 -05:00
|
|
|
os.remove(f"{mp3_name}.wav")
|
2024-02-12 13:57:42 -05:00
|
|
|
return (text)
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Error during conversion: {e}")
|
|
|
|
return ""
|
|
|
|
|
2024-03-25 14:28:34 -04:00
|
|
|
|
|
|
|
|
2024-02-10 09:48:35 -05:00
|
|
|
## upload to discord
|
2024-02-12 12:01:13 -05:00
|
|
|
async def upload_to_discord(mp4_file,text):
|
2024-02-10 09:48:35 -05:00
|
|
|
## Check to make sure conversion worked.
|
|
|
|
if mp4_file is None:
|
|
|
|
print("Conversion failed. Skipping upload.")
|
|
|
|
return
|
|
|
|
## Get channel to send in
|
2024-03-25 14:28:34 -04:00
|
|
|
channel = client.get_channel(secrets_file.channel_id)
|
2024-02-10 09:48:35 -05:00
|
|
|
if channel:
|
|
|
|
filename = os.path.basename(mp4_file)
|
|
|
|
## Send Video with name
|
|
|
|
with open(mp4_file, 'rb') as f:
|
|
|
|
await channel.send(filename,file=discord.File(f))
|
2024-02-12 12:01:13 -05:00
|
|
|
if secrets_file.delete_after_upload:
|
|
|
|
os.remove(mp4_file)
|
|
|
|
## Send transcribed voice if present.
|
|
|
|
if (text != ""):
|
|
|
|
await channel.send(f"The following text was transcoded from the recording: \n{text}")
|
2024-02-10 09:48:35 -05:00
|
|
|
## Ping users with the appropriate number
|
|
|
|
role_name = filename.split('-', 1)[0].strip()
|
|
|
|
role = discord.utils.get(channel.guild.roles, name=role_name)
|
|
|
|
if role:
|
2024-02-24 07:51:20 -05:00
|
|
|
await channel.send(f"{role.mention} {secrets_file.notify_text}")
|
2024-02-10 09:48:35 -05:00
|
|
|
else:
|
|
|
|
print(f"Role '{role_name}' not found.")
|
|
|
|
else:
|
|
|
|
print(f"Could not find channel with ID {channel}")
|
|
|
|
|
2024-10-25 10:33:36 -04:00
|
|
|
|
2024-03-25 14:28:34 -04:00
|
|
|
def launch_and_watch(program_path):
|
2024-02-15 14:03:56 -05:00
|
|
|
program_directory = os.path.dirname(program_path)
|
2024-10-25 10:02:45 -04:00
|
|
|
restart_time = secrets_file.restart_time # Format: "HH:MM" or None
|
|
|
|
|
|
|
|
if restart_time:
|
|
|
|
restart_hour, restart_minute = map(int, restart_time.split(":"))
|
2024-10-25 10:33:36 -04:00
|
|
|
|
|
|
|
restart_triggered = False # Flag to prevent multiple restarts within the same minute
|
2024-10-25 10:45:38 -04:00
|
|
|
process = None # Initialize the process variable outside the loop
|
2024-10-25 10:02:45 -04:00
|
|
|
|
2024-02-15 13:55:23 -05:00
|
|
|
while True:
|
2024-10-25 10:45:38 -04:00
|
|
|
# If a process is already running, terminate it before starting a new one
|
|
|
|
if process and process.poll() is None:
|
|
|
|
print("Terminating the existing TTD process before restarting...")
|
|
|
|
process.terminate()
|
|
|
|
process.wait()
|
|
|
|
|
2024-10-25 10:02:45 -04:00
|
|
|
# Launch the TTD program
|
2024-10-25 10:33:36 -04:00
|
|
|
print("Starting TTD program...")
|
2024-10-25 10:02:45 -04:00
|
|
|
process = subprocess.Popen(program_path, cwd=program_directory)
|
2024-03-25 14:28:34 -04:00
|
|
|
|
2024-10-25 10:02:45 -04:00
|
|
|
while True:
|
|
|
|
if restart_time:
|
|
|
|
current_time = datetime.now()
|
2024-10-25 10:33:36 -04:00
|
|
|
|
|
|
|
# Check if it's the restart time and restart hasn't been triggered in the current minute
|
|
|
|
if (current_time.hour == restart_hour and current_time.minute == restart_minute and not restart_triggered):
|
2024-10-25 10:02:45 -04:00
|
|
|
print(f"Scheduled restart time reached ({restart_time}). Restarting TTD...")
|
2024-10-25 10:33:36 -04:00
|
|
|
process.terminate()
|
|
|
|
process.wait()
|
|
|
|
restart_triggered = True # Set flag to prevent multiple restarts in the same minute
|
2024-10-25 10:02:45 -04:00
|
|
|
break # Exit inner loop to relaunch TTD
|
2024-10-25 10:33:36 -04:00
|
|
|
|
|
|
|
# Reset the flag once we're past the restart minute
|
|
|
|
if current_time.minute != restart_minute:
|
|
|
|
restart_triggered = False
|
2024-10-25 10:02:45 -04:00
|
|
|
|
|
|
|
# Check if TTD has crashed
|
|
|
|
if process.poll() is not None:
|
|
|
|
print("TTD has crashed. Relaunching...")
|
|
|
|
break # Exit inner loop to relaunch TTD
|
|
|
|
|
|
|
|
# Sleep for a short interval before checking again
|
|
|
|
time.sleep(60) # Check once every minute
|
|
|
|
|
|
|
|
# Wait for a few seconds before relaunching after crash or scheduled restart
|
2024-10-25 10:33:36 -04:00
|
|
|
print("Waiting before relaunching TTD...")
|
2024-02-15 15:29:49 -05:00
|
|
|
time.sleep(2)
|
2024-02-15 13:55:23 -05:00
|
|
|
|
2024-10-25 10:45:38 -04:00
|
|
|
|
2024-02-10 09:48:35 -05:00
|
|
|
if __name__ == "__main__":
|
2024-03-25 14:28:34 -04:00
|
|
|
|
|
|
|
## initialize watchdogs
|
2024-02-10 09:48:35 -05:00
|
|
|
event_handler = MyHandler()
|
|
|
|
observer = Observer()
|
|
|
|
observer.schedule(event_handler, secrets_file.watch_folder, recursive=False)
|
|
|
|
observer.start()
|
|
|
|
|
2024-03-25 14:28:34 -04:00
|
|
|
## Launch TTD
|
|
|
|
if (secrets_file.ttd_path != ""):
|
|
|
|
watchdog_thread = threading.Thread(target=launch_and_watch, args=(secrets_file.ttd_path,))
|
|
|
|
watchdog_thread.start()
|
2024-02-15 13:55:23 -05:00
|
|
|
|
2024-03-25 14:28:34 -04:00
|
|
|
## initialize discord
|
|
|
|
intents = discord.Intents.default()
|
|
|
|
intents.message_content = True
|
|
|
|
client = discord.Client(intents=intents)
|
|
|
|
client.run(secrets_file.key)
|
|
|
|
|
2024-02-10 09:48:35 -05:00
|
|
|
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
time.sleep(1)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
observer.stop()
|
|
|
|
observer.join()
|
|
|
|
|
2024-03-25 14:28:34 -04:00
|
|
|
##discord stuffs
|
|
|
|
@client.event
|
2024-02-10 09:48:35 -05:00
|
|
|
async def on_ready():
|
2024-03-25 14:28:34 -04:00
|
|
|
print(f'We have logged in as {client.user}')
|
|
|
|
|
|
|
|
|