nvd commited on
Update bot.py
Browse files
bot.py
CHANGED
|
@@ -9,6 +9,7 @@ import httpx
|
|
| 9 |
import logging
|
| 10 |
import tempfile
|
| 11 |
import zipfile
|
|
|
|
| 12 |
from urllib.parse import unquote
|
| 13 |
|
| 14 |
# ---------- Logging ----------
|
|
@@ -22,7 +23,7 @@ try:
|
|
| 22 |
except ImportError:
|
| 23 |
BALE_TOKEN = os.getenv("BALE_BOT_TOKEN")
|
| 24 |
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
|
| 25 |
-
ALLOWED_USERS = []
|
| 26 |
logger.info("Tokens loaded from environment variables; no user restriction")
|
| 27 |
|
| 28 |
if not BALE_TOKEN:
|
|
@@ -137,7 +138,7 @@ async def send_document_file(chat_id, file_path, caption="", display_filename=No
|
|
| 137 |
files = {"document": (display_filename, f, "application/octet-stream")}
|
| 138 |
logger.debug(f"Uploading file as: {display_filename}, local path: {file_path}")
|
| 139 |
async with httpx.AsyncClient() as client:
|
| 140 |
-
resp = await client.post(url, data=data, files=files, timeout=
|
| 141 |
if resp.status_code != 200:
|
| 142 |
logger.error(f"β Bale upload failed: {resp.status_code} {resp.text}")
|
| 143 |
return {"ok": False, "description": resp.text}
|
|
@@ -186,14 +187,13 @@ async def download_file(url, dest_path):
|
|
| 186 |
f.write(chunk)
|
| 187 |
logger.info(f"β
Downloaded file to {dest_path}")
|
| 188 |
|
| 189 |
-
# ---------- Compression
|
| 190 |
COMPRESSED_EXTENSIONS = {
|
| 191 |
'.zip', '.gz', '.tgz', '.tar.gz', '.rar', '.7z',
|
| 192 |
'.bz2', '.xz', '.tar', '.tar.bz2', '.tar.xz', '.zst', '.lz'
|
| 193 |
}
|
| 194 |
|
| 195 |
def is_compressed(filename: str) -> bool:
|
| 196 |
-
"""Return True if the filename already indicates a compressed archive."""
|
| 197 |
lower = filename.lower()
|
| 198 |
for ext in COMPRESSED_EXTENSIONS:
|
| 199 |
if lower.endswith(ext):
|
|
@@ -201,17 +201,44 @@ def is_compressed(filename: str) -> bool:
|
|
| 201 |
return False
|
| 202 |
|
| 203 |
def compress_to_zip(file_path: str, original_filename: str, zip_path: str):
|
| 204 |
-
"""
|
| 205 |
-
Create a ZIP archive containing the file (preserving its original name).
|
| 206 |
-
"""
|
| 207 |
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zf:
|
| 208 |
zf.write(file_path, arcname=original_filename)
|
| 209 |
logger.info(f"π¦ Compressed to ZIP: {zip_path}")
|
| 210 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
# ---------- Message / Callback handlers ----------
|
| 212 |
def is_authorized(chat_id: int) -> bool:
|
| 213 |
-
|
| 214 |
-
if not ALLOWED_USERS: # empty list = no restrictions
|
| 215 |
return True
|
| 216 |
return chat_id in ALLOWED_USERS
|
| 217 |
|
|
@@ -290,16 +317,6 @@ async def handle_callback(callback: dict):
|
|
| 290 |
|
| 291 |
logger.info(f"β¬οΈ Requested asset: {original_filename}")
|
| 292 |
|
| 293 |
-
# Determine if we need to compress the file
|
| 294 |
-
need_compress = not is_compressed(original_filename)
|
| 295 |
-
if need_compress:
|
| 296 |
-
zip_filename = original_filename + ".zip"
|
| 297 |
-
caption = f"π¦ File compressed to ZIP for delivery.\nOriginal: `{original_filename}`"
|
| 298 |
-
await send_message(chat_id, "π¦ Compressing file into ZIP...")
|
| 299 |
-
else:
|
| 300 |
-
zip_filename = None
|
| 301 |
-
caption = "Here you go!"
|
| 302 |
-
|
| 303 |
# Status message
|
| 304 |
status_msg = await send_message(chat_id, "β³ Downloading file from GitHub...")
|
| 305 |
status_message_id = status_msg.get("result", {}).get("message_id") if status_msg.get("ok") else None
|
|
@@ -309,61 +326,115 @@ async def handle_callback(callback: dict):
|
|
| 309 |
tmp_path = tmp.name
|
| 310 |
try:
|
| 311 |
await download_file(url, tmp_path)
|
|
|
|
|
|
|
|
|
|
| 312 |
|
| 313 |
-
if
|
| 314 |
-
#
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
|
| 330 |
-
size_mb = file_size / (1024 * 1024)
|
| 331 |
-
logger.info(f"β
Ready to upload: {size_mb:.2f} MB, file: {upload_filename}")
|
| 332 |
-
|
| 333 |
-
if status_message_id:
|
| 334 |
-
await bale_request("editMessageText", {
|
| 335 |
-
"chat_id": chat_id,
|
| 336 |
-
"message_id": status_message_id,
|
| 337 |
-
"text": f"π₯ Downloaded {size_mb:.2f} MB β uploading to Bale..."
|
| 338 |
-
})
|
| 339 |
else:
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
# Upload
|
| 343 |
-
result = await send_document_file(chat_id, upload_path, caption=caption, display_filename=upload_filename)
|
| 344 |
-
if result.get("ok"):
|
| 345 |
if status_message_id:
|
| 346 |
-
await bale_request("
|
| 347 |
"chat_id": chat_id,
|
| 348 |
-
"message_id": status_message_id
|
|
|
|
| 349 |
})
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
except Exception as e:
|
| 356 |
logger.error(f"β Download/Upload error: {e}", exc_info=True)
|
| 357 |
await send_message(chat_id, f"β Error: {e}")
|
| 358 |
await answer_callback(callback_id, "Failed.")
|
| 359 |
finally:
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
except:
|
| 366 |
-
pass
|
| 367 |
else:
|
| 368 |
await answer_callback(callback_id, "Unknown action.")
|
| 369 |
|
|
@@ -420,7 +491,7 @@ async def release_poller():
|
|
| 420 |
keyboard = build_inline_keyboard(release)
|
| 421 |
for cid in chat_ids:
|
| 422 |
if not is_authorized(int(cid)):
|
| 423 |
-
continue
|
| 424 |
try:
|
| 425 |
await send_message(int(cid), f"π New release for {owner}/{repo}: *{release['name']}*", reply_markup=keyboard)
|
| 426 |
logger.info(f"π Notified {cid} about {repo_url}")
|
|
|
|
| 9 |
import logging
|
| 10 |
import tempfile
|
| 11 |
import zipfile
|
| 12 |
+
import math
|
| 13 |
from urllib.parse import unquote
|
| 14 |
|
| 15 |
# ---------- Logging ----------
|
|
|
|
| 23 |
except ImportError:
|
| 24 |
BALE_TOKEN = os.getenv("BALE_BOT_TOKEN")
|
| 25 |
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
|
| 26 |
+
ALLOWED_USERS = []
|
| 27 |
logger.info("Tokens loaded from environment variables; no user restriction")
|
| 28 |
|
| 29 |
if not BALE_TOKEN:
|
|
|
|
| 138 |
files = {"document": (display_filename, f, "application/octet-stream")}
|
| 139 |
logger.debug(f"Uploading file as: {display_filename}, local path: {file_path}")
|
| 140 |
async with httpx.AsyncClient() as client:
|
| 141 |
+
resp = await client.post(url, data=data, files=files, timeout=120)
|
| 142 |
if resp.status_code != 200:
|
| 143 |
logger.error(f"β Bale upload failed: {resp.status_code} {resp.text}")
|
| 144 |
return {"ok": False, "description": resp.text}
|
|
|
|
| 187 |
f.write(chunk)
|
| 188 |
logger.info(f"β
Downloaded file to {dest_path}")
|
| 189 |
|
| 190 |
+
# ---------- Compression helpers ----------
|
| 191 |
COMPRESSED_EXTENSIONS = {
|
| 192 |
'.zip', '.gz', '.tgz', '.tar.gz', '.rar', '.7z',
|
| 193 |
'.bz2', '.xz', '.tar', '.tar.bz2', '.tar.xz', '.zst', '.lz'
|
| 194 |
}
|
| 195 |
|
| 196 |
def is_compressed(filename: str) -> bool:
|
|
|
|
| 197 |
lower = filename.lower()
|
| 198 |
for ext in COMPRESSED_EXTENSIONS:
|
| 199 |
if lower.endswith(ext):
|
|
|
|
| 201 |
return False
|
| 202 |
|
| 203 |
def compress_to_zip(file_path: str, original_filename: str, zip_path: str):
|
|
|
|
|
|
|
|
|
|
| 204 |
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zf:
|
| 205 |
zf.write(file_path, arcname=original_filename)
|
| 206 |
logger.info(f"π¦ Compressed to ZIP: {zip_path}")
|
| 207 |
|
| 208 |
+
def compress_chunk_to_zip(chunk_path: str, arcname: str, zip_path: str):
|
| 209 |
+
"""Wrap a binary chunk into a ZIP with the given arcname."""
|
| 210 |
+
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zf:
|
| 211 |
+
zf.write(chunk_path, arcname=arcname)
|
| 212 |
+
logger.info(f"π¦ Chunk zipped: {zip_path}")
|
| 213 |
+
|
| 214 |
+
# ---------- File splitting ----------
|
| 215 |
+
CHUNK_SIZE = 19 * 1024 * 1024 # 19 MB per chunk
|
| 216 |
+
|
| 217 |
+
def split_file(source_path: str, base_name: str, parts_dir: str):
|
| 218 |
+
"""
|
| 219 |
+
Splits a file into chunks.
|
| 220 |
+
Yields (chunk_path, part_number, total_parts).
|
| 221 |
+
chunk_path is the raw binary chunk (will be zipped later).
|
| 222 |
+
"""
|
| 223 |
+
file_size = os.path.getsize(source_path)
|
| 224 |
+
total_parts = math.ceil(file_size / CHUNK_SIZE)
|
| 225 |
+
part_num = 1
|
| 226 |
+
with open(source_path, 'rb') as f:
|
| 227 |
+
while True:
|
| 228 |
+
chunk_data = f.read(CHUNK_SIZE)
|
| 229 |
+
if not chunk_data:
|
| 230 |
+
break
|
| 231 |
+
chunk_name = f"{base_name}.part{part_num}"
|
| 232 |
+
chunk_path = os.path.join(parts_dir, chunk_name)
|
| 233 |
+
with open(chunk_path, 'wb') as chunk_file:
|
| 234 |
+
chunk_file.write(chunk_data)
|
| 235 |
+
logger.info(f"βοΈ Created part {part_num}/{total_parts}: {chunk_name}")
|
| 236 |
+
yield chunk_path, part_num, total_parts
|
| 237 |
+
part_num += 1
|
| 238 |
+
|
| 239 |
# ---------- Message / Callback handlers ----------
|
| 240 |
def is_authorized(chat_id: int) -> bool:
|
| 241 |
+
if not ALLOWED_USERS:
|
|
|
|
| 242 |
return True
|
| 243 |
return chat_id in ALLOWED_USERS
|
| 244 |
|
|
|
|
| 317 |
|
| 318 |
logger.info(f"β¬οΈ Requested asset: {original_filename}")
|
| 319 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
# Status message
|
| 321 |
status_msg = await send_message(chat_id, "β³ Downloading file from GitHub...")
|
| 322 |
status_message_id = status_msg.get("result", {}).get("message_id") if status_msg.get("ok") else None
|
|
|
|
| 326 |
tmp_path = tmp.name
|
| 327 |
try:
|
| 328 |
await download_file(url, tmp_path)
|
| 329 |
+
file_size = os.path.getsize(tmp_path)
|
| 330 |
+
size_mb = file_size / (1024 * 1024)
|
| 331 |
+
logger.info(f"β
Downloaded: {size_mb:.2f} MB")
|
| 332 |
|
| 333 |
+
if file_size > CHUNK_SIZE:
|
| 334 |
+
# Large file: split, wrap each chunk in a ZIP, and send them
|
| 335 |
+
await send_message(chat_id, f"π¦ File is {size_mb:.2f} MB β splitting and compressing parts...")
|
| 336 |
+
# Delete the old status message
|
| 337 |
+
if status_message_id:
|
| 338 |
+
await bale_request("deleteMessage", {"chat_id": chat_id, "message_id": status_message_id})
|
| 339 |
+
|
| 340 |
+
# Prepare a safe base name for the ZIP files (avoid .apk in ZIP name)
|
| 341 |
+
if original_filename.lower().endswith('.apk'):
|
| 342 |
+
safe_base = original_filename[:-4] # remove .apk
|
| 343 |
+
else:
|
| 344 |
+
safe_base = original_filename
|
| 345 |
+
|
| 346 |
+
with tempfile.TemporaryDirectory() as parts_dir:
|
| 347 |
+
total_parts = math.ceil(file_size / CHUNK_SIZE)
|
| 348 |
+
for chunk_path, part_num, total in split_file(tmp_path, original_filename, parts_dir):
|
| 349 |
+
# Compress the chunk into a ZIP
|
| 350 |
+
zip_name = f"{safe_base}.part{part_num}.zip"
|
| 351 |
+
zip_path = os.path.join(parts_dir, zip_name)
|
| 352 |
+
# The arcname inside the ZIP should be something like "original.apk.part1"
|
| 353 |
+
arcname = f"{original_filename}.part{part_num}"
|
| 354 |
+
compress_chunk_to_zip(chunk_path, arcname, zip_path)
|
| 355 |
+
|
| 356 |
+
caption = (
|
| 357 |
+
f"π¦ Part {part_num}/{total} (inside ZIP `{zip_name}`)\n"
|
| 358 |
+
f"Original file: `{original_filename}`\n\n"
|
| 359 |
+
"After downloading all parts:\n"
|
| 360 |
+
"1. Extract each ZIP to get the `.part*` files.\n"
|
| 361 |
+
f"2. Combine them:\n"
|
| 362 |
+
f" Windows: `copy /b *.part* {original_filename}`\n"
|
| 363 |
+
f" Linux/Mac: `cat *.part* > {original_filename}`"
|
| 364 |
+
)
|
| 365 |
+
result = await send_document_file(chat_id, zip_path,
|
| 366 |
+
caption=caption,
|
| 367 |
+
display_filename=zip_name)
|
| 368 |
+
if not result.get("ok"):
|
| 369 |
+
await send_message(chat_id, f"β Failed to send part {part_num}. Error: {result.get('description')}")
|
| 370 |
+
break # stop sending further parts
|
| 371 |
+
# Clean up the raw chunk and its ZIP after sending
|
| 372 |
+
try:
|
| 373 |
+
os.unlink(chunk_path)
|
| 374 |
+
except:
|
| 375 |
+
pass
|
| 376 |
+
try:
|
| 377 |
+
os.unlink(zip_path)
|
| 378 |
+
except:
|
| 379 |
+
pass
|
| 380 |
+
|
| 381 |
+
await answer_callback(callback_id, "All parts sent." if file_size > CHUNK_SIZE else "Part sent.")
|
| 382 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
else:
|
| 384 |
+
# Small file β proceed with compression if needed
|
|
|
|
|
|
|
|
|
|
|
|
|
| 385 |
if status_message_id:
|
| 386 |
+
await bale_request("editMessageText", {
|
| 387 |
"chat_id": chat_id,
|
| 388 |
+
"message_id": status_message_id,
|
| 389 |
+
"text": f"π₯ Downloaded {size_mb:.2f} MB β processing..."
|
| 390 |
})
|
| 391 |
+
else:
|
| 392 |
+
await send_message(chat_id, f"π₯ Downloaded {size_mb:.2f} MB β processing...")
|
| 393 |
+
|
| 394 |
+
need_compress = not is_compressed(original_filename)
|
| 395 |
+
if need_compress:
|
| 396 |
+
zip_filename = original_filename + ".zip"
|
| 397 |
+
caption = f"π¦ File compressed to ZIP for delivery.\nOriginal: `{original_filename}`"
|
| 398 |
+
await send_message(chat_id, "π¦ Compressing file into ZIP...")
|
| 399 |
+
zip_tmp = tempfile.NamedTemporaryFile(delete=False, suffix="_" + zip_filename)
|
| 400 |
+
zip_path = zip_tmp.name
|
| 401 |
+
zip_tmp.close()
|
| 402 |
+
compress_to_zip(tmp_path, original_filename, zip_path)
|
| 403 |
+
os.unlink(tmp_path) # remove original
|
| 404 |
+
upload_path = zip_path
|
| 405 |
+
upload_filename = zip_filename
|
| 406 |
+
else:
|
| 407 |
+
upload_path = tmp_path
|
| 408 |
+
upload_filename = original_filename
|
| 409 |
+
caption = "Here you go!"
|
| 410 |
+
|
| 411 |
+
result = await send_document_file(chat_id, upload_path,
|
| 412 |
+
caption=caption,
|
| 413 |
+
display_filename=upload_filename)
|
| 414 |
+
if result.get("ok"):
|
| 415 |
+
if status_message_id:
|
| 416 |
+
await bale_request("deleteMessage", {"chat_id": chat_id, "message_id": status_message_id})
|
| 417 |
+
await answer_callback(callback_id, "Sent!")
|
| 418 |
+
else:
|
| 419 |
+
raw_error = result.get("description", str(result))
|
| 420 |
+
await send_message(chat_id, f"β Upload failed. Bale response:\n{raw_error}")
|
| 421 |
+
await answer_callback(callback_id, "Failed to send.")
|
| 422 |
+
# Clean up temp files
|
| 423 |
+
try:
|
| 424 |
+
os.unlink(upload_path)
|
| 425 |
+
except:
|
| 426 |
+
pass
|
| 427 |
+
|
| 428 |
except Exception as e:
|
| 429 |
logger.error(f"β Download/Upload error: {e}", exc_info=True)
|
| 430 |
await send_message(chat_id, f"β Error: {e}")
|
| 431 |
await answer_callback(callback_id, "Failed.")
|
| 432 |
finally:
|
| 433 |
+
if os.path.exists(tmp_path):
|
| 434 |
+
try:
|
| 435 |
+
os.unlink(tmp_path)
|
| 436 |
+
except:
|
| 437 |
+
pass
|
|
|
|
|
|
|
| 438 |
else:
|
| 439 |
await answer_callback(callback_id, "Unknown action.")
|
| 440 |
|
|
|
|
| 491 |
keyboard = build_inline_keyboard(release)
|
| 492 |
for cid in chat_ids:
|
| 493 |
if not is_authorized(int(cid)):
|
| 494 |
+
continue
|
| 495 |
try:
|
| 496 |
await send_message(int(cid), f"π New release for {owner}/{repo}: *{release['name']}*", reply_markup=keyboard)
|
| 497 |
logger.info(f"π Notified {cid} about {repo_url}")
|