from http.server import HTTPServer, BaseHTTPRequestHandler import subprocess import json import os import pickle import base64 # Load YouTube token from environment token_b64 = os.environ.get('YOUTUBE_TOKEN') if token_b64: try: token = pickle.loads(base64.b64decode(token_b64)) with open('/app/token.pickle', 'wb') as f: pickle.dump(token, f) print("Token loaded successfully!") except Exception as e: print(f"Token error: {e}") class Handler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.end_headers() self.wfile.write(b'{"status": "ShortBot running!"}') def do_POST(self): try: content_length = self.headers.get('Content-Length') if not content_length: self.send_response(400) self.end_headers() self.wfile.write(b'{"error": "No Content-Length"}') return length = int(content_length) body = json.loads(self.rfile.read(length)) # FB Mirror endpoint if self.path == '/fb-mirror': video_url = body.get('video_url', '') title = body.get('title', 'Facebook Short') description = body.get('description', '') + '\n#Shorts' fb_path = '/tmp/fb_input.mp4' result = subprocess.run([ 'yt-dlp', '--no-check-certificate', '--user-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36', '-o', fb_path, '-f', 'best[ext=mp4]/best', '--no-playlist', video_url ], capture_output=True, text=True) print("yt-dlp stdout:", result.stdout) print("yt-dlp stderr:", result.stderr) if result.returncode != 0: self.send_response(500) self.end_headers() self.wfile.write(f'{{"error": "yt-dlp failed: {result.stderr[:200]}"}}'.encode()) return subprocess.Popen([ 'python', '/app/upload.py', fb_path, title, description ]) self.send_response(200) self.end_headers() self.wfile.write(b'{"status": "fb mirror started"}') return # Original flow script = body.get('script', '') title = body.get('title', 'YouTube Short') description = body.get('description', '') subprocess.Popen([ 'python', '/app/generate.py', script, title, description ]) self.send_response(200) self.end_headers() self.wfile.write(b'{"status": "video generation started"}') except Exception as e: self.send_response(500) self.end_headers() self.wfile.write(f'{{"error": "{str(e)}"}}'.encode()) def log_message(self, format, *args): pass print("ShortBot server running on port 7860...") HTTPServer(('0.0.0.0', 7860), Handler).serve_forever()