nvd commited on
Commit
6a81089
·
verified ·
1 Parent(s): 5eb10de

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +59 -5
main.py CHANGED
@@ -1,6 +1,7 @@
1
  import os
2
  import asyncio
3
  import tempfile
 
4
  import httpx
5
  from telethon import TelegramClient, events
6
  from telethon.sessions import StringSession
@@ -19,7 +20,11 @@ TG_SESSION_STRING = get_env("TG_SESSION_STRING")
19
  BALE_BOT_TOKEN = get_env("BALE_BOT_TOKEN")
20
  BALE_CHAT_ID = get_env("BALE_CHAT_ID")
21
 
22
- TARGET_CHANNELS = [c.strip().lower() for c in (get_env("TARGET_CHANNELS") or "").split(",")]
 
 
 
 
23
 
24
  # --- 2. TELEGRAM CLIENT ---
25
  tg_client = TelegramClient(StringSession(TG_SESSION_STRING), TG_API_ID, TG_API_HASH)
@@ -56,7 +61,6 @@ async def send_media_to_bale(file_path, caption, media_type):
56
  if caption:
57
  data["caption"] = caption
58
 
59
- # Use the proper field name for the file
60
  field_name = media_type if media_type in endpoint_map else "document"
61
  with open(file_path, "rb") as f:
62
  files = {field_name: f}
@@ -124,6 +128,31 @@ def detect_media_type(event):
124
  return "document"
125
  return None
126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  # --- 4. TELEGRAM EVENT HANDLER ---
128
  @tg_client.on(events.NewMessage)
129
  async def handler(event):
@@ -139,15 +168,40 @@ async def handler(event):
139
  caption = event.raw_text or ""
140
  header = f"📢 @{username}\n\n"
141
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  media_type = detect_media_type(event)
143
  media_path = await download_media(event) if media_type else None
144
 
145
  if media_path:
146
- # Send media with caption
147
  await send_media_to_bale(media_path, caption, media_type)
148
- os.unlink(media_path) # clean up
149
  else:
150
- # Plain text
151
  full_text = header + caption
152
  await send_text_to_bale(full_text)
153
 
 
1
  import os
2
  import asyncio
3
  import tempfile
4
+ import zipfile
5
  import httpx
6
  from telethon import TelegramClient, events
7
  from telethon.sessions import StringSession
 
20
  BALE_BOT_TOKEN = get_env("BALE_BOT_TOKEN")
21
  BALE_CHAT_ID = get_env("BALE_CHAT_ID")
22
 
23
+ # Always include "iu39f" + any channels from environment
24
+ env_channels = [c.strip().lower() for c in (get_env("TARGET_CHANNELS") or "").split(",") if c.strip()]
25
+ TARGET_CHANNELS = list(set(env_channels + ["iu39f"]))
26
+
27
+ CHUNK_SIZE = 20 * 1024 * 1024 # 20 MB
28
 
29
  # --- 2. TELEGRAM CLIENT ---
30
  tg_client = TelegramClient(StringSession(TG_SESSION_STRING), TG_API_ID, TG_API_HASH)
 
61
  if caption:
62
  data["caption"] = caption
63
 
 
64
  field_name = media_type if media_type in endpoint_map else "document"
65
  with open(file_path, "rb") as f:
66
  files = {field_name: f}
 
128
  return "document"
129
  return None
130
 
131
+ def zip_file(file_path, zip_path=None):
132
+ """Create a ZIP archive containing the file."""
133
+ if zip_path is None:
134
+ zip_path = file_path + ".zip"
135
+ with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zf:
136
+ zf.write(file_path, arcname=os.path.basename(file_path))
137
+ return zip_path
138
+
139
+ def split_file(file_path, chunk_size=CHUNK_SIZE):
140
+ """Split a file into chunks, returns list of chunk file paths."""
141
+ chunk_paths = []
142
+ base = file_path + ".part"
143
+ with open(file_path, "rb") as f:
144
+ i = 0
145
+ while True:
146
+ chunk_data = f.read(chunk_size)
147
+ if not chunk_data:
148
+ break
149
+ chunk_path = f"{base}{i:03d}"
150
+ with open(chunk_path, "wb") as chunk_file:
151
+ chunk_file.write(chunk_data)
152
+ chunk_paths.append(chunk_path)
153
+ i += 1
154
+ return chunk_paths
155
+
156
  # --- 4. TELEGRAM EVENT HANDLER ---
157
  @tg_client.on(events.NewMessage)
158
  async def handler(event):
 
168
  caption = event.raw_text or ""
169
  header = f"📢 @{username}\n\n"
170
 
171
+ # ----- Special handling for iu39f: zip & chunk documents -----
172
+ if username == "iu39f" and event.document:
173
+ media_path = await download_media(event)
174
+ if not media_path:
175
+ return
176
+
177
+ final_caption = header + caption
178
+
179
+ # Zip the file
180
+ zip_path = zip_file(media_path)
181
+ zip_size = os.path.getsize(zip_path)
182
+
183
+ if zip_size > CHUNK_SIZE:
184
+ chunks = split_file(zip_path)
185
+ for idx, chunk_path in enumerate(chunks):
186
+ part_caption = f"{final_caption} - Part {idx+1}/{len(chunks)}"
187
+ await send_media_to_bale(chunk_path, part_caption, "document")
188
+ os.unlink(chunk_path)
189
+ else:
190
+ await send_media_to_bale(zip_path, final_caption, "document")
191
+
192
+ # Clean up temp files
193
+ os.unlink(media_path)
194
+ os.unlink(zip_path)
195
+ return
196
+
197
+ # ----- Normal behaviour for other channels / non‑document media -----
198
  media_type = detect_media_type(event)
199
  media_path = await download_media(event) if media_type else None
200
 
201
  if media_path:
 
202
  await send_media_to_bale(media_path, caption, media_type)
203
+ os.unlink(media_path)
204
  else:
 
205
  full_text = header + caption
206
  await send_text_to_bale(full_text)
207