Update server.py
Browse files
server.py
CHANGED
|
@@ -5,7 +5,7 @@ import os
|
|
| 5 |
import pickle
|
| 6 |
import base64
|
| 7 |
|
| 8 |
-
# Load YouTube token
|
| 9 |
token_b64 = os.environ.get('YOUTUBE_TOKEN')
|
| 10 |
if token_b64:
|
| 11 |
try:
|
|
@@ -17,6 +17,7 @@ if token_b64:
|
|
| 17 |
print(f"Token error: {e}")
|
| 18 |
|
| 19 |
class Handler(BaseHTTPRequestHandler):
|
|
|
|
| 20 |
def do_GET(self):
|
| 21 |
self.send_response(200)
|
| 22 |
self.end_headers()
|
|
@@ -24,70 +25,50 @@ class Handler(BaseHTTPRequestHandler):
|
|
| 24 |
|
| 25 |
def do_POST(self):
|
| 26 |
try:
|
| 27 |
-
|
| 28 |
-
if not content_length:
|
| 29 |
-
self.send_response(400)
|
| 30 |
-
self.end_headers()
|
| 31 |
-
self.wfile.write(b'{"error": "No Content-Length"}')
|
| 32 |
-
return
|
| 33 |
-
length = int(content_length)
|
| 34 |
body = json.loads(self.rfile.read(length))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
video_url = body.get('video_url', '')
|
| 39 |
-
title = body.get('title', 'Facebook Short')
|
| 40 |
-
description = body.get('description', '') + '\n#Shorts'
|
| 41 |
-
|
| 42 |
-
fb_path = '/tmp/fb_input.mp4'
|
| 43 |
-
|
| 44 |
-
result = subprocess.run([
|
| 45 |
-
'yt-dlp',
|
| 46 |
-
'--no-check-certificate',
|
| 47 |
-
'--user-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
| 48 |
-
'-o', fb_path,
|
| 49 |
-
'-f', 'best[ext=mp4]/best',
|
| 50 |
-
'--no-playlist',
|
| 51 |
-
video_url
|
| 52 |
-
], capture_output=True, text=True)
|
| 53 |
-
|
| 54 |
-
print("yt-dlp stdout:", result.stdout)
|
| 55 |
-
print("yt-dlp stderr:", result.stderr)
|
| 56 |
-
|
| 57 |
-
if result.returncode != 0:
|
| 58 |
-
self.send_response(500)
|
| 59 |
-
self.end_headers()
|
| 60 |
-
self.wfile.write(f'{{"error": "yt-dlp failed: {result.stderr[:200]}"}}'.encode())
|
| 61 |
return
|
| 62 |
|
|
|
|
| 63 |
subprocess.Popen([
|
| 64 |
-
'python', '/app/
|
| 65 |
-
|
| 66 |
])
|
| 67 |
|
| 68 |
-
self.
|
| 69 |
-
self.end_headers()
|
| 70 |
-
self.wfile.write(b'{"status": "fb mirror started"}')
|
| 71 |
-
return
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
title = body.get('title', 'YouTube Short')
|
| 76 |
-
description = body.get('description', '')
|
| 77 |
-
|
| 78 |
-
subprocess.Popen([
|
| 79 |
-
'python', '/app/generate.py',
|
| 80 |
-
script, title, description
|
| 81 |
-
])
|
| 82 |
-
|
| 83 |
-
self.send_response(200)
|
| 84 |
-
self.end_headers()
|
| 85 |
-
self.wfile.write(b'{"status": "video generation started"}')
|
| 86 |
|
| 87 |
except Exception as e:
|
| 88 |
-
self.
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
def log_message(self, format, *args):
|
| 93 |
pass
|
|
|
|
| 5 |
import pickle
|
| 6 |
import base64
|
| 7 |
|
| 8 |
+
# Load YouTube token (Channel B - plipStory)
|
| 9 |
token_b64 = os.environ.get('YOUTUBE_TOKEN')
|
| 10 |
if token_b64:
|
| 11 |
try:
|
|
|
|
| 17 |
print(f"Token error: {e}")
|
| 18 |
|
| 19 |
class Handler(BaseHTTPRequestHandler):
|
| 20 |
+
|
| 21 |
def do_GET(self):
|
| 22 |
self.send_response(200)
|
| 23 |
self.end_headers()
|
|
|
|
| 25 |
|
| 26 |
def do_POST(self):
|
| 27 |
try:
|
| 28 |
+
length = int(self.headers['Content-Length'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
body = json.loads(self.rfile.read(length))
|
| 30 |
+
path = self.path
|
| 31 |
+
|
| 32 |
+
# ββ Existing: generate Short from script ββββββββββββββββββ
|
| 33 |
+
if path == '/' or path == '':
|
| 34 |
+
script = body.get('script', '')
|
| 35 |
+
title = body.get('title', 'YouTube Short')
|
| 36 |
+
description = body.get('description', '')
|
| 37 |
+
|
| 38 |
+
print(f"Generating video for: {title}")
|
| 39 |
+
subprocess.Popen(['python', '/app/generate.py', script, title, description])
|
| 40 |
+
|
| 41 |
+
self._respond(200, '{"status": "video generation started"}')
|
| 42 |
+
|
| 43 |
+
# ββ NEW: mirror a YouTube video to Channel B βββββββββββββββ
|
| 44 |
+
elif path == '/mirror':
|
| 45 |
+
video_id = body.get('video_id', '')
|
| 46 |
+
title = body.get('title', 'Mirrored Video')
|
| 47 |
+
description = body.get('description', '')
|
| 48 |
|
| 49 |
+
if not video_id:
|
| 50 |
+
self._respond(400, '{"error": "video_id is required"}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
return
|
| 52 |
|
| 53 |
+
print(f"Mirroring video_id: {video_id}")
|
| 54 |
subprocess.Popen([
|
| 55 |
+
'python', '/app/mirror.py',
|
| 56 |
+
video_id, title, description
|
| 57 |
])
|
| 58 |
|
| 59 |
+
self._respond(200, f'{{"status": "mirror started", "video_id": "{video_id}"}}')
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
else:
|
| 62 |
+
self._respond(404, '{"error": "not found"}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
except Exception as e:
|
| 65 |
+
self._respond(500, f'{{"error": "{str(e)}"}}')
|
| 66 |
+
|
| 67 |
+
def _respond(self, code, body):
|
| 68 |
+
self.send_response(code)
|
| 69 |
+
self.send_header('Content-Type', 'application/json')
|
| 70 |
+
self.end_headers()
|
| 71 |
+
self.wfile.write(body.encode())
|
| 72 |
|
| 73 |
def log_message(self, format, *args):
|
| 74 |
pass
|