Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import time
|
|
| 3 |
import subprocess
|
| 4 |
import threading
|
| 5 |
import psutil
|
| 6 |
-
from flask import Flask
|
| 7 |
|
| 8 |
TIKTOK_USERNAME = "@abdulaziz999989"
|
| 9 |
STREAM_KEY = "p3dq-5cex-bc1k-vet7-bsyx"
|
|
@@ -11,6 +10,7 @@ FFMPEG_LOG = "/tmp/ffmpeg.log"
|
|
| 11 |
MONITOR_LOG = "/tmp/monitor.log"
|
| 12 |
LOCK_FILE = "/tmp/app_monitor_core.lock"
|
| 13 |
|
|
|
|
| 14 |
TIKTOK_COOKIES = os.getenv("TIKTOK_COOKIES", "")
|
| 15 |
TIKTOK_COOKIES_ADULT = os.getenv("TIKTOK_COOKIES_ADULT", "")
|
| 16 |
|
|
@@ -94,10 +94,8 @@ def start_stream(stream_key, stream_url):
|
|
| 94 |
"-flags", "+global_header",
|
| 95 |
"-i", stream_url,
|
| 96 |
|
| 97 |
-
# فيديو: نسخ بدون أي تغيير
|
| 98 |
"-c:v", "copy",
|
| 99 |
|
| 100 |
-
# صوت: ترميز AAC 192kbps مع تنقية
|
| 101 |
"-c:a", "aac",
|
| 102 |
"-b:a", "192k",
|
| 103 |
"-ar", "44100",
|
|
@@ -106,7 +104,6 @@ def start_stream(stream_key, stream_url):
|
|
| 106 |
"-af", "highpass=f=60,lowpass=f=17000,aresample=async=1:first_pts=0",
|
| 107 |
"-async", "1",
|
| 108 |
|
| 109 |
-
# إصلاح التزامن (يمنع تعليق "جارٍ تحضير البث")
|
| 110 |
"-start_at_zero",
|
| 111 |
"-copyts",
|
| 112 |
"-vsync", "2",
|
|
@@ -126,19 +123,7 @@ def start_stream(stream_key, stream_url):
|
|
| 126 |
log(f"✅ ffmpeg PID: {proc.pid}")
|
| 127 |
return proc
|
| 128 |
|
| 129 |
-
current_status = {
|
| 130 |
-
"live": False,
|
| 131 |
-
"pid": None,
|
| 132 |
-
"start_time": "",
|
| 133 |
-
"last_check": "",
|
| 134 |
-
"cpu_load": "?",
|
| 135 |
-
"ram": "?",
|
| 136 |
-
"ffmpeg_cpu": "?",
|
| 137 |
-
}
|
| 138 |
-
|
| 139 |
def monitor_loop():
|
| 140 |
-
global current_status
|
| 141 |
-
|
| 142 |
try:
|
| 143 |
subprocess.run(["killall", "-9", "ffmpeg", "yt-dlp", "streamlink"], stderr=subprocess.DEVNULL)
|
| 144 |
except:
|
|
@@ -163,7 +148,7 @@ def monitor_loop():
|
|
| 163 |
consecutive_fails = 0
|
| 164 |
ffmpeg_restart_count = 0
|
| 165 |
|
| 166 |
-
log("🚀 محرك النقل الشفاف (
|
| 167 |
|
| 168 |
while True:
|
| 169 |
try:
|
|
@@ -184,8 +169,6 @@ def monitor_loop():
|
|
| 184 |
ffmpeg_proc = None
|
| 185 |
is_streaming = False
|
| 186 |
was_streaming = False
|
| 187 |
-
current_status["live"] = False
|
| 188 |
-
current_status["pid"] = None
|
| 189 |
time.sleep(300)
|
| 190 |
ffmpeg_restart_count = 0
|
| 191 |
continue
|
|
@@ -200,9 +183,6 @@ def monitor_loop():
|
|
| 200 |
was_streaming = True
|
| 201 |
consecutive_fails = 0
|
| 202 |
ffmpeg_restart_count = 0
|
| 203 |
-
current_status["live"] = True
|
| 204 |
-
current_status["pid"] = ffmpeg_proc.pid
|
| 205 |
-
current_status["start_time"] = time.strftime("%H:%M:%S")
|
| 206 |
else:
|
| 207 |
consecutive_fails += 1
|
| 208 |
if was_streaming and consecutive_fails > 5:
|
|
@@ -213,73 +193,17 @@ def monitor_loop():
|
|
| 213 |
is_streaming = False
|
| 214 |
was_streaming = False
|
| 215 |
consecutive_fails = 0
|
| 216 |
-
current_status["live"] = False
|
| 217 |
-
current_status["pid"] = None
|
| 218 |
-
|
| 219 |
-
# تحديث الإحصائيات
|
| 220 |
-
try:
|
| 221 |
-
load1, load5, load15 = os.getloadavg()
|
| 222 |
-
mem = psutil.virtual_memory()
|
| 223 |
-
current_status["cpu_load"] = f"{load1:.2f} / {load5:.2f} / {load15:.2f}"
|
| 224 |
-
current_status["ram"] = f"{mem.percent:.1f}%"
|
| 225 |
-
except:
|
| 226 |
-
pass
|
| 227 |
-
|
| 228 |
-
if ffmpeg_proc and ffmpeg_proc.poll() is None:
|
| 229 |
-
try:
|
| 230 |
-
p = psutil.Process(ffmpeg_proc.pid)
|
| 231 |
-
current_status["ffmpeg_cpu"] = f"{p.cpu_percent(interval=1):.1f}%"
|
| 232 |
-
except:
|
| 233 |
-
current_status["ffmpeg_cpu"] = "?"
|
| 234 |
-
else:
|
| 235 |
-
current_status["ffmpeg_cpu"] = "—"
|
| 236 |
|
| 237 |
-
current_status["last_check"] = time.strftime("%H:%M:%S")
|
| 238 |
time.sleep(30)
|
| 239 |
|
| 240 |
except Exception as e:
|
| 241 |
log(f"💥 خطأ: {e}")
|
| 242 |
time.sleep(30)
|
| 243 |
|
| 244 |
-
app = Flask(__name__)
|
| 245 |
-
|
| 246 |
-
@app.route("/")
|
| 247 |
-
def index():
|
| 248 |
-
s = current_status
|
| 249 |
-
status_emoji = "🔴" if s["live"] else "⚫"
|
| 250 |
-
html = f"""
|
| 251 |
-
<html dir="rtl">
|
| 252 |
-
<head><meta charset="UTF-8"><title>مراقب البث</title>
|
| 253 |
-
<meta http-equiv="refresh" content="15">
|
| 254 |
-
<style>
|
| 255 |
-
body {{ font-family: sans-serif; background: #0e1117; color: #eee; padding: 30px; }}
|
| 256 |
-
.card {{ background: #1e222d; padding: 20px; border-radius: 12px; margin: 10px 0; }}
|
| 257 |
-
h1 {{ color: #ff4b4b; }}
|
| 258 |
-
</style></head>
|
| 259 |
-
<body>
|
| 260 |
-
<h1>📡 رادار بث {TIKTOK_USERNAME}</h1>
|
| 261 |
-
<div class="card">
|
| 262 |
-
<strong>الحالة:</strong> {status_emoji} {"المذيع يبث الآن!" if s["live"] else "متوقف"}
|
| 263 |
-
<br>معرف العملية: {s.get("pid") or "—"}
|
| 264 |
-
<br>بدء البث: {s.get("start_time") or "—"}
|
| 265 |
-
</div>
|
| 266 |
-
<div class="card">
|
| 267 |
-
<strong>تحميل المعالج (كامل):</strong> {s["cpu_load"]}
|
| 268 |
-
<br><strong>الذاكرة:</strong> {s["ram"]}
|
| 269 |
-
<br><strong>🎯 استهلاك ffmpeg الفعلي:</strong> {s["ffmpeg_cpu"]}
|
| 270 |
-
<br>آخر فحص: {s["last_check"]}
|
| 271 |
-
</div>
|
| 272 |
-
<div class="card">
|
| 273 |
-
<h3>آخر سجلات النظام</h3>
|
| 274 |
-
<pre style="color:#aaa; background:#000; padding:10px;">{open(MONITOR_LOG).read()[-2000:] if os.path.exists(MONITOR_LOG) else "لا يوجد سجل"}</pre>
|
| 275 |
-
</div>
|
| 276 |
-
</body>
|
| 277 |
-
</html>
|
| 278 |
-
"""
|
| 279 |
-
return html
|
| 280 |
-
|
| 281 |
if __name__ == "__main__":
|
| 282 |
-
log("✅ بدء تشغيل المحرك
|
| 283 |
t = threading.Thread(target=monitor_loop, daemon=True)
|
| 284 |
t.start()
|
| 285 |
-
|
|
|
|
|
|
|
|
|
| 3 |
import subprocess
|
| 4 |
import threading
|
| 5 |
import psutil
|
|
|
|
| 6 |
|
| 7 |
TIKTOK_USERNAME = "@abdulaziz999989"
|
| 8 |
STREAM_KEY = "p3dq-5cex-bc1k-vet7-bsyx"
|
|
|
|
| 10 |
MONITOR_LOG = "/tmp/monitor.log"
|
| 11 |
LOCK_FILE = "/tmp/app_monitor_core.lock"
|
| 12 |
|
| 13 |
+
# الكوكيز من متغيرات البيئة (Secrets)
|
| 14 |
TIKTOK_COOKIES = os.getenv("TIKTOK_COOKIES", "")
|
| 15 |
TIKTOK_COOKIES_ADULT = os.getenv("TIKTOK_COOKIES_ADULT", "")
|
| 16 |
|
|
|
|
| 94 |
"-flags", "+global_header",
|
| 95 |
"-i", stream_url,
|
| 96 |
|
|
|
|
| 97 |
"-c:v", "copy",
|
| 98 |
|
|
|
|
| 99 |
"-c:a", "aac",
|
| 100 |
"-b:a", "192k",
|
| 101 |
"-ar", "44100",
|
|
|
|
| 104 |
"-af", "highpass=f=60,lowpass=f=17000,aresample=async=1:first_pts=0",
|
| 105 |
"-async", "1",
|
| 106 |
|
|
|
|
| 107 |
"-start_at_zero",
|
| 108 |
"-copyts",
|
| 109 |
"-vsync", "2",
|
|
|
|
| 123 |
log(f"✅ ffmpeg PID: {proc.pid}")
|
| 124 |
return proc
|
| 125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
def monitor_loop():
|
|
|
|
|
|
|
| 127 |
try:
|
| 128 |
subprocess.run(["killall", "-9", "ffmpeg", "yt-dlp", "streamlink"], stderr=subprocess.DEVNULL)
|
| 129 |
except:
|
|
|
|
| 148 |
consecutive_fails = 0
|
| 149 |
ffmpeg_restart_count = 0
|
| 150 |
|
| 151 |
+
log("🚀 محرك النقل الشفاف (بدون واجهة) بدأ")
|
| 152 |
|
| 153 |
while True:
|
| 154 |
try:
|
|
|
|
| 169 |
ffmpeg_proc = None
|
| 170 |
is_streaming = False
|
| 171 |
was_streaming = False
|
|
|
|
|
|
|
| 172 |
time.sleep(300)
|
| 173 |
ffmpeg_restart_count = 0
|
| 174 |
continue
|
|
|
|
| 183 |
was_streaming = True
|
| 184 |
consecutive_fails = 0
|
| 185 |
ffmpeg_restart_count = 0
|
|
|
|
|
|
|
|
|
|
| 186 |
else:
|
| 187 |
consecutive_fails += 1
|
| 188 |
if was_streaming and consecutive_fails > 5:
|
|
|
|
| 193 |
is_streaming = False
|
| 194 |
was_streaming = False
|
| 195 |
consecutive_fails = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
|
|
|
| 197 |
time.sleep(30)
|
| 198 |
|
| 199 |
except Exception as e:
|
| 200 |
log(f"💥 خطأ: {e}")
|
| 201 |
time.sleep(30)
|
| 202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
if __name__ == "__main__":
|
| 204 |
+
log("✅ بدء تشغيل المحرك بدون واجهة")
|
| 205 |
t = threading.Thread(target=monitor_loop, daemon=True)
|
| 206 |
t.start()
|
| 207 |
+
# إبقاء العملية حية
|
| 208 |
+
while True:
|
| 209 |
+
time.sleep(3600)
|