Update streamlit_app.py
Browse files- streamlit_app.py +40 -30
streamlit_app.py
CHANGED
|
@@ -1,45 +1,55 @@
|
|
| 1 |
import os, time, asyncio, threading, urllib.request, subprocess
|
| 2 |
import streamlit as st
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# إعدادات
|
| 5 |
FONT_PATH = "/tmp/Palestine.ttf"
|
| 6 |
FONT_URL = "https://github.com/mokhtarbsaid/rare-arabic-fonts/raw/main/palestine/palestine-regular.ttf"
|
| 7 |
COMMENTS_FILE = "/tmp/comments.txt"
|
| 8 |
|
| 9 |
-
# رابط ملف التعليقات الخام من مشروع Alluser (تأكد من تحديثه إذا تغير اسم الفرع أو الملف)
|
| 10 |
-
ALUSER_COMMENTS_URL = "https://huggingface.co/spaces/Reoo88/Alluser/resolve/main/comments.txt"
|
| 11 |
-
|
| 12 |
if not os.path.exists(FONT_PATH):
|
| 13 |
urllib.request.urlretrieve(FONT_URL, FONT_PATH)
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
try:
|
| 18 |
-
|
| 19 |
-
return True
|
| 20 |
except Exception as e:
|
| 21 |
-
print(f"
|
| 22 |
-
return False
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
fetch_comments_from_alluser()
|
| 29 |
|
|
|
|
| 30 |
def start_stream(yt_key, username):
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
f.write("")
|
| 35 |
-
else:
|
| 36 |
-
# امسح المحتوى القديم
|
| 37 |
-
open(COMMENTS_FILE, "w").close()
|
| 38 |
-
|
| 39 |
-
# شغّل خيط تنزيل التعليقات
|
| 40 |
-
threading.Thread(target=comments_updater, daemon=True).start()
|
| 41 |
-
|
| 42 |
-
# أوامر البث (نفس الناجحة سابقاً)
|
| 43 |
sl_cmd = f"streamlink https://www.tiktok.com/@{username}/live best --hls-live-edge 3 --stdout"
|
| 44 |
ff_cmd = (
|
| 45 |
f"ffmpeg -thread_queue_size 8192 -i pipe:0 "
|
|
@@ -53,11 +63,11 @@ def start_stream(yt_key, username):
|
|
| 53 |
)
|
| 54 |
subprocess.Popen(f"{sl_cmd} | {ff_cmd}", shell=True).wait()
|
| 55 |
|
| 56 |
-
st.title("بث تيك توك مع تعليقات
|
| 57 |
with st.form("f"):
|
| 58 |
-
user = st.text_input("معرف تيك توك",
|
| 59 |
key = st.text_input("مفتاح يوتيوب", type="password")
|
| 60 |
if st.form_submit_button("ابدأ"):
|
| 61 |
if user and key:
|
| 62 |
threading.Thread(target=start_stream, args=(key, user)).start()
|
| 63 |
-
st.success("تم البث!
|
|
|
|
| 1 |
import os, time, asyncio, threading, urllib.request, subprocess
|
| 2 |
import streamlit as st
|
| 3 |
+
from TikTokLive import TikTokLiveClient
|
| 4 |
+
from TikTokLive.events import CommentEvent, ConnectEvent
|
| 5 |
|
| 6 |
+
# إعدادات
|
| 7 |
FONT_PATH = "/tmp/Palestine.ttf"
|
| 8 |
FONT_URL = "https://github.com/mokhtarbsaid/rare-arabic-fonts/raw/main/palestine/palestine-regular.ttf"
|
| 9 |
COMMENTS_FILE = "/tmp/comments.txt"
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
if not os.path.exists(FONT_PATH):
|
| 12 |
urllib.request.urlretrieve(FONT_URL, FONT_PATH)
|
| 13 |
|
| 14 |
+
# إنشاء ملف تعليقات فارغ
|
| 15 |
+
with open(COMMENTS_FILE, "w", encoding="utf-8") as f:
|
| 16 |
+
f.write("")
|
| 17 |
+
|
| 18 |
+
comments_list, lock = [], threading.Lock()
|
| 19 |
+
|
| 20 |
+
def update_file():
|
| 21 |
+
with lock:
|
| 22 |
+
text = "\n".join(comments_list[-10:])
|
| 23 |
+
with open(COMMENTS_FILE, "w", encoding="utf-8") as f:
|
| 24 |
+
f.write(text)
|
| 25 |
+
|
| 26 |
+
# عميل التعليقات المباشر
|
| 27 |
+
async def fetch_comments(username):
|
| 28 |
+
client = TikTokLiveClient(unique_id=f"@{username}")
|
| 29 |
+
@client.on(ConnectEvent)
|
| 30 |
+
async def on_connect(_):
|
| 31 |
+
print("✅ متصل بالتعليقات")
|
| 32 |
+
@client.on(CommentEvent)
|
| 33 |
+
async def on_comment(e):
|
| 34 |
+
user = e.user.nickname or e.user.unique_id
|
| 35 |
+
with lock:
|
| 36 |
+
comments_list.append(f"{user}: {e.comment}")
|
| 37 |
+
update_file()
|
| 38 |
try:
|
| 39 |
+
await client.start()
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
+
print(f"❌ خطأ: {e}")
|
|
|
|
| 42 |
|
| 43 |
+
def run_comments(username):
|
| 44 |
+
loop = asyncio.new_event_loop()
|
| 45 |
+
asyncio.set_event_loop(loop)
|
| 46 |
+
loop.run_until_complete(fetch_comments(username))
|
|
|
|
| 47 |
|
| 48 |
+
# تشغيل البث
|
| 49 |
def start_stream(yt_key, username):
|
| 50 |
+
threading.Thread(target=run_comments, args=(username,), daemon=True).start()
|
| 51 |
+
time.sleep(5)
|
| 52 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
sl_cmd = f"streamlink https://www.tiktok.com/@{username}/live best --hls-live-edge 3 --stdout"
|
| 54 |
ff_cmd = (
|
| 55 |
f"ffmpeg -thread_queue_size 8192 -i pipe:0 "
|
|
|
|
| 63 |
)
|
| 64 |
subprocess.Popen(f"{sl_cmd} | {ff_cmd}", shell=True).wait()
|
| 65 |
|
| 66 |
+
st.title("بث تيك توك مع تعليقات")
|
| 67 |
with st.form("f"):
|
| 68 |
+
user = st.text_input("معرف تيك توك", "s.og94")
|
| 69 |
key = st.text_input("مفتاح يوتيوب", type="password")
|
| 70 |
if st.form_submit_button("ابدأ"):
|
| 71 |
if user and key:
|
| 72 |
threading.Thread(target=start_stream, args=(key, user)).start()
|
| 73 |
+
st.success("تم البث!")
|