Spaces:
Running
Running
Update main.py
Browse files718 unmodified lines7197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547551 unmodified line7577587597607617627637647657662 unmodified lines769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823718 unmodified lines</html>"""# --- ADDED FOR /last_action --- START ---@app.get("/last_action", response_class=HTMLResponse)async def get_last_action():"""Serves a simple HTML page displaying the content of last_action.txt."""file_content_raw = ""error_message = None# --- Add Debugging ---print("--- Debugging /last_action ---")current_cwd = os.getcwd()print(f"Current Working Directory: {current_cwd}")absolute_path = os.path.abspath(LAST_ACTION_FILE)print(f"Attempting to read absolute path: {absolute_path}")print(f"Checking existence with os.path.exists: {os.path.exists(absolute_path)}")try:print(f"Listing contents of CWD ({current_cwd}): {os.listdir(current_cwd)}")except Exception as list_err:print(f"Could not list CWD: {list_err}")try:# Explicitly check /app if CWD is differentif current_cwd != '/app':print(f"Listing contents of /app: {os.listdir('/app')}")except Exception as list_err:print(f"Could not list /app: {list_err}")print("--- End Debugging ---")# --- End Debugging ---try:# Read the file content fresh on each requestwith open(LAST_ACTION_FILE, "r", encoding="utf-8") as f:file_content_raw = f.read()except FileNotFoundError:error_message = f"Error: The file '{LAST_ACTION_FILE}' was not found in the application directory."print(f"WARN: {error_message}") # Log server-sideexcept Exception as e:error_message = f"An unexpected error occurred while reading '{LAST_ACTION_FILE}': {e}"1 unmodified linetraceback.print_exc() # Log full traceback for debugging# Escape the raw content to prevent XSS if the file contains HTML/JS# Display error message if reading faileddisplay_content = html.escape(file_content_raw) if not error_message else error_message# Create the simple HTML responsehtml_output = f"""<!DOCTYPE html><html lang="en">2 unmodified lines<meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Last Action Log</title><style>body {{font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;line-height: 1.6;padding: 25px;background-color: #f8f9fa;color: #212529;margin: 0;}}h1 {{color: #0d6efd; /* Bootstrap primary blue */border-bottom: 2px solid #dee2e6;padding-bottom: 10px;margin-top: 0;}}pre {{background-color: #ffffff;border: 1px solid #ced4da;border-radius: 5px;padding: 20px;white-space: pre-wrap; /* Allows text to wrap */word-wrap: break-word; /* Breaks long words */font-family: Consolas, 'Courier New', monospace;font-size: 0.95em;box-shadow: 0 2px 4px rgba(0,0,0,0.05);margin-top: 20px;color: #343a40; /* Darker text for contrast */}}.error {{color: #dc3545; /* Bootstrap danger red */font-weight: bold;background-color: #f8d7da; /* Light red background */border: 1px solid #f5c2c7;padding: 15px;border-radius: 5px;}}</style></head><body><h1>Last Recorded Action</h1>{'<p class="error">' + error_message + '</p>' if error_message else '<pre>' + display_content + '</pre>'}<script>// Optional: Auto-refresh the page every N seconds// setTimeout(() => window.location.reload(), 5000); // Refresh every 5 seconds</script></body></html>"""return HTMLResponse(content=html_output)# --- ADDED FOR /last_action --- END ---@app.websocket("/ws")718 unmodified lines7197207217227237247257267277287297307317327337347357367377381 unmodified line7407417427437447457467477487497502 unmodified lines753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845718 unmodified lines</html>"""@app.get("/last_action", response_class=HTMLResponse)async def get_last_action():"""Serves a simple HTML page displaying the content of last_action.txt,styled for OBS integration."""file_content_raw = ""error_message = Nonetry:# Read the file content fresh on each requestwith open(LAST_ACTION_FILE, "r", encoding="utf-8") as f:file_content_raw = f.read()except FileNotFoundError:error_message = f"Error: File '{LAST_ACTION_FILE}' not found."print(f"WARN: {error_message}") # Log server-sideexcept Exception as e:error_message = f"An unexpected error occurred while reading '{LAST_ACTION_FILE}': {e}"1 unmodified linetraceback.print_exc() # Log full traceback for debugging# Escape the raw content to prevent XSS if the file contains HTML/JSdisplay_content = html.escape(file_content_raw) if not error_message else error_message# Use a class to differentiate normal content from error messages for stylingcontent_class = "error" if error_message else "log-content"# Create the simple HTML response with updated styles for OBShtml_output = f"""<!DOCTYPE html><html lang="en">2 unmodified lines<meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Last Action Log</title><style>/* Import fonts to potentially match the main stream interface *//* You already import Poppins and Press Start 2P in the main '/' route's HTML *//* No extra import needed here if loaded by the browser already, but safe to include */@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;700&family=Press+Start+2P&display=swap');/* Basic Reset */* {{box-sizing: border-box;margin: 0;padding: 0;}}/* Ensure html and body take full height/width and hide overflow */html, body {{height: 100%;width: 100%;overflow: hidden; /* Crucial for OBS Browser Source */}}body {{/* Use a font that matches your stream's text */font-family: 'Poppins', sans-serif;/* Fallback font *//* font-family: Consolas, 'Courier New', monospace; */line-height: 1.5; /* Adjust line spacing if needed */padding: 15px; /* Add some padding inside the OBS box *//* Make background transparent for seamless overlay in OBS */background-color: transparent;/* Set default text color to white for dark OBS backgrounds */color: #FFFFFF;/* Center the text block */display: flex;justify-content: center;align-items: center;text-align: center;}}/* Wrapper for content, useful for centering and potential future overflow */.content-wrapper {{max-width: 100%;max-height: 100%;/* Optional: Add scroll if content exceeds OBS source size *//* overflow-y: auto; */}}/* Style for the main action text */.log-content {{/* Significantly larger font size */font-size: 2em; /* Example: Adjust as needed *//* Alternative responsive size based on OBS source width: *//* font-size: calc(12px + 1.8vw); */white-space: pre-wrap; /* Allow text wrapping */word-wrap: break-word; /* Break long words */color: #EAEAEA; /* Slightly off-white *//* Optional: Add subtle shadow for better readability on complex backgrounds */text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.7);}}/* Style for error messages */.error {{font-family: 'Poppins', sans-serif; /* Keep font consistent */font-size: 1.6em; /* Make errors large but slightly smaller than normal */color: #FFBDBD; /* Light red text, visible on dark */font-weight: bold;background-color: rgba(100, 0, 0, 0.7); /* Dark red, semi-transparent */border: 1px solid #FF8080; /* Lighter red border */padding: 10px 15px;border-radius: 8px;text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.8);white-space: normal; /* Allow errors to wrap normally */}}</style></head><body><div class="content-wrapper"><div class="{content_class}">{display_content}</div></div></body></html>"""return HTMLResponse(content=html_output)@app.websocket("/ws")
main.py+81-59