Alide21 commited on
Commit
7952180
·
verified ·
1 Parent(s): 81bd079

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +106 -61
main.py CHANGED
@@ -1,38 +1,57 @@
1
  import os
2
  import time
3
  import subprocess
 
 
4
  from datetime import datetime
5
  from flask import Flask, jsonify
6
- import threading
7
 
8
  # ==========================================
9
- # إعدادات التاريخ والبث
10
  # ==========================================
11
- TARGET_DATE = datetime(2026, 2, 19, 0, 0, 0)
12
  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
  app = Flask(__name__)
17
 
18
- @app.route('/time_data')
19
- def time_data():
20
- diff = TARGET_DATE - datetime.now()
21
- if diff.total_seconds() < 0:
22
- return jsonify({'days': '00', 'hours': '00', 'minutes': '00', 'seconds': '00'})
 
 
 
 
 
 
 
 
 
 
 
23
  days = diff.days
24
  hours, rem = divmod(diff.seconds, 3600)
25
  minutes, seconds = divmod(rem, 60)
26
- return jsonify({
27
- 'days': f"{days:02}",
28
- 'hours': f"{hours:02}",
29
- 'minutes': f"{minutes:02}",
30
- 'seconds': f"{seconds:02}"
31
- })
 
 
 
 
 
 
 
32
 
33
  @app.route('/')
34
  def home():
35
- # استخدمنا رندر مباشر للنص لتجنب مشاكل f-string مع JavaScript
36
  html_content = """
37
  <!DOCTYPE html>
38
  <html lang="ar" dir="rtl">
@@ -44,46 +63,56 @@ def home():
44
  body { background-color: #0f0f0f; color: white; font-family: 'Cairo', sans-serif; margin: 0; overflow: hidden; }
45
  .video-player {
46
  position: relative; width: 100vw; height: 100vh;
47
- background: url('""" + BG_IMAGE + """') no-repeat center;
48
  background-size: cover; display: flex; flex-direction: column; justify-content: center; align-items: center;
49
  }
50
- .overlay { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.6); }
51
- .content { z-index: 10; text-align: center; }
52
- .title { font-size: 45px; margin-bottom: 30px; text-shadow: 2px 2px 10px rgba(0,0,0,0.8); }
53
-
54
- .countdown { display: flex; gap: 20px; direction: ltr; }
55
- .unit { display: flex; flex-direction: column; align-items: center; }
56
  .card {
57
  background: linear-gradient(180deg, #4CAF50, #2E7D32);
58
- width: 120px; height: 160px; border-radius: 12px;
59
- font-size: 80px; font-weight: 900; display: flex;
60
  justify-content: center; align-items: center;
61
- box-shadow: 0 10px 20px rgba(0,0,0,0.4);
62
- border: 1px solid rgba(255,255,255,0.1);
63
  }
64
- .label { margin-top: 10px; font-size: 20px; color: #4CAF50; font-weight: bold; }
 
 
65
  </style>
66
  </head>
67
  <body>
68
  <div class="video-player">
69
  <div class="overlay"></div>
70
  <div class="content">
71
- <div class="title">الوقت المتبقي على رمضان 2026</div>
72
- <div class="countdown">
73
- <div class="unit"><div class="card" id="days">00</div><div class="label">أيام</div></div>
74
- <div class="unit"><div class="card" id="hours">00</div><div class="label">ساعة</div></div>
75
- <div class="unit"><div class="card" id="minutes">00</div><div class="label">دقيقة</div></div>
76
- <div class="unit"><div class="card" id="seconds">00</div><div class="label">ثانية</div></div>
 
 
 
77
  </div>
 
78
  </div>
79
  </div>
80
  <script>
81
  function update() {
82
  fetch('/time_data').then(res => res.json()).then(data => {
83
- document.getElementById('days').innerText = data.days;
84
- document.getElementById('hours').innerText = data.hours;
85
- document.getElementById('minutes').innerText = data.minutes;
86
- document.getElementById('seconds').innerText = data.seconds;
 
 
 
 
 
 
87
  });
88
  }
89
  setInterval(update, 1000);
@@ -95,29 +124,45 @@ def home():
95
  return html_content
96
 
97
  def start_stream():
98
- if not STREAM_KEY: return
99
- target_ts = int(TARGET_DATE.timestamp())
100
-
101
- # تحسين جودة فيديو اليوتيوب ليكون مشابهاً للتصميم الأخضر
102
- ffmpeg_cmd = [
103
- 'ffmpeg', '-re', '-loop', '1', '-i', BG_IMAGE,
104
- '-f', 'lavfi', '-i', 'anullsrc',
105
- '-vf', (
106
- f"scale=1280:720,setsar=1,"
107
- f"drawbox=y=ih/2-100:color=black@0.6:width=iw:height=250:t=fill," # شريط خلفية للعداد
108
- f"drawtext=text='Ramadan 2026':x=(w-text_w)/2:y=200:fontsize=60:fontcolor=white,"
109
- # العداد الأخضر في البث
110
- f"drawtext=text=' %{{pts\\:localtime\\:{target_ts}\\:%d}} %{{pts\\:localtime\\:{target_ts}\\:%H}} %{{pts\\:localtime\\:{target_ts}\\:%M}} %{{pts\\:localtime\\:{target_ts}\\:%S}} ':"
111
- f"x=(w-text_w)/2:y=(h-text_h)/2+30:fontsize=100:fontcolor=white:box=1:boxcolor=#4CAF50@1:boxborderw=30"
112
- ),
113
- '-vcodec', 'libx264', '-preset', 'veryfast', '-b:v', '3500k', '-maxrate', '3500k', '-bufsize', '7000k',
114
- '-pix_fmt', 'yuv420p', '-g', '60', '-f', 'flv',
115
- f"{STREAM_URL}{STREAM_KEY}"
116
- ]
117
- subprocess.run(ffmpeg_cmd)
118
 
119
- if __name__ == "__main__":
120
- threading.Thread(target=lambda: app.run(host='0.0.0.0', port=7860), daemon=True).start()
121
  while True:
122
- try: start_stream()
123
- except: time.sleep(5)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import time
3
  import subprocess
4
+ import threading
5
+ import requests
6
  from datetime import datetime
7
  from flask import Flask, jsonify
 
8
 
9
  # ==========================================
10
+ # الإعدادات الأساسية
11
  # ==========================================
12
+ TARGET_DATE = datetime(2026, 2, 19, 0, 0, 0) # موعد رمضان المتوقع
13
  STREAM_KEY = os.environ.get("STREAM_KEY")
14
  STREAM_URL = "rtmp://a.rtmp.youtube.com/live2/"
15
  BG_IMAGE = "https://images.unsplash.com/photo-1555400038-63f5ba517a47?q=80&w=1920&auto=format&fit=crop"
16
 
17
  app = Flask(__name__)
18
 
19
+ # دالة لجلب البيانات من الـ API أو الحساب المحلي
20
+ def get_detailed_data():
21
+ now = datetime.now()
22
+ hijri_date = ""
23
+ try:
24
+ # جلب التاريخ الهجري من API (توقيت مكة)
25
+ res = requests.get("http://api.aladhan.com/v1/timingsByCity?city=Mecca&country=SA", timeout=5)
26
+ if res.status_code == 200:
27
+ hijri_date = res.json()['data']['date']['hijri']['date']
28
+ except:
29
+ hijri_date = "جاري التحديث..."
30
+
31
+ diff = TARGET_DATE - now
32
+ if diff.total_seconds() <= 0:
33
+ return {"status": "finished", "msg": "جاء رمضان.. مبارك عليكم الشهر", "hijri": hijri_date}
34
+
35
  days = diff.days
36
  hours, rem = divmod(diff.seconds, 3600)
37
  minutes, seconds = divmod(rem, 60)
38
+
39
+ return {
40
+ "status": "counting",
41
+ "days": f"{days:02}",
42
+ "hours": f"{hours:02}",
43
+ "minutes": f"{minutes:02}",
44
+ "seconds": f"{seconds:02}",
45
+ "hijri": hijri_date
46
+ }
47
+
48
+ @app.route('/time_data')
49
+ def time_data():
50
+ return jsonify(get_detailed_data())
51
 
52
  @app.route('/')
53
  def home():
54
+ # كود الـ HTML مفصل تماماً عن الـ f-string لتجنب أخطاء Syntax
55
  html_content = """
56
  <!DOCTYPE html>
57
  <html lang="ar" dir="rtl">
 
63
  body { background-color: #0f0f0f; color: white; font-family: 'Cairo', sans-serif; margin: 0; overflow: hidden; }
64
  .video-player {
65
  position: relative; width: 100vw; height: 100vh;
66
+ background: url('""" + BG_IMAGE + """') no-repeat center center;
67
  background-size: cover; display: flex; flex-direction: column; justify-content: center; align-items: center;
68
  }
69
+ .overlay { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); }
70
+ .content { z-index: 10; text-align: center; width: 100%; }
71
+ .hijri-badge { background: #4CAF50; padding: 5px 20px; border-radius: 50px; font-size: 20px; margin-bottom: 20px; display: inline-block; }
72
+ .title { font-size: 40px; margin-bottom: 30px; text-shadow: 2px 2px 10px rgba(0,0,0,0.8); }
73
+ .countdown { display: flex; gap: 15px; justify-content: center; direction: ltr; }
 
74
  .card {
75
  background: linear-gradient(180deg, #4CAF50, #2E7D32);
76
+ width: 110px; height: 140px; border-radius: 15px;
77
+ font-size: 70px; font-weight: 900; display: flex;
78
  justify-content: center; align-items: center;
79
+ box-shadow: 0 8px 15px rgba(0,0,0,0.5);
 
80
  }
81
+ .label { margin-top: 8px; font-size: 18px; color: #4CAF50; font-weight: bold; }
82
+ #celebration { display: none; font-size: 60px; color: #FFD700; animation: pulse 2s infinite; }
83
+ @keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.1); } }
84
  </style>
85
  </head>
86
  <body>
87
  <div class="video-player">
88
  <div class="overlay"></div>
89
  <div class="content">
90
+ <div class="hijri-badge" id="hijri-top">--/--/----</div>
91
+ <div id="main-ui">
92
+ <div class="title">الوقت المتبقي على شهر رمضان 2026</div>
93
+ <div class="countdown">
94
+ <div class="unit"><div class="card" id="days">00</div><div class="label">أيام</div></div>
95
+ <div class="unit"><div class="card" id="hours">00</div><div class="label">ساعة</div></div>
96
+ <div class="unit"><div class="card" id="minutes">00</div><div class="label">دقيقة</div></div>
97
+ <div class="unit"><div class="card" id="seconds">00</div><div class="label">ثانية</div></div>
98
+ </div>
99
  </div>
100
+ <div id="celebration">قد جاء رمضان.. مبارك عليكم الشهر</div>
101
  </div>
102
  </div>
103
  <script>
104
  function update() {
105
  fetch('/time_data').then(res => res.json()).then(data => {
106
+ document.getElementById('hijri-top').innerText = data.hijri;
107
+ if(data.status === 'finished') {
108
+ document.getElementById('main-ui').style.display = 'none';
109
+ document.getElementById('celebration').style.display = 'block';
110
+ } else {
111
+ document.getElementById('days').innerText = data.days;
112
+ document.getElementById('hours').innerText = data.hours;
113
+ document.getElementById('minutes').innerText = data.minutes;
114
+ document.getElementById('seconds').innerText = data.seconds;
115
+ }
116
  });
117
  }
118
  setInterval(update, 1000);
 
124
  return html_content
125
 
126
  def start_stream():
127
+ if not STREAM_KEY:
128
+ print("Error: STREAM_KEY not found.")
129
+ return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
 
 
 
131
  while True:
132
+ data = get_detailed_data()
133
+ target_ts = int(TARGET_DATE.timestamp())
134
+
135
+ # إعداد النص ليوتيوب (بما أن FFmpeg لا يدعم العربية بسهولة، نستخدم الإنجليزية أو أرقام واضحة)
136
+ if data['status'] == 'finished':
137
+ display_text = "RAMADAN KAREEM - MUBARAK"
138
+ box_color = "0xD4AF37" # ذهبي للعيد/رمضان
139
+ else:
140
+ # تنسيق العداد: أيام : ساعات : دقائق : ثواني
141
+ display_text = f" %{{pts\\:localtime\\:{target_ts}\\:%d}}d : %{{H}}h : %{{M}}m : %{{S}}s "
142
+ box_color = "0x4CAF50" # أخضر للانتظار
143
+
144
+ ffmpeg_cmd = [
145
+ 'ffmpeg', '-re', '-loop', '1', '-i', BG_IMAGE,
146
+ '-f', 'lavfi', '-i', 'anullsrc',
147
+ '-vf', (
148
+ f"scale=1280:720,setsar=1,"
149
+ f"drawbox=y=ih/2-80:color=black@0.6:width=iw:height=200:t=fill," # خلفية العداد
150
+ f"drawtext=text='{data['hijri']}':x=50:y=50:fontsize=30:fontcolor=white:box=1:boxcolor=0x4CAF50@0.8," # التاريخ الهجري
151
+ f"drawtext=text='RAMADAN 2026 COUNTDOWN':x=(w-text_w)/2:y=230:fontsize=40:fontcolor=white,"
152
+ f"drawtext=text='{display_text}':x=(w-text_w)/2:y=(h-text_h)/2+50:fontsize=90:fontcolor=white:box=1:boxcolor={box_color}@1:boxborderw=25"
153
+ ),
154
+ '-vcodec', 'libx264', '-preset', 'veryfast', '-b:v', '3000k', '-maxrate', '3000k', '-bufsize', '6000k',
155
+ '-pix_fmt', 'yuv420p', '-g', '60', '-f', 'flv', f"{STREAM_URL}{STREAM_KEY}"
156
+ ]
157
+
158
+ try:
159
+ subprocess.run(ffmpeg_cmd, check=True)
160
+ except Exception as e:
161
+ print(f"Stream error: {e}")
162
+ time.sleep(5)
163
+
164
+ if __name__ == "__main__":
165
+ # تشغيل سيرفر الويب في خلفية مستقلة
166
+ threading.Thread(target=lambda: app.run(host='0.0.0.0', port=7860, debug=False, use_reloader=False), daemon=True).start()
167
+ # بدء البث المباشر
168
+ start_stream()