Update streamlit_app.py
Browse files- streamlit_app.py +25 -16
streamlit_app.py
CHANGED
|
@@ -1,28 +1,34 @@
|
|
| 1 |
-
import os, time, threading, subprocess, urllib.request, requests
|
| 2 |
import streamlit as st
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
| 6 |
COMMENTS_FILE = "/tmp/comments.txt"
|
| 7 |
COMMENTS_URL = "https://reoo88-signserver.hf.space/comments"
|
| 8 |
|
| 9 |
if not os.path.exists(FONT_PATH):
|
| 10 |
urllib.request.urlretrieve(FONT_URL, FONT_PATH)
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
def fetch_comments():
|
| 18 |
try:
|
| 19 |
r = requests.get(COMMENTS_URL, timeout=5)
|
| 20 |
if r.status_code == 200:
|
|
|
|
|
|
|
|
|
|
| 21 |
with open(COMMENTS_FILE, "w", encoding="utf-8") as f:
|
| 22 |
-
f.write(
|
| 23 |
print("✅ تم تحديث التعليقات")
|
|
|
|
|
|
|
| 24 |
except Exception as e:
|
| 25 |
-
print(f"❌ خطأ: {e}")
|
| 26 |
|
| 27 |
def comments_updater():
|
| 28 |
while True:
|
|
@@ -30,29 +36,32 @@ def comments_updater():
|
|
| 30 |
fetch_comments()
|
| 31 |
|
| 32 |
def start_stream(yt_key, username):
|
|
|
|
|
|
|
|
|
|
| 33 |
threading.Thread(target=comments_updater, daemon=True).start()
|
| 34 |
time.sleep(3)
|
| 35 |
|
| 36 |
sl_cmd = f"streamlink https://www.tiktok.com/@{username}/live best --hls-live-edge 3 --stdout"
|
| 37 |
-
|
| 38 |
-
#
|
| 39 |
ff_cmd = (
|
| 40 |
f"ffmpeg -thread_queue_size 8192 -i pipe:0 "
|
| 41 |
-
f"-vf \"drawtext=textfile='{COMMENTS_FILE}':reload=1:fontfile={FONT_PATH}:fontsize=
|
|
|
|
| 42 |
f"-c:v libx264 -preset ultrafast -tune zerolatency -b:v 1500k -maxrate 2000k -bufsize 3000k -g 30 "
|
| 43 |
f"-c:a aac -ar 48000 -b:a 192k -ac 2 "
|
| 44 |
f"-f flv rtmp://a.rtmp.youtube.com/live2/{yt_key}"
|
| 45 |
)
|
| 46 |
|
| 47 |
full_cmd = f"{sl_cmd} | {ff_cmd}"
|
| 48 |
-
|
| 49 |
-
process.wait()
|
| 50 |
|
| 51 |
-
st.title("بث تيك توك مع تعليقات")
|
| 52 |
with st.form("f"):
|
| 53 |
user = st.text_input("معرف تيك توك", "majd.alban")
|
| 54 |
key = st.text_input("مفتاح يوتيوب", type="password")
|
| 55 |
if st.form_submit_button("ابدأ"):
|
| 56 |
if user and key:
|
| 57 |
threading.Thread(target=start_stream, args=(key, user)).start()
|
| 58 |
-
st.success("تم البث! التعليقات ستظهر
|
|
|
|
| 1 |
+
import os, time, threading, subprocess, urllib.request, requests, re
|
| 2 |
import streamlit as st
|
| 3 |
|
| 4 |
+
# استخدم خط "Cairo" المعروف بدعمه الممتاز للعربية
|
| 5 |
+
FONT_PATH = "/tmp/Cairo.ttf"
|
| 6 |
+
FONT_URL = "https://github.com/google/fonts/raw/main/ofl/cairo/Cairo%5Bwght%5D.ttf"
|
| 7 |
COMMENTS_FILE = "/tmp/comments.txt"
|
| 8 |
COMMENTS_URL = "https://reoo88-signserver.hf.space/comments"
|
| 9 |
|
| 10 |
if not os.path.exists(FONT_PATH):
|
| 11 |
urllib.request.urlretrieve(FONT_URL, FONT_PATH)
|
| 12 |
|
| 13 |
+
def clean_arabic_text(text):
|
| 14 |
+
"""تحتفظ فقط بالحروف العربية والإنجليزية والأرقام والمسافات وعلامات الترقيم الأساسية"""
|
| 15 |
+
# تسمح بالعربية، الإنجليزية، الأرقام، المسافات، النقطتين، @, ., ,, !, ?
|
| 16 |
+
return re.sub(r'[^\u0600-\u06FF\u0750-\u077F\w\s:@.,!?]+', '', text, flags=re.UNICODE)
|
| 17 |
|
| 18 |
def fetch_comments():
|
| 19 |
try:
|
| 20 |
r = requests.get(COMMENTS_URL, timeout=5)
|
| 21 |
if r.status_code == 200:
|
| 22 |
+
# تنظيف النص من الرموز الغريبة
|
| 23 |
+
cleaned_lines = [clean_arabic_text(line) for line in r.text.split('\n')]
|
| 24 |
+
cleaned_text = '\n'.join(cleaned_lines)
|
| 25 |
with open(COMMENTS_FILE, "w", encoding="utf-8") as f:
|
| 26 |
+
f.write(cleaned_text)
|
| 27 |
print("✅ تم تحديث التعليقات")
|
| 28 |
+
else:
|
| 29 |
+
print(f"⚠️ فشل الجلب: {r.status_code}")
|
| 30 |
except Exception as e:
|
| 31 |
+
print(f"❌ خطأ في جلب التعليقات: {e}")
|
| 32 |
|
| 33 |
def comments_updater():
|
| 34 |
while True:
|
|
|
|
| 36 |
fetch_comments()
|
| 37 |
|
| 38 |
def start_stream(yt_key, username):
|
| 39 |
+
# إنشاء ملف فارغ لتجنب أخطاء ffmpeg
|
| 40 |
+
with open(COMMENTS_FILE, "w", encoding="utf-8") as f:
|
| 41 |
+
f.write("")
|
| 42 |
threading.Thread(target=comments_updater, daemon=True).start()
|
| 43 |
time.sleep(3)
|
| 44 |
|
| 45 |
sl_cmd = f"streamlink https://www.tiktok.com/@{username}/live best --hls-live-edge 3 --stdout"
|
| 46 |
+
|
| 47 |
+
# أمر ffmpeg مع drawtext بخط Cairo
|
| 48 |
ff_cmd = (
|
| 49 |
f"ffmpeg -thread_queue_size 8192 -i pipe:0 "
|
| 50 |
+
f"-vf \"drawtext=textfile='{COMMENTS_FILE}':reload=1:fontfile={FONT_PATH}:fontsize=26:fontcolor=white@0.9:"
|
| 51 |
+
f"x=10:y=h-th-50:shadowx=2:shadowy=2:shadowcolor=black@0.6\" "
|
| 52 |
f"-c:v libx264 -preset ultrafast -tune zerolatency -b:v 1500k -maxrate 2000k -bufsize 3000k -g 30 "
|
| 53 |
f"-c:a aac -ar 48000 -b:a 192k -ac 2 "
|
| 54 |
f"-f flv rtmp://a.rtmp.youtube.com/live2/{yt_key}"
|
| 55 |
)
|
| 56 |
|
| 57 |
full_cmd = f"{sl_cmd} | {ff_cmd}"
|
| 58 |
+
subprocess.Popen(full_cmd, shell=True).wait()
|
|
|
|
| 59 |
|
| 60 |
+
st.title("بث تيك توك مع تعليقات عربية نظيفة")
|
| 61 |
with st.form("f"):
|
| 62 |
user = st.text_input("معرف تيك توك", "majd.alban")
|
| 63 |
key = st.text_input("مفتاح يوتيوب", type="password")
|
| 64 |
if st.form_submit_button("ابدأ"):
|
| 65 |
if user and key:
|
| 66 |
threading.Thread(target=start_stream, args=(key, user)).start()
|
| 67 |
+
st.success("تم البث! التعليقات ستظهر في الأسفل بدون رموز.")
|