Update app.py
Browse files
app.py
CHANGED
|
@@ -5,23 +5,18 @@ import threading
|
|
| 5 |
import json
|
| 6 |
import pickle
|
| 7 |
import psutil
|
| 8 |
-
import http.cookiejar
|
| 9 |
-
import urllib.request
|
| 10 |
-
import re
|
| 11 |
from datetime import datetime, timezone
|
| 12 |
from google.oauth2.credentials import Credentials
|
| 13 |
from google.auth.transport.requests import Request
|
| 14 |
from googleapiclient.discovery import build
|
| 15 |
|
| 16 |
# ------------------------------------------------------------
|
| 17 |
-
# الإعدادات
|
| 18 |
# ------------------------------------------------------------
|
| 19 |
-
TIKTOK_USERNAME = "@
|
| 20 |
CUSTOM_STREAM_KEY = os.getenv("CUSTOM_STREAM_KEY", "p3dq-5cex-bc1k-vet7-bsyx")
|
| 21 |
FFMPEG_LOG = "/tmp/ffmpeg.log"
|
| 22 |
MONITOR_LOG = "/tmp/monitor.log"
|
| 23 |
-
LOCK_FILE = "/tmp/app_monitor_core.lock"
|
| 24 |
-
TOKEN_FILE = "/tmp/yt_token.pkl"
|
| 25 |
|
| 26 |
def log(msg):
|
| 27 |
ts = time.strftime("%Y-%m-%d %H:%M:%S")
|
|
@@ -31,53 +26,26 @@ def log(msg):
|
|
| 31 |
f.write(line + "\n")
|
| 32 |
|
| 33 |
# ------------------------------------------------------------
|
| 34 |
-
# ال
|
| 35 |
# ------------------------------------------------------------
|
| 36 |
def save_cookies():
|
| 37 |
-
log("🔍 جاري فحص وتجهيز الكوكيز...")
|
| 38 |
-
found_any = False
|
| 39 |
-
|
| 40 |
for env_var, file_path in [("TIKTOK_COOKIES", "/tmp/tiktok_cookies.txt"), ("TIKTOK_COOKIES_ADULT", "/tmp/tiktok_cookies_adult.txt")]:
|
| 41 |
cookie_data = os.getenv(env_var, "").strip()
|
| 42 |
-
|
| 43 |
-
if not cookie_data:
|
| 44 |
-
log(f"⚠️ المتغير {env_var} غير موجود أو فارغ في الـ Secrets!")
|
| 45 |
-
continue
|
| 46 |
-
|
| 47 |
-
found_any = True
|
| 48 |
-
log(f"✅ تم العثور على بيانات في {env_var} بطول {len(cookie_data)} حرف.")
|
| 49 |
-
|
| 50 |
-
# فلترة ذكية لأسطر تيك توك فقط
|
| 51 |
-
tiktok_lines = [line for line in cookie_data.splitlines() if "tiktok.com" in line and not line.startswith("#")]
|
| 52 |
-
|
| 53 |
-
if not tiktok_lines:
|
| 54 |
-
log(f"❌ خطأ: لم يتم العثور على سطور صالحة تحتوي على 'tiktok.com' في {env_var}!")
|
| 55 |
-
else:
|
| 56 |
with open(file_path, "w", encoding="utf-8") as f:
|
| 57 |
-
|
| 58 |
-
f.write(
|
| 59 |
-
log(f"✅ تم حفظ
|
| 60 |
-
|
| 61 |
-
if not found_any:
|
| 62 |
-
log("🚨 تنبيه: البوت لا يرى أي كوكيز! تأكد من إضافتها في قسم Settings -> Secrets.")
|
| 63 |
|
| 64 |
# ------------------------------------------------------------
|
| 65 |
-
# دال
|
| 66 |
-
# ------------------------------------------------------------
|
| 67 |
-
def check_system_health():
|
| 68 |
-
cpu = psutil.cpu_percent(interval=1)
|
| 69 |
-
ram = psutil.virtual_memory().percent
|
| 70 |
-
log(f"📊 حالة النظام: CPU: {cpu}% | RAM: {ram}%")
|
| 71 |
-
|
| 72 |
-
# ------------------------------------------------------------
|
| 73 |
-
# بقية دوال البث واليوتيوب (كما هي)
|
| 74 |
# ------------------------------------------------------------
|
| 75 |
def get_tiktok_stream_url():
|
| 76 |
try:
|
| 77 |
url = f"https://www.tiktok.com/{TIKTOK_USERNAME}/live"
|
| 78 |
cmd = ["streamlink", "--stream-url", url, "best"]
|
| 79 |
if os.path.exists("/tmp/tiktok_cookies.txt"): cmd.extend(["--cookies", "/tmp/tiktok_cookies.txt"])
|
| 80 |
-
result = subprocess.run(cmd, capture_output=True, text=True, timeout=
|
| 81 |
return result.stdout.strip().split("\n")[0] if result.returncode == 0 else None
|
| 82 |
except: return None
|
| 83 |
|
|
@@ -88,12 +56,11 @@ def start_stream(stream_key, stream_url):
|
|
| 88 |
return subprocess.Popen(cmd, stderr=f, preexec_fn=os.setsid)
|
| 89 |
|
| 90 |
# ------------------------------------------------------------
|
| 91 |
-
#
|
| 92 |
# ------------------------------------------------------------
|
| 93 |
def monitor_loop():
|
| 94 |
while True:
|
| 95 |
try:
|
| 96 |
-
check_system_health()
|
| 97 |
stream_url = get_tiktok_stream_url()
|
| 98 |
if stream_url:
|
| 99 |
log("🟢 البث متاح، بدء التشغيل...")
|
|
@@ -109,12 +76,12 @@ def monitor_loop():
|
|
| 109 |
|
| 110 |
if __name__ == "__main__":
|
| 111 |
log("✅ بدء تشغيل المحرك النهائي...")
|
| 112 |
-
save_cookies()
|
| 113 |
t = threading.Thread(target=monitor_loop, daemon=True)
|
| 114 |
t.start()
|
| 115 |
while True:
|
| 116 |
if not t.is_alive():
|
| 117 |
-
log("🚨 إعادة تشغيل
|
| 118 |
t = threading.Thread(target=monitor_loop, daemon=True)
|
| 119 |
t.start()
|
| 120 |
time.sleep(3600)
|
|
|
|
| 5 |
import json
|
| 6 |
import pickle
|
| 7 |
import psutil
|
|
|
|
|
|
|
|
|
|
| 8 |
from datetime import datetime, timezone
|
| 9 |
from google.oauth2.credentials import Credentials
|
| 10 |
from google.auth.transport.requests import Request
|
| 11 |
from googleapiclient.discovery import build
|
| 12 |
|
| 13 |
# ------------------------------------------------------------
|
| 14 |
+
# الإعدادات
|
| 15 |
# ------------------------------------------------------------
|
| 16 |
+
TIKTOK_USERNAME = "@a3dam_111"
|
| 17 |
CUSTOM_STREAM_KEY = os.getenv("CUSTOM_STREAM_KEY", "p3dq-5cex-bc1k-vet7-bsyx")
|
| 18 |
FFMPEG_LOG = "/tmp/ffmpeg.log"
|
| 19 |
MONITOR_LOG = "/tmp/monitor.log"
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def log(msg):
|
| 22 |
ts = time.strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
| 26 |
f.write(line + "\n")
|
| 27 |
|
| 28 |
# ------------------------------------------------------------
|
| 29 |
+
# الحل المرن للكوكيز (يقبل أي تنسيق)
|
| 30 |
# ------------------------------------------------------------
|
| 31 |
def save_cookies():
|
|
|
|
|
|
|
|
|
|
| 32 |
for env_var, file_path in [("TIKTOK_COOKIES", "/tmp/tiktok_cookies.txt"), ("TIKTOK_COOKIES_ADULT", "/tmp/tiktok_cookies_adult.txt")]:
|
| 33 |
cookie_data = os.getenv(env_var, "").strip()
|
| 34 |
+
if cookie_data:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
with open(file_path, "w", encoding="utf-8") as f:
|
| 36 |
+
# كتابة البيانات كما هي دون شروط تعجيزية
|
| 37 |
+
f.write(cookie_data)
|
| 38 |
+
log(f"✅ تم حفظ بيانات الكوكيز في {file_path}")
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# ------------------------------------------------------------
|
| 41 |
+
# الدوال الأساسية
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
# ------------------------------------------------------------
|
| 43 |
def get_tiktok_stream_url():
|
| 44 |
try:
|
| 45 |
url = f"https://www.tiktok.com/{TIKTOK_USERNAME}/live"
|
| 46 |
cmd = ["streamlink", "--stream-url", url, "best"]
|
| 47 |
if os.path.exists("/tmp/tiktok_cookies.txt"): cmd.extend(["--cookies", "/tmp/tiktok_cookies.txt"])
|
| 48 |
+
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
|
| 49 |
return result.stdout.strip().split("\n")[0] if result.returncode == 0 else None
|
| 50 |
except: return None
|
| 51 |
|
|
|
|
| 56 |
return subprocess.Popen(cmd, stderr=f, preexec_fn=os.setsid)
|
| 57 |
|
| 58 |
# ------------------------------------------------------------
|
| 59 |
+
# المحرك الرئيسي
|
| 60 |
# ------------------------------------------------------------
|
| 61 |
def monitor_loop():
|
| 62 |
while True:
|
| 63 |
try:
|
|
|
|
| 64 |
stream_url = get_tiktok_stream_url()
|
| 65 |
if stream_url:
|
| 66 |
log("🟢 البث متاح، بدء التشغيل...")
|
|
|
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
| 78 |
log("✅ بدء تشغيل المحرك النهائي...")
|
| 79 |
+
save_cookies()
|
| 80 |
t = threading.Thread(target=monitor_loop, daemon=True)
|
| 81 |
t.start()
|
| 82 |
while True:
|
| 83 |
if not t.is_alive():
|
| 84 |
+
log("🚨 إعادة تشغيل المحرك...")
|
| 85 |
t = threading.Thread(target=monitor_loop, daemon=True)
|
| 86 |
t.start()
|
| 87 |
time.sleep(3600)
|