Spaces:
Running on Zero
Running on Zero
Update TechVJ/start.py
Browse files- TechVJ/start.py +101 -66
TechVJ/start.py
CHANGED
|
@@ -1002,71 +1002,50 @@ async def send_chunked_markdown(sender_client: Client, target_chat, full_html_te
|
|
| 1002 |
)
|
| 1003 |
|
| 1004 |
async def fetch_from_public_web(username: str, msgid: int):
|
| 1005 |
-
"""
|
| 1006 |
-
|
| 1007 |
-
|
| 1008 |
-
|
| 1009 |
-
|
| 1010 |
-
|
| 1011 |
-
|
| 1012 |
-
|
| 1013 |
-
|
| 1014 |
-
|
| 1015 |
-
|
| 1016 |
-
|
| 1017 |
-
|
| 1018 |
-
|
| 1019 |
-
|
| 1020 |
-
|
| 1021 |
-
|
| 1022 |
-
|
| 1023 |
-
|
| 1024 |
-
|
| 1025 |
-
|
| 1026 |
-
|
| 1027 |
-
|
| 1028 |
-
r'<div class="tgme_widget_message_text[^"]*"[^>]*>(.*?)</div>',
|
| 1029 |
-
block_html,
|
| 1030 |
-
re.DOTALL
|
| 1031 |
-
)
|
| 1032 |
-
if not txt_match:
|
| 1033 |
-
return None, None
|
| 1034 |
-
|
| 1035 |
-
inner_html = txt_match.group(1)
|
| 1036 |
-
# <br> को नई लाइन में बदलें
|
| 1037 |
-
inner_html = re.sub(r'<br\s*/?>', '\n', inner_html, flags=re.IGNORECASE)
|
| 1038 |
-
|
| 1039 |
-
# सभी <b>...</b> या <strong>...</strong> शब्दों को निकालें
|
| 1040 |
-
bold_raw = re.findall(r'<(?:b|strong)[^>]*>(.*?)</(?:b|strong)>', inner_html, flags=re.DOTALL | re.IGNORECASE)
|
| 1041 |
-
bold_words = []
|
| 1042 |
-
for bw in bold_raw:
|
| 1043 |
-
clean_bw = html.unescape(re.sub(r'<[^>]+>', '', bw)).strip()
|
| 1044 |
-
if clean_bw:
|
| 1045 |
-
bold_words.append(clean_bw)
|
| 1046 |
-
|
| 1047 |
-
plain_text = html.unescape(re.sub(r'<[^>]+>', '', inner_html)).strip()
|
| 1048 |
-
return plain_text, bold_words
|
| 1049 |
-
except Exception as e:
|
| 1050 |
-
print(f"Public web fetch error: {e}")
|
| 1051 |
-
return None, None
|
| 1052 |
-
|
| 1053 |
-
async def fetch_message_reliably(acc: Client, client: Client, source_chat, msgid: int):
|
| 1054 |
-
try:
|
| 1055 |
-
msg = await asyncio.wait_for(acc.get_messages(source_chat, msgid), timeout=20.0)
|
| 1056 |
-
if msg and not getattr(msg, "empty", False):
|
| 1057 |
-
return msg
|
| 1058 |
-
except Exception:
|
| 1059 |
-
pass
|
| 1060 |
-
|
| 1061 |
-
try:
|
| 1062 |
-
msg = await asyncio.wait_for(client.get_messages(source_chat, msgid), timeout=15.0)
|
| 1063 |
-
if msg and not getattr(msg, "empty", False):
|
| 1064 |
-
return msg
|
| 1065 |
-
except Exception:
|
| 1066 |
-
pass
|
| 1067 |
|
| 1068 |
-
|
| 1069 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1070 |
clean_user = source_chat.replace("@", "").strip()
|
| 1071 |
web_txt, bold_list = await fetch_from_public_web(clean_user, msgid)
|
| 1072 |
if web_txt:
|
|
@@ -1091,18 +1070,74 @@ async def fetch_message_reliably(acc: Client, client: Client, source_chat, msgid
|
|
| 1091 |
web_page = None
|
| 1092 |
return WebMsgWrapper()
|
| 1093 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1094 |
return None
|
| 1095 |
|
| 1096 |
async def process_media(client: Client, acc: Client, user_id: int, source_chat, msgid: int, target_chat, target_topic_id, task: dict, status_msg: Message, current_idx: int, total_count: int):
|
| 1097 |
if batch_temp.IS_BATCH.get(user_id):
|
| 1098 |
raise asyncio.CancelledError()
|
| 1099 |
|
| 1100 |
-
|
|
|
|
| 1101 |
if not msg or getattr(msg, "empty", False) or getattr(msg, "service", False):
|
| 1102 |
return False, 0
|
| 1103 |
|
| 1104 |
is_direct_bot = task.get("is_direct_bot", False)
|
| 1105 |
-
is_upsc_mode = task.get("is_upsc_mode", False)
|
| 1106 |
sender_client = client if is_direct_bot else acc
|
| 1107 |
topic_kwargs = {"message_thread_id": target_topic_id} if target_topic_id and not is_direct_bot else {}
|
| 1108 |
|
|
|
|
| 1002 |
)
|
| 1003 |
|
| 1004 |
async def fetch_from_public_web(username: str, msgid: int):
|
| 1005 |
+
"""टेलीग्राम के Embed और Public दोनों एंडपॉइंट्स से ब्राउज़र हेडर के साथ पोस्ट निकालता है"""
|
| 1006 |
+
headers = {
|
| 1007 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
|
| 1008 |
+
}
|
| 1009 |
+
urls = [
|
| 1010 |
+
f"https://t.me/{username}/{msgid}?embed=1&mode=tme",
|
| 1011 |
+
f"https://t.me/s/{username}/{msgid}"
|
| 1012 |
+
]
|
| 1013 |
+
for url in urls:
|
| 1014 |
+
try:
|
| 1015 |
+
async with aiohttp.ClientSession(headers=headers) as session:
|
| 1016 |
+
async with session.get(url, timeout=15) as resp:
|
| 1017 |
+
if resp.status != 200:
|
| 1018 |
+
continue
|
| 1019 |
+
page_html = await resp.text()
|
| 1020 |
+
|
| 1021 |
+
txt_match = re.search(
|
| 1022 |
+
r'<div class="tgme_widget_message_text[^"]*"[^>]*>(.*?)</div>',
|
| 1023 |
+
page_html,
|
| 1024 |
+
re.DOTALL
|
| 1025 |
+
)
|
| 1026 |
+
if not txt_match:
|
| 1027 |
+
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1028 |
|
| 1029 |
+
inner_html = txt_match.group(1)
|
| 1030 |
+
inner_html = re.sub(r'<br\s*/?>', '\n', inner_html, flags=re.IGNORECASE)
|
| 1031 |
+
|
| 1032 |
+
bold_raw = re.findall(r'<(?:b|strong)[^>]*>(.*?)</(?:b|strong)>', inner_html, flags=re.DOTALL | re.IGNORECASE)
|
| 1033 |
+
bold_words = []
|
| 1034 |
+
for bw in bold_raw:
|
| 1035 |
+
clean_bw = html.unescape(re.sub(r'<[^>]+>', '', bw)).strip()
|
| 1036 |
+
if clean_bw:
|
| 1037 |
+
bold_words.append(clean_bw)
|
| 1038 |
+
|
| 1039 |
+
plain_text = html.unescape(re.sub(r'<[^>]+>', '', inner_html)).strip()
|
| 1040 |
+
if plain_text:
|
| 1041 |
+
return plain_text, bold_words
|
| 1042 |
+
except Exception as e:
|
| 1043 |
+
print(f"Embed fetch error ({url}): {e}")
|
| 1044 |
+
return None, None
|
| 1045 |
+
|
| 1046 |
+
async def fetch_message_reliably(acc: Client, client: Client, source_chat, msgid: int, is_upsc_mode: bool = False):
|
| 1047 |
+
# 1. यदि UPSC मोड में पब्लिक चैनल ��ै, तो पहले ब्राउज़र Embed इंजन चलाएं (टेबल और ब्लॉक पोस्ट के लिए सबसे तेज़)
|
| 1048 |
+
if is_upsc_mode and isinstance(source_chat, str):
|
| 1049 |
clean_user = source_chat.replace("@", "").strip()
|
| 1050 |
web_txt, bold_list = await fetch_from_public_web(clean_user, msgid)
|
| 1051 |
if web_txt:
|
|
|
|
| 1070 |
web_page = None
|
| 1071 |
return WebMsgWrapper()
|
| 1072 |
|
| 1073 |
+
# 2. सभी सामान्य फ़ाइल्स (PDF, वीडियो, इमेज, ऑडियो) और प्राइवेट चैनल्स के लिए स्टैंडर्ड MTProto क्लाइंट
|
| 1074 |
+
try:
|
| 1075 |
+
msg = await asyncio.wait_for(acc.get_messages(source_chat, msgid), timeout=20.0)
|
| 1076 |
+
if msg and not getattr(msg, "empty", False):
|
| 1077 |
+
return msg
|
| 1078 |
+
except Exception:
|
| 1079 |
+
pass
|
| 1080 |
+
|
| 1081 |
+
try:
|
| 1082 |
+
msg = await asyncio.wait_for(client.get_messages(source_chat, msgid), timeout=15.0)
|
| 1083 |
+
if msg and not getattr(msg, "empty", False):
|
| 1084 |
+
return msg
|
| 1085 |
+
except Exception:
|
| 1086 |
+
pass
|
| 1087 |
+
|
| 1088 |
+
# 3. अंतिम फ़ॉलबैक: डायरेक्ट Raw MTProto GetMessages (चाहे प्राइवेट चैनल में टेबल पोस्ट हो)
|
| 1089 |
+
try:
|
| 1090 |
+
peer = await acc.resolve_peer(source_chat)
|
| 1091 |
+
if isinstance(peer, (raw.types.InputPeerChannel, raw.types.InputChannel)):
|
| 1092 |
+
raw_res = await acc.invoke(
|
| 1093 |
+
raw.functions.channels.GetMessages(
|
| 1094 |
+
channel=peer,
|
| 1095 |
+
id=[raw.types.InputMessageID(id=msgid)]
|
| 1096 |
+
)
|
| 1097 |
+
)
|
| 1098 |
+
else:
|
| 1099 |
+
raw_res = await acc.invoke(
|
| 1100 |
+
raw.functions.messages.GetMessages(
|
| 1101 |
+
id=[raw.types.InputMessageID(id=msgid)]
|
| 1102 |
+
)
|
| 1103 |
+
)
|
| 1104 |
+
if getattr(raw_res, "messages", None):
|
| 1105 |
+
raw_m = raw_res.messages[0]
|
| 1106 |
+
raw_str = getattr(raw_m, "message", "")
|
| 1107 |
+
if raw_str:
|
| 1108 |
+
class RawMsgWrapper:
|
| 1109 |
+
id = msgid
|
| 1110 |
+
empty = False
|
| 1111 |
+
service = False
|
| 1112 |
+
text = raw_str
|
| 1113 |
+
entities = getattr(raw_m, "entities", []) or []
|
| 1114 |
+
caption = None
|
| 1115 |
+
caption_entities = []
|
| 1116 |
+
photo = None
|
| 1117 |
+
video = None
|
| 1118 |
+
document = None
|
| 1119 |
+
audio = None
|
| 1120 |
+
voice = None
|
| 1121 |
+
video_note = None
|
| 1122 |
+
animation = None
|
| 1123 |
+
sticker = None
|
| 1124 |
+
web_page = None
|
| 1125 |
+
return RawMsgWrapper()
|
| 1126 |
+
except Exception as raw_e:
|
| 1127 |
+
print(f"Raw MTProto fetch error: {raw_e}")
|
| 1128 |
+
|
| 1129 |
return None
|
| 1130 |
|
| 1131 |
async def process_media(client: Client, acc: Client, user_id: int, source_chat, msgid: int, target_chat, target_topic_id, task: dict, status_msg: Message, current_idx: int, total_count: int):
|
| 1132 |
if batch_temp.IS_BATCH.get(user_id):
|
| 1133 |
raise asyncio.CancelledError()
|
| 1134 |
|
| 1135 |
+
is_upsc_mode = task.get("is_upsc_mode", False)
|
| 1136 |
+
msg = await fetch_message_reliably(acc, client, source_chat, msgid, is_upsc_mode=is_upsc_mode)
|
| 1137 |
if not msg or getattr(msg, "empty", False) or getattr(msg, "service", False):
|
| 1138 |
return False, 0
|
| 1139 |
|
| 1140 |
is_direct_bot = task.get("is_direct_bot", False)
|
|
|
|
| 1141 |
sender_client = client if is_direct_bot else acc
|
| 1142 |
topic_kwargs = {"message_thread_id": target_topic_id} if target_topic_id and not is_direct_bot else {}
|
| 1143 |
|