RealtimeSDWebRTC / _server.py
Jon Taylor
base setup
ad5d266
raw
history blame
3.04 kB
from flask import Flask, send_from_directory, jsonify, request
from flask_cors import CORS
from dotenv import load_dotenv
import os
import requests
import subprocess
import time
from app.auth import get_meeting_token
load_dotenv()
app = Flask(__name__, static_url_path="/", static_folder="frontend/out")
CORS(app)
def _start_bot(bot_path, args=None):
daily_api_key = os.getenv("DAILY_API_KEY") or ""
api_path = os.getenv("DAILY_API_PATH") or "https://api.daily.co/v1"
timeout = int(os.getenv("ROOM_TIMEOUT") or os.getenv("BOT_MAX_DURATION") or 300)
exp = time.time() + timeout
'''
res = requests.post(
f"{api_path}/rooms",
headers={"Authorization": f"Bearer {daily_api_key}"},
json={
"properties": {
"exp": exp,
"enable_chat": True,
"enable_emoji_reactions": True,
"eject_at_room_exp": True,
"enable_prejoin_ui": False,
}
},
)
if res.status_code != 200:
return (
jsonify(
{
"error": "Unable to create room",
"status_code": res.status_code,
"text": res.text,
}
),
500,
)
'''
room_url = os.getenv("DAILY_ROOM_URL") #res.json()["url"]
room_name = os.getenv("DAILY_ROOM_NAME") #res.json()["name"]
meeting_token = get_meeting_token(room_url, daily_api_key, exp)
if args:
extra_args = " ".join([f'-{x[0]} "{x[1]}"' for x in args])
else:
extra_args = ""
proc = subprocess.Popen(
[
f"python3 {bot_path} -u {room_url} -t {meeting_token} {extra_args}"
],
shell=True,
bufsize=1,
)
# Don't return until the bot has joined the room, but wait for at most 2 seconds.
attempts = 0
while attempts < 20:
time.sleep(0.1)
attempts += 1
res = requests.get(
f"{api_path}/rooms/{room_name}/get-session-data",
headers={"Authorization": f"Bearer {daily_api_key}"},
)
if res.status_code == 200:
break
print(f"Took {attempts} attempts to join room {room_name}")
return jsonify({"room_url": room_url, "token": meeting_token}), 200
# Routes
#@app.route("/start-bot", methods=["POST"])
#def start_bot():
# return _start_bot("./app/bot.py")
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
full_path = os.path.join(app.static_folder, path)
# Check if path is a directory and index.html exists
if os.path.isdir(full_path) and os.path.exists(os.path.join(full_path, 'index.html')):
return send_from_directory(full_path, 'index.html')
# Check if path.html exists
elif os.path.exists(full_path + '.html'):
return send_from_directory(app.static_folder, path + '.html')
# Serve index.html by default
else:
return send_from_directory(app.static_folder, 'index.html')