From 1514ce1dc6e6681ac0c2e2f9f544c9b14bd4e356 Mon Sep 17 00:00:00 2001 From: Patryk Mazur Date: Wed, 22 Apr 2026 19:36:56 +0200 Subject: [PATCH] Rotation and hashing V1.0.1 --- bot/__init__.py | 8 +++--- bot/cogs/mateusz.py | 11 ++++++--- bot/utils/helpers.py | 58 ++++++++++++++++++++++++++++++++++++++------ main.py | 4 +-- 4 files changed, 65 insertions(+), 16 deletions(-) diff --git a/bot/__init__.py b/bot/__init__.py index c9dd72f..d4de865 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -1,7 +1,7 @@ import discord -__version__ = "1.0.0" -__author__ = "Kartoniarz" +__version__ = '1.0.1' +__author__ = 'Kartoniarz' class Bot(discord.Bot): @@ -13,5 +13,5 @@ class Bot(discord.Bot): super().__init__(intents=intents) async def on_ready(self): - print(f"Zalogowano jako {self.user} (ID: {self.user.id})") - print(f" Pycord {discord.__version__} | Bot v{__version__}") + print(f'Zalogowano jako {self.user} (ID: {self.user.id})') + print(f' Pycord {discord.__version__} | Bot v{__version__}') diff --git a/bot/cogs/mateusz.py b/bot/cogs/mateusz.py index 03af15e..fec69d0 100644 --- a/bot/cogs/mateusz.py +++ b/bot/cogs/mateusz.py @@ -7,12 +7,17 @@ class Simp(commands.Cog): def __init__(self, bot: discord.Bot): self.bot = bot - @discord.slash_command(description="Nie, nie jestem pantoflem") + @discord.slash_command(description='Nie, nie jestem pantoflem') async def pantofel(self, ctx: discord.ApplicationContext, url: str): await ctx.defer() - filename = helpers.download_video(url) - await ctx.respond(file=discord.File(filename)) + result = helpers.download_video(url) + + if result['status'] == 'error': + await ctx.respond(f'Błąd pobierania: {result['message']}') + return + + await ctx.respond(file=discord.File(result['filename'])) def setup(bot: discord.Bot): diff --git a/bot/utils/helpers.py b/bot/utils/helpers.py index 5e74ea3..6de68c2 100644 --- a/bot/utils/helpers.py +++ b/bot/utils/helpers.py @@ -1,20 +1,64 @@ import os import yt_dlp +import hashlib -def download_video(yt_url: str) -> str: +def rotate_downloads(downloads_dir: str): + MAX_FILES = os.getenv('MAX_FILES') + + files = [ + os.path.join(downloads_dir, f) + for f in os.listdir(downloads_dir) + if os.path.isfile(os.path.join(downloads_dir, f)) + ] + + if len(files) >= MAX_FILES: + files.sort(key=os.path.getmtime) + to_delete = files[:len(files) - MAX_FILES + 1] + for f in to_delete: + os.remove(f) + print(f'Usunięto stary plik: {os.path.basename(f)}') + + +def download_video(yt_url: str) -> dict: + MAX_DUR = os.getenv('MAX_DURATION') BASE_DIR = os.path.dirname(os.path.abspath(__file__)) - DOWNLOADS = os.path.join(BASE_DIR, "downloads") + DOWNLOADS = os.path.join(BASE_DIR, 'downloads') + os.makedirs(DOWNLOADS, exist_ok=True) + + with yt_dlp.YoutubeDL({'quiet': True}) as ydl: + try: + info = ydl.extract_info(yt_url, download=False) + except Exception as e: + return {'status': 'error', 'message': f'Nie można pobrać metadanych: {e}'} + + duration = info.get('duration', 0) + if duration > MAX_DUR: + return { + 'status': 'error', + 'message': f'Wideo jest za długie ({duration // 60}m {duration % 60}s). Maksymalnie 10 minut.' + } + + url_hash = hashlib.md5(yt_url.encode()).hexdigest()[:10] ydl_opts = { 'format': 'bestvideo+bestaudio/best', 'merge_output_format': 'mp4', - 'outtmpl': f'{DOWNLOADS}/%(title)s.%(ext)s', + 'outtmpl': os.path.join(DOWNLOADS, f'%(title)s_{url_hash}.%(ext)s'), + 'quiet': True, } with yt_dlp.YoutubeDL(ydl_opts) as ydl: - os.makedirs(DOWNLOADS, exist_ok=True) - info = ydl.extract_info(yt_url, download=True) - filename = ydl.prepare_filename(info) + try: + info = ydl.extract_info(yt_url, download=True) + filename = ydl.prepare_filename(info) + except Exception as e: + return {'status': 'error', 'message': f'Błąd pobierania: {e}'} - return filename + return { + 'status': 'success', + 'filename': filename, + 'title': info.get('title'), + 'duration': duration, + 'url': yt_url, + } diff --git a/main.py b/main.py index edc7b64..d1b70dd 100644 --- a/main.py +++ b/main.py @@ -8,6 +8,6 @@ load_dotenv() asyncio.set_event_loop(asyncio.new_event_loop()) bot = Bot() -bot.load_extension("bot.cogs") +bot.load_extension('bot.cogs') -bot.run(os.getenv("TOKEN")) +bot.run(os.getenv('TOKEN'))