Spaces:
Running
Running
| import os | |
| import sys | |
| import subprocess | |
| import threading | |
| from http.server import SimpleHTTPRequestHandler | |
| from socketserver import TCPServer | |
| class HealthCheckHandler(SimpleHTTPRequestHandler): | |
| def do_GET(self): | |
| self.send_response(200) | |
| self.send_header("Content-type", "text/html; charset=utf-8") | |
| self.end_headers() | |
| html = """ | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>elshater77 Chess Bot</title> | |
| <style> | |
| body { font-family: sans-serif; text-align: center; padding: 50px; background-color: #121212; color: #ffffff; } | |
| h1 { color: #4CAF50; } | |
| p { font-size: 18px; color: #cccccc; } | |
| .status { display: inline-block; padding: 10px 20px; background-color: #2e7d32; border-radius: 5px; font-weight: bold; margin-top: 20px; } | |
| </style> | |
| </head> | |
| <body> | |
| <h1>♟️ elshater77 Chess Bot 🤖</h1> | |
| <p>The Stockfish-powered Lichess Bot is running successfully 24/7 on Hugging Face Spaces!</p> | |
| <div class="status">STATUS: ONLINE & ACTIVE 🟢</div> | |
| </body> | |
| </html> | |
| """ | |
| self.wfile.write(html.encode("utf-8")) | |
| def log_message(self, format, *args): | |
| # Silence HTTP logs to keep console clean | |
| pass | |
| def run_health_server(): | |
| port = int(os.environ.get("PORT", 7860)) | |
| print(f"📡 Starting background health check server on port {port}...") | |
| TCPServer.allow_reuse_address = True | |
| try: | |
| with TCPServer(("", port), HealthCheckHandler) as httpd: | |
| httpd.serve_forever() | |
| except Exception as e: | |
| print(f"⚠️ Health check server encountered an error: {e}") | |
| def main(): | |
| print("🚀 Starting elshater77 Chess Bot wrapper...") | |
| # 1. Start HTTP Health Check Server in a background thread | |
| health_thread = threading.Thread(target=run_health_server, daemon=True) | |
| health_thread.start() | |
| # 2. Retrieve Lichess token from Hugging Face Space secrets | |
| token = os.environ.get("LICHESS_BOT_TOKEN") | |
| if not token: | |
| print("❌ Error: LICHESS_BOT_TOKEN environment variable not set.") | |
| print("Please add 'LICHESS_BOT_TOKEN' as a Secret in your Hugging Face Space Settings.") | |
| print("Waiting indefinitely to prevent crash loop...") | |
| import time | |
| while True: | |
| time.sleep(3600) | |
| # 3. Read template and inject token | |
| template_path = "config.yml.template" | |
| config_path = "config.yml" | |
| if not os.path.exists(template_path): | |
| print(f"❌ Error: Template file {template_path} not found.") | |
| sys.exit(1) | |
| print("📝 Injecting credentials into config.yml...") | |
| with open(template_path, "r", encoding="utf-8") as f: | |
| config_data = f.read() | |
| config_data = config_data.replace("LICHESS_BOT_TOKEN_PLACEHOLDER", token) | |
| with open(config_path, "w", encoding="utf-8") as f: | |
| f.write(config_data) | |
| print("✅ Configuration ready. Launching lichess-bot...") | |
| # 4. Execute the official lichess-bot script | |
| try: | |
| subprocess.run([sys.executable, "lichess-bot.py"], check=True) | |
| except KeyboardInterrupt: | |
| print("\n👋 Bot stopped by user.") | |
| except Exception as e: | |
| print(f"❌ lichess-bot crashed with error: {e}") | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| main() | |