58 lines
1.9 KiB
Python
58 lines
1.9 KiB
Python
import asyncio
|
|
import json
|
|
from fastapi import FastAPI, WebSocket
|
|
from fastapi.responses import HTMLResponse
|
|
from starlette.websockets import WebSocketDisconnect
|
|
|
|
from chat_listeners import listen_youtube_chat, listen_twitch_chat
|
|
from auth import router as auth_router # Import the auth router
|
|
|
|
app = FastAPI()
|
|
|
|
app.include_router(auth_router, prefix="/auth") # Include the auth router
|
|
|
|
connected_clients = []
|
|
|
|
async def broadcast_message(message: dict):
|
|
# Convert the message dictionary to a JSON string before sending
|
|
message_json = json.dumps(message)
|
|
for client in connected_clients:
|
|
try:
|
|
await client.send_text(message_json)
|
|
except RuntimeError:
|
|
# Handle cases where client might have disconnected
|
|
connected_clients.remove(client)
|
|
|
|
@app.on_event("startup")
|
|
async def startup_event():
|
|
# Start chat listeners in the background
|
|
# Replace with actual video ID and Twitch token/channel
|
|
# For now, using placeholders. These will need to be configured.
|
|
asyncio.create_task(listen_youtube_chat("YOUR_YOUTUBE_VIDEO_ID", broadcast_message))
|
|
asyncio.create_task(listen_twitch_chat("YOUR_TWITCH_OAUTH_TOKEN", "YOUR_TWITCH_CHANNEL", broadcast_message))
|
|
|
|
@app.get("/")
|
|
async def read_root():
|
|
return {"Hello": "World"}
|
|
|
|
@app.get("/login", response_class=HTMLResponse)
|
|
async def get_login_page():
|
|
with open("login.html", "r") as f:
|
|
return f.read()
|
|
|
|
@app.get("/overlay", response_class=HTMLResponse)
|
|
async def get_overlay():
|
|
with open("index.html", "r") as f:
|
|
return f.read()
|
|
|
|
@app.websocket("/ws")
|
|
async def websocket_endpoint(websocket: WebSocket):
|
|
await websocket.accept()
|
|
connected_clients.append(websocket)
|
|
try:
|
|
while True:
|
|
# Keep the connection alive, or handle incoming messages if needed
|
|
await websocket.receive_text()
|
|
except WebSocketDisconnect:
|
|
connected_clients.remove(websocket)
|