← Voltar

cleaner.py

import os
import shutil
import time
from telegram.ext import ContextTypes

TEMP_BASE = "/tmp"
PREFIXES = ("yt_audio_", "yt_video_", "instagram_", "facebook_", "tiktok_")

async def clean_temp_folders(context: ContextTypes.DEFAULT_TYPE):
    now = time.time()
    removed_count = 0

    for name in os.listdir(TEMP_BASE):
        if not name.startswith(PREFIXES):
            continue
        full_path = os.path.join(TEMP_BASE, name)
        if not os.path.isdir(full_path):
            continue
        age = now - os.path.getmtime(full_path)
        if age > 3600:
            shutil.rmtree(full_path, ignore_errors=True)
            removed_count += 1

    if removed_count > 0:
        print(f"[Cleaner] Removidas {removed_count} pastas temporárias antigas.")