Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -6,7 +6,15 @@ from flask import Flask
|
|
| 6 |
import threading
|
| 7 |
import sys
|
| 8 |
|
| 9 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
app = Flask(__name__)
|
| 11 |
@app.route('/')
|
| 12 |
def health_check():
|
|
@@ -15,66 +23,52 @@ def health_check():
|
|
| 15 |
def run_flask():
|
| 16 |
app.run(host='0.0.0.0', port=7860)
|
| 17 |
|
| 18 |
-
# 2. إعدادات البث (يوتيوب)
|
| 19 |
-
STREAM_URL = "rtmp://a.rtmp.youtube.com/live2/"
|
| 20 |
-
STREAM_KEY = os.environ.get("STREAM_KEY")
|
| 21 |
-
|
| 22 |
-
# موعد رمضان المتوقع (20 مارس 2026)
|
| 23 |
-
RAMADAN_DATE = datetime(2026, 3, 20, 0, 0, 0)
|
| 24 |
-
|
| 25 |
-
# --- وظيفة عداد الشاشة ---
|
| 26 |
def print_console_countdown():
|
| 27 |
-
|
|
|
|
| 28 |
now = datetime.now()
|
| 29 |
remaining = RAMADAN_DATE - now
|
| 30 |
-
|
| 31 |
if remaining.total_seconds() > 0:
|
| 32 |
-
# حساب الأيام، الساعات، الدقائق، والثواني
|
| 33 |
days = remaining.days
|
| 34 |
hours, rem = divmod(remaining.seconds, 3600)
|
| 35 |
minutes, seconds = divmod(rem, 60)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
sys.stdout.write(
|
| 39 |
-
sys.stdout.flush()
|
| 40 |
-
else:
|
| 41 |
-
sys.stdout.write("\r[!!!] RAMADAN HAS STARTED! Mubarak! ")
|
| 42 |
sys.stdout.flush()
|
| 43 |
-
|
| 44 |
time.sleep(1)
|
| 45 |
|
| 46 |
def start_stream():
|
| 47 |
if not STREAM_KEY:
|
| 48 |
-
print("\
|
| 49 |
return
|
| 50 |
|
| 51 |
-
#
|
| 52 |
ffmpeg_cmd = [
|
| 53 |
'ffmpeg',
|
| 54 |
'-re', '-f', 'lavfi', '-i', 'color=c=black:s=1280x720:r=30',
|
| 55 |
'-vf', f"drawtext=text='Ramadan 2026 Countdown':x=(w-text_w)/2:y=(h-text_h)/2-60:fontsize=70:fontcolor=white, "
|
| 56 |
f"drawtext=text='%{{pts\\:localtime\\:{RAMADAN_DATE.timestamp()}\\:%d Days %H\\:%M\\:%S}}':x=(w-text_w)/2:y=(h-text_h)/2+60:fontsize=90:fontcolor=yellow",
|
| 57 |
-
'-vcodec', 'libx264', '-preset', 'veryfast', '-
|
| 58 |
-
'-
|
| 59 |
-
f"{STREAM_URL}{STREAM_KEY}"
|
| 60 |
]
|
| 61 |
|
| 62 |
-
#
|
| 63 |
subprocess.run(ffmpeg_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
| 66 |
-
#
|
| 67 |
threading.Thread(target=run_flask, daemon=True).start()
|
| 68 |
|
| 69 |
-
#
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
-
print("---
|
| 73 |
-
print(f"Target Date: {RAMADAN_DATE}")
|
| 74 |
|
| 75 |
while True:
|
| 76 |
try:
|
| 77 |
start_stream()
|
| 78 |
except Exception as e:
|
| 79 |
-
print(f"\nStream
|
| 80 |
-
time.sleep(
|
|
|
|
| 6 |
import threading
|
| 7 |
import sys
|
| 8 |
|
| 9 |
+
# ==========================================
|
| 10 |
+
# إعدادات التحكم - غيّرها من هنا
|
| 11 |
+
# ==========================================
|
| 12 |
+
SHOW_MONITOR = True # اجعله False لتوفير موارد السيرفر وإخفاء العداد من الـ Logs
|
| 13 |
+
RAMADAN_DATE = datetime(2026, 3, 20, 0, 0, 0)
|
| 14 |
+
STREAM_URL = "rtmp://a.rtmp.youtube.com/live2/"
|
| 15 |
+
STREAM_KEY = os.environ.get("STREAM_KEY")
|
| 16 |
+
# ==========================================
|
| 17 |
+
|
| 18 |
app = Flask(__name__)
|
| 19 |
@app.route('/')
|
| 20 |
def health_check():
|
|
|
|
| 23 |
def run_flask():
|
| 24 |
app.run(host='0.0.0.0', port=7860)
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def print_console_countdown():
|
| 27 |
+
"""وظيفة مراقبة العداد في الـ Logs"""
|
| 28 |
+
while SHOW_MONITOR:
|
| 29 |
now = datetime.now()
|
| 30 |
remaining = RAMADAN_DATE - now
|
|
|
|
| 31 |
if remaining.total_seconds() > 0:
|
|
|
|
| 32 |
days = remaining.days
|
| 33 |
hours, rem = divmod(remaining.seconds, 3600)
|
| 34 |
minutes, seconds = divmod(rem, 60)
|
| 35 |
+
# استخدام \r لتحديث نفس السطر في الـ Logs
|
| 36 |
+
output = f"\r[Monitor] Remaining: {days}d {hours:02}:{minutes:02}:{seconds:02}"
|
| 37 |
+
sys.stdout.write(output)
|
|
|
|
|
|
|
|
|
|
| 38 |
sys.stdout.flush()
|
|
|
|
| 39 |
time.sleep(1)
|
| 40 |
|
| 41 |
def start_stream():
|
| 42 |
if not STREAM_KEY:
|
| 43 |
+
print("\n[!] Error: STREAM_KEY is missing in Settings > Secrets")
|
| 44 |
return
|
| 45 |
|
| 46 |
+
# تم تحسين السطر ليكون أخف على المعالج (veryfast & zero-latency)
|
| 47 |
ffmpeg_cmd = [
|
| 48 |
'ffmpeg',
|
| 49 |
'-re', '-f', 'lavfi', '-i', 'color=c=black:s=1280x720:r=30',
|
| 50 |
'-vf', f"drawtext=text='Ramadan 2026 Countdown':x=(w-text_w)/2:y=(h-text_h)/2-60:fontsize=70:fontcolor=white, "
|
| 51 |
f"drawtext=text='%{{pts\\:localtime\\:{RAMADAN_DATE.timestamp()}\\:%d Days %H\\:%M\\:%S}}':x=(w-text_w)/2:y=(h-text_h)/2+60:fontsize=90:fontcolor=yellow",
|
| 52 |
+
'-vcodec', 'libx264', '-preset', 'veryfast', '-tune', 'zerolatency',
|
| 53 |
+
'-b:v', '1500k', '-f', 'flv', f"{STREAM_URL}{STREAM_KEY}"
|
|
|
|
| 54 |
]
|
| 55 |
|
| 56 |
+
# إرسال مخرجات FFmpeg إلى الـ NULL ليبقى الـ Logs نظيفاً للعداد الخاص بنا
|
| 57 |
subprocess.run(ffmpeg_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
+
# تشغيل السيرفر لضمان بقاء الـ Space مستيقظاً
|
| 61 |
threading.Thread(target=run_flask, daemon=True).start()
|
| 62 |
|
| 63 |
+
# تشغيل العداد فقط إذا كان المفتاح True
|
| 64 |
+
if SHOW_MONITOR:
|
| 65 |
+
threading.Thread(target=print_console_countdown, daemon=True).start()
|
| 66 |
|
| 67 |
+
print(f"\n--- System Active | Show Monitor: {SHOW_MONITOR} ---")
|
|
|
|
| 68 |
|
| 69 |
while True:
|
| 70 |
try:
|
| 71 |
start_stream()
|
| 72 |
except Exception as e:
|
| 73 |
+
print(f"\nStream error: {e}")
|
| 74 |
+
time.sleep(15)
|