From dbee7cf2face681571630a4bd8c5dd4a2ce4f9e8 Mon Sep 17 00:00:00 2001 From: ArabCoders Date: Sat, 25 Jan 2025 22:46:33 +0300 Subject: [PATCH] standardize more events. --- app/library/DownloadQueue.py | 6 +- app/library/Emitter.py | 13 +- app/library/HttpSocket.py | 16 +-- ui/components/NewDownload.vue | 240 +++++++++++++++++----------------- ui/stores/SocketStore.js | 12 +- 5 files changed, 145 insertions(+), 142 deletions(-) diff --git a/app/library/DownloadQueue.py b/app/library/DownloadQueue.py index c70badde..5495219c 100644 --- a/app/library/DownloadQueue.py +++ b/app/library/DownloadQueue.py @@ -326,7 +326,7 @@ class DownloadQueue: await item.close() LOG.debug(f"Deleting from queue {itemMessage}") self.queue.delete(id) - asyncio.create_task(self.emitter.canceled(id=id, dl=item.info.serialize()), name=f"notifier-c-{id}") + asyncio.create_task(self.emitter.canceled(dl=item.info.serialize()), name=f"notifier-c-{id}") item.info.status = "canceled" item.info.error = "Canceled by user." self.done.put(item) @@ -373,7 +373,7 @@ class DownloadQueue: LOG.error(f"Unable to remove '{itemRef}' local file '{filename}'. {str(e)}") self.done.delete(id) - asyncio.create_task(self.emitter.cleared(id, dl=item.info.serialize()), name=f"notifier-c-{id}") + asyncio.create_task(self.emitter.cleared(dl=item.info.serialize()), name=f"notifier-c-{id}") msg = f"Deleted completed download '{itemRef}'." if fileDeleted and filename: msg += f" and removed local file '{filename}'." @@ -475,7 +475,7 @@ class DownloadQueue: self.queue.delete(key=id) if entry.is_canceled() is True: - asyncio.create_task(self.emitter.canceled(id, dl=entry.info.serialize()), name=f"notifier-c-{id}") + asyncio.create_task(self.emitter.canceled(dl=entry.info.serialize()), name=f"notifier-c-{id}") entry.info.status = "canceled" entry.info.error = "Canceled by user." diff --git a/app/library/Emitter.py b/app/library/Emitter.py index c130e523..73fbaa9e 100644 --- a/app/library/Emitter.py +++ b/app/library/Emitter.py @@ -29,21 +29,18 @@ class Emitter: async def completed(self, dl: dict, **kwargs): await self.emit("completed", dl, **kwargs) - async def canceled(self, id: str, dl: dict | None = None, **kwargs): - await self.emit("canceled", id, **kwargs) + async def canceled(self, dl: dict, **kwargs): + await self.emit("canceled", dl, **kwargs) - async def cleared(self, id: str, dl: dict | None = None, **kwargs): - await self.emit("cleared", id, **kwargs) + async def cleared(self, dl: dict | None = None, **kwargs): + await self.emit("cleared", dl, **kwargs) async def error(self, message: str, data: dict = {}, **kwargs): - msg = {"status": "error", "message": message, "data": {}} + msg = {"type": "error", "message": message, "data": {}} if data: msg.update({"data": data}) await self.emit("error", msg, **kwargs) - async def warning(self, message: str, **kwargs): - await self.emit("error", message, **kwargs) - async def info(self, message: str, data: dict = {}, **kwargs): msg = {"type": "info", "message": message, "data": {}} if data: diff --git a/app/library/HttpSocket.py b/app/library/HttpSocket.py index 8340ec80..82b292a4 100644 --- a/app/library/HttpSocket.py +++ b/app/library/HttpSocket.py @@ -159,7 +159,7 @@ class HttpSocket(common): url: str | None = data.get("url") if not url: - await self.emitter.warning("No URL provided.", to=sid) + await self.emitter.error("No URL provided.", to=sid) return preset: str = str(data.get("preset", self.config.default_preset)) @@ -172,7 +172,7 @@ class HttpSocket(common): try: ytdlp_config = json.loads(ytdlp_config) except Exception as e: - await self.emitter.warning(f"Failed to parse json yt-dlp config. {str(e)}", to=sid) + await self.emitter.error(f"Failed to parse json yt-dlp config. {str(e)}", to=sid) return status = await self.add( @@ -189,7 +189,7 @@ class HttpSocket(common): @ws_event # type: ignore async def item_cancel(self, sid: str, id: str): if not id: - await self.emitter.warning("Invalid request.", to=sid) + await self.emitter.error("Invalid request.", to=sid) return status: dict[str, str] = {} @@ -201,12 +201,12 @@ class HttpSocket(common): @ws_event # type: ignore async def item_delete(self, sid: str, data: dict): if not data: - await self.emitter.warning("Invalid request.", to=sid) + await self.emitter.error("Invalid request.", to=sid) return id: str | None = data.get("id") if not id: - await self.emitter.warning("Invalid request.", to=sid) + await self.emitter.error("Invalid request.", to=sid) return status: dict[str, str] = {} @@ -280,13 +280,13 @@ class HttpSocket(common): @ws_event async def ytdlp_convert(self, sid: str, data: dict): if not isinstance(data, dict) or "args" not in data: - await self.emitter.warning("Invalid request or no options were given.", to=sid) + await self.emitter.error("Invalid request or no options were given.", to=sid) return args: str | None = data.get("args") if not args: - await self.emitter.warning("no options were given.", to=sid) + await self.emitter.error("no options were given.", to=sid) return try: @@ -296,4 +296,4 @@ class HttpSocket(common): err = str(e).strip() err = err.split("\n")[-1] if "\n" in err else err LOG.error(f"Failed to convert args. '{err}'.") - await self.emitter.error(f"Failed to convert options. '{e}'.", to=sid) + await self.emitter.error(f"Failed to convert options. '{err}'.", to=sid) diff --git a/ui/components/NewDownload.vue b/ui/components/NewDownload.vue index 01cfb797..c0d50ee5 100644 --- a/ui/components/NewDownload.vue +++ b/ui/components/NewDownload.vue @@ -1,133 +1,135 @@