Minor fixes and .part detection

This commit is contained in:
Patryk Mazur
2026-05-15 20:13:02 +02:00
parent c188c515e2
commit 8f5c8d3d1c
3 changed files with 20 additions and 9 deletions
@@ -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',