Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -13,109 +13,56 @@ STREAM_KEY = os.environ.get("STREAM_KEY")
|
|
| 13 |
STREAM_URL = "rtmp://a.rtmp.youtube.com/live2/"
|
| 14 |
BG_IMAGE = "https://images.unsplash.com/photo-1555400038-63f5ba517a47?q=80&w=1920&auto=format&fit=crop"
|
| 15 |
|
| 16 |
-
# اسم الملف الصوتي المرفوع على الـ Space
|
| 17 |
TICK_SOUND_FILE = "time.mp3"
|
| 18 |
|
| 19 |
app = Flask(__name__)
|
| 20 |
|
| 21 |
-
# ==========================================
|
| 22 |
-
# منطق تحديد الوقت والحالة (رمضان/عيد/عد تنازلي)
|
| 23 |
-
# ==========================================
|
| 24 |
def get_current_status():
|
| 25 |
try:
|
| 26 |
-
# جلب التاريخ الهجري الحالي بدقة
|
| 27 |
res = requests.get("http://api.aladhan.com/v1/timingsByCity?city=Mecca&country=SA&method=4", timeout=5)
|
| 28 |
data = res.json()['data']
|
| 29 |
-
|
| 30 |
hijri_date = data['date']['hijri']
|
|
|
|
| 31 |
h_day = int(hijri_date['day'])
|
| 32 |
h_month = int(hijri_date['month']['number'])
|
| 33 |
h_year = int(hijri_date['year'])
|
| 34 |
-
date_str = hijri_date['date']
|
| 35 |
|
| 36 |
status = ""
|
| 37 |
target_date_gregorian = None
|
| 38 |
msg = ""
|
| 39 |
|
| 40 |
-
# الحالة 1: نحن في شهر رمضان (الشهر 9)
|
| 41 |
if h_month == 9:
|
| 42 |
status = "ramadan_live"
|
| 43 |
msg = "جاء رمضان.. مبارك عليكم الشهر"
|
| 44 |
-
|
| 45 |
-
# الحالة 2: نحن في عيد الفطر (الشهر 10، الأيام 1-3)
|
| 46 |
elif h_month == 10 and h_day <= 3:
|
| 47 |
status = "eid_live"
|
| 48 |
msg = "عيد مبارك - كل عام وأنتم بخير"
|
| 49 |
-
|
| 50 |
-
# الحالة 3: العد التنازلي (قبل رمضان أو بعد العيد)
|
| 51 |
else:
|
| 52 |
status = "counting"
|
| 53 |
-
|
| 54 |
-
if h_month < 9:
|
| 55 |
-
target_h_year = h_year # نفس السنة
|
| 56 |
-
else:
|
| 57 |
-
target_h_year = h_year + 1 # السنة القادمة
|
| 58 |
-
|
| 59 |
-
# تحويل 1 رمضان للسنة المستهدفة إلى ميلادي
|
| 60 |
conv_res = requests.get(f"http://api.aladhan.com/v1/hToG?date=01-09-{target_h_year}", timeout=5)
|
| 61 |
g_data = conv_res.json()['data']['gregorian']
|
| 62 |
-
|
| 63 |
-
target_date_str = g_data['date']
|
| 64 |
-
target_date_gregorian = datetime.strptime(target_date_str, "%d-%m-%Y")
|
| 65 |
-
|
| 66 |
-
return {
|
| 67 |
-
"status": status,
|
| 68 |
-
"msg": msg,
|
| 69 |
-
"hijri_str": date_str,
|
| 70 |
-
"target_date": target_date_gregorian
|
| 71 |
-
}
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
return {
|
| 77 |
-
"status": "counting",
|
| 78 |
-
"msg": "",
|
| 79 |
-
"hijri_str": "1447-09-01", # تاريخ تقريبي
|
| 80 |
-
"target_date": datetime(2026, 2, 18, 0, 0, 0) # تاريخ تقريبي
|
| 81 |
-
}
|
| 82 |
|
| 83 |
@app.route('/time_data')
|
| 84 |
def time_data():
|
| 85 |
current_state = get_current_status()
|
| 86 |
-
|
| 87 |
if current_state['status'] in ['ramadan_live', 'eid_live']:
|
| 88 |
-
return jsonify({
|
| 89 |
-
"status": "finished",
|
| 90 |
-
"msg": current_state['msg'],
|
| 91 |
-
"hijri": current_state['hijri_str']
|
| 92 |
-
})
|
| 93 |
|
| 94 |
-
|
| 95 |
-
if current_state['target_date']:
|
| 96 |
-
diff = current_state['target_date'] - now
|
| 97 |
-
else:
|
| 98 |
-
diff = timedelta(0)
|
| 99 |
-
|
| 100 |
if diff.total_seconds() <= 0:
|
| 101 |
-
|
| 102 |
|
| 103 |
days = diff.days
|
| 104 |
hours, rem = divmod(diff.seconds, 3600)
|
| 105 |
minutes, seconds = divmod(rem, 60)
|
| 106 |
-
|
| 107 |
-
return jsonify({
|
| 108 |
-
"status": "counting",
|
| 109 |
-
"days": f"{days:02}",
|
| 110 |
-
"hours": f"{hours:02}",
|
| 111 |
-
"minutes": f"{minutes:02}",
|
| 112 |
-
"seconds": f"{seconds:02}",
|
| 113 |
-
"hijri": current_state['hijri_str']
|
| 114 |
-
})
|
| 115 |
|
| 116 |
-
# ==========================================
|
| 117 |
-
# صفحة الويب (HTML + CSS)
|
| 118 |
-
# ==========================================
|
| 119 |
@app.route('/')
|
| 120 |
def home():
|
| 121 |
html_content = """
|
|
@@ -137,8 +84,14 @@ def home():
|
|
| 137 |
.hijri-badge { background: #4CAF50; padding: 5px 20px; border-radius: 50px; font-size: 20px; margin-bottom: 20px; display: inline-block; }
|
| 138 |
.title { font-size: 40px; margin-bottom: 30px; text-shadow: 2px 2px 10px rgba(0,0,0,0.8); }
|
| 139 |
|
| 140 |
-
/*
|
| 141 |
-
.countdown {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
.card {
|
| 144 |
background: linear-gradient(180deg, #4CAF50, #2E7D32);
|
|
@@ -146,16 +99,22 @@ def home():
|
|
| 146 |
font-size: 70px; font-weight: 900; display: flex;
|
| 147 |
justify-content: center; align-items: center;
|
| 148 |
box-shadow: 0 8px 15px rgba(0,0,0,0.5);
|
|
|
|
| 149 |
}
|
| 150 |
.unit { display: flex; flex-direction: column; align-items: center; }
|
| 151 |
-
.label { margin-top:
|
| 152 |
|
| 153 |
-
/* ال
|
| 154 |
.colon {
|
| 155 |
-
font-size:
|
| 156 |
color: #4CAF50;
|
| 157 |
font-weight: 900;
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
}
|
| 160 |
|
| 161 |
#celebration { display: none; font-size: 60px; color: #FFD700; animation: pulse 2s infinite; }
|
|
@@ -193,10 +152,10 @@ def home():
|
|
| 193 |
} else {
|
| 194 |
document.getElementById('main-ui').style.display = 'block';
|
| 195 |
document.getElementById('celebration').style.display = 'none';
|
| 196 |
-
document.getElementById('days').innerText = data.days;
|
| 197 |
-
document.getElementById('hours').innerText = data.hours;
|
| 198 |
-
document.getElementById('minutes').innerText = data.minutes;
|
| 199 |
-
document.getElementById('seconds').innerText = data.seconds;
|
| 200 |
}
|
| 201 |
});
|
| 202 |
}
|
|
@@ -208,51 +167,22 @@ def home():
|
|
| 208 |
"""
|
| 209 |
return html_content
|
| 210 |
|
| 211 |
-
# ==========================================
|
| 212 |
-
# دالة البث المباشر (FFmpeg)
|
| 213 |
-
# ==========================================
|
| 214 |
def start_stream():
|
| 215 |
-
if not STREAM_KEY:
|
| 216 |
-
print("Error: STREAM_KEY not found.")
|
| 217 |
-
return
|
| 218 |
-
|
| 219 |
while True:
|
| 220 |
current_state = get_current_status()
|
| 221 |
-
|
| 222 |
-
# إعدادات النصوص والألوان
|
| 223 |
if current_state['status'] == 'ramadan_live':
|
| 224 |
-
display_text = "Ramadan Mubarak
|
| 225 |
-
box_color = "0xD4AF37"
|
| 226 |
-
title_text = "RAMADAN KAREEM"
|
| 227 |
-
font_size = "65"
|
| 228 |
-
|
| 229 |
elif current_state['status'] == 'eid_live':
|
| 230 |
-
display_text = "Eid Mubarak"
|
| 231 |
-
box_color = "0x9C27B0"
|
| 232 |
-
title_text = "EID AL-FITR"
|
| 233 |
-
font_size = "90"
|
| 234 |
-
|
| 235 |
else:
|
| 236 |
-
if current_state['target_date']
|
| 237 |
-
target_ts = int(current_state['target_date'].timestamp())
|
| 238 |
-
else:
|
| 239 |
-
target_ts = int(datetime.now().timestamp())
|
| 240 |
-
|
| 241 |
display_text = f" %{{pts\\:localtime\\:{target_ts}\\:%d}} : %{{pts\\:localtime\\:{target_ts}\\:%H}} : %{{pts\\:localtime\\:{target_ts}\\:%M}} : %{{pts\\:localtime\\:{target_ts}\\:%S}} "
|
| 242 |
-
box_color = "0x4CAF50"
|
| 243 |
-
title_text = "RAMADAN COUNTDOWN"
|
| 244 |
-
font_size = "90"
|
| 245 |
|
| 246 |
-
# أوامر FFmpeg
|
| 247 |
-
# استخدام الملف المحلي time.mp3
|
| 248 |
ffmpeg_cmd = [
|
| 249 |
-
'ffmpeg',
|
| 250 |
-
'-
|
| 251 |
-
'-stream_loop', '-1', '-i', TICK_SOUND_FILE, # استخدام الملف المحلي
|
| 252 |
-
'-loop', '1', '-i', BG_IMAGE,
|
| 253 |
-
'-map', '1:v',
|
| 254 |
-
'-map', '0:a',
|
| 255 |
-
'-f', 'lavfi', '-i', 'anullsrc',
|
| 256 |
'-vf', (
|
| 257 |
f"scale=1280:720,setsar=1,"
|
| 258 |
f"drawbox=y=ih/2-80:color=black@0.6:width=iw:height=200:t=fill,"
|
|
@@ -261,19 +191,13 @@ def start_stream():
|
|
| 261 |
f"drawtext=text='{display_text}':x=(w-text_w)/2:y=(h-text_h)/2+50:fontsize={font_size}:fontcolor=white:box=1:boxcolor={box_color}@1:boxborderw=25"
|
| 262 |
),
|
| 263 |
'-vcodec', 'libx264', '-preset', 'veryfast', '-b:v', '3000k', '-maxrate', '3000k', '-bufsize', '6000k',
|
| 264 |
-
'-pix_fmt', 'yuv420p', '-g', '60',
|
| 265 |
-
'-c:a', 'aac', '-b:a', '128k',
|
| 266 |
-
'-shortest',
|
| 267 |
-
'-f', 'flv', f"{STREAM_URL}{STREAM_KEY}"
|
| 268 |
]
|
| 269 |
-
|
| 270 |
try:
|
| 271 |
subprocess.run(ffmpeg_cmd, check=True)
|
| 272 |
-
except
|
| 273 |
-
print(f"Stream error/restart: {e}")
|
| 274 |
time.sleep(5)
|
| 275 |
|
| 276 |
if __name__ == "__main__":
|
| 277 |
-
# تشغيل التطبيق والبث
|
| 278 |
threading.Thread(target=lambda: app.run(host='0.0.0.0', port=7860, debug=False, use_reloader=False), daemon=True).start()
|
| 279 |
start_stream()
|
|
|
|
| 13 |
STREAM_URL = "rtmp://a.rtmp.youtube.com/live2/"
|
| 14 |
BG_IMAGE = "https://images.unsplash.com/photo-1555400038-63f5ba517a47?q=80&w=1920&auto=format&fit=crop"
|
| 15 |
|
|
|
|
| 16 |
TICK_SOUND_FILE = "time.mp3"
|
| 17 |
|
| 18 |
app = Flask(__name__)
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
def get_current_status():
|
| 21 |
try:
|
|
|
|
| 22 |
res = requests.get("http://api.aladhan.com/v1/timingsByCity?city=Mecca&country=SA&method=4", timeout=5)
|
| 23 |
data = res.json()['data']
|
|
|
|
| 24 |
hijri_date = data['date']['hijri']
|
| 25 |
+
date_str = hijri_date['date']
|
| 26 |
h_day = int(hijri_date['day'])
|
| 27 |
h_month = int(hijri_date['month']['number'])
|
| 28 |
h_year = int(hijri_date['year'])
|
|
|
|
| 29 |
|
| 30 |
status = ""
|
| 31 |
target_date_gregorian = None
|
| 32 |
msg = ""
|
| 33 |
|
|
|
|
| 34 |
if h_month == 9:
|
| 35 |
status = "ramadan_live"
|
| 36 |
msg = "جاء رمضان.. مبارك عليكم الشهر"
|
|
|
|
|
|
|
| 37 |
elif h_month == 10 and h_day <= 3:
|
| 38 |
status = "eid_live"
|
| 39 |
msg = "عيد مبارك - كل عام وأنتم بخير"
|
|
|
|
|
|
|
| 40 |
else:
|
| 41 |
status = "counting"
|
| 42 |
+
target_h_year = h_year if h_month < 9 else h_year + 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
conv_res = requests.get(f"http://api.aladhan.com/v1/hToG?date=01-09-{target_h_year}", timeout=5)
|
| 44 |
g_data = conv_res.json()['data']['gregorian']
|
| 45 |
+
target_date_gregorian = datetime.strptime(g_data['date'], "%d-%m-%Y")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
return {"status": status, "msg": msg, "hijri_str": date_str, "target_date": target_date_gregorian}
|
| 48 |
+
except:
|
| 49 |
+
return {"status": "counting", "msg": "", "hijri_str": "1447-09-01", "target_date": datetime(2026, 2, 18, 0, 0, 0)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
@app.route('/time_data')
|
| 52 |
def time_data():
|
| 53 |
current_state = get_current_status()
|
|
|
|
| 54 |
if current_state['status'] in ['ramadan_live', 'eid_live']:
|
| 55 |
+
return jsonify({"status": "finished", "msg": current_state['msg'], "hijri": current_state['hijri_str']})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
diff = current_state['target_date'] - datetime.now()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
if diff.total_seconds() <= 0:
|
| 59 |
+
return jsonify({"status": "loading", "hijri": current_state['hijri_str']})
|
| 60 |
|
| 61 |
days = diff.days
|
| 62 |
hours, rem = divmod(diff.seconds, 3600)
|
| 63 |
minutes, seconds = divmod(rem, 60)
|
| 64 |
+
return jsonify({"status": "counting", "days": f"{days:02}", "hours": f"{hours:02}", "minutes": f"{minutes:02}", "seconds": f"{seconds:02}", "hijri": current_state['hijri_str']})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
|
|
|
|
|
|
|
|
|
| 66 |
@app.route('/')
|
| 67 |
def home():
|
| 68 |
html_content = """
|
|
|
|
| 84 |
.hijri-badge { background: #4CAF50; padding: 5px 20px; border-radius: 50px; font-size: 20px; margin-bottom: 20px; display: inline-block; }
|
| 85 |
.title { font-size: 40px; margin-bottom: 30px; text-shadow: 2px 2px 10px rgba(0,0,0,0.8); }
|
| 86 |
|
| 87 |
+
/* الحاوية الأساسية للعد التنازلي */
|
| 88 |
+
.countdown {
|
| 89 |
+
display: flex;
|
| 90 |
+
gap: 15px;
|
| 91 |
+
justify-content: center;
|
| 92 |
+
direction: ltr;
|
| 93 |
+
align-items: flex-start; /* محاذاة العناصر من الأعلى لضمان تناسق الوحدات */
|
| 94 |
+
}
|
| 95 |
|
| 96 |
.card {
|
| 97 |
background: linear-gradient(180deg, #4CAF50, #2E7D32);
|
|
|
|
| 99 |
font-size: 70px; font-weight: 900; display: flex;
|
| 100 |
justify-content: center; align-items: center;
|
| 101 |
box-shadow: 0 8px 15px rgba(0,0,0,0.5);
|
| 102 |
+
line-height: 1; /* لضمان توسط الرقم داخل المربع */
|
| 103 |
}
|
| 104 |
.unit { display: flex; flex-direction: column; align-items: center; }
|
| 105 |
+
.label { margin-top: 10px; font-size: 18px; color: #4CAF50; font-weight: bold; }
|
| 106 |
|
| 107 |
+
/* التعديل المطلوب: وضع النقاط في نصف ارتفاع المربع بالضبط */
|
| 108 |
.colon {
|
| 109 |
+
font-size: 60px;
|
| 110 |
color: #4CAF50;
|
| 111 |
font-weight: 900;
|
| 112 |
+
height: 140px; /* نفس ارتفاع الـ card */
|
| 113 |
+
display: flex;
|
| 114 |
+
align-items: center; /* يتوسط عمودياً بالنسبة للـ 140px */
|
| 115 |
+
justify-content: center;
|
| 116 |
+
line-height: 1;
|
| 117 |
+
margin-top: 0;
|
| 118 |
}
|
| 119 |
|
| 120 |
#celebration { display: none; font-size: 60px; color: #FFD700; animation: pulse 2s infinite; }
|
|
|
|
| 152 |
} else {
|
| 153 |
document.getElementById('main-ui').style.display = 'block';
|
| 154 |
document.getElementById('celebration').style.display = 'none';
|
| 155 |
+
document.getElementById('days').innerText = data.days || "00";
|
| 156 |
+
document.getElementById('hours').innerText = data.hours || "00";
|
| 157 |
+
document.getElementById('minutes').innerText = data.minutes || "00";
|
| 158 |
+
document.getElementById('seconds').innerText = data.seconds || "00";
|
| 159 |
}
|
| 160 |
});
|
| 161 |
}
|
|
|
|
| 167 |
"""
|
| 168 |
return html_content
|
| 169 |
|
|
|
|
|
|
|
|
|
|
| 170 |
def start_stream():
|
| 171 |
+
if not STREAM_KEY: return
|
|
|
|
|
|
|
|
|
|
| 172 |
while True:
|
| 173 |
current_state = get_current_status()
|
|
|
|
|
|
|
| 174 |
if current_state['status'] == 'ramadan_live':
|
| 175 |
+
display_text, box_color, title_text, font_size = "Ramadan Mubarak", "0xD4AF37", "RAMADAN KAREEM", "65"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
elif current_state['status'] == 'eid_live':
|
| 177 |
+
display_text, box_color, title_text, font_size = "Eid Mubarak", "0x9C27B0", "EID AL-FITR", "90"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
else:
|
| 179 |
+
target_ts = int(current_state['target_date'].timestamp()) if current_state['target_date'] else int(time.time())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
display_text = f" %{{pts\\:localtime\\:{target_ts}\\:%d}} : %{{pts\\:localtime\\:{target_ts}\\:%H}} : %{{pts\\:localtime\\:{target_ts}\\:%M}} : %{{pts\\:localtime\\:{target_ts}\\:%S}} "
|
| 181 |
+
box_color, title_text, font_size = "0x4CAF50", "RAMADAN COUNTDOWN", "90"
|
|
|
|
|
|
|
| 182 |
|
|
|
|
|
|
|
| 183 |
ffmpeg_cmd = [
|
| 184 |
+
'ffmpeg', '-re', '-stream_loop', '-1', '-i', TICK_SOUND_FILE,
|
| 185 |
+
'-loop', '1', '-i', BG_IMAGE, '-map', '1:v', '-map', '0:a',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
'-vf', (
|
| 187 |
f"scale=1280:720,setsar=1,"
|
| 188 |
f"drawbox=y=ih/2-80:color=black@0.6:width=iw:height=200:t=fill,"
|
|
|
|
| 191 |
f"drawtext=text='{display_text}':x=(w-text_w)/2:y=(h-text_h)/2+50:fontsize={font_size}:fontcolor=white:box=1:boxcolor={box_color}@1:boxborderw=25"
|
| 192 |
),
|
| 193 |
'-vcodec', 'libx264', '-preset', 'veryfast', '-b:v', '3000k', '-maxrate', '3000k', '-bufsize', '6000k',
|
| 194 |
+
'-pix_fmt', 'yuv420p', '-g', '60', '-c:a', 'aac', '-b:a', '128k', '-f', 'flv', f"{STREAM_URL}{STREAM_KEY}"
|
|
|
|
|
|
|
|
|
|
| 195 |
]
|
|
|
|
| 196 |
try:
|
| 197 |
subprocess.run(ffmpeg_cmd, check=True)
|
| 198 |
+
except:
|
|
|
|
| 199 |
time.sleep(5)
|
| 200 |
|
| 201 |
if __name__ == "__main__":
|
|
|
|
| 202 |
threading.Thread(target=lambda: app.run(host='0.0.0.0', port=7860, debug=False, use_reloader=False), daemon=True).start()
|
| 203 |
start_stream()
|