Reoo88 commited on
Commit
a88bb26
·
verified ·
1 Parent(s): 99b8de5

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +23 -22
streamlit_app.py CHANGED
@@ -4,6 +4,7 @@ import base64
4
  import pickle
5
  import subprocess
6
  import yt_dlp
 
7
  from googleapiclient.discovery import build
8
 
9
  # ==========================================
@@ -13,56 +14,56 @@ YOUTUBE_STREAM_URL = "rtmp://a.rtmp.youtube.com/live2/skqf-rd92-dfa7-74cx-bxr2"
13
  TIKTOK_LIVE_URL = "https://www.tiktok.com/@abdulaziz999989/live"
14
 
15
  # ==========================================
16
- # 2. تهيئة YouTube API
17
  # ==========================================
18
- def get_youtube_client():
19
- b64_token = os.environ.get("YT_TOKEN_B64")
20
- pickle_bytes = base64.b64decode(b64_token)
21
- credentials = pickle.loads(pickle_bytes)
22
- return build('youtube', 'v3', credentials=credentials)
 
 
 
23
 
24
  # ==========================================
25
- # 3. سحب رابط تيك توك المباشر باستخدام yt-dlp
26
  # ==========================================
27
- def get_tiktok_stream_url():
28
  ydl_opts = {
29
  'format': 'best',
30
  'quiet': True,
31
- 'no_warnings': True,
 
32
  }
33
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
34
  try:
35
  info = ydl.extract_info(TIKTOK_LIVE_URL, download=False)
36
  return info.get('url')
37
  except Exception as e:
38
- print(f"[Error] TikTok might be offline or blocked: {e}")
39
  return None
40
 
41
  # ==========================================
42
- # 4. إعادة البث (Restreaming)
43
  # ==========================================
44
  def run_restreamer():
45
- print("[System] Starting Restreaming Radar...")
 
46
 
47
  while True:
48
- tiktok_url = get_tiktok_stream_url()
49
 
50
  if tiktok_url:
51
- print("[System] TikTok stream found! Starting FFmpeg...")
52
-
53
- # أمر FFmpeg لسحب الرابط وإعادة توجيهه ليوتيوب
54
  ffmpeg_cmd = [
55
  'ffmpeg', '-re', '-i', tiktok_url,
56
  '-c:v', 'libx264', '-preset', 'veryfast', '-b:v', '2000k',
57
  '-c:a', 'aac', '-b:a', '128k', '-f', 'flv', YOUTUBE_STREAM_URL
58
  ]
59
-
60
- process = subprocess.Popen(ffmpeg_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
61
- process.wait() # انتظر حتى ينتهي البث
62
- print("[System] Stream ended. Restarting...")
63
  else:
64
- print("[System] TikTok is not live. Waiting 30 seconds...")
65
  time.sleep(30)
66
 
67
  if __name__ == "__main__":
68
- run_restreamer()
 
4
  import pickle
5
  import subprocess
6
  import yt_dlp
7
+ import streamlit as st # للوصول للـ Secrets
8
  from googleapiclient.discovery import build
9
 
10
  # ==========================================
 
14
  TIKTOK_LIVE_URL = "https://www.tiktok.com/@abdulaziz999989/live"
15
 
16
  # ==========================================
17
+ # 2. تجهيز ملف الكوكيز مؤقتاً
18
  # ==========================================
19
+ def setup_cookies():
20
+ # افترضنا أنك وضعت الكوكيز في الـ Secrets باسم TIKTOK_COOKIES
21
+ cookies_data = st.secrets.get("TIKTOK_COOKIES")
22
+ if cookies_data:
23
+ with open("cookies.txt", "w") as f:
24
+ f.write(cookies_data)
25
+ return "cookies.txt"
26
+ return None
27
 
28
  # ==========================================
29
+ # 3. سحب الرابط مع الكوكيز
30
  # ==========================================
31
+ def get_tiktok_stream_url(cookie_file):
32
  ydl_opts = {
33
  'format': 'best',
34
  'quiet': True,
35
+ 'cookiefile': cookie_file, # هنا نقوم بتمرير ملف الكوكيز
36
+ 'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
37
  }
38
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
39
  try:
40
  info = ydl.extract_info(TIKTOK_LIVE_URL, download=False)
41
  return info.get('url')
42
  except Exception as e:
43
+ print(f"[Error] TikTok connection failed (Cookies might be expired): {e}")
44
  return None
45
 
46
  # ==========================================
47
+ # 4. التنفيذ
48
  # ==========================================
49
  def run_restreamer():
50
+ cookie_file = setup_cookies()
51
+ print(f"[System] Starting Restreaming Radar using cookies: {cookie_file}")
52
 
53
  while True:
54
+ tiktok_url = get_tiktok_stream_url(cookie_file)
55
 
56
  if tiktok_url:
57
+ print("[System] Stream link captured! Starting FFmpeg...")
 
 
58
  ffmpeg_cmd = [
59
  'ffmpeg', '-re', '-i', tiktok_url,
60
  '-c:v', 'libx264', '-preset', 'veryfast', '-b:v', '2000k',
61
  '-c:a', 'aac', '-b:a', '128k', '-f', 'flv', YOUTUBE_STREAM_URL
62
  ]
63
+ subprocess.run(ffmpeg_cmd)
 
 
 
64
  else:
65
+ print("[System] Waiting for live stream...")
66
  time.sleep(30)
67
 
68
  if __name__ == "__main__":
69
+ run_restreamer()