WIP: Migrated DownloadQueue to use Path

This commit is contained in:
arabcoders 2025-06-09 00:14:14 +03:00
parent 1f1fbbac01
commit 6a9beea89e
2 changed files with 21 additions and 33 deletions

View file

@ -2,7 +2,6 @@ import asyncio
import functools import functools
import glob import glob
import logging import logging
import os
import time import time
import uuid import uuid
from datetime import UTC, datetime, timedelta from datetime import UTC, datetime, timedelta
@ -10,7 +9,6 @@ from email.utils import formatdate, parsedate_to_datetime
from pathlib import Path from pathlib import Path
from sqlite3 import Connection from sqlite3 import Connection
import anyio
import yt_dlp import yt_dlp
from aiohttp import web from aiohttp import web
@ -399,7 +397,7 @@ class DownloadQueue(metaclass=Singleton):
item.template = _preset.template item.template = _preset.template
yt_conf = {} yt_conf = {}
cookie_file = os.path.join(self.config.temp_path, f"c_{uuid.uuid4().hex}.txt") cookie_file = Path(self.config.temp_path) / f"c_{uuid.uuid4().hex}.txt"
LOG.info(f"Adding '{item.__repr__()}'.") LOG.info(f"Adding '{item.__repr__()}'.")
@ -441,10 +439,8 @@ class DownloadQueue(metaclass=Singleton):
if item.cookies: if item.cookies:
try: try:
async with await anyio.open_file(cookie_file, "w") as f: cookie_file.write_text(item.cookies)
await f.write(item.cookies) yt_conf["cookiefile"] = str(cookie_file.as_posix())
yt_conf["cookiefile"] = f.name
load_cookies(cookie_file) load_cookies(cookie_file)
except Exception as e: except Exception as e:
msg = f"Failed to create cookie file for '{item.url}'. '{e!s}'." msg = f"Failed to create cookie file for '{item.url}'. '{e!s}'."
@ -493,10 +489,10 @@ class DownloadQueue(metaclass=Singleton):
"msg": f"TimeoutError: {self.config.extract_info_timeout}s reached Unable to extract info.", "msg": f"TimeoutError: {self.config.extract_info_timeout}s reached Unable to extract info.",
} }
finally: finally:
if cookie_file and os.path.exists(cookie_file): if cookie_file and cookie_file.exists():
try: try:
os.remove(cookie_file) cookie_file.unlink(missing_ok=True)
del yt_conf["cookiefile"] yt_conf.pop("cookiefile", None)
except Exception as e: except Exception as e:
LOG.error(f"Failed to remove cookie file '{yt_conf['cookiefile']}'. {e!s}") LOG.error(f"Failed to remove cookie file '{yt_conf['cookiefile']}'. {e!s}")
@ -579,18 +575,19 @@ class DownloadQueue(metaclass=Singleton):
filename = f"{item.info.folder}/{item.info.filename}" filename = f"{item.info.folder}/{item.info.filename}"
try: try:
realFile: str = calc_download_path( rf = Path(
base_path=self.config.download_path, calc_download_path(
folder=filename, base_path=self.config.download_path,
create_path=False, folder=filename,
create_path=False,
)
) )
rf = Path(realFile) if rf.stem and rf.is_file() and rf.exists():
if rf.is_file() and rf.exists():
for f in rf.parent.glob(f"{glob.escape(rf.stem)}.*"): for f in rf.parent.glob(f"{glob.escape(rf.stem)}.*"):
if f.is_file() and f.exists() and not f.name.startswith("."): if f.is_file() and f.exists() and not f.name.startswith("."):
removed_files += 1 removed_files += 1
LOG.debug(f"Removing '{itemRef}' local file '{f.name}'.") LOG.debug(f"Removing '{itemRef}' local file '{f.name}'.")
os.remove(f) f.unlink(missing_ok=True)
else: else:
LOG.warning(f"Failed to remove '{itemRef}' local file '{filename}'. File not found.") LOG.warning(f"Failed to remove '{itemRef}' local file '{filename}'. File not found.")
except Exception as e: except Exception as e:
@ -701,22 +698,13 @@ class DownloadQueue(metaclass=Singleton):
""" """
filePath = calc_download_path(base_path=self.config.download_path, folder=entry.info.folder) filePath = calc_download_path(base_path=self.config.download_path, folder=entry.info.folder)
LOG.info( LOG.info(f"Downloading 'id: {id}', 'Title: {entry.info.title}', 'URL: {entry.info.url}' To '{filePath}'.")
f"Downloading 'id: {id}', 'Title: {entry.info.title}', 'URL: {entry.info.url}' to 'Folder: {filePath}'."
)
try: try:
self._active[entry.info._id] = entry self._active[entry.info._id] = entry
await entry.start() await entry.start()
if "finished" != entry.info.status: if "finished" != entry.info.status:
if entry.tmpfilename and os.path.isfile(entry.tmpfilename):
try:
os.remove(entry.tmpfilename)
entry.tmpfilename = None
except Exception:
pass
entry.info.status = "error" entry.info.status = "error"
finally: finally:
if entry.info._id in self._active: if entry.info._id in self._active:

View file

@ -89,17 +89,17 @@ def calc_download_path(base_path: str, folder: str | None = None, create_path: b
if folder.startswith("/"): if folder.startswith("/"):
folder = folder[1:] folder = folder[1:]
realBasePath = os.path.realpath(base_path) realBasePath = pathlib.Path(base_path).absolute()
download_path = os.path.realpath(os.path.join(base_path, folder)) download_path = pathlib.Path(realBasePath / folder).absolute()
if not download_path.startswith(realBasePath): if not str(download_path).startswith(str(realBasePath)):
msg = f'Folder "{folder}" must resolve inside the base download folder "{realBasePath}".' msg = f'Folder "{folder}" must resolve inside the base download folder "{realBasePath}".'
raise Exception(msg) raise Exception(msg)
if not os.path.isdir(download_path) and create_path: if not download_path.is_dir() and create_path:
os.makedirs(download_path, exist_ok=True) download_path.mkdir(parents=True, exist_ok=True)
return download_path return str(download_path)
def extract_info( def extract_info(