From 46f1ea616cf188306d3364b44ae79084091f2a6c Mon Sep 17 00:00:00 2001 From: ArabCoders Date: Wed, 27 Dec 2023 16:28:42 +0300 Subject: [PATCH] Separate downloads fragments into their own temp dir --- README.md | 1 - app/src/Download.py | 14 ++++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 92581831..f84bec06 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,6 @@ Your `yt-dlp` config should include the following options for optimal working co ```json { "windowsfilenames": true, - "continue_dl": true, "live_from_start": true, "format_sort": [ "codec:avc:m4a" diff --git a/app/src/Download.py b/app/src/Download.py index abe4f77c..9dbc4fb0 100644 --- a/app/src/Download.py +++ b/app/src/Download.py @@ -5,6 +5,7 @@ import logging import multiprocessing import os import re +import shutil import yt_dlp from src.Utils import get_format, get_opts, jsonCookie, mergeConfig from src.DTO.ItemDTO import ItemDTO @@ -23,6 +24,7 @@ class Download: info: ItemDTO = None default_ytdl_opts: dict = None debug: bool = False + tempPath: str = None _ytdlp_fields: tuple = ( 'tmpfilename', @@ -85,12 +87,17 @@ class Download: } ) + # Create temp dir for each download. + self.tempPath = os.path.join(self.temp_dir, self.info._id) + if not os.path.exists(self.tempPath): + os.makedirs(self.tempPath, exist_ok=True) + params: dict = { 'no_color': True, 'format': self.format, 'paths': { 'home': self.download_dir, - 'temp': self.temp_dir + 'temp': self.tempPath, }, 'outtmpl': { 'default': self.output_template, @@ -111,7 +118,7 @@ class Download: if not data: logging.warning( f'The cookie string that was provided for {self.info.title} is empty or not in expected spec.') - with open(os.path.join(self.temp_dir, f'{self.info._id}.txt'), 'w') as f: + with open(os.path.join(self.tempPath, f'cookie_{self.info._id}.txt'), 'w') as f: f.write(data) params['cookiefile'] = f.name @@ -129,6 +136,9 @@ class Download: except yt_dlp.utils.YoutubeDLError as exc: self.status_queue.put({'status': 'error', 'msg': str(exc)}) + if self.tempPath and self.info._id and os.path.exists(self.tempPath): + shutil.rmtree(self.tempPath, ignore_errors=True) + async def start(self, notifier): if self.manager is None: self.manager = multiprocessing.Manager()