diff --git a/bot/__init__.py b/bot/__init__.py index 6dd51e1..1d07bf2 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -15,3 +15,5 @@ class Bot(discord.Bot): async def on_ready(self): print(f'Zalogowano jako {self.user} (ID: {self.user.id})') print(f' Pycord {discord.__version__} | Bot v{__version__}') + activity = discord.Activity(type=discord.ActivityType.listening, name='audiobooka Biblii po śląsku') + await self.change_presence(activity=activity) diff --git a/bot/cogs/mateusz.py b/bot/cogs/mateusz.py index 5f4ab0e..674d697 100644 --- a/bot/cogs/mateusz.py +++ b/bot/cogs/mateusz.py @@ -1,5 +1,5 @@ import discord -from ..utils import helpers +from ..utils import ytdlp_helpers as helpers from discord.ext import commands @@ -17,9 +17,9 @@ class Simp(commands.Cog): await ctx.respond(f'Błąd pobierania: {result["message"]}') return - print(f'Wysyłanie "{result["title"]}" na kanał {ctx.channel.name} ({ctx.guild.name})') + print(f'Wysyłanie "{result["title"]}" na kanał #{ctx.channel.name} ({ctx.guild.name})') await ctx.respond(file=discord.File(result['filename'])) - print(f'Wysyłano "{result["title"]}" na kanał {ctx.channel.name} ({ctx.guild.name})') + print(f'Wysyłano "{result["title"]}" na kanał #{ctx.channel.name} ({ctx.guild.name})') def setup(bot: discord.Bot): diff --git a/bot/utils/helpers.py b/bot/utils/ytdlp_helpers.py similarity index 77% rename from bot/utils/helpers.py rename to bot/utils/ytdlp_helpers.py index bb7603b..56ed8df 100644 --- a/bot/utils/helpers.py +++ b/bot/utils/ytdlp_helpers.py @@ -24,7 +24,7 @@ def rotate_downloads(downloads_dir: str): def download_video(yt_url: str) -> dict: - yt_url = yt_url.split('&')[0] + yt_url = yt_url.split('/?')[0].split('&')[0] MAX_DUR = os.getenv('MAX_DURATION') if not MAX_DUR.isnumeric(): return {'status': 'error', 'message': f'MAX_DUR: "{MAX_DUR}" - nie jest cyfrą.'} @@ -37,6 +37,10 @@ def download_video(yt_url: str) -> dict: existing = [f for f in os.listdir(DOWNLOADS) if url_hash in f] if existing: + if existing[0].split('.')[-1] != 'mp4': + os.remove(os.path.join(DOWNLOADS, existing[0])) + return {'status': 'error', 'message': f'Błędnie pobrany plik - został usunięty.'} + print(f'Znaleziono plik "{existing[0]}" ({yt_url})') return { 'status': 'success', @@ -51,14 +55,15 @@ def download_video(yt_url: str) -> dict: info = ydl.extract_info(yt_url, download=False) except Exception as e: print(f'Nie można pobrać metadanych: {e}') - return {'status': 'error', 'message': f'Nie można pobrać metadanych: {e}'} + return {'status': 'error', 'message': f'Nie można pobrać metadanych - {e}'} duration = info.get('duration', 0) - if duration > MAX_DUR: - print(f'Wideo jest za długie ({duration // 60}m {duration % 60}s). Maksymalnie {MAX_DUR // 60}m {MAX_DUR % 60}s.') + + if duration > MAX_DUR or duration == 0: + print(f'Błędna długość wideo ({duration // 60}m {duration % 60}s). Maksymalnie {MAX_DUR // 60}m {MAX_DUR % 60}s.') return { 'status': 'error', - 'message': f'Wideo jest za długie ({duration // 60}m {duration % 60}s). Maksymalnie {MAX_DUR // 60}m {MAX_DUR % 60}s.' + 'message': f'Błędna długość wideo ({duration // 60}m {duration % 60}s). Maksymalnie {MAX_DUR // 60}m {MAX_DUR % 60}s.' } rotate_downloads(DOWNLOADS) @@ -68,16 +73,20 @@ def download_video(yt_url: str) -> dict: 'merge_output_format': 'mp4', 'outtmpl': os.path.join(DOWNLOADS, f'%(title)s_{url_hash}.%(ext)s'), 'quiet': True, + 'http_headers': { + 'User-Agent': 'Mozilla/5.0' + } } with yt_dlp.YoutubeDL(ydl_opts) as ydl: try: info = ydl.extract_info(yt_url, download=True) filename = ydl.prepare_filename(info) + print(filename) print(f'Pobrano "{info.get("title")}" ({yt_url})') except Exception as e: print(f'Błąd pobierania: {e}') - return {'status': 'error', 'message': f'Błąd pobierania: {e}'} + return {'status': 'error', 'message': f'{e}'} return { 'status': 'success',