File size: 3,260 Bytes
ea46f56 e4a12ea ea46f56 94905e0 ea46f56 94905e0 ea46f56 94905e0 fe243df 94905e0 e4a12ea 0f35c56 75d5330 0f35c56 75d5330 0f35c56 e4a12ea 94905e0 e4a12ea 94905e0 e4a12ea 94905e0 e4a12ea 94905e0 e4a12ea 0f35c56 94905e0 e4a12ea ea46f56 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | 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() |