diff --git a/app/library/HttpAPI.py b/app/library/HttpAPI.py index 2c308ee4..2e1ab8b8 100644 --- a/app/library/HttpAPI.py +++ b/app/library/HttpAPI.py @@ -408,7 +408,10 @@ class HttpAPI(common): if not url: return web.json_response(data={"error": "url param is required."}, status=web.HTTPBadRequest.status_code) - status = await self.add(**self._formatItem({"url": url})) + try: + status = await self.add(**self.format_item({"url": url})) + except ValueError as e: + return web.json_response(data={"error": str(e)}, status=web.HTTPBadRequest.status_code) return web.json_response(data=status, status=web.HTTPOk.status_code, dumps=self.encoder.encode) @@ -430,7 +433,7 @@ class HttpAPI(common): for item in data: try: - item = self._formatItem(item) + item = self.format_item(item) except ValueError as e: return web.json_response(data={"error": str(e), "data": item}, status=web.HTTPBadRequest.status_code) @@ -1155,45 +1158,3 @@ class HttpAPI(common): await self.emitter.emit(Events.TEST, data) return web.json_response(data=data, status=web.HTTPOk.status_code, dumps=self.encoder.encode) - - def _formatItem(self, item: dict) -> dict: - """ - Format the item to be added to the download queue. - - Args: - item (dict): The item to be formatted. - - Raises: - ValueError: If the url is not provided. - ValueError: If the yt-dlp config is not a valid json. - - Returns: - dict: The formatted item - """ - url: str = item.get("url") - - if not url: - raise ValueError("url param is required.") - - preset: str = str(item.get("preset", self.config.default_preset)) - folder: str = str(item.get("folder")) if item.get("folder") else "" - cookies: str = str(item.get("cookies")) if item.get("cookies") else "" - template: str = str(item.get("template")) if item.get("template") else "" - - config = item.get("config") - if isinstance(config, str) and config: - try: - config = json.loads(config) - except Exception as e: - raise ValueError(f"Failed to parse json yt-dlp config for '{url}'. {str(e)}") - - item = { - "url": url, - "preset": preset, - "folder": folder, - "ytdlp_cookies": cookies, - "ytdlp_config": config if isinstance(config, dict) else {}, - "output_template": template, - } - - return item diff --git a/app/library/HttpSocket.py b/app/library/HttpSocket.py index 8271a97f..62d7276f 100644 --- a/app/library/HttpSocket.py +++ b/app/library/HttpSocket.py @@ -180,27 +180,12 @@ class HttpSocket(common): await self.emitter.error("No URL provided.", data={"unlock": True}, to=sid) return - preset: str = str(data.get("preset", self.config.default_preset)) - folder: str = str(data.get("folder")) if data.get("folder") else "" - cookies: str = str(data.get("cookies")) if data.get("cookies") else "" - template: str = str(data.get("template")) if data.get("template") else "" + try: + item = self.format_item(data) + except ValueError as e: + return web.json_response(data={"error": str(e)}, status=web.HTTPBadRequest.status_code) - config = data.get("config") - if isinstance(config, str) and config: - try: - config = json.loads(config) - except Exception as e: - await self.emitter.error(f"Failed to parse json yt-dlp config. {str(e)}", data={"unlock": True}, to=sid) - return - - status = await self.add( - url=url, - preset=preset, - folder=folder, - ytdlp_cookies=cookies, - ytdlp_config=config if isinstance(config, dict) else {}, - output_template=template, - ) + status = await self.add(**item) await self.emitter.emit(event=Events.STATUS, data=status, to=sid) diff --git a/app/library/common.py b/app/library/common.py index 97324298..47a9d32b 100644 --- a/app/library/common.py +++ b/app/library/common.py @@ -1,3 +1,4 @@ +import json import logging from .DownloadQueue import DownloadQueue @@ -35,3 +36,45 @@ class common: ) return status + + def format_item(self, item: dict) -> dict: + """ + Format the item to be added to the download queue. + + Args: + item (dict): The item to be formatted. + + Raises: + ValueError: If the url is not provided. + ValueError: If the yt-dlp config is not a valid json. + + Returns: + dict: The formatted item + """ + url: str = item.get("url") + + if not url: + raise ValueError("url param is required.") + + preset: str = str(item.get("preset", self.config.default_preset)) + folder: str = str(item.get("folder")) if item.get("folder") else "" + cookies: str = str(item.get("cookies")) if item.get("cookies") else "" + template: str = str(item.get("template")) if item.get("template") else "" + + config = item.get("config") + if isinstance(config, str) and config: + try: + config = json.loads(config) + except Exception as e: + raise ValueError(f"Failed to parse json yt-dlp config for '{url}'. {str(e)}") + + item = { + "url": url, + "preset": preset, + "folder": folder, + "ytdlp_cookies": cookies, + "ytdlp_config": config if isinstance(config, dict) else {}, + "output_template": template, + } + + return item