Reoo88 commited on
Commit
eea3019
·
verified ·
1 Parent(s): 58d4ac8

Upload 2 files

Browse files
Files changed (1) hide show
  1. streamlit_app.py +79 -7
streamlit_app.py CHANGED
@@ -1,15 +1,79 @@
1
- import os, time, threading, subprocess, requests
2
  import streamlit as st
3
 
4
  # ---------- إعدادات ----------
5
  COMMENTS_URL = "https://reoo88-signserver.hf.space/comments"
6
  COMMENTS_FILE = "/tmp/comments.txt"
7
- FONT_PATH = "/usr/share/fonts/opentype/fonts-hosny-amiri/Amiri-Regular.ttf"
8
  MAX_LINES = 6
9
  MAX_LINE_LEN = 50
10
  RECONNECT_MAX = 5
11
 
12
- # تهيئة ملف التعليقات
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  if not os.path.exists(COMMENTS_FILE):
14
  with open(COMMENTS_FILE, "w", encoding="utf-8") as f:
15
  f.write(" ")
@@ -24,16 +88,23 @@ def fetch_comments_once():
24
  r = requests.get(COMMENTS_URL, timeout=5)
25
  if r.status_code != 200:
26
  return
27
- lines = [ln.strip() for ln in r.text.strip().split("\n") if ln.strip()][-MAX_LINES:]
28
- lines = [(ln[:MAX_LINE_LEN] + "...") if len(ln) > MAX_LINE_LEN else ln for ln in lines]
29
- new_txt = "\n".join(lines) if lines else " "
 
 
 
 
 
 
 
30
 
31
  tmp = COMMENTS_FILE + ".tmp"
32
  with text_lock:
33
  with open(tmp, "w", encoding="utf-8") as f:
34
  f.write(new_txt)
35
  os.replace(tmp, COMMENTS_FILE)
36
- print(f"✅ تم تحديث التعليقات ({len(lines)} سطر)")
37
  except Exception as e:
38
  print(f"❌ خطأ بالتعليقات: {e}")
39
 
@@ -149,6 +220,7 @@ def start_stream(yt_key, username):
149
  # ---------- واجهة Streamlit ----------
150
  st.set_page_config(page_title="بث تيك توك + تعليقات", page_icon="📹")
151
  st.title("📹 بث تيك توك مع تعليقات عربية مباشرة")
 
152
 
153
  with st.form("stream_form"):
154
  user = st.text_input("معرف تيك توك (بدون @)", "majd.alban")
 
1
+ import os, re, time, threading, subprocess, urllib.request, requests
2
  import streamlit as st
3
 
4
  # ---------- إعدادات ----------
5
  COMMENTS_URL = "https://reoo88-signserver.hf.space/comments"
6
  COMMENTS_FILE = "/tmp/comments.txt"
 
7
  MAX_LINES = 6
8
  MAX_LINE_LEN = 50
9
  RECONNECT_MAX = 5
10
 
11
+ # ---------- إعداد الخط (Palestine) ----------
12
+ LOCAL_FONT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "Palestine-Regular.ttf")
13
+ TMP_FONT = "/tmp/Palestine-Regular.ttf"
14
+ FONT_URL = "https://cdn.jsdelivr.net/gh/mokhtarbsaid/rare-arabic-fonts/palestine/palestine-regular.ttf"
15
+
16
+
17
+ def setup_font() -> str:
18
+ # 1) خط محلّي في المستودع
19
+ if os.path.exists(LOCAL_FONT) and os.path.getsize(LOCAL_FONT) > 10000:
20
+ print(f"[INFO] ✅ Palestine محلّي: {LOCAL_FONT}")
21
+ return LOCAL_FONT
22
+ # 2) تنزيل من jsDelivr
23
+ if not os.path.exists(TMP_FONT) or os.path.getsize(TMP_FONT) < 10000:
24
+ try:
25
+ req = urllib.request.Request(FONT_URL, headers={"User-Agent": "Mozilla/5.0"})
26
+ with urllib.request.urlopen(req, timeout=15) as r, open(TMP_FONT, "wb") as f:
27
+ f.write(r.read())
28
+ print(f"[INFO] ✅ تم تنزيل Palestine ({os.path.getsize(TMP_FONT)} bytes)")
29
+ return TMP_FONT
30
+ except Exception as e:
31
+ print(f"[WARN] فشل تنزيل Palestine: {e}")
32
+ if os.path.exists(TMP_FONT):
33
+ return TMP_FONT
34
+ # 3) احتياط: Amiri من apt
35
+ fallback = "/usr/share/fonts/opentype/fonts-hosny-amiri/Amiri-Regular.ttf"
36
+ print(f"[WARN] استخدام احتياط: {fallback}")
37
+ return fallback
38
+
39
+
40
+ FONT_PATH = setup_font()
41
+
42
+ # ---------- حذف الإيموجي ----------
43
+ # نطاقات Unicode للإيموجي + الرموز التي لا تدعمها الخطوط العربية
44
+ EMOJI_PATTERN = re.compile(
45
+ "["
46
+ "\U0001F600-\U0001F64F" # وجوه (emoticons)
47
+ "\U0001F300-\U0001F5FF" # رموز ومجاميع
48
+ "\U0001F680-\U0001F6FF" # نقل وخرائط
49
+ "\U0001F700-\U0001F77F" # خيمياء
50
+ "\U0001F780-\U0001F7FF" # أشكال هندسية
51
+ "\U0001F800-\U0001F8FF" # أسهم تكميلية
52
+ "\U0001F900-\U0001F9FF" # رموز تكميلية ومخططات
53
+ "\U0001FA00-\U0001FA6F" # شطرنج + رموز إضافية
54
+ "\U0001FA70-\U0001FAFF" # رموز وأشكال تكميلية
55
+ "\U00002600-\U000026FF" # رموز متنوّعة (☀ ☁ ⚡...)
56
+ "\U00002700-\U000027BF" # Dingbats (✂ ✈ ❤...)
57
+ "\U0001F1E0-\U0001F1FF" # أعلام الدول
58
+ "\U0000FE00-\U0000FE0F" # variation selectors
59
+ "\U0001F000-\U0001F02F" # mahjong
60
+ "\U0001F0A0-\U0001F0FF" # ورق لعب
61
+ "\U00002B00-\U00002BFF" # أسهم متنوّعة
62
+ "\U000024C2-\U0001F251" # رموز محصورة
63
+ "\U0000200D" # ZWJ (Zero Width Joiner للإيموجي المركّبة)
64
+ "]+",
65
+ flags=re.UNICODE,
66
+ )
67
+
68
+
69
+ def clean_text(text: str) -> str:
70
+ """يحذف الإيموجي ويُنظّف الفراغات المتعدّدة."""
71
+ cleaned = EMOJI_PATTERN.sub("", text)
72
+ cleaned = re.sub(r"\s+", " ", cleaned).strip()
73
+ return cleaned
74
+
75
+
76
+ # ---------- تهيئة ----------
77
  if not os.path.exists(COMMENTS_FILE):
78
  with open(COMMENTS_FILE, "w", encoding="utf-8") as f:
79
  f.write(" ")
 
88
  r = requests.get(COMMENTS_URL, timeout=5)
89
  if r.status_code != 200:
90
  return
91
+ raw_lines = [ln.strip() for ln in r.text.strip().split("\n") if ln.strip()]
92
+ clean_lines = []
93
+ for ln in raw_lines:
94
+ c = clean_text(ln)
95
+ if c: # تجاهل الأسطر الفارغة بعد التنظيف
96
+ if len(c) > MAX_LINE_LEN:
97
+ c = c[:MAX_LINE_LEN] + "..."
98
+ clean_lines.append(c)
99
+ clean_lines = clean_lines[-MAX_LINES:]
100
+ new_txt = "\n".join(clean_lines) if clean_lines else " "
101
 
102
  tmp = COMMENTS_FILE + ".tmp"
103
  with text_lock:
104
  with open(tmp, "w", encoding="utf-8") as f:
105
  f.write(new_txt)
106
  os.replace(tmp, COMMENTS_FILE)
107
+ print(f"✅ تم تحديث التعليقات ({len(clean_lines)} سطر)")
108
  except Exception as e:
109
  print(f"❌ خطأ بالتعليقات: {e}")
110
 
 
220
  # ---------- واجهة Streamlit ----------
221
  st.set_page_config(page_title="بث تيك توك + تعليقات", page_icon="📹")
222
  st.title("📹 بث تيك توك مع تعليقات عربية مباشرة")
223
+ st.caption(f"الخط: {os.path.basename(FONT_PATH)} · الإيموجي تُحذف تلقائياً")
224
 
225
  with st.form("stream_form"):
226
  user = st.text_input("معرف تيك توك (بدون @)", "majd.alban")