Spaces:
Running on Zero
Running on Zero
File size: 11,285 Bytes
8ce9e44 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | import json
import os
from config import DB_FILE, DATA_DIR
DB_CACHE = os.path.join(DATA_DIR, "manga_cache.json")
DB_CACHE = os.path.join(DATA_DIR, "manga_cache.json")
DB_TAGS = os.path.join(DATA_DIR, "hashtags.json")
DB_VIRAL = os.path.join(DATA_DIR, "viral_shorts.json")
# --- VARIABLES GLOBALES (NO BORRAR) ---
url_storage = {} # <-- Esta es la variable que te faltaba
user_config = {}
downloads_db = {}
downloads_db = {}
hashtag_db = {} # <-- Base de datos de hashtags
viral_music_db = [] # <-- Base de datos de Shorts/Music Viral (Lista de Dicts)
active_downloads = {} # {chat_id: {msg_id: future/task}}
user_cooldowns = {} # {chat_id: timestamp}
task_progress = {} # {chat_id: {msg_id: {percent, speed, status, name, url}}}
# --- FUNCIONES ---
def cargar_db():
global downloads_db
if os.path.exists(DB_FILE):
try:
with open(DB_FILE, 'r') as f:
downloads_db.update(json.load(f))
except:
downloads_db = {}
def guardar_db():
try:
with open(DB_FILE, 'w') as f:
json.dump(downloads_db, f, indent=4)
except:
pass
def load_tags():
global hashtag_db
if os.path.exists(DB_TAGS):
try:
with open(DB_TAGS, 'r', encoding='utf-8') as f:
hashtag_db.update(json.load(f))
except:
hashtag_db = {}
def save_tags():
try:
with open(DB_TAGS, 'w', encoding='utf-8') as f:
json.dump(hashtag_db, f, indent=4, ensure_ascii=False)
except:
pass
def load_viral():
global viral_music_db
if os.path.exists(DB_VIRAL):
try:
with open(DB_VIRAL, 'r', encoding='utf-8') as f:
viral_music_db.extend(json.load(f))
except:
viral_music_db = []
def save_viral():
try:
with open(DB_VIRAL, 'w', encoding='utf-8') as f:
json.dump(viral_music_db, f, indent=4, ensure_ascii=False)
except:
pass
def get_config(chat_id):
if chat_id not in user_config:
user_config[chat_id] = {
'lang': 'es',
'fmt': 'mp4',
'q_fixed': None,
'q_auto': None,
'meta': True,
'fast_enabled': True,
'html_mode': False,
'doc_mode': False,
'replay_enabled': False, # <--- Nuevo modo replay
'party_mode': False,
'join_party_mode': False,
'extract_mode': False,
'ai_mode': False, # Modo Party
'auto_join_party': False,
'auto_extract': False,
'gif_mode': False,
'sticker_mode': False,
'join_party_zip': False,
'extract_party_zip': True,
'collage_mode': False, # <-- Nuevo Modo Collage
'collage_auto_mode': False, # <-- DEPRECATED (Migration might be needed or just ignore)
'collage_grid': '3x3', # '3x3', '2x2', '2x1'
'collage_auto_3x3': False,
'collage_auto_2x2': False,
'collage_auto_2x1': False,
'sub_mode': False, # Nuevo modo subtitulado (Manual/One-Shot)
'sub_auto_mode': False, # Nuevo modo subtitulado (Auto/Persistent)
'viral_mode': False, # <-- Modo Short Virales Music (Recoleccion)
'smart_cut_mode': False, # <-- Smart Cut (Detector Intro)
'collage_type': 'png', # 'png' o 'thumb'
'bot_link_mode': False, # <-- Modo Enlace Directo (Servidor Local)
}
return user_config[chat_id]
# --- LISTA DE MODOS PARA EXCLUSIVIDAD ---
ALL_MODES_KEYS = [
'party_mode', 'join_party_mode', 'extract_mode', 'gif_mode',
'sticker_mode', 'saucenao_mode', 'mp3_mode', 'cut_init_mode',
'cut_auto_mode', 'auto_party', 'resize_mode', 'resize_auto_mode',
'thumb_mode', 'convert_qh_mode', 'convert_h264_mode',
'smart_cut_mode', 'collage_mode', 'sub_mode', 'sub_auto_mode',
'viral_mode', 'research_mode', 'tagger_mode', 'translate_manga_mode',
'renviado_directo', 'renviado_agrupado',
'auto_join_party', 'auto_extract', 'smart_cut_auto_mode',
'waifux2_mode', 'video_mixer_mode', 'tiktok_fast_mode', 'fusion_mode',
'parallel_thumb_mode', 'bot_link_mode', 'fusion_auto_mode'
]
ALL_ZIP_KEYS = [
'party_zip', 'join_party_zip', 'extract_party_zip', 'join_party_zip'
]
# Friendly Names Mapping
MODE_NAMES = {
'party_mode': 'Modo Party',
'join_party_mode': 'Modo Unir Party',
'extract_mode': 'Modo Extractor',
'gif_mode': 'Modo GIF',
'sticker_mode': 'Modo Sticker',
'saucenao_mode': 'Modo SauceNAO',
'mp3_mode': 'Modo Extractor MP3',
'cut_init_mode': 'Modo Corte Inicial',
'cut_auto_mode': 'Modo Auto Corte',
'auto_party': 'Auto Party',
'resize_mode': 'Modo Dimensionador',
'resize_auto_mode': 'Auto Dimensionador',
'thumb_mode': 'Modo Miniatura',
'convert_qh_mode': 'Convertidor QH',
'convert_h264_mode': 'Convertidor H.264',
'smart_cut_mode': 'Smart Cut',
'collage_mode': 'Modo Collage',
'sub_mode': 'Modo Subtitulado',
'sub_auto_mode': 'Auto Subtitulado',
'viral_mode': 'Modo Virales',
'research_mode': 'Investigaci贸n Profunda',
'tagger_mode': 'Etiquetadora',
'translate_manga_mode': 'Traductor Manga',
'custom_thumb_mode': 'Miniatura Personalizada',
'renviado_directo': 'Reenviado Directo',
'renviado_agrupado': 'Reenviado Agrupado',
'auto_join_party': 'Auto Unir Party',
'auto_extract': 'Auto Extractor',
'smart_cut_auto_mode': 'Auto Smart Cut',
'waifux2_mode': 'Modo Waifu2x',
'tiktok_fast_mode': 'Tiktok Fast',
'fusion_mode': 'Modo Fusi贸n',
'parallel_thumb_mode': 'Miniatura Personalizada',
'bot_link_mode': 'Enlace Directo'
}
async def set_exclusive_mode(chat_id, target_mode=None, is_toggle=False):
"""
Activa un modo y desactiva TODOS los demas.
Retorna: (new_state, disabled_mode_name)
disabled_mode_name es el nombre legible del modo que se apag贸 (si hubo uno).
"""
conf = get_config(chat_id)
# 1. Identificar qu茅 modo estaba activo antes de limpiar (para el mensaje)
disabled_name = None
for k in ALL_MODES_KEYS:
if conf.get(k, False):
# Ignoramos si es el mismo que vamos a togglear (eso no es "conflicto", es toggle)
if k != target_mode:
disabled_name = MODE_NAMES.get(k, k)
break # Asumimos solo uno activo por las reglas previas
# 2. Determinar nuevo estado deseado
new_state = True
if target_mode:
if is_toggle:
new_state = not conf.get(target_mode, False)
else:
new_state = True
# 3. Limpiar TODO siempre (Regla de Oro)
# Incluso si apagamos (new_state False), limpiamos para asegurar consistencia
for k in ALL_MODES_KEYS:
conf[k] = False
# Reset Zips (Clean Slate Rule)
for k in ALL_ZIP_KEYS:
conf[k] = False
# 4. Aplicar nuevo estado SOLO si es True
if target_mode and new_state:
conf[target_mode] = True
# 5. Guardar
from firebase_service import save_user_config_fb
await save_user_config_fb(chat_id, conf)
return new_state, disabled_name
def can_download(chat_id, max_concurrent=3, cooldown_sec=2):
# Restricciones deshabilitadas a petici贸n del usuario (Modo Ilimitado)
return True, None
def add_active(chat_id, msg_id, task=None, pid=None):
if chat_id not in active_downloads:
active_downloads[chat_id] = {}
# Guardamos diccionario con task y pid opcional
active_downloads[chat_id][msg_id] = {'task': task, 'pid': pid}
def remove_active(chat_id, msg_id):
if chat_id in active_downloads and msg_id in active_downloads[chat_id]:
del active_downloads[chat_id][msg_id]
if not active_downloads[chat_id]:
del active_downloads[chat_id]
# Tambi茅n limpiar progreso
remove_progress(chat_id, msg_id)
# --- FUNCIONES DE PROGRESO (TRACKING EN VIVO) ---
def update_progress(chat_id, msg_id, percent=None, speed=None, status=None, name=None, url=None):
if chat_id not in task_progress:
task_progress[chat_id] = {}
if msg_id not in task_progress[chat_id]:
task_progress[chat_id][msg_id] = {'percent': 0, 'speed': 'N/A', 'status': 'Esperando', 'name': 'Archivo', 'url': 'N/A'}
if percent is not None: task_progress[chat_id][msg_id]['percent'] = percent
if speed is not None: task_progress[chat_id][msg_id]['speed'] = speed
if status is not None: task_progress[chat_id][msg_id]['status'] = status
if name is not None: task_progress[chat_id][msg_id]['name'] = name
if url is not None: task_progress[chat_id][msg_id]['url'] = url
def get_all_progress():
return task_progress
def remove_progress(chat_id, msg_id):
if chat_id in task_progress and msg_id in task_progress[chat_id]:
del task_progress[chat_id][msg_id]
if not task_progress[chat_id]:
del task_progress[chat_id]
async def cancel_all(chat_id):
cancelled_items = []
if chat_id in active_downloads:
tasks_map = active_downloads[chat_id]
for mid, info in tasks_map.items():
# Soportar formato antiguo (solo task) o nuevo (dict)
if isinstance(info, dict):
task = info.get('task')
pid = info.get('pid')
else:
task = info
pid = None
# --- COLLECT INFO FOR REPORT ---
# Try to get URL/Name from task_progress
url = "Enlace desconocido"
if chat_id in task_progress and mid in task_progress[chat_id]:
url = task_progress[chat_id][mid].get('url', 'Enlace desconocido')
cancelled_items.append({'url': url, 'msg_id': mid})
# 1. Cancelar Task de Python
if task and not task.done():
task.cancel()
# 2. Matar proceso del sistema (FFmpeg / Aria2 / 7z)
if pid:
try:
import signal
# En Windows, os.kill con SIGTERM suele funcionar, o taskkill forzado
os.kill(pid, signal.SIGTERM)
print(f"馃敧 Process Killed: PID {pid}")
except Exception as e:
print(f"鈿狅笍 Kill Error {pid}: {e}")
# Intento Force Windows
try:
import subprocess
subprocess.run(["taskkill", "/F", "/T", "/PID", str(pid)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except: pass
active_downloads.pop(chat_id, None)
# Clean progress entirely for this chat
task_progress.pop(chat_id, None)
return cancelled_items
# Cargar base de datos al iniciar
cargar_db()
load_tags() |