Spaces:
Paused
Paused
Download plugins/dl_mega.py from saske560/userbotsaskeds: direct link, hf CLI and curl.
- Browser
- Download file 7.44 kB
-
https://huggingface.co/spaces/saske560/userbotsaskeds/resolve/main/plugins/dl_mega.py
- Command line
-
hf download hf://spaces/saske560/userbotsaskeds/plugins/dl_mega.py
-
curl -L -o dl_mega.py https://huggingface.co/spaces/saske560/userbotsaskeds/resolve/main/plugins/dl_mega.py
7.44 kB
| """ | |
| plugins/dl_mega.py β Descargador de Mega.nz. | |
| Descarga archivos de mega.nz/file/... o mega.nz/folder/... y los sube a Telegram | |
| detectando automΓ‘ticamente el tipo (video, audio, imagen, documento). | |
| Comandos: | |
| .mega <url> β Descarga archivo de Mega.nz | |
| .mcancel β Cancela descarga activa en este chat | |
| Requiere: pip install mega.py pycryptodome | |
| """ | |
| import asyncio | |
| import os | |
| import re | |
| import time | |
| from pathlib import Path | |
| import requests | |
| from pyrogram import filters | |
| from pyrogram.types import Message | |
| from bot import app | |
| from config import Config | |
| P = Config.CMD_PREFIX | |
| DL_DIR = Path("downloads"); DL_DIR.mkdir(exist_ok=True) | |
| _active: dict = {} | |
| VIDEO_EXT = {".mp4", ".mkv", ".avi", ".mov", ".wmv", ".flv", ".webm"} | |
| AUDIO_EXT = {".mp3", ".m4a", ".wav", ".flac", ".aac", ".ogg"} | |
| PHOTO_EXT = {".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp"} | |
| # ββ Helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def _format_size(b: int) -> str: | |
| for u in ("B", "KB", "MB", "GB"): | |
| if b < 1024: | |
| return f"{b:.1f} {u}" | |
| b /= 1024 | |
| return f"{b:.1f} TB" | |
| def _extract_url(text: str) -> str | None: | |
| for pat in [r"https?://mega\.nz/file/[^\s]+", r"https?://mega\.nz/folder/[^\s]+"]: | |
| found = re.findall(pat, text) | |
| if found: | |
| return found[0] | |
| return None | |
| def _thumb(path: str) -> str | None: | |
| try: | |
| import subprocess | |
| out = path + ".jpg" | |
| subprocess.run(["ffmpeg", "-y", "-i", path, "-ss", "00:00:01", "-vframes", "1", out], | |
| capture_output=True, timeout=15) | |
| return out if Path(out).exists() else None | |
| except Exception: | |
| return None | |
| # ββ .mega βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| async def cmd_mega(client, msg: Message): | |
| await msg.delete() | |
| args = msg.text.split(None, 1) | |
| text = "" | |
| if len(args) > 1: | |
| text = args[1].strip() | |
| elif msg.reply_to_message: | |
| text = msg.reply_to_message.text or msg.reply_to_message.caption or "" | |
| url = _extract_url(text) | |
| if not url: | |
| m = await client.send_message(msg.chat.id, | |
| f"β **Uso:** `{P}mega <url_mega.nz>`\n" | |
| f"Soporta: `mega.nz/file/...` y `mega.nz/folder/...`") | |
| await asyncio.sleep(8); await m.delete(); return | |
| chat_id = msg.chat.id | |
| if chat_id in _active and not _active[chat_id].get("cancelled"): | |
| m = await client.send_message(msg.chat.id, | |
| f"β οΈ Descarga activa. Usa `{P}mcancel` para cancelar.") | |
| await asyncio.sleep(8); await m.delete(); return | |
| status = await client.send_message(msg.chat.id, "π **Procesando enlace de Mega...**") | |
| ev = asyncio.Event() | |
| _active[chat_id] = {"cancelled": False, "cancel_event": ev} | |
| async def _run(): | |
| try: | |
| await _process(client, msg, url, status, ev) | |
| except Exception as e: | |
| try: await status.edit(f"β `{e}`") | |
| except Exception: pass | |
| finally: | |
| _active.pop(chat_id, None) | |
| _active[chat_id]["task"] = asyncio.create_task(_run()) | |
| async def _process(client, msg: Message, url: str, status, ev: asyncio.Event): | |
| if ev.is_set(): | |
| await status.edit("β Cancelada."); await asyncio.sleep(5); await status.delete(); return | |
| await status.edit("π **Obteniendo informaciΓ³n del archivo...**") | |
| try: | |
| import mega as mega_lib | |
| except ImportError: | |
| await status.edit("β **mega.py** no instalado.\nAΓ±ade `mega.py` a requirements.txt.") | |
| await asyncio.sleep(8); await status.delete(); return | |
| try: | |
| m = mega_lib.Mega() | |
| m.login_anonymous() | |
| file_info = m.get_public_url_info(url) | |
| except Exception as e: | |
| await status.edit(f"β Error obteniendo info de Mega: `{e}`") | |
| await asyncio.sleep(8); await status.delete(); return | |
| if not file_info: | |
| await status.edit("β No se pudo obtener informaciΓ³n del archivo.") | |
| await asyncio.sleep(8); await status.delete(); return | |
| filename = file_info.get("name", "mega_file") | |
| filesize = file_info.get("size", 0) | |
| await status.edit( | |
| f"π **Archivo encontrado:**\n" | |
| f"**Nombre:** `{filename}`\n" | |
| f"**TamaΓ±o:** `{_format_size(filesize)}`\n\n" | |
| f"β¬οΈ **Descargando...**" | |
| ) | |
| if ev.is_set(): | |
| await status.edit("β Cancelada."); await asyncio.sleep(5); await status.delete(); return | |
| loop = asyncio.get_event_loop() | |
| safe_name = "".join(c for c in filename if c.isalnum() or c in "._- ") | |
| dest_path = str(DL_DIR / safe_name) | |
| try: | |
| def _dl(): | |
| m.download_url(url, dest_path=str(DL_DIR), dest_filename=safe_name) | |
| await loop.run_in_executor(None, _dl) | |
| except Exception as e: | |
| await status.edit(f"β Error descargando: `{e}`") | |
| await asyncio.sleep(8); await status.delete(); return | |
| if not Path(dest_path).exists(): | |
| await status.edit("β El archivo no se descargΓ³ correctamente.") | |
| await asyncio.sleep(8); await status.delete(); return | |
| await status.edit("π€ **Subiendo a Telegram...**") | |
| ext = Path(filename).suffix.lower() | |
| stem = Path(filename).stem | |
| try: | |
| if ext in VIDEO_EXT: | |
| thumb = _thumb(dest_path) | |
| await client.send_video( | |
| msg.chat.id, dest_path, | |
| caption=f"`{stem}`", | |
| thumb=thumb, | |
| ) | |
| if thumb: Path(thumb).unlink(missing_ok=True) | |
| elif ext in AUDIO_EXT: | |
| await client.send_audio(msg.chat.id, dest_path, caption=f"`{stem}`") | |
| elif ext in PHOTO_EXT: | |
| await client.send_photo(msg.chat.id, dest_path, caption=f"`{stem}`") | |
| else: | |
| await client.send_document(msg.chat.id, dest_path, caption=f"`{stem}`") | |
| except Exception as e: | |
| await client.send_message(msg.chat.id, f"β Error al subir: `{e}`") | |
| Path(dest_path).unlink(missing_ok=True) | |
| await status.delete() | |
| m2 = await client.send_message(msg.chat.id, f"β **Descargado:** `{filename}`") | |
| await asyncio.sleep(5); await m2.delete() | |
| # ββ .mcancel ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| async def cmd_mcancel(client, msg: Message): | |
| await msg.delete() | |
| chat_id = msg.chat.id | |
| entry = _active.get(chat_id) | |
| if entry and not entry.get("cancelled"): | |
| entry["cancelled"] = True | |
| ev = entry.get("cancel_event") | |
| if ev: ev.set() | |
| t = entry.get("task") | |
| if t and not t.done(): t.cancel() | |
| m = await client.send_message(chat_id, "β **Descarga de Mega cancelada.**") | |
| else: | |
| m = await client.send_message(chat_id, "βΉοΈ No hay descarga activa de Mega.") | |
| await asyncio.sleep(6); await m.delete() | |