Add the ability to disable temp file handling
This commit is contained in:
parent
dfe1acbec4
commit
47b5967c58
4 changed files with 35 additions and 13 deletions
|
|
@ -322,4 +322,5 @@ Certain configuration values can be set via environment variables, using the `-e
|
||||||
| YTP_PREVENT_LIVE_PREMIERE | Prevents the initial youtube premiere stream from being downloaded | `false` |
|
| YTP_PREVENT_LIVE_PREMIERE | Prevents the initial youtube premiere stream from being downloaded | `false` |
|
||||||
| YTP_TASKS_HANDLER_TIMER | The cron expression for the tasks handler timer | `15 */1 * * *` |
|
| YTP_TASKS_HANDLER_TIMER | The cron expression for the tasks handler timer | `15 */1 * * *` |
|
||||||
| YTP_PLAYLIST_ITEMS_CONCURRENCY | The number of playlist items be to processed at same time | `1` |
|
| YTP_PLAYLIST_ITEMS_CONCURRENCY | The number of playlist items be to processed at same time | `1` |
|
||||||
|
| YTP_TEMP_DISABLED | Disable temp files handling. | `false` |
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,8 @@ class Download:
|
||||||
id: str = None
|
id: str = None
|
||||||
download_dir: str = None
|
download_dir: str = None
|
||||||
temp_dir: str = None
|
temp_dir: str = None
|
||||||
|
temp_disabled: bool = False
|
||||||
|
"Disable the temporary files feature."
|
||||||
template: str = None
|
template: str = None
|
||||||
template_chapter: str = None
|
template_chapter: str = None
|
||||||
info: ItemDTO = None
|
info: ItemDTO = None
|
||||||
|
|
@ -99,7 +101,7 @@ class Download:
|
||||||
logs (list): Logs from yt-dlp.
|
logs (list): Logs from yt-dlp.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
config = Config.get_instance()
|
config: Config = Config.get_instance()
|
||||||
|
|
||||||
self.download_dir = info.download_dir
|
self.download_dir = info.download_dir
|
||||||
self.temp_dir = info.temp_dir
|
self.temp_dir = info.temp_dir
|
||||||
|
|
@ -117,6 +119,7 @@ class Download:
|
||||||
self._notify = EventBus.get_instance()
|
self._notify = EventBus.get_instance()
|
||||||
self.max_workers = int(config.max_workers)
|
self.max_workers = int(config.max_workers)
|
||||||
self.temp_keep = bool(config.temp_keep)
|
self.temp_keep = bool(config.temp_keep)
|
||||||
|
self.temp_disabled = bool(config.temp_disabled)
|
||||||
self.is_live = bool(info.is_live) or info.live_in is not None
|
self.is_live = bool(info.is_live) or info.live_in is not None
|
||||||
self.info_dict = info_dict
|
self.info_dict = info_dict
|
||||||
self.logger = logging.getLogger(f"Download.{info.id if info.id else info._id}")
|
self.logger = logging.getLogger(f"Download.{info.id if info.id else info._id}")
|
||||||
|
|
@ -164,6 +167,8 @@ class Download:
|
||||||
self.status_queue.put({"id": self.id, "filename": filename})
|
self.status_queue.put({"id": self.id, "filename": filename})
|
||||||
|
|
||||||
def _download(self):
|
def _download(self):
|
||||||
|
cookie_file = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
params: dict = (
|
params: dict = (
|
||||||
YTDLPOpts.get_instance()
|
YTDLPOpts.get_instance()
|
||||||
|
|
@ -175,7 +180,7 @@ class Download:
|
||||||
"color": "no_color",
|
"color": "no_color",
|
||||||
"paths": {
|
"paths": {
|
||||||
"home": str(self.download_dir),
|
"home": str(self.download_dir),
|
||||||
"temp": str(self.temp_path),
|
"temp": str(self.temp_path) if self.temp_path else self.download_dir,
|
||||||
},
|
},
|
||||||
"outtmpl": {
|
"outtmpl": {
|
||||||
"default": self.template,
|
"default": self.template,
|
||||||
|
|
@ -203,7 +208,9 @@ class Download:
|
||||||
|
|
||||||
if self.info.cookies:
|
if self.info.cookies:
|
||||||
try:
|
try:
|
||||||
cookie_file = Path(self.temp_path) / f"cookie_{self.info._id}.txt"
|
cookie_file: Path = (
|
||||||
|
Path(self.temp_path if self.temp_path else self.temp_dir) / f"cookie_{self.info._id}.txt"
|
||||||
|
)
|
||||||
self.logger.debug(
|
self.logger.debug(
|
||||||
f"Creating cookie file for '{self.info.id}: {self.info.title}' - '{cookie_file}'."
|
f"Creating cookie file for '{self.info.id}: {self.info.title}' - '{cookie_file}'."
|
||||||
)
|
)
|
||||||
|
|
@ -273,9 +280,9 @@ class Download:
|
||||||
def mark_cancelled(*_):
|
def mark_cancelled(*_):
|
||||||
cls._interrupted = True
|
cls._interrupted = True
|
||||||
cls.to_screen("[info] Interrupt received, exiting cleanly...")
|
cls.to_screen("[info] Interrupt received, exiting cleanly...")
|
||||||
raise SystemExit(130) # noqa: TRY301
|
raise KeyboardInterrupt # noqa: TRY301
|
||||||
|
|
||||||
signal.signal(signal.SIGUSR1, mark_cancelled)
|
signal.signal(signal.SIGINT, mark_cancelled)
|
||||||
|
|
||||||
if isinstance(self.info_dict, dict) and len(self.info_dict) > 1:
|
if isinstance(self.info_dict, dict) and len(self.info_dict) > 1:
|
||||||
self.logger.debug(f"Downloading '{self.info.url}' using pre-info.")
|
self.logger.debug(f"Downloading '{self.info.url}' using pre-info.")
|
||||||
|
|
@ -299,6 +306,12 @@ class Download:
|
||||||
self.status_queue.put({"id": self.id, "status": "error", "msg": str(exc), "error": str(exc)})
|
self.status_queue.put({"id": self.id, "status": "error", "msg": str(exc), "error": str(exc)})
|
||||||
finally:
|
finally:
|
||||||
self.status_queue.put(Terminator())
|
self.status_queue.put(Terminator())
|
||||||
|
if cookie_file and cookie_file.exists():
|
||||||
|
try:
|
||||||
|
cookie_file.unlink()
|
||||||
|
self.logger.debug(f"Deleted cookie file: {cookie_file}")
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.error(f"Failed to delete cookie file: {cookie_file}. {e}")
|
||||||
|
|
||||||
self.logger.info(f'Task id="{self.info.id}" PID="{os.getpid()}" title="{self.info.title}" completed.')
|
self.logger.info(f'Task id="{self.info.id}" PID="{os.getpid()}" title="{self.info.title}" completed.')
|
||||||
|
|
||||||
|
|
@ -306,12 +319,13 @@ class Download:
|
||||||
self.status_queue = Config.get_manager().Queue()
|
self.status_queue = Config.get_manager().Queue()
|
||||||
|
|
||||||
# Create temp dir for each download.
|
# Create temp dir for each download.
|
||||||
self.temp_path = Path(self.temp_dir) / hashlib.shake_256(f"D-{self.info.id}".encode()).hexdigest(5)
|
if not self.temp_disabled:
|
||||||
|
self.temp_path = Path(self.temp_dir) / hashlib.shake_256(f"D-{self.info.id}".encode()).hexdigest(5)
|
||||||
|
|
||||||
if not self.temp_path.exists():
|
if not self.temp_path.exists():
|
||||||
self.temp_path.mkdir(parents=True, exist_ok=True)
|
self.temp_path.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
self.info.temp_path = str(self.temp_path)
|
self.info.temp_path = str(self.temp_path)
|
||||||
|
|
||||||
self.proc = multiprocessing.Process(name=f"download-{self.id}", target=self._download)
|
self.proc = multiprocessing.Process(name=f"download-{self.id}", target=self._download)
|
||||||
self.proc.start()
|
self.proc.start()
|
||||||
|
|
@ -407,7 +421,7 @@ class Download:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def delete_temp(self):
|
def delete_temp(self):
|
||||||
if self.temp_keep is True or not self.temp_path:
|
if self.temp_disabled or self.temp_keep is True or not self.temp_path:
|
||||||
return
|
return
|
||||||
|
|
||||||
if "finished" != self.info.status and self.info.downloaded_bytes > 0:
|
if "finished" != self.info.status and self.info.downloaded_bytes > 0:
|
||||||
|
|
@ -461,7 +475,10 @@ class Download:
|
||||||
try:
|
try:
|
||||||
self.info.filename = str(Path(status.get("filename")).relative_to(Path(self.download_dir)))
|
self.info.filename = str(Path(status.get("filename")).relative_to(Path(self.download_dir)))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
self.info.filename = str(Path(status.get("filename")).relative_to(Path(self.temp_path)))
|
if self.temp_path:
|
||||||
|
self.info.filename = str(Path(status.get("filename")).relative_to(Path(self.temp_path)))
|
||||||
|
else:
|
||||||
|
self.info.filename = str(fl)
|
||||||
|
|
||||||
if fl.is_file() and fl.exists():
|
if fl.is_file() and fl.exists():
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
|
|
@ -901,7 +901,7 @@ class DownloadQueue(metaclass=Singleton):
|
||||||
await entry.close()
|
await entry.close()
|
||||||
|
|
||||||
if self.queue.exists(key=id):
|
if self.queue.exists(key=id):
|
||||||
LOG.debug(f"Download '{id}' is done. Removing from queue.")
|
LOG.debug(f"Download Task '{id}' is completed. Removing from queue.")
|
||||||
self.queue.delete(key=id)
|
self.queue.delete(key=id)
|
||||||
|
|
||||||
if entry.is_cancelled() is True:
|
if entry.is_cancelled() is True:
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,9 @@ class Config:
|
||||||
temp_keep: bool = False
|
temp_keep: bool = False
|
||||||
"""Keep temporary files after the download is complete."""
|
"""Keep temporary files after the download is complete."""
|
||||||
|
|
||||||
|
temp_disabled: bool = False
|
||||||
|
"""Disable the temporary files feature."""
|
||||||
|
|
||||||
output_template: str = "%(title)s.%(ext)s"
|
output_template: str = "%(title)s.%(ext)s"
|
||||||
"""The output template to use for the downloaded files."""
|
"""The output template to use for the downloaded files."""
|
||||||
|
|
||||||
|
|
@ -233,6 +236,7 @@ class Config:
|
||||||
"browser_control_enabled",
|
"browser_control_enabled",
|
||||||
"ytdlp_auto_update",
|
"ytdlp_auto_update",
|
||||||
"prevent_premiere_live",
|
"prevent_premiere_live",
|
||||||
|
"temp_disabled",
|
||||||
)
|
)
|
||||||
"The variables that are booleans."
|
"The variables that are booleans."
|
||||||
|
|
||||||
|
|
@ -580,7 +584,7 @@ class Config:
|
||||||
return
|
return
|
||||||
|
|
||||||
commit_date, commit_sha = commit_info.split("_", 1)
|
commit_date, commit_sha = commit_info.split("_", 1)
|
||||||
commit_date = time.strftime("%Y%m%d", time.localtime(int(commit_date)))
|
commit_date: str = time.strftime("%Y%m%d", time.localtime(int(commit_date)))
|
||||||
|
|
||||||
self.app_version = f"{branch_name}-{commit_date}-{commit_sha[:8]}"
|
self.app_version = f"{branch_name}-{commit_date}-{commit_sha[:8]}"
|
||||||
self.app_branch = branch_name
|
self.app_branch = branch_name
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue