Agnivesh10M commited on
Commit
8d08cae
·
verified ·
1 Parent(s): 9faa1c2

Update TechVJ/start.py

Browse files
Files changed (1) hide show
  1. 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
- जब Pyrogram किसी नए Premium Table / Expandable Quote ब्लॉक को empty दिखाता है,
1007
- तब यह इंजन सीधे t.me/s/ से पोस्ट का पूरा टेक्स्ट और बोल्ड हेडिंग्स निकालता है (बिना किसी अतिरिक्त लाइब्रेरी के)।
1008
- """
1009
- url = f"https://t.me/s/{username}/{msgid}"
1010
- try:
1011
- async with aiohttp.ClientSession() as session:
1012
- async with session.get(url, timeout=15) as resp:
1013
- if resp.status != 200:
1014
- return None, None
1015
- page_html = await resp.text()
1016
-
1017
- # उस पोस्ट का ब्लॉक ढूंढें
1018
- post_pattern = re.compile(
1019
- rf'data-post="{re.escape(username)}/{msgid}"(.*?)tgme_widget_message_footer',
1020
- re.DOTALL | re.IGNORECASE
1021
- )
1022
- m_post = post_pattern.search(page_html)
1023
- if not m_post:
1024
- return None, None
1025
-
1026
- block_html = m_post.group(1)
1027
- txt_match = re.search(
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
- # पब्लिक चैनल के लिए Direct Web Engine फ़ॉलबैक (Premium टेबल ब्लॉक्स के लिए)
1069
- if isinstance(source_chat, str):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- msg = await fetch_message_reliably(acc, client, source_chat, msgid)
 
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