This commit is contained in:
arabcoders 2025-07-20 16:53:56 +03:00
parent 94193b808f
commit 472b8cb468
4 changed files with 17 additions and 68 deletions

View file

@ -16,7 +16,7 @@ LOG = logging.getLogger("datastore")
class StoreType(str, Enum):
DONE = "done"
HISTORY = "done"
QUEUE = "queue"
@classmethod

View file

@ -76,7 +76,7 @@ class DownloadQueue(metaclass=Singleton):
self.config = config or Config.get_instance()
self._notify = EventBus.get_instance()
self.done = DataStore(type=StoreType.DONE, connection=connection)
self.done = DataStore(type=StoreType.HISTORY, connection=connection)
self.queue = DataStore(type=StoreType.QUEUE, connection=connection)
self.done.load()
self.queue.load()
@ -981,13 +981,13 @@ class DownloadQueue(metaclass=Singleton):
if entry.is_cancelled() is True:
nTitle = "Download Cancelled"
nMessage = f"Download '{entry.info.title}' has been cancelled."
nMessage = f"Cancelled '{entry.info.title}' download."
await self._notify.emit(Events.ITEM_CANCELLED, data=entry.info, title=nTitle, message=nMessage)
entry.info.status = "cancelled"
if entry.info.status == "finished" and entry.info.filename:
nTitle = "Download Completed"
nMessage = f"Download '{entry.info.title}' has been finished."
nMessage = f"Completed '{entry.info.title}' download."
_tasks.append(self._notify.emit(Events.ITEM_COMPLETED, data=entry.info, title=nTitle, message=nMessage))
self.done.put(value=entry)

View file

@ -3,13 +3,12 @@ from pathlib import Path
from .config import Config
from .Presets import Preset, Presets
from .Singleton import Singleton
from .Utils import REMOVE_KEYS, arg_converter, calc_download_path, load_cookies, merge_dict
LOG: logging.Logger = logging.getLogger("YTDLPOpts")
class YTDLPOpts(metaclass=Singleton):
class YTDLPOpts:
_item_opts: dict = {}
"""The item options."""
@ -22,9 +21,6 @@ class YTDLPOpts(metaclass=Singleton):
_preset_cli: str = ""
"""The command options for yt-dlp from preset."""
_instance = None
"""The instance of the class."""
def __init__(self):
self._config = Config.get_instance()
@ -37,10 +33,7 @@ class YTDLPOpts(metaclass=Singleton):
Presets: The instance of the class
"""
if not YTDLPOpts._instance:
YTDLPOpts._instance = YTDLPOpts()
return YTDLPOpts._instance
return YTDLPOpts()
def add_cli(self, args: str, from_user: int | bool = False) -> "YTDLPOpts":
"""
@ -133,7 +126,7 @@ class YTDLPOpts(metaclass=Singleton):
load_cookies(file)
self._preset_opts["cookiefile"] = str(file)
except ValueError as e:
LOG.error(str(e))
LOG.error(f"Failed to load '{preset.name}' cookies from '{file}'. {e!s}")
if preset.template:
self._preset_opts["outtmpl"] = {"default": preset.template, "chapter": self._config.output_template_chapter}
@ -210,6 +203,6 @@ class YTDLPOpts(metaclass=Singleton):
if data["format"] == "-best":
data["format"] = data["format"][1:]
LOG.debug(f"Parsed yt-dlp options are: '{data!s}'.")
LOG.debug(f"Final yt-dlp options: '{data!s}'.")
return data

View file

@ -40,15 +40,9 @@ async def resume(notify: EventBus, queue: DownloadQueue):
@route(RouteType.SOCKET, "add_url", "add_url")
async def add_url(queue: DownloadQueue, notify: EventBus, sid: str, data: dict):
url: str | None = data.get("url")
if not url:
await notify.emit(
Events.LOG_ERROR,
title="Invalid URL",
message="Please provide a valid URL to add to the download queue.",
to=sid,
)
data = data if isinstance(data, dict) else {}
if not (url := data.get("url", None)):
await notify.emit(Events.LOG_ERROR, title="Invalid request", message="No URL provided.", to=sid)
return
try:
@ -67,57 +61,19 @@ async def add_url(queue: DownloadQueue, notify: EventBus, sid: str, data: dict):
@route(RouteType.SOCKET, "item_cancel", "item_cancel")
async def item_cancel(queue: DownloadQueue, notify: EventBus, sid: str, data: str):
if not data:
await notify.emit(
Events.LOG_ERROR,
title="Invalid Request",
message="No item ID provided to cancel.",
to=sid,
)
if not (data := data if isinstance(data, str) else None):
await notify.emit(Events.LOG_ERROR, title="Invalid Request", message="No item ID provided.", to=sid)
return
try:
item = queue.get_item(id=data)
except KeyError:
await notify.emit(
Events.LOG_ERROR,
title="Item Not Found",
message=f"Item with ID '{data}' not found.",
to=sid,
)
return
status: dict[str, str] = {}
status = await queue.cancel([data])
status.update({"identifier": data})
await notify.emit(
Events.ITEM_CANCELLED,
data=item.info,
title="Item Cancelled",
message=f"Cancelled '{item.info.title}'.",
)
await queue.cancel([data])
@route(RouteType.SOCKET, "item_delete", "item_delete")
async def item_delete(queue: DownloadQueue, notify: EventBus, sid: str, data: dict):
if not data:
await notify.emit(
Events.LOG_ERROR,
title="Invalid Request",
message="No item ID provided to delete.",
to=sid,
)
return
data = data if isinstance(data, dict) else {}
id: str | None = data.get("id")
if not id:
await notify.emit(
Events.LOG_ERROR,
title="Invalid Request",
message="No item ID provided to delete.",
to=sid,
)
if not (id := data.get("id", None)):
await notify.emit(Events.LOG_ERROR, title="Invalid Request", message="No item ID provided.", to=sid)
return
await queue.clear([id], remove_file=bool(data.get("remove_file", False)))