Update streamlit_app.py
Browse files- streamlit_app.py +38 -20
streamlit_app.py
CHANGED
|
@@ -6,12 +6,16 @@ import subprocess
|
|
| 6 |
import threading
|
| 7 |
import json
|
| 8 |
import fcntl
|
|
|
|
|
|
|
|
|
|
| 9 |
from TikTokLive import TikTokLiveClient
|
| 10 |
|
| 11 |
# ══════════════════════════════════════════════════════════════════════════════ #
|
| 12 |
# الإعدادات
|
| 13 |
# ══════════════════════════════════════════════════════════════════════════════ #
|
| 14 |
TIKTOK_USERNAME = "@ah_md107"
|
|
|
|
| 15 |
STATUS = "/tmp/status.json"
|
| 16 |
LOCK_FILE = "/tmp/app_monitor_core.lock"
|
| 17 |
POLL_INTERVAL = 60
|
|
@@ -19,7 +23,25 @@ GRACE_PERIOD_SECONDS = 120
|
|
| 19 |
CUSTOM_STREAM_KEY = os.getenv("CUSTOM_STREAM_KEY", "skqf-rd92-dfa7-74cx-bxr2")
|
| 20 |
|
| 21 |
# ══════════════════════════════════════════════════════════════════════════════ #
|
| 22 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# ══════════════════════════════════════════════════════════════════════════════ #
|
| 24 |
def run_monitor():
|
| 25 |
lock_file = open(LOCK_FILE, 'w')
|
|
@@ -32,11 +54,11 @@ def run_monitor():
|
|
| 32 |
|
| 33 |
while True:
|
| 34 |
try:
|
| 35 |
-
# التحقق من سلامة العملية
|
| 36 |
if is_streaming and (process is None or process.poll() is not None):
|
| 37 |
is_streaming = False
|
| 38 |
process = None
|
| 39 |
-
with open(STATUS, "w") as f: json.dump({"live": False, "msg": "انقطع الاتصال
|
| 40 |
|
| 41 |
client = TikTokLiveClient(unique_id=TIKTOK_USERNAME)
|
| 42 |
is_live = asyncio.run(client.is_live())
|
|
@@ -44,17 +66,12 @@ def run_monitor():
|
|
| 44 |
if is_live:
|
| 45 |
offline_start_time = None
|
| 46 |
if not is_streaming:
|
| 47 |
-
# الكود الكامل مع الفلاتر والأداء
|
| 48 |
cmd = (
|
| 49 |
f"streamlink --loglevel error https://www.tiktok.com/{TIKTOK_USERNAME}/live best --hls-live-edge 3 --stdout | "
|
| 50 |
f"ffmpeg -hide_banner -loglevel error -thread_queue_size 8192 -i pipe:0 "
|
| 51 |
-
f"-c:v copy "
|
| 52 |
-
f"-c:a aac -ar 48000 -b:a 192k -ac 2 "
|
| 53 |
f"-af 'highpass=f=200,lowpass=f=3000,aresample=async=1' "
|
| 54 |
-
f"-buffer_size 4096k "
|
| 55 |
-
f"-fflags +genpts+igndts "
|
| 56 |
-
f"-max_interleave_delta 0 "
|
| 57 |
-
f"-f flv 'rtmp://a.rtmp.youtube.com/live2/{CUSTOM_STREAM_KEY}'"
|
| 58 |
)
|
| 59 |
process = subprocess.Popen(cmd, shell=True)
|
| 60 |
is_streaming = True
|
|
@@ -63,40 +80,41 @@ def run_monitor():
|
|
| 63 |
else:
|
| 64 |
if is_streaming:
|
| 65 |
if offline_start_time is None: offline_start_time = time.time()
|
|
|
|
| 66 |
if time.time() - offline_start_time >= GRACE_PERIOD_SECONDS:
|
|
|
|
| 67 |
if process: process.terminate()
|
|
|
|
|
|
|
|
|
|
| 68 |
is_streaming = False
|
| 69 |
offline_start_time = None
|
| 70 |
-
with open(STATUS, "w") as f: json.dump({"live": False, "msg": "
|
| 71 |
|
| 72 |
time.sleep(POLL_INTERVAL)
|
| 73 |
except Exception:
|
| 74 |
time.sleep(POLL_INTERVAL)
|
| 75 |
|
| 76 |
-
# تشغيل ال
|
| 77 |
if "core_monitor_initialized" not in st.session_state:
|
| 78 |
threading.Thread(target=run_monitor, name="CoreTikTokMonitor", daemon=True).start()
|
| 79 |
st.session_state.core_monitor_initialized = True
|
| 80 |
|
| 81 |
# ══════════════════════════════════════════════════════════════════════════════ #
|
| 82 |
-
# الواجهة
|
| 83 |
# ══════════════════════════════════════════════════════════════════════════════ #
|
| 84 |
st.set_page_config(page_title="AutoStreamer Pro", page_icon="📡")
|
| 85 |
-
st.title("📡 رادار
|
| 86 |
|
| 87 |
def get_status():
|
| 88 |
if not os.path.exists(STATUS) or os.path.getsize(STATUS) == 0:
|
| 89 |
-
return {"live": False, "msg": "جاري
|
| 90 |
try:
|
| 91 |
with open(STATUS, "r") as f: return json.load(f)
|
| 92 |
except: return {"live": False, "msg": "بانتظار المذيع..."}
|
| 93 |
|
| 94 |
s = get_status()
|
| 95 |
if s.get("live"):
|
| 96 |
-
st.success("🔴 أحمد عبدالله مباشر
|
| 97 |
-
st.markdown("✅ **تم تفعيل الفلاتر الصوتية (Highpass/Lowpass) وضبط استقرار البث.**")
|
| 98 |
else:
|
| 99 |
st.info(s.get("msg", "بانتظار المذيع..."))
|
| 100 |
-
|
| 101 |
-
if st.button("🔄 تحديث حالة الرادار"):
|
| 102 |
-
st.rerun()
|
|
|
|
| 6 |
import threading
|
| 7 |
import json
|
| 8 |
import fcntl
|
| 9 |
+
import pickle
|
| 10 |
+
from datetime import datetime
|
| 11 |
+
from googleapiclient.discovery import build
|
| 12 |
from TikTokLive import TikTokLiveClient
|
| 13 |
|
| 14 |
# ══════════════════════════════════════════════════════════════════════════════ #
|
| 15 |
# الإعدادات
|
| 16 |
# ══════════════════════════════════════════════════════════════════════════════ #
|
| 17 |
TIKTOK_USERNAME = "@ah_md107"
|
| 18 |
+
TOKEN = "/tmp/yt.pkl"
|
| 19 |
STATUS = "/tmp/status.json"
|
| 20 |
LOCK_FILE = "/tmp/app_monitor_core.lock"
|
| 21 |
POLL_INTERVAL = 60
|
|
|
|
| 23 |
CUSTOM_STREAM_KEY = os.getenv("CUSTOM_STREAM_KEY", "skqf-rd92-dfa7-74cx-bxr2")
|
| 24 |
|
| 25 |
# ══════════════════════════════════════════════════════════════════════════════ #
|
| 26 |
+
# دالة الإغلاق الاحترافي عبر الـ API
|
| 27 |
+
# ══════════════════════════════════════════════════════════════════════════════ #
|
| 28 |
+
def finalize_broadcast_via_api():
|
| 29 |
+
try:
|
| 30 |
+
if not os.path.exists(TOKEN): return
|
| 31 |
+
creds = pickle.load(open(TOKEN, "rb"))
|
| 32 |
+
yt = build('youtube', 'v3', credentials=creds)
|
| 33 |
+
# جلب البث الثابت النشط حالياً
|
| 34 |
+
broadcasts = yt.liveBroadcasts().list(broadcastType="persistent", mine=True, part="id").execute()
|
| 35 |
+
if broadcasts.get("items"):
|
| 36 |
+
b_id = broadcasts["items"][0]["id"]
|
| 37 |
+
# إرسال أمر الإنهاء الرسمي
|
| 38 |
+
yt.liveBroadcasts().transition(broadcastStatus="complete", id=b_id, part="status").execute()
|
| 39 |
+
print(f"✅ [API] تم إغلاق البث {b_id} رسمياً وأرشفته.")
|
| 40 |
+
except Exception as e:
|
| 41 |
+
print(f"⚠️ خطأ في إغلاق البث عبر API: {e}")
|
| 42 |
+
|
| 43 |
+
# ══════════════════════════════════════════════════════════════════════════════ #
|
| 44 |
+
# محرك المراقبة (النسخة الكاملة + إغلاق API)
|
| 45 |
# ══════════════════════════════════════════════════════════════════════════════ #
|
| 46 |
def run_monitor():
|
| 47 |
lock_file = open(LOCK_FILE, 'w')
|
|
|
|
| 54 |
|
| 55 |
while True:
|
| 56 |
try:
|
| 57 |
+
# 1. التحقق من سلامة العملية
|
| 58 |
if is_streaming and (process is None or process.poll() is not None):
|
| 59 |
is_streaming = False
|
| 60 |
process = None
|
| 61 |
+
with open(STATUS, "w") as f: json.dump({"live": False, "msg": "انقطع الاتصال..."}, f)
|
| 62 |
|
| 63 |
client = TikTokLiveClient(unique_id=TIKTOK_USERNAME)
|
| 64 |
is_live = asyncio.run(client.is_live())
|
|
|
|
| 66 |
if is_live:
|
| 67 |
offline_start_time = None
|
| 68 |
if not is_streaming:
|
|
|
|
| 69 |
cmd = (
|
| 70 |
f"streamlink --loglevel error https://www.tiktok.com/{TIKTOK_USERNAME}/live best --hls-live-edge 3 --stdout | "
|
| 71 |
f"ffmpeg -hide_banner -loglevel error -thread_queue_size 8192 -i pipe:0 "
|
| 72 |
+
f"-c:v copy -c:a aac -ar 48000 -b:a 192k -ac 2 "
|
|
|
|
| 73 |
f"-af 'highpass=f=200,lowpass=f=3000,aresample=async=1' "
|
| 74 |
+
f"-buffer_size 4096k -f flv 'rtmp://a.rtmp.youtube.com/live2/{CUSTOM_STREAM_KEY}'"
|
|
|
|
|
|
|
|
|
|
| 75 |
)
|
| 76 |
process = subprocess.Popen(cmd, shell=True)
|
| 77 |
is_streaming = True
|
|
|
|
| 80 |
else:
|
| 81 |
if is_streaming:
|
| 82 |
if offline_start_time is None: offline_start_time = time.time()
|
| 83 |
+
|
| 84 |
if time.time() - offline_start_time >= GRACE_PERIOD_SECONDS:
|
| 85 |
+
# أ. إيقاف الـ FFmpeg
|
| 86 |
if process: process.terminate()
|
| 87 |
+
# ب. إرسال أمر الإغلاق عبر الـ API
|
| 88 |
+
finalize_broadcast_via_api()
|
| 89 |
+
|
| 90 |
is_streaming = False
|
| 91 |
offline_start_time = None
|
| 92 |
+
with open(STATUS, "w") as f: json.dump({"live": False, "msg": "انتهى البث وأرشفته بنجاح!"}, f)
|
| 93 |
|
| 94 |
time.sleep(POLL_INTERVAL)
|
| 95 |
except Exception:
|
| 96 |
time.sleep(POLL_INTERVAL)
|
| 97 |
|
| 98 |
+
# تشغيل الخيط
|
| 99 |
if "core_monitor_initialized" not in st.session_state:
|
| 100 |
threading.Thread(target=run_monitor, name="CoreTikTokMonitor", daemon=True).start()
|
| 101 |
st.session_state.core_monitor_initialized = True
|
| 102 |
|
| 103 |
# ══════════════════════════════════════════════════════════════════════════════ #
|
| 104 |
+
# الواجهة
|
| 105 |
# ══════════════════════════════════════════════════════════════════════════════ #
|
| 106 |
st.set_page_config(page_title="AutoStreamer Pro", page_icon="📡")
|
| 107 |
+
st.title("📡 رادار أحمد عبدالله - الإغلاق الذكي Pro")
|
| 108 |
|
| 109 |
def get_status():
|
| 110 |
if not os.path.exists(STATUS) or os.path.getsize(STATUS) == 0:
|
| 111 |
+
return {"live": False, "msg": "جاري التحقق من النظام..."}
|
| 112 |
try:
|
| 113 |
with open(STATUS, "r") as f: return json.load(f)
|
| 114 |
except: return {"live": False, "msg": "بانتظار المذيع..."}
|
| 115 |
|
| 116 |
s = get_status()
|
| 117 |
if s.get("live"):
|
| 118 |
+
st.success("🔴 أحمد عبدالله مباشر (يعمل نظام الأرشفة التلقائية)")
|
|
|
|
| 119 |
else:
|
| 120 |
st.info(s.get("msg", "بانتظار المذيع..."))
|
|
|
|
|
|
|
|
|