Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import time
|
|
|
|
| 3 |
import threading
|
| 4 |
import requests
|
| 5 |
import asyncio
|
|
@@ -65,7 +66,7 @@ async def db_query(func):
|
|
| 65 |
|
| 66 |
# ==================== UNIVERSAL MEDIA HELPER ====================
|
| 67 |
def get_media_obj(msg):
|
| 68 |
-
"""মেসেজ থেকে
|
| 69 |
if not msg:
|
| 70 |
return None
|
| 71 |
if msg.video:
|
|
@@ -79,7 +80,7 @@ def get_media_obj(msg):
|
|
| 79 |
return None
|
| 80 |
|
| 81 |
def get_msg_file_id(msg):
|
| 82 |
-
"""মেসেজ থেকে ক্র্যাশ-ফ্রি
|
| 83 |
if not msg:
|
| 84 |
return None
|
| 85 |
if msg.photo:
|
|
@@ -92,37 +93,37 @@ def get_msg_file_id(msg):
|
|
| 92 |
|
| 93 |
# ==================== CUSTOM VIDEO STREAMING ENGINE ====================
|
| 94 |
def get_file_stream(message_id):
|
| 95 |
-
"""টেলিগ্রামের স্টোরেজ চ্যানেল থেকে
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
try:
|
| 98 |
msg = await bot.get_messages(STORAGE_CHANNEL_ID, message_id)
|
| 99 |
media = get_media_obj(msg)
|
| 100 |
if not media:
|
|
|
|
| 101 |
return
|
| 102 |
|
| 103 |
-
#
|
| 104 |
async for chunk in bot.stream_media(media):
|
| 105 |
-
|
| 106 |
except Exception as e:
|
| 107 |
-
print(f"Error in
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
#
|
| 110 |
-
|
| 111 |
-
def run():
|
| 112 |
-
asyncio.set_event_loop(loop)
|
| 113 |
-
gen = stream_generator()
|
| 114 |
while True:
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
yield chunk
|
| 118 |
-
except StopAsyncIteration:
|
| 119 |
break
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
loop.close()
|
| 124 |
-
|
| 125 |
-
return run()
|
| 126 |
|
| 127 |
@app.route('/stream/<int:message_id>')
|
| 128 |
def stream_video(message_id):
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
+
import queue # thread-safe queue ইম্পোর্ট করা হলো
|
| 4 |
import threading
|
| 5 |
import requests
|
| 6 |
import asyncio
|
|
|
|
| 66 |
|
| 67 |
# ==================== UNIVERSAL MEDIA HELPER ====================
|
| 68 |
def get_media_obj(msg):
|
| 69 |
+
"""মেসেজ থেকে মিডিয়া অবজেক্ট (ভিডিও, জিআইএফ, ডকুমেন্ট) খুঁজে বের করার ফাংশন"""
|
| 70 |
if not msg:
|
| 71 |
return None
|
| 72 |
if msg.video:
|
|
|
|
| 80 |
return None
|
| 81 |
|
| 82 |
def get_msg_file_id(msg):
|
| 83 |
+
"""মেসেজ থেকে ক্র্যাশ-ফ্রিfile_id বের করার ফাংশন"""
|
| 84 |
if not msg:
|
| 85 |
return None
|
| 86 |
if msg.photo:
|
|
|
|
| 93 |
|
| 94 |
# ==================== CUSTOM VIDEO STREAMING ENGINE ====================
|
| 95 |
def get_file_stream(message_id):
|
| 96 |
+
"""টেলিগ্রামের স্টোরেজ চ্যানেল থেকে মেইন ইভেন্ট লুপে থ্রেড-সেফ কিউ ব্যবহার করে ডাটা স্ট্রিম করার ফাংশন"""
|
| 97 |
+
q = queue.Queue(maxsize=10) # মেমোরি নিয়ন্ত্রণে রাখার জন্য সর্বোচ্চ সাইজ ১০ রাখা হয়েছে
|
| 98 |
+
|
| 99 |
+
async def producer():
|
| 100 |
try:
|
| 101 |
msg = await bot.get_messages(STORAGE_CHANNEL_ID, message_id)
|
| 102 |
media = get_media_obj(msg)
|
| 103 |
if not media:
|
| 104 |
+
q.put(None)
|
| 105 |
return
|
| 106 |
|
| 107 |
+
# ডাটা রিড করে থ্রেড-সেফ কিউ-তে রাখা হচ্ছে
|
| 108 |
async for chunk in bot.stream_media(media):
|
| 109 |
+
q.put(chunk)
|
| 110 |
except Exception as e:
|
| 111 |
+
print(f"Error in stream producer: {e}")
|
| 112 |
+
finally:
|
| 113 |
+
q.put(None) # স্ট্রিম শেষ হওয়ার সিগন্যাল
|
| 114 |
+
|
| 115 |
+
# প্রোডিউসারটিকে বটের নিজের মেইন ইভেন্ট লুপে রান করানো হলো
|
| 116 |
+
asyncio.run_coroutine_threadsafe(producer(), main_loop)
|
| 117 |
|
| 118 |
+
# ফ্লাস্কের জন্য সিনক্রোনাস কন্সুমার জেনারেটর
|
| 119 |
+
def consumer():
|
|
|
|
|
|
|
|
|
|
| 120 |
while True:
|
| 121 |
+
chunk = q.get()
|
| 122 |
+
if chunk is None:
|
|
|
|
|
|
|
| 123 |
break
|
| 124 |
+
yield chunk
|
| 125 |
+
|
| 126 |
+
return consumer()
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
@app.route('/stream/<int:message_id>')
|
| 129 |
def stream_video(message_id):
|