Update bot.py
Browse files
bot.py
CHANGED
|
@@ -73,6 +73,35 @@ def progress_bar(current: int, total: int, width: int = 12) -> str:
|
|
| 73 |
return f"[{bar}] {pct * 100:.1f}%"
|
| 74 |
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
def get_file_info(msg: Message) -> tuple[str, int]:
|
| 77 |
"""ุงุณุชุฎุฑุงุฌ ุงุณู
ุงูู
ูู ูุญุฌู
ู ู
ู ุงูุฑุณุงูุฉ"""
|
| 78 |
media = getattr(msg, msg.media.value, None)
|
|
@@ -222,10 +251,12 @@ async def upload_single(
|
|
| 222 |
srv_speed: float,
|
| 223 |
file_num: int,
|
| 224 |
total_files: int,
|
|
|
|
| 225 |
) -> dict:
|
| 226 |
"""ุชุญู
ูู ุงูู
ูู ู
ู ุชูููุบุฑุงู
ุซู
ุฑูุนู ุนูู GoFile ู
ุน ุชุชุจุน ุงูุชูุฏู
"""
|
| 227 |
|
| 228 |
file_name, file_size = get_file_info(msg)
|
|
|
|
| 229 |
|
| 230 |
# โโ ุฅูุดุงุก ู
ูู ู
ุคูุช โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 231 |
tmp_fd, tmp_path = tempfile.mkstemp()
|
|
@@ -235,7 +266,7 @@ async def upload_single(
|
|
| 235 |
# โโ 1. ุชุญู
ูู ู
ู ุชูููุบุฑุงู
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 236 |
await status_msg.edit(
|
| 237 |
f"โฌ๏ธ **ุฌุงุฑู ุงูุชุญู
ูู ู
ู ุชูููุบุฑุงู
...**\n\n"
|
| 238 |
-
f"๐ `{
|
| 239 |
f"๐ ุงูู
ูู `{file_num}` ู
ู `{total_files}`\n"
|
| 240 |
f"๐ฅ ุงูุณูุฑูุฑ: `{server}`"
|
| 241 |
+ (f" โก `{fmt_speed(srv_speed)}`" if srv_speed > 0 else "")
|
|
@@ -257,7 +288,7 @@ async def upload_single(
|
|
| 257 |
try:
|
| 258 |
await status_msg.edit(
|
| 259 |
f"โฌ๏ธ **ุฌุงุฑู ุงูุฑูุน ุนูู GoFile...**\n\n"
|
| 260 |
-
f"๐ `{
|
| 261 |
f"๐ ุงูู
ูู `{file_num}` ู
ู `{total_files}`\n"
|
| 262 |
f"๐ฅ ุงูุณูุฑูุฑ: `{server}`"
|
| 263 |
+ (f" โก `{fmt_speed(srv_speed)}`" if srv_speed > 0 else "")
|
|
@@ -269,21 +300,20 @@ async def upload_single(
|
|
| 269 |
progress_task = asyncio.create_task(_live_progress())
|
| 270 |
|
| 271 |
try:
|
| 272 |
-
async with
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
resp_data = await resp.json()
|
| 287 |
|
| 288 |
stop_evt.set()
|
| 289 |
await progress_task
|
|
@@ -322,16 +352,26 @@ async def upload_single(
|
|
| 322 |
async def process_user_queue(client: Client, user_id: int):
|
| 323 |
"""
|
| 324 |
ููุชุธุฑ ููููุงู ูุชุฌู
ูุน ู
ููุงุช ู
ุชุนุฏุฏุฉ ูุฏ ูุฑุณููุง ุงูู
ุณุชุฎุฏู
ุฏูุนุฉ ูุงุญุฏุฉุ
|
| 325 |
-
|
|
|
|
| 326 |
"""
|
| 327 |
-
|
|
|
|
|
|
|
| 328 |
|
| 329 |
-
|
| 330 |
-
|
| 331 |
|
| 332 |
-
|
| 333 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
|
|
|
|
|
|
|
|
|
|
| 335 |
total = len(messages)
|
| 336 |
first = messages[0]
|
| 337 |
names = [get_file_info(m)[0] for m in messages]
|
|
@@ -340,33 +380,34 @@ async def process_user_queue(client: Client, user_id: int):
|
|
| 340 |
status_msg = await first.reply(
|
| 341 |
f"๐ **ุฌุงุฑู ุงุฎุชุจุงุฑ ุงูุณูุฑูุฑุงุช...**\n\n"
|
| 342 |
f"๐ ุนุฏุฏ ุงูู
ููุงุช: `{total}`\n"
|
| 343 |
-
+ "\n".join(f" โข `{n}`" for n in names)
|
| 344 |
)
|
| 345 |
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
server, srv_speed = await get_best_server(
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
speed_label = f"`{fmt_speed(srv_speed)}`" if srv_speed > 0 else "`โ`"
|
| 355 |
-
await status_msg.edit(
|
| 356 |
-
f"โ
**ุชู
ุงุฎุชูุงุฑ ุงูุณูุฑูุฑ ุงูุฃุณุฑุน!**\n\n"
|
| 357 |
-
f"๐ฅ ุงูุณูุฑูุฑ: `{server}` | โก {speed_label}\n"
|
| 358 |
-
f"๐ ุนุฏุฏ ุงูู
ููุงุช: `{total}`\n\n"
|
| 359 |
-
f"โณ ุจุฏุก ุงูุฑูุน..."
|
| 360 |
-
)
|
| 361 |
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
)
|
| 369 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
|
| 371 |
# โโ ุจูุงุก ุฑุณุงูุฉ ุงููุชุงุฆุฌ ุงูููุงุฆูุฉ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 372 |
ok_list = [r for r in results if r["ok"]]
|
|
@@ -377,14 +418,14 @@ async def process_user_queue(client: Client, user_id: int):
|
|
| 377 |
if ok_list:
|
| 378 |
lines.append(f"โ
**ูุฌุญ ุงูุฑูุน ({len(ok_list)} ู
ูู):**")
|
| 379 |
for r in ok_list:
|
| 380 |
-
lines.append(f"โ ๐ `{r['name']}` โ `{fmt_size(r['size'])}`")
|
| 381 |
lines.append(f"โ ๐ {r['link']}")
|
| 382 |
lines.append("")
|
| 383 |
|
| 384 |
if fail_list:
|
| 385 |
lines.append(f"โ **ูุดู ุงูุฑูุน ({len(fail_list)} ู
ูู):**")
|
| 386 |
for r in fail_list:
|
| 387 |
-
lines.append(f"โข `{r['name']}` โ _{r['error']}_")
|
| 388 |
lines.append("")
|
| 389 |
|
| 390 |
lines.append(f"๐ฅ **ุงูุณูุฑูุฑ:** `{server}` | โก {speed_label}")
|
|
@@ -397,7 +438,7 @@ async def process_user_queue(client: Client, user_id: int):
|
|
| 397 |
except Exception:
|
| 398 |
pass
|
| 399 |
|
| 400 |
-
await
|
| 401 |
|
| 402 |
|
| 403 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
@@ -419,12 +460,27 @@ async def cmd_start(client: Client, message: Message):
|
|
| 419 |
" โฃ ูุฑุณู ุฑูุงุจุท ุงูุชุญู
ูู ุจุนุฏ ุงูุงูุชูุงุก\n\n"
|
| 420 |
"๐ **ุงูุฃููุงุน ุงูู
ุฏุนูู
ุฉ:**\n"
|
| 421 |
" ู
ุณุชูุฏุงุช ยท ููุฏูู ยท ุตูุช ยท ุตูุฑ ยท ุฃู ู
ูู\n\n"
|
|
|
|
| 422 |
"โโโโโโโโโโโโโโโโโโโโโโโ\n"
|
| 423 |
"๐ **ุงูุฎุฏู
ุฉ:** [GoFile.io](https://gofile.io)",
|
| 424 |
disable_web_page_preview=True,
|
| 425 |
)
|
| 426 |
|
| 427 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 428 |
@app.on_message(
|
| 429 |
filters.private
|
| 430 |
& (
|
|
|
|
| 73 |
return f"[{bar}] {pct * 100:.1f}%"
|
| 74 |
|
| 75 |
|
| 76 |
+
def safe_md(text: str) -> str:
|
| 77 |
+
"""ูู
ูุน ูุณุฑ ุชูุณูู Markdown ุฅุฐุง ุงุญุชูู ุงููุต (ู
ุซู ุงุณู
ู
ูู) ุนูู backtick"""
|
| 78 |
+
return str(text).replace("`", "ห")
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
TELEGRAM_MAX_LEN = 4000 # ูุงู
ุด ุฃู
ุงู ุชุญุช ุญุฏ ุชูููุบุฑุงู
(4096)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
async def send_long(target: Message, text: str, **kwargs):
|
| 85 |
+
"""ูุฑุณู ูุตุงู ุทูููุงู ู
ูุณูู
ุงู ุนูู ุนุฏุฉ ุฑุณุงุฆู ุฅุฐุง ุชุฌุงูุฒ ุญุฏ ุชูููุบุฑุงู
"""
|
| 86 |
+
if len(text) <= TELEGRAM_MAX_LEN:
|
| 87 |
+
await target.reply(text, **kwargs)
|
| 88 |
+
return
|
| 89 |
+
|
| 90 |
+
chunks = []
|
| 91 |
+
current = ""
|
| 92 |
+
for line in text.split("\n"):
|
| 93 |
+
if len(current) + len(line) + 1 > TELEGRAM_MAX_LEN:
|
| 94 |
+
chunks.append(current)
|
| 95 |
+
current = line
|
| 96 |
+
else:
|
| 97 |
+
current = f"{current}\n{line}" if current else line
|
| 98 |
+
if current:
|
| 99 |
+
chunks.append(current)
|
| 100 |
+
|
| 101 |
+
for chunk in chunks:
|
| 102 |
+
await target.reply(chunk, **kwargs)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
def get_file_info(msg: Message) -> tuple[str, int]:
|
| 106 |
"""ุงุณุชุฎุฑุงุฌ ุงุณู
ุงูู
ูู ูุญุฌู
ู ู
ู ุงูุฑุณุงูุฉ"""
|
| 107 |
media = getattr(msg, msg.media.value, None)
|
|
|
|
| 251 |
srv_speed: float,
|
| 252 |
file_num: int,
|
| 253 |
total_files: int,
|
| 254 |
+
session: aiohttp.ClientSession,
|
| 255 |
) -> dict:
|
| 256 |
"""ุชุญู
ูู ุงูู
ูู ู
ู ุชูููุบุฑุงู
ุซู
ุฑูุนู ุนูู GoFile ู
ุน ุชุชุจุน ุงูุชูุฏู
"""
|
| 257 |
|
| 258 |
file_name, file_size = get_file_info(msg)
|
| 259 |
+
safe_name = safe_md(file_name)
|
| 260 |
|
| 261 |
# โโ ุฅูุดุงุก ู
ูู ู
ุคูุช โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 262 |
tmp_fd, tmp_path = tempfile.mkstemp()
|
|
|
|
| 266 |
# โโ 1. ุชุญู
ูู ู
ู ุชูููุบุฑุงู
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 267 |
await status_msg.edit(
|
| 268 |
f"โฌ๏ธ **ุฌุงุฑู ุงูุชุญู
ูู ู
ู ุชูููุบุฑุงู
...**\n\n"
|
| 269 |
+
f"๐ `{safe_name}`\n"
|
| 270 |
f"๐ ุงูู
ูู `{file_num}` ู
ู `{total_files}`\n"
|
| 271 |
f"๐ฅ ุงูุณูุฑูุฑ: `{server}`"
|
| 272 |
+ (f" โก `{fmt_speed(srv_speed)}`" if srv_speed > 0 else "")
|
|
|
|
| 288 |
try:
|
| 289 |
await status_msg.edit(
|
| 290 |
f"โฌ๏ธ **ุฌุงุฑู ุงูุฑูุน ุนูู GoFile...**\n\n"
|
| 291 |
+
f"๐ `{safe_name}`\n"
|
| 292 |
f"๐ ุงูู
ูู `{file_num}` ู
ู `{total_files}`\n"
|
| 293 |
f"๐ฅ ุงูุณูุฑูุฑ: `{server}`"
|
| 294 |
+ (f" โก `{fmt_speed(srv_speed)}`" if srv_speed > 0 else "")
|
|
|
|
| 300 |
progress_task = asyncio.create_task(_live_progress())
|
| 301 |
|
| 302 |
try:
|
| 303 |
+
async with TrackingFileReader(tmp_path, tracker) as reader:
|
| 304 |
+
form = aiohttp.FormData()
|
| 305 |
+
form.add_field(
|
| 306 |
+
"file",
|
| 307 |
+
reader,
|
| 308 |
+
filename=file_name,
|
| 309 |
+
content_type="application/octet-stream",
|
| 310 |
+
)
|
| 311 |
+
async with session.post(
|
| 312 |
+
f"https://{server}.gofile.io/contents/uploadfile",
|
| 313 |
+
data=form,
|
| 314 |
+
timeout=aiohttp.ClientTimeout(total=0), # ุจุฏูู ุญุฏ ุฒู
ูู
|
| 315 |
+
) as resp:
|
| 316 |
+
resp_data = await resp.json()
|
|
|
|
| 317 |
|
| 318 |
stop_evt.set()
|
| 319 |
await progress_task
|
|
|
|
| 352 |
async def process_user_queue(client: Client, user_id: int):
|
| 353 |
"""
|
| 354 |
ููุชุธุฑ ููููุงู ูุชุฌู
ูุน ู
ููุงุช ู
ุชุนุฏุฏุฉ ูุฏ ูุฑุณููุง ุงูู
ุณุชุฎุฏู
ุฏูุนุฉ ูุงุญุฏุฉุ
|
| 355 |
+
ูุนุงูุฌูุงุ ุซู
ูุชุญูู ุฅุฐุง ูุตูุช ู
ููุงุช ุฌุฏูุฏุฉ ุฃุซูุงุก ุงูู
ุนุงูุฌุฉ (ู
ุซูุงู ุงูู
ุณุชุฎุฏู
|
| 356 |
+
ุฃุฑุณู ุฏูุนุฉ ุซุงููุฉ ููู ููุชุธุฑ) ูููู
ู ู
ุนุงูุฌุชูุง ุจููุณ ุงูู
ูู
ุฉ ุจุฏู ู
ุง ุชุถู๏ฟฝ๏ฟฝ.
|
| 357 |
"""
|
| 358 |
+
try:
|
| 359 |
+
while True:
|
| 360 |
+
await asyncio.sleep(FILE_COLLECT_DELAY)
|
| 361 |
|
| 362 |
+
async with pending_locks[user_id]:
|
| 363 |
+
messages = pending_files.pop(user_id, [])
|
| 364 |
|
| 365 |
+
if not messages:
|
| 366 |
+
return
|
| 367 |
+
|
| 368 |
+
await _process_batch(client, messages)
|
| 369 |
+
finally:
|
| 370 |
+
pending_tasks.pop(user_id, None)
|
| 371 |
|
| 372 |
+
|
| 373 |
+
async def _process_batch(client: Client, messages: list[Message]):
|
| 374 |
+
"""ูุนุงูุฌ ุฏูุนุฉ ูุงุญุฏุฉ ู
ู ุงูู
ููุงุช: ุงุฎุชูุงุฑ ุณูุฑูุฑุ ุฑูุน ุชุณูุณููุ ุฅุฑุณุงู ุงููุชุงุฆุฌ"""
|
| 375 |
total = len(messages)
|
| 376 |
first = messages[0]
|
| 377 |
names = [get_file_info(m)[0] for m in messages]
|
|
|
|
| 380 |
status_msg = await first.reply(
|
| 381 |
f"๐ **ุฌุงุฑู ุงุฎุชุจุงุฑ ุงูุณูุฑูุฑุงุช...**\n\n"
|
| 382 |
f"๐ ุนุฏุฏ ุงูู
ููุงุช: `{total}`\n"
|
| 383 |
+
+ "\n".join(f" โข `{safe_md(n)}`" for n in names)
|
| 384 |
)
|
| 385 |
|
| 386 |
+
async with aiohttp.ClientSession() as session:
|
| 387 |
+
# โโ ุงุฎุชูุงุฑ ุฃุณุฑุน ุณูุฑูุฑ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 388 |
+
try:
|
| 389 |
+
server, srv_speed = await get_best_server(session)
|
| 390 |
+
except Exception as ex:
|
| 391 |
+
await status_msg.edit(f"โ ูุดู ุงูุงุชุตุงู ุจู GoFile: `{ex}`")
|
| 392 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
|
| 394 |
+
speed_label = f"`{fmt_speed(srv_speed)}`" if srv_speed > 0 else "`โ`"
|
| 395 |
+
await status_msg.edit(
|
| 396 |
+
f"โ
**ุชู
ุงุฎุชูุงุฑ ุงูุณูุฑูุฑ ุงูุฃุณุฑุน!**\n\n"
|
| 397 |
+
f"๐ฅ ุงูุณูุฑูุฑ: `{server}` | โก {speed_label}\n"
|
| 398 |
+
f"๐ ุนุฏุฏ ุงูู
ููุงุช: `{total}`\n\n"
|
| 399 |
+
f"โณ ุจุฏุก ุงูุฑูุน..."
|
| 400 |
)
|
| 401 |
+
|
| 402 |
+
results = []
|
| 403 |
+
for idx, msg in enumerate(messages, 1):
|
| 404 |
+
result = await upload_single(
|
| 405 |
+
client, msg, status_msg,
|
| 406 |
+
server, srv_speed,
|
| 407 |
+
idx, total,
|
| 408 |
+
session,
|
| 409 |
+
)
|
| 410 |
+
results.append(result)
|
| 411 |
|
| 412 |
# โโ ุจูุงุก ุฑุณุงูุฉ ุงููุชุงุฆุฌ ุงูููุงุฆูุฉ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 413 |
ok_list = [r for r in results if r["ok"]]
|
|
|
|
| 418 |
if ok_list:
|
| 419 |
lines.append(f"โ
**ูุฌุญ ุงูุฑูุน ({len(ok_list)} ู
ูู):**")
|
| 420 |
for r in ok_list:
|
| 421 |
+
lines.append(f"โ ๐ `{safe_md(r['name'])}` โ `{fmt_size(r['size'])}`")
|
| 422 |
lines.append(f"โ ๐ {r['link']}")
|
| 423 |
lines.append("")
|
| 424 |
|
| 425 |
if fail_list:
|
| 426 |
lines.append(f"โ **ูุดู ุงูุฑูุน ({len(fail_list)} ู
ูู):**")
|
| 427 |
for r in fail_list:
|
| 428 |
+
lines.append(f"โข `{safe_md(r['name'])}` โ _{safe_md(r['error'])}_")
|
| 429 |
lines.append("")
|
| 430 |
|
| 431 |
lines.append(f"๐ฅ **ุงูุณูุฑูุฑ:** `{server}` | โก {speed_label}")
|
|
|
|
| 438 |
except Exception:
|
| 439 |
pass
|
| 440 |
|
| 441 |
+
await send_long(first, "\n".join(lines), disable_web_page_preview=True)
|
| 442 |
|
| 443 |
|
| 444 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
|
|
| 460 |
" โฃ ูุฑุณู ุฑูุงุจุท ุงูุชุญู
ูู ุจุนุฏ ุงูุงูุชูุงุก\n\n"
|
| 461 |
"๐ **ุงูุฃููุงุน ุงูู
ุฏุนูู
ุฉ:**\n"
|
| 462 |
" ู
ุณุชูุฏุงุช ยท ููุฏูู ยท ุตูุช ยท ุตูุฑ ยท ุฃู ู
ูู\n\n"
|
| 463 |
+
"๐ **ุฅูุบุงุก ุนู
ููุฉ ุดุบุงูุฉ:** /cancel\n\n"
|
| 464 |
"โโโโโโโโโโโโโโโโโโโโโโโ\n"
|
| 465 |
"๐ **ุงูุฎุฏู
ุฉ:** [GoFile.io](https://gofile.io)",
|
| 466 |
disable_web_page_preview=True,
|
| 467 |
)
|
| 468 |
|
| 469 |
|
| 470 |
+
@app.on_message(filters.command("cancel") & filters.private)
|
| 471 |
+
async def cmd_cancel(client: Client, message: Message):
|
| 472 |
+
user_id = message.from_user.id
|
| 473 |
+
|
| 474 |
+
task = pending_tasks.get(user_id)
|
| 475 |
+
if task and not task.done():
|
| 476 |
+
task.cancel()
|
| 477 |
+
async with pending_locks[user_id]:
|
| 478 |
+
pending_files.pop(user_id, None)
|
| 479 |
+
await message.reply("๐ **ุชู
ุฅูุบุงุก ุนู
ููุฉ ุงูุฑูุน.**")
|
| 480 |
+
else:
|
| 481 |
+
await message.reply("ู
ุงูู ุนู
ููุฉ ุฑูุน ุดุบุงูุฉ ุญุงููุงู.")
|
| 482 |
+
|
| 483 |
+
|
| 484 |
@app.on_message(
|
| 485 |
filters.private
|
| 486 |
& (
|