Rotation and hashing V1.0.1
This commit is contained in:
+4
-4
@@ -1,7 +1,7 @@
|
|||||||
import discord
|
import discord
|
||||||
|
|
||||||
__version__ = "1.0.0"
|
__version__ = '1.0.1'
|
||||||
__author__ = "Kartoniarz"
|
__author__ = 'Kartoniarz'
|
||||||
|
|
||||||
|
|
||||||
class Bot(discord.Bot):
|
class Bot(discord.Bot):
|
||||||
@@ -13,5 +13,5 @@ class Bot(discord.Bot):
|
|||||||
super().__init__(intents=intents)
|
super().__init__(intents=intents)
|
||||||
|
|
||||||
async def on_ready(self):
|
async def on_ready(self):
|
||||||
print(f"Zalogowano jako {self.user} (ID: {self.user.id})")
|
print(f'Zalogowano jako {self.user} (ID: {self.user.id})')
|
||||||
print(f" Pycord {discord.__version__} | Bot v{__version__}")
|
print(f' Pycord {discord.__version__} | Bot v{__version__}')
|
||||||
|
|||||||
+8
-3
@@ -7,12 +7,17 @@ class Simp(commands.Cog):
|
|||||||
def __init__(self, bot: discord.Bot):
|
def __init__(self, bot: discord.Bot):
|
||||||
self.bot = 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):
|
async def pantofel(self, ctx: discord.ApplicationContext, url: str):
|
||||||
await ctx.defer()
|
await ctx.defer()
|
||||||
|
|
||||||
filename = helpers.download_video(url)
|
result = helpers.download_video(url)
|
||||||
await ctx.respond(file=discord.File(filename))
|
|
||||||
|
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):
|
def setup(bot: discord.Bot):
|
||||||
|
|||||||
+49
-5
@@ -1,20 +1,64 @@
|
|||||||
import os
|
import os
|
||||||
import yt_dlp
|
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__))
|
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 = {
|
ydl_opts = {
|
||||||
'format': 'bestvideo+bestaudio/best',
|
'format': 'bestvideo+bestaudio/best',
|
||||||
'merge_output_format': 'mp4',
|
'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:
|
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
||||||
os.makedirs(DOWNLOADS, exist_ok=True)
|
try:
|
||||||
info = ydl.extract_info(yt_url, download=True)
|
info = ydl.extract_info(yt_url, download=True)
|
||||||
filename = ydl.prepare_filename(info)
|
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,
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user